Spaces:
Sleeping
Sleeping
Tim Seufert
commited on
Commit
·
b9b646c
1
Parent(s):
c3f3e46
update interface beta test2
Browse files
app.py
CHANGED
@@ -1,37 +1,72 @@
|
|
1 |
-
import time
|
2 |
import os
|
3 |
-
import
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
self.create_window = tk.Tk()
|
17 |
-
self.create_window.title("Clock")
|
18 |
-
self.create_window.geometry("400x400")
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
-
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
import cohere
|
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, 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 |
|
25 |
+
try:
|
26 |
+
# Generate response using Cohere
|
27 |
+
response = co.generate(
|
28 |
+
model='command-r-plus-2024',
|
29 |
+
prompt=f"{prompt} '{message_content}'",
|
30 |
+
max_tokens=100,
|
31 |
+
temperature=0.3,
|
32 |
+
k=0,
|
33 |
+
p=0.75,
|
34 |
+
frequency_penalty=0,
|
35 |
+
presence_penalty=0,
|
36 |
+
stop_sequences=["--"]
|
37 |
+
).generations[0].text.strip()
|
38 |
|
39 |
+
# Update chat history
|
40 |
+
chat_history.append((message, response))
|
41 |
+
return "", chat_history
|
42 |
|
43 |
+
except Exception as e:
|
44 |
+
return str(e), chat_history
|
45 |
|
46 |
+
# Create Gradio interface
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
chatbot = gr.Chatbot()
|
49 |
+
msg = gr.Textbox()
|
50 |
+
clear = gr.ClearButton([msg, chatbot])
|
51 |
|
52 |
+
# Set up message submission
|
53 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
54 |
+
|
55 |
+
# New Chat Interface with history
|
56 |
+
chat_interface = gr.ChatInterface(
|
57 |
+
respond,
|
58 |
+
type="messages",
|
59 |
+
flagging_mode="manual",
|
60 |
+
flagging_options=["Like", "Spam", "Inappropriate", "Other"],
|
61 |
+
save_history=True,
|
62 |
+
)
|
63 |
+
|
64 |
+
# Launch the demo
|
65 |
+
demo.launch(
|
66 |
+
share=True,
|
67 |
+
server_name="0.0.0.0",
|
68 |
+
allowed_paths=["*"]
|
69 |
+
)
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
+
demo.launch()
|