Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
MarziehFadaee
commited on
Commit
•
beb9ce6
1
Parent(s):
9bea719
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cohere
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
import uuid
|
6 |
+
from functools import partial
|
7 |
+
from urllib.error import HTTPError
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
cohere_api_key = os.getenv("COHERE_API_KEY")
|
12 |
+
co = cohere.Client(cohere_api_key)
|
13 |
+
|
14 |
+
history = []
|
15 |
+
chat = []
|
16 |
+
cid = str(uuid.uuid4())
|
17 |
+
|
18 |
+
def trigger_example(example):
|
19 |
+
chat, updated_history = generate_response(example)
|
20 |
+
return chat, updated_history
|
21 |
+
|
22 |
+
def generate_response(user_message, history=None):
|
23 |
+
|
24 |
+
if history is None:
|
25 |
+
history = []
|
26 |
+
|
27 |
+
history.append(user_message)
|
28 |
+
|
29 |
+
stream = co.chat_stream(message=user_message, conversation_id=cid, model='command-r-plus', connectors=[], temperature=0.3)
|
30 |
+
|
31 |
+
output = ""
|
32 |
+
|
33 |
+
for idx, response in enumerate(stream):
|
34 |
+
if response.event_type == "text-generation":
|
35 |
+
output += response.text
|
36 |
+
if idx == 0:
|
37 |
+
history.append(" " + output)
|
38 |
+
else:
|
39 |
+
history[-1] = output
|
40 |
+
chat = [
|
41 |
+
(history[i].strip(), history[i + 1].strip())
|
42 |
+
for i in range(0, len(history) - 1, 2)
|
43 |
+
]
|
44 |
+
yield chat, history
|
45 |
+
|
46 |
+
return chat, history
|
47 |
+
|
48 |
+
|
49 |
+
def clear_chat():
|
50 |
+
cid = str(uuid.uuid4())
|
51 |
+
return [], []
|
52 |
+
|
53 |
+
|
54 |
+
examples = [
|
55 |
+
"What are some good questions to get to know a stranger?",
|
56 |
+
"Create a list of unusual excuses people might use to get out of a work meeting",
|
57 |
+
"Write a python code to reverse a string",
|
58 |
+
"Explain the relativity theory in French",
|
59 |
+
"Como sair de um helicóptero que caiu na água?",
|
60 |
+
"Formally introduce the transformer architecture with notation.",
|
61 |
+
"¿Cómo le explicarías el aprendizaje automático a un extraterrestre?",
|
62 |
+
"Summarize recent news about the North American tech job market"
|
63 |
+
]
|
64 |
+
|
65 |
+
custom_css = """
|
66 |
+
#logo-img {
|
67 |
+
border: none !important;
|
68 |
+
}
|
69 |
+
#chat-message {
|
70 |
+
font-size: 14px;
|
71 |
+
min-height: 300px;
|
72 |
+
}
|
73 |
+
"""
|
74 |
+
|
75 |
+
with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
|
76 |
+
|
77 |
+
with gr.Row():
|
78 |
+
with gr.Column(scale=1):
|
79 |
+
gr.Image("logoplus.png", elem_id="logo-img", show_label=False, show_share_button=False, show_download_button=False)
|
80 |
+
with gr.Column(scale=3):
|
81 |
+
gr.Markdown("""C4AI Command R+ is a research open weights release of a 103B billion parameter with highly advanced Retrieval Augmented Generation (RAG) capabilities, tool Use to automate sophisticated tasks, and excels at the 10 key languages of business. Command R + is optimized for a variety of use cases including reasoning, summarization, and question answering.
|
82 |
+
<br/><br/>
|
83 |
+
**Model**: [c4ai-command-r-plus-v01](https://huggingface.co/CohereForAI/c4ai-command-r-plus-v01)
|
84 |
+
<br/>
|
85 |
+
**Developed by**: [Cohere](https://cohere.com/) and [Cohere for AI](https://cohere.com/research)
|
86 |
+
<br/>
|
87 |
+
**License**: CC-BY-NC, requires also adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy)
|
88 |
+
"""
|
89 |
+
)
|
90 |
+
|
91 |
+
with gr.Column():
|
92 |
+
with gr.Row():
|
93 |
+
chatbot = gr.Chatbot(show_label=False)
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
user_message = gr.Textbox(lines=1, placeholder="Ask anything ...", label="Input", show_label=False)
|
97 |
+
|
98 |
+
|
99 |
+
with gr.Row():
|
100 |
+
submit_button = gr.Button("Submit")
|
101 |
+
clear_button = gr.Button("Clear chat")
|
102 |
+
|
103 |
+
|
104 |
+
history = gr.State([])
|
105 |
+
cid = str(uuid.uuid4())
|
106 |
+
|
107 |
+
|
108 |
+
user_message.submit(fn=generate_response, inputs=[user_message, history], outputs=[chatbot, history], concurrency_limit=32)
|
109 |
+
|
110 |
+
submit_button.click(fn=generate_response, inputs=[user_message, history], outputs=[chatbot, history], concurrency_limit=32)
|
111 |
+
clear_button.click(fn=clear_chat, inputs=None, outputs=[chatbot, history], concurrency_limit=32)
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
gr.Examples(
|
115 |
+
examples=examples,
|
116 |
+
inputs=[user_message],
|
117 |
+
cache_examples=False,
|
118 |
+
fn=trigger_example,
|
119 |
+
outputs=[chatbot],
|
120 |
+
)
|
121 |
+
|
122 |
+
if __name__ == "__main__":
|
123 |
+
# demo.launch(debug=True)
|
124 |
+
demo.queue(api_open=False).launch()
|