Command_RTC / app.py
RSHVR's picture
Update app.py
9c2fe2f verified
raw
history blame
4.69 kB
import gradio as gr
import os
import cohereAPI
# Conversation history storage
conversation_history = []
# Model configurations
COHERE_MODELS = [
"command-a-03-2025",
"command-r7b-12-2024",
"command-r-plus-08-2024",
"command-r-08-2024",
"command-light",
"command-light-nightly",
"command",
"command-nightly"
]
def update_model_choices(provider):
"""Update model dropdown choices based on selected provider"""
if provider == "Cohere":
return gr.Dropdown(choices=COHERE_MODELS, value=COHERE_MODELS[0])
else:
return gr.Dropdown(choices=[], value=None)
def show_model_change_info(model_name):
"""Show info modal when model is changed"""
if model_name:
gr.Info(f"picking up from here with {model_name}")
return model_name
def respond(message, history, model_name="command-a-03-2025"):
"""Generate streaming response using Cohere API"""
global conversation_history
# Get API key from environment
api_key = os.getenv('COHERE_API_KEY')
if not api_key:
yield "Error: COHERE_API_KEY environment variable not set"
return
# System message for the chatbot
system_message = """You are a helpful AI assistant. Provide concise but complete responses.
Be direct and to the point while ensuring you fully address the user's question or request.
Do not repeat the user's question in your response. Do not exceed 50 words."""
try:
# Use streaming function
partial_message = ""
for chunk in cohereAPI.send_message_stream(
system_message=system_message,
user_message=message,
conversation_history=conversation_history,
api_key=api_key,
model_name=model_name
):
partial_message += chunk
yield partial_message
except Exception as e:
yield f"Error: {str(e)}"
with gr.Blocks() as demo:
gr.Markdown("## Modular Chatbot")
with gr.Row():
with gr.Column(scale=2):
chat_interface = gr.ChatInterface(
fn=respond,
type="messages",
save_history=True
)
with gr.Accordion("Chat Settings", elem_id="chat_settings_group"):
with gr.Row():
with gr.Column(scale=3):
provider = gr.Dropdown(
info="Provider",
choices=["Cohere", "OpenAI", "Anthropic", "Google", "HuggingFace"],
value="Cohere",
elem_id="provider_dropdown",
interactive=True,
show_label=False
)
model = gr.Dropdown(
info="Model",
choices=COHERE_MODELS,
value=COHERE_MODELS[0],
elem_id="model_dropdown",
interactive=True,
show_label=False
)
# Set up event handler for provider change
provider.change(
fn=update_model_choices,
inputs=[provider],
outputs=[model]
)
# Set up event handler for model change
model.change(
fn=show_model_change_info,
inputs=[model],
outputs=[model]
)
with gr.Column(scale=1):
temperature = gr.Slider(
label="Temperature",
info="Higher values make output more creative",
minimum=0.0,
maximum=1.0,
value=0.7,
step=0.01,
elem_id="temperature_slider",
interactive=True,
)
max_tokens = gr.Textbox(
label="Max Tokens",
info="Higher values allow longer responses.",
value="8192",
elem_id="max_tokens_input",
interactive=True,
show_label=False,
)
if __name__ == "__main__":
demo.launch()