Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cohere
|
| 3 |
import os
|
| 4 |
-
import re
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
-
|
| 8 |
cohere_api_key = os.getenv("COHERE_API_KEY")
|
| 9 |
co = cohere.Client(cohere_api_key, client_name="huggingface-rp")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
def trigger_example(example):
|
| 13 |
chat, updated_history = generate_response(example)
|
| 14 |
return chat, updated_history
|
| 15 |
-
|
| 16 |
-
def generate_response(user_message, cid, history=None):
|
| 17 |
-
|
| 18 |
if history is None:
|
| 19 |
history = []
|
| 20 |
-
if cid
|
| 21 |
cid = str(uuid.uuid4())
|
| 22 |
-
|
| 23 |
history.append(user_message)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
output = ""
|
| 28 |
-
|
| 29 |
for idx, response in enumerate(stream):
|
| 30 |
if response.event_type == "text-generation":
|
| 31 |
output += response.text
|
|
@@ -33,98 +28,71 @@ def generate_response(user_message, cid, history=None):
|
|
| 33 |
history.append(" " + output)
|
| 34 |
else:
|
| 35 |
history[-1] = output
|
| 36 |
-
chat = [
|
| 37 |
-
(history[i].strip(), history[i + 1].strip())
|
| 38 |
-
for i in range(0, len(history) - 1, 2)
|
| 39 |
-
]
|
| 40 |
yield chat, history, cid
|
| 41 |
-
|
| 42 |
return chat, history, cid
|
| 43 |
-
|
| 44 |
|
| 45 |
-
def clear_chat():
|
| 46 |
return [], [], str(uuid.uuid4())
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
"Write a python code to reverse a string",
|
| 53 |
-
"Explain the relativity theory in French",
|
| 54 |
-
"Como sair de um helicóptero que caiu na água?",
|
| 55 |
-
"Formally introduce the transformer architecture with notation.",
|
| 56 |
-
"¿Cómo le explicarías el aprendizaje automático a un extraterrestre?",
|
| 57 |
-
"Summarize recent news about the North American tech job market",
|
| 58 |
-
"Explain gravity to a chicken.",
|
| 59 |
-
"Is the world discrete or analog?",
|
| 60 |
-
"What is the memory cost in a typical implementation of an all-gather operation?",
|
| 61 |
-
"Give me a brief history of the golden era of Cantopop.",
|
| 62 |
-
"Descrivi il processo di creazione di un capolavoro, come se fossi un artista del Rinascimento a Firenze.",
|
| 63 |
-
"Explique-moi le sens de la vie selon un grand auteur littéraire.",
|
| 64 |
-
"Give me an example of an endangered species and let me know what I can do to help preserve it"
|
| 65 |
-
]
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
cid = gr.State("")
|
|
|
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
-
with gr.Column(scale=
|
| 82 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Column(scale=3):
|
| 84 |
-
gr.Markdown("""C4AI Command R+ is a research open weights release of a 104B billion parameter with highly advanced Retrieval Augmented Generation (RAG) capabilities, tool Use to automate sophisticated tasks, and is multilingual in 10 languages: English, French, Spanish, Italian, German, Portuguese, Japanese, Korean, Arabic, and Chinese. Command R+ is optimized for a variety of use cases including reasoning, summarization, and question answering.
|
| 85 |
-
<br/><br/>
|
| 86 |
-
**Model**: [c4ai-command-r-plus](https://huggingface.co/CohereForAI/c4ai-command-r-plus)
|
| 87 |
-
<br/>
|
| 88 |
-
**Developed by**: [Cohere](https://cohere.com/) and [Cohere for AI](https://cohere.com/research)
|
| 89 |
-
<br/>
|
| 90 |
-
**License**: [CC-BY-NC](https://cohere.com/c4ai-cc-by-nc-license), requires also adhering to [C4AI's Acceptable Use Policy](https://docs.cohere.com/docs/c4ai-acceptable-use-policy)
|
| 91 |
-
"""
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
with gr.Column():
|
| 95 |
-
with gr.Row():
|
| 96 |
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True)
|
| 97 |
-
|
| 98 |
-
with gr.Row():
|
| 99 |
-
user_message = gr.Textbox(lines=1, placeholder="Ask anything ...", label="Input", show_label=False)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
history = gr.State([])
|
| 108 |
-
|
| 109 |
-
user_message.submit(fn=generate_response, inputs=[user_message, cid, history], outputs=[chatbot, history, cid], concurrency_limit=32)
|
| 110 |
-
|
| 111 |
-
submit_button.click(fn=generate_response, inputs=[user_message, cid, history], outputs=[chatbot, history, cid], concurrency_limit=32)
|
| 112 |
-
clear_button.click(fn=clear_chat, inputs=None, outputs=[chatbot, history, cid], concurrency_limit=32)
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 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 |
-
examples_per_page=100
|
| 126 |
-
)
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
-
# demo.launch(debug=True)
|
| 130 |
demo.queue(api_open=False, max_size=40).launch(show_api=False)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cohere
|
| 3 |
import os
|
|
|
|
| 4 |
import uuid
|
| 5 |
|
|
|
|
| 6 |
cohere_api_key = os.getenv("COHERE_API_KEY")
|
| 7 |
co = cohere.Client(cohere_api_key, client_name="huggingface-rp")
|
| 8 |
|
| 9 |
+
def trigger_example(example: str) -> tuple:
|
|
|
|
| 10 |
chat, updated_history = generate_response(example)
|
| 11 |
return chat, updated_history
|
| 12 |
+
|
| 13 |
+
def generate_response(user_message: str, cid: str, history: list = None) -> tuple:
|
|
|
|
| 14 |
if history is None:
|
| 15 |
history = []
|
| 16 |
+
if not cid: # Simplified cid check
|
| 17 |
cid = str(uuid.uuid4())
|
|
|
|
| 18 |
history.append(user_message)
|
| 19 |
+
stream = co.chat_stream(
|
| 20 |
+
message=user_message, conversation_id=cid, model='command-r-plus',
|
| 21 |
+
connectors=[{"id": "web-search"}], temperature=0.3
|
| 22 |
+
)
|
| 23 |
output = ""
|
|
|
|
| 24 |
for idx, response in enumerate(stream):
|
| 25 |
if response.event_type == "text-generation":
|
| 26 |
output += response.text
|
|
|
|
| 28 |
history.append(" " + output)
|
| 29 |
else:
|
| 30 |
history[-1] = output
|
| 31 |
+
chat = [(history[i].strip(), history[i + 1].strip()) for i in range(0, len(history) - 1, 2)]
|
|
|
|
|
|
|
|
|
|
| 32 |
yield chat, history, cid
|
|
|
|
| 33 |
return chat, history, cid
|
|
|
|
| 34 |
|
| 35 |
+
def clear_chat() -> tuple:
|
| 36 |
return [], [], str(uuid.uuid4())
|
| 37 |
|
| 38 |
+
custom_css = """
|
| 39 |
+
.gradio-container {
|
| 40 |
+
max-width: 800px;
|
| 41 |
+
margin: 40px auto;
|
| 42 |
+
padding: 20px;
|
| 43 |
+
border: 1px solid #ddd;
|
| 44 |
+
border-radius: 10px;
|
| 45 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
.gradio-input {
|
| 49 |
+
margin-bottom: 20px;
|
| 50 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
.gradio-button {
|
| 53 |
+
background-color: #4CAF50;
|
| 54 |
+
color: #fff;
|
| 55 |
+
padding: 10px 20px;
|
| 56 |
+
border: none;
|
| 57 |
+
border-radius: 5px;
|
| 58 |
+
cursor: pointer;
|
| 59 |
}
|
| 60 |
+
|
| 61 |
+
.gradio-button:hover {
|
| 62 |
+
background-color: #3e8e41;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
#chat-message {
|
| 66 |
font-size: 14px;
|
| 67 |
min-height: 300px;
|
| 68 |
+
padding: 10px;
|
| 69 |
+
border: 1px solid #ddd;
|
| 70 |
+
border-radius: 10px;
|
| 71 |
}
|
| 72 |
"""
|
| 73 |
|
| 74 |
with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
|
| 75 |
cid = gr.State("")
|
| 76 |
+
history = gr.State([])
|
| 77 |
|
| 78 |
with gr.Row():
|
| 79 |
+
with gr.Column(scale=2):
|
| 80 |
+
gr.Markdown("""
|
| 81 |
+
**C4AI Command R+**
|
| 82 |
+
===============
|
| 83 |
+
A research open weights release of a 104B billion parameter with highly advanced Retrieval Augmented Generation (RAG) capabilities.
|
| 84 |
+
""")
|
| 85 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True)
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
with gr.Row():
|
| 89 |
+
user_message = gr.Textbox(lines=1, placeholder="Ask anything...", label="Input", show_label=False)
|
| 90 |
+
submit_button = gr.Button("Submit")
|
| 91 |
+
clear_button = gr.Button("Clear chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
user_message.submit(fn=generate_response, inputs=[user_message, cid, history], outputs=[chatbot, history, cid], concurrency_limit=32)
|
| 94 |
+
submit_button.click(fn=generate_response, inputs=[user_message, cid, history], outputs=[chatbot, history, cid], concurrency_limit=32)
|
| 95 |
+
clear_button.click(fn=clear_chat, inputs=None, outputs=[chatbot, history, cid], concurrency_limit=32)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 98 |
demo.queue(api_open=False, max_size=40).launch(show_api=False)
|