CoolFace
Apppublic

RSHVR/Command_RTC

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
app.py212 linesDownload Raw Back to root
1import gradio as gr2import os3 4import cohereAPI5 6 7 8# Model configurations9COHERE_MODELS = [10    "command-a-03-2025",11    "command-r7b-12-2024",12    "command-r-plus-08-2024",13    "command-r-08-2024",14    "command-light",15    "command-light-nightly",16    "command",17    "command-nightly"18]19 20COHERE_LABS_MODELS = [21    "command-a-translate-08-2025",22    "command-a-reasoning-08-2025"23]24 25def update_model_choices(provider):26    """Update model dropdown choices based on selected provider"""27    if provider == "Cohere":28        return gr.Dropdown(choices=COHERE_MODELS, value=COHERE_MODELS[0])29    elif provider =="Cohere Labs":30        return gr.Dropdown(choices=COHERE_LABS_MODELS, value=COHERE_LABS_MODELS[0])31    else:32        return gr.Dropdown(choices=[], value=None)33 34def show_model_change_info(model_name):35    """Show info modal when model is changed"""36    if model_name:37        gr.Info(f"picking up from here with {model_name}")38    return model_name39 40 41async def respond(message, history, model_name="command-a-03-2025", temperature=0.7, max_tokens=None):42    """Generate streaming response using Cohere API"""43    44    # Convert Gradio history format to API format45    conversation_history = []46    if history:47        for entry in history:48            if isinstance(entry, dict):49                # Clean dict format - only keep role and content50                if "role" in entry and "content" in entry:51                    conversation_history.append({52                        "role": entry["role"], 53                        "content": entry["content"]54                    })55            elif isinstance(entry, (list, tuple)) and len(entry) == 2:56                # Old format: [user_msg, assistant_msg]57                user_msg, assistant_msg = entry58                if user_msg:59                    conversation_history.append({"role": "user", "content": str(user_msg)})60                if assistant_msg:61                    conversation_history.append({"role": "assistant", "content": str(assistant_msg)})62            else:63                # Handle other formats gracefully64                continue65    66    # Get API key from environment67    api_key = os.getenv('COHERE_API_KEY')68    if not api_key:69        yield "Error: COHERE_API_KEY environment variable not set"70        return71    72    # System message for the chatbot73    system_message = """You are a helpful AI assistant. Provide concise but complete responses. 74                        Be direct and to the point while ensuring you fully address the user's question or request. 75                        Do not repeat the user's question in your response. Do not exceed 50 words."""76 77    try:78        # Use async streaming function79        partial_message = ""80        async for chunk in cohereAPI.send_message_stream_async(81            system_message=system_message,82            user_message=message,83            conversation_history=conversation_history,84            api_key=api_key,85            model_name=model_name,86            temperature=temperature,87            max_tokens=max_tokens88        ):89            partial_message += chunk90            yield partial_message91    except Exception as e:92        yield f"Error: {str(e)}"93 94with gr.Blocks() as demo:95    gr.Markdown("""## Modular TTS-Chatbot96    Status: In Development97    98    The goal of this project is to enable voice-chat with any supported LLM which currently do not have speech ability similar to Gemini or GPT-4o.99    100    101    """)102    103    # State components to track current values104    temperature_state = gr.State(value=0.7)105    max_tokens_state = gr.State(value=None)106    model_state = gr.State(value=COHERE_MODELS[0])107 108    with gr.Row():109        with gr.Column(scale=2):110             # Define wrapper function after all components are created111            async def chat_wrapper(message, history, model_val, temp_val, tokens_val):112                # Use the state values directly113                current_model = model_val if model_val else COHERE_MODELS[0]114                current_temp = temp_val if temp_val is not None else 0.7115                current_max_tokens = tokens_val116                117                # Stream the response118                async for chunk in respond(message, history, current_model, current_temp, current_max_tokens):119                    yield chunk120 121            # Create chat interface using the wrapper with additional inputs122            chat_interface = gr.ChatInterface(123                fn=chat_wrapper,124                type="messages",125                save_history=True,126                additional_inputs=[model_state, temperature_state, max_tokens_state]127            )128 129            with gr.Accordion("Chat Settings", elem_id="chat_settings_group"):130                with gr.Row():131                    with gr.Column(scale=3):132                        provider = gr.Dropdown(133                            info="Provider",134                            choices=["Cohere", "Cohere Labs"],135                            value="Cohere",136                            elem_id="provider_dropdown",137                            interactive=True,138                            show_label=False139                        )140                        model = gr.Dropdown(141                            info="Model",142                            choices=COHERE_MODELS,143                            value=COHERE_MODELS[0],144                            elem_id="model_dropdown",145                            interactive=True,146                            show_label=False147                        )148        149                    # Set up event handler for provider change150                    provider.change(151                        fn=update_model_choices,152                        inputs=[provider],153                        outputs=[model]154                    )155                    156                    # Set up event handler for model change157                    model.change(158                        fn=show_model_change_info,159                        inputs=[model],160                        outputs=[model]161                    )162                    163                    # Update state when model changes164                    model.change(165                        fn=lambda x: x,166                        inputs=[model],167                        outputs=[model_state]168                    )169                    170                    171                    172                    with gr.Column(scale=1):173                        temperature = gr.Slider(174                            label="Temperature",175                            info="Higher values make output more creative",176                            minimum=0.0,177                            maximum=1.0,178                            value=0.7,179                            step=0.01,180                            elem_id="temperature_slider",181                            interactive=True,182                            183                        )184                        max_tokens = gr.Textbox(185                            label="Max Tokens",186                            info="Higher values allow longer responses. Leave empty for default.",187                            value="8192",188                            elem_id="max_tokens_input",189                            interactive=True,190                            show_label=True,191                        )192 193                        # Update state when temperature changes194                        temperature.change(195                            fn=lambda x: x,196                            inputs=[temperature],197                            outputs=[temperature_state]198                        )199                        200                        # Update state when max_tokens changes201                        max_tokens.change(202                            fn=lambda x: int(x) if x and str(x).strip() else None,203                            inputs=[max_tokens],204                            outputs=[max_tokens_state]205                        )206            207           208            209            210        211if __name__ == "__main__":212    demo.launch()