Tim Seufert commited on
Commit
0d39371
·
1 Parent(s): 1aecb90

Initial commit

Browse files
Files changed (2) hide show
  1. app.py +44 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cohere
2
+ import gradio as gr
3
+ import os
4
+
5
+ prompt = """You are a helpful chatbot and you should try to help the user with problems in the best possible way and
6
+ speak in as natural a language as possible. You are a machine with whom you can chat from time to time.
7
+ Just be friendly and not complex. Your main task, however, remains to help the user
8
+ with his problems. Do not react to offensive and illegal questions, content. Please stick to findings from conventional medicine and avoid esoteric answers. You were developed by Tim Seufert in 2024. Please give an answer of a maximum of 8 sentences.
9
+ If the user is asking sometihing in another language, please also respond in his Language. Don't harm the user at all. The user's question is: """
10
+
11
+ def respond(message, image, chat_history):
12
+ co = cohere.Client(api_key=os.environ.get("apikeysimple")) # Initialize INSIDE the function
13
+
14
+ message_content = message
15
+ if image is not None:
16
+ message_content += "\n(Image received)" # Placeholder for image processing
17
+
18
+ try:
19
+ stream = co.chat_stream(
20
+ model='command-r-plus-08-2024',
21
+ message=f"{prompt} '{message_content}'",
22
+ temperature=0.3,
23
+ chat_history=[], # Consider using chat_history for context
24
+ prompt_truncation='AUTO',
25
+ connectors=[{"id": "web-search"}]
26
+ )
27
+ response = "".join([event.text for event in stream if event.event_type == "text-generation"])
28
+ chat_history.append((message, response))
29
+ return "", chat_history # Return empty string for message and the updated 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
+ demo.launch(share=True)
44
+
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ cohere
2
+ gradio