Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cohere
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
import uuid
|
6 |
+
from functools import partial
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
cohere_api_key = os.getenv("COHERE_API_KEY")
|
11 |
+
co = cohere.Client(cohere_api_key)
|
12 |
+
|
13 |
+
def trigger_example(example):
|
14 |
+
chat, updated_history = generate_response(example)
|
15 |
+
return chat, updated_history
|
16 |
+
|
17 |
+
def generate_response(user_message, history=None):
|
18 |
+
global cid
|
19 |
+
|
20 |
+
if history is None:
|
21 |
+
history = []
|
22 |
+
|
23 |
+
history.append(user_message)
|
24 |
+
|
25 |
+
stream = co.chat_stream(message=user_message, conversation_id=cid, model='command-r', connectors=[], temperature=0.3)
|
26 |
+
|
27 |
+
output = ""
|
28 |
+
for idx, response in enumerate(stream):
|
29 |
+
if response.event_type == "text-generation":
|
30 |
+
output += response.text
|
31 |
+
|
32 |
+
if idx == 0:
|
33 |
+
history.append(" " + output)
|
34 |
+
else:
|
35 |
+
history[-1] = output
|
36 |
+
|
37 |
+
chat = [
|
38 |
+
(history[i].strip(), history[i + 1].strip())
|
39 |
+
for i in range(0, len(history) - 1, 2)
|
40 |
+
]
|
41 |
+
|
42 |
+
yield chat, history
|
43 |
+
|
44 |
+
return chat, history
|
45 |
+
|
46 |
+
|
47 |
+
def clear_chat():
|
48 |
+
global cid
|
49 |
+
cid = str(uuid.uuid4())
|
50 |
+
return [], []
|
51 |
+
|
52 |
+
|
53 |
+
examples = [
|
54 |
+
"Get a quick overview of current market condition of solar panels",
|
55 |
+
"Gather business intelligence on the Chinese markets",
|
56 |
+
"Summarize recent news about the North American tech job market",
|
57 |
+
"Give me a rundown of AI startups in the productivity space",
|
58 |
+
"Write a python code to reverse a string",
|
59 |
+
"Write a children's story about a polar bear who goes to the market to buy a new coat",
|
60 |
+
"Create a list of unusual excuses people might use to get out of a work meeting",
|
61 |
+
"Explain the relativity theory in french"
|
62 |
+
]
|
63 |
+
|
64 |
+
title = """<h1 align="center">Cohere for AI Command R</h1>"""
|
65 |
+
custom_css = """
|
66 |
+
#logo-img {
|
67 |
+
display: block;
|
68 |
+
margin-left: auto;
|
69 |
+
margin-right: auto;
|
70 |
+
}
|
71 |
+
#chat-message {
|
72 |
+
font-size: 14px;
|
73 |
+
min-height: 300px;
|
74 |
+
}
|
75 |
+
"""
|
76 |
+
|
77 |
+
with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
|
78 |
+
#gr.HTML(title)
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column(scale=1):
|
82 |
+
gr.Image("cmdr.png", elem_id="logo-img", show_label=False)
|
83 |
+
with gr.Column(scale=3):
|
84 |
+
gr.Markdown("""Command R is a large language model with open weights optimized for various use cases
|
85 |
+
including reasoning, summarization, and question answering. Command R is capable of multilingual generation
|
86 |
+
evaluated in 10 languages and highly performant RAG capabilities.
|
87 |
+
<br/><br/>
|
88 |
+
**Model**: [c4ai-command-r-v01](https://huggingface.co/CohereForAI/c4ai-command-r-v01)
|
89 |
+
<br/>
|
90 |
+
**Developed by**: [Cohere](https://cohere.com/) and [Cohere for AI](https://cohere.com/research)
|
91 |
+
<br/>
|
92 |
+
**License**: CC-BY-NC, requires also adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy)
|
93 |
+
"""
|
94 |
+
)
|
95 |
+
|
96 |
+
with gr.Column():
|
97 |
+
with gr.Row():
|
98 |
+
chatbot = gr.Chatbot(show_label=False)
|
99 |
+
|
100 |
+
with gr.Row():
|
101 |
+
user_message = gr.Textbox(lines=1, placeholder="Ask anything ...", label="Input", show_label=False)
|
102 |
+
|
103 |
+
|
104 |
+
with gr.Row():
|
105 |
+
submit_button = gr.Button("Submit")
|
106 |
+
clear_button = gr.Button("Clear chat")
|
107 |
+
|
108 |
+
|
109 |
+
history = gr.State([])
|
110 |
+
cid = str(uuid.uuid4())
|
111 |
+
|
112 |
+
|
113 |
+
user_message.submit(fn=generate_response, inputs=[user_message, history], outputs=[chatbot, history])
|
114 |
+
|
115 |
+
submit_button.click(fn=generate_response, inputs=[user_message, history], outputs=[chatbot, history])
|
116 |
+
clear_button.click(fn=clear_chat, inputs=None, outputs=[chatbot, history])
|
117 |
+
|
118 |
+
with gr.Row():
|
119 |
+
gr.Examples(
|
120 |
+
examples=examples,
|
121 |
+
inputs=[user_message],
|
122 |
+
cache_examples=False,
|
123 |
+
fn=trigger_example,
|
124 |
+
outputs=[chatbot],
|
125 |
+
)
|
126 |
+
|
127 |
+
if __name__ == "__main__":
|
128 |
+
demo.launch(share=True)
|