import gradio as gr
import cohere
import os
import re
import uuid
from functools import partial
cohere_api_key = os.getenv("COHERE_API_KEY")
co = cohere.Client(cohere_api_key)
def trigger_example(example):
chat, updated_history = generate_response(example)
return chat, updated_history
def generate_response(user_message, history=None):
global cid
if history is None:
history = []
history.append(user_message)
stream = co.chat_stream(message=user_message, conversation_id=cid, model='command-r', connectors=[], temperature=0.3)
output = ""
for idx, response in enumerate(stream):
if response.event_type == "text-generation":
output += response.text
if idx == 0:
history.append(" " + output)
else:
history[-1] = output
chat = [
(history[i].strip(), history[i + 1].strip())
for i in range(0, len(history) - 1, 2)
]
yield chat, history
return chat, history
def clear_chat():
global cid
cid = str(uuid.uuid4())
return [], []
examples = [
"Get a quick overview of current market condition of solar panels",
"Gather business intelligence on the Chinese markets",
"Summarize recent news about the North American tech job market",
"Give me a rundown of AI startups in the productivity space",
"Write a python code to reverse a string",
"Write a children's story about a polar bear who goes to the market to buy a new coat",
"Create a list of unusual excuses people might use to get out of a work meeting",
"Explain the relativity theory in french"
]
title = """
Cohere for AI Command R
"""
custom_css = """
#logo-img {
display: block;
margin-left: auto;
margin-right: auto;
}
#chat-message {
font-size: 14px;
min-height: 300px;
}
"""
with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
#gr.HTML(title)
with gr.Row():
with gr.Column(scale=1):
gr.Image("cmdr.png", elem_id="logo-img", show_label=False)
with gr.Column(scale=3):
gr.Markdown("""Command R is a large language model with open weights optimized for various use cases
including reasoning, summarization, and question answering. Command R is capable of multilingual generation
evaluated in 10 languages and highly performant RAG capabilities.
**Model**: [c4ai-command-r-v01](https://huggingface.co/CohereForAI/c4ai-command-r-v01)
**Developed by**: [Cohere](https://cohere.com/) and [Cohere for AI](https://cohere.com/research)
**License**: CC-BY-NC, requires also adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy)
"""
)
with gr.Column():
with gr.Row():
chatbot = gr.Chatbot(show_label=False)
with gr.Row():
user_message = gr.Textbox(lines=1, placeholder="Ask anything ...", label="Input", show_label=False)
with gr.Row():
submit_button = gr.Button("Submit")
clear_button = gr.Button("Clear chat")
history = gr.State([])
cid = str(uuid.uuid4())
user_message.submit(fn=generate_response, inputs=[user_message, history], outputs=[chatbot, history])
submit_button.click(fn=generate_response, inputs=[user_message, history], outputs=[chatbot, history])
clear_button.click(fn=clear_chat, inputs=None, outputs=[chatbot, history])
with gr.Row():
gr.Examples(
examples=examples,
inputs=[user_message],
cache_examples=False,
fn=trigger_example,
outputs=[chatbot],
)
if __name__ == "__main__":
demo.launch(share=True)