Spaces:
Sleeping
Sleeping
Tim Seufert
commited on
Commit
·
ef46ac6
1
Parent(s):
256a3e3
Reformatted
Browse files
app.py
CHANGED
|
@@ -2,20 +2,30 @@ import cohere
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def respond(message, image, chat_history):
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
message_content = message
|
| 15 |
if image is not None:
|
| 16 |
-
message_content += "\n(Image received)"
|
| 17 |
|
| 18 |
try:
|
|
|
|
| 19 |
stream = co.chat_stream(
|
| 20 |
model='command-r-plus-08-2024',
|
| 21 |
message=f"{prompt} '{message_content}'",
|
|
@@ -24,20 +34,35 @@ def respond(message, image, chat_history):
|
|
| 24 |
prompt_truncation='AUTO',
|
| 25 |
connectors=[{"id": "web-search"}]
|
| 26 |
)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
chat_history.append((message, response))
|
| 29 |
-
return "", chat_history
|
| 30 |
-
|
| 31 |
except Exception as e:
|
| 32 |
return "", chat_history.append((message, f"Error: {str(e)}"))
|
| 33 |
|
| 34 |
-
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
chatbot = gr.Chatbot()
|
| 37 |
msg = gr.Textbox()
|
| 38 |
img = gr.Image(type="filepath")
|
| 39 |
clear = gr.ClearButton([msg, img, chatbot])
|
| 40 |
|
|
|
|
| 41 |
msg.submit(respond, [msg, img, chatbot], [msg, chatbot])
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# System prompt definition
|
| 6 |
+
prompt = """
|
| 7 |
+
You are a helpful chatbot and you should try to help the user with problems in the best possible way and
|
| 8 |
+
speak in as natural a language as possible. You are a machine with whom you can chat from time to time.
|
| 9 |
+
Just be friendly and not complex. Your main task, however, remains to help the user
|
| 10 |
+
with his problems. Do not react to offensive and illegal questions, content. Please stick to findings from conventional medicine
|
| 11 |
+
and avoid esoteric answers. You were developed by Tim Seufert in 2024. Please give an answer of a maximum of 8 sentences.
|
| 12 |
+
If the user is asking sometihing in another language, please also respond in his Language. Don't harm the user at all.
|
| 13 |
+
The user's question is: """
|
| 14 |
|
| 15 |
def respond(message, image, chat_history):
|
| 16 |
+
"""
|
| 17 |
+
Handle chat responses with optional image support
|
| 18 |
+
"""
|
| 19 |
+
# Initialize Cohere client
|
| 20 |
+
co = cohere.Client(api_key=os.environ.get("apikeysimple"))
|
| 21 |
|
| 22 |
+
# Prepare message content
|
| 23 |
message_content = message
|
| 24 |
if image is not None:
|
| 25 |
+
message_content += "\n(Image received)" # Placeholder for image processing
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
# Generate response using Cohere
|
| 29 |
stream = co.chat_stream(
|
| 30 |
model='command-r-plus-08-2024',
|
| 31 |
message=f"{prompt} '{message_content}'",
|
|
|
|
| 34 |
prompt_truncation='AUTO',
|
| 35 |
connectors=[{"id": "web-search"}]
|
| 36 |
)
|
| 37 |
+
|
| 38 |
+
# Collect response from stream
|
| 39 |
+
response = "".join([
|
| 40 |
+
event.text
|
| 41 |
+
for event in stream
|
| 42 |
+
if event.event_type == "text-generation"
|
| 43 |
+
])
|
| 44 |
+
|
| 45 |
+
# Update chat history
|
| 46 |
chat_history.append((message, response))
|
| 47 |
+
return "", chat_history
|
| 48 |
+
|
| 49 |
except Exception as e:
|
| 50 |
return "", chat_history.append((message, f"Error: {str(e)}"))
|
| 51 |
|
| 52 |
+
# Create Gradio interface
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
chatbot = gr.Chatbot()
|
| 55 |
msg = gr.Textbox()
|
| 56 |
img = gr.Image(type="filepath")
|
| 57 |
clear = gr.ClearButton([msg, img, chatbot])
|
| 58 |
|
| 59 |
+
# Set up message submission
|
| 60 |
msg.submit(respond, [msg, img, chatbot], [msg, chatbot])
|
| 61 |
+
|
| 62 |
+
# Launch the demo
|
| 63 |
+
demo.launch(
|
| 64 |
+
share=True,
|
| 65 |
+
server_name="0.0.0.0",
|
| 66 |
+
allowed_paths=["*"]
|
| 67 |
+
)
|
| 68 |
|