Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import cohere | |
# System prompt definition | |
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, chat_history): | |
""" | |
Handle chat responses with optional image support | |
""" | |
# Initialize Cohere client | |
co = cohere.Client(api_key=os.environ.get("apikeysimple")) | |
# Prepare message content | |
message_content = message | |
try: | |
# Generate response using Cohere chat API | |
response = co.chat( | |
model='command-r-08-2024', # Ensure the correct model ID is used | |
query=f"{prompt} '{message_content}'", | |
temperature=0.3, | |
k=0, | |
p=0.75, | |
frequency_penalty=0, | |
presence_penalty=0, | |
stop_sequences=["--"] | |
).generations[0].text.strip() | |
# Update chat history | |
chat_history.append((message, response)) | |
return chat_history | |
except Exception as e: | |
chat_history.append((message, f"Error: {str(e)}")) | |
return chat_history | |
# Create Gradio interface | |
demo = gr.ChatInterface( | |
fn=respond, | |
type="messages", | |
flagging_mode="manual", | |
flagging_options=["Like", "Spam", "Inappropriate", "Other"], | |
save_history=True, | |
) | |
if __name__ == "__main__": | |
demo.launch( | |
share=True, | |
server_name="0.0.0.0", | |
allowed_paths=["*"] | |
) |