SimplestMachine / app.py
Tim Seufert
Update settings
256a3e3
raw
history blame
2.01 kB
import cohere
import gradio as gr
import os
prompt = """You are a helpful chatbot and you should try to help the user with problems in the best possible way and
speak in as natural a language as possible. You are a machine with whom you can chat from time to time.
Just be friendly and not complex. Your main task, however, remains to help the user
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.
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: """
def respond(message, image, chat_history):
co = cohere.Client(api_key=os.environ.get("apikeysimple")) # Initialize INSIDE the function
message_content = message
if image is not None:
message_content += "\n(Image received)" # Placeholder for image processing
try:
stream = co.chat_stream(
model='command-r-plus-08-2024',
message=f"{prompt} '{message_content}'",
temperature=0.3,
chat_history=[], # Consider using chat_history for context
prompt_truncation='AUTO',
connectors=[{"id": "web-search"}]
)
response = "".join([event.text for event in stream if event.event_type == "text-generation"])
chat_history.append((message, response))
return "", chat_history # Return empty string for message and the updated history
except Exception as e:
return "", chat_history.append((message, f"Error: {str(e)}"))
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
img = gr.Image(type="filepath")
clear = gr.ClearButton([msg, img, chatbot])
msg.submit(respond, [msg, img, chatbot], [msg, chatbot])
demo.launch(server_name="0.0.0.0", allowed_paths=["*"])