SimplestMachine / app.py
Tim Seufert
updateback
fa3ed48
raw
history blame
3.51 kB
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: """
# Stellen Sie sicher, dass der API-Schlüssel verfügbar ist
api_key = os.environ.get("apikeysimple")
# Client außerhalb der Funktion initialisieren
co = cohere.Client(api_key=api_key)
# Globale Variable für das aktuelle Bild
current_image = None
def respond(message, history):
"""Einfache Antwortfunktion für das Gradio-Chatinterface"""
global current_image
try:
# Bildkontext hinzufügen, wenn ein Bild vorhanden ist
image_context = ""
if current_image:
image_context = "\nEin Bild wurde hochgeladen. Bitte beschreibe und analysiere dieses Bild: " + current_image
# Bild zurücksetzen nach Verwendung
current_image = None
stream = co.chat_stream(
model='command-r-plus-08-2024', # Hier ggf. ein Vision-Modell verwenden
message=f"{prompt} '{message}{image_context}'",
temperature=0.3,
chat_history=[],
prompt_truncation='AUTO',
connectors=[{"id": "web-search"}]
)
response = "".join([
event.text
for event in stream
if event.event_type == "text-generation"
])
return response
except Exception as e:
print(f"Fehler: {str(e)}")
return f"Es ist ein Fehler aufgetreten: {str(e)}"
def set_image(image):
"""Speichert das hochgeladene Bild global"""
global current_image
current_image = image
return "Bild wurde hochgeladen! Es wird mit Ihrer nächsten Nachricht verarbeitet."
# Kombiniertes Interface mit Blocks
with gr.Blocks() as demo:
# ChatInterface oben
chat_interface = gr.ChatInterface(
fn=respond,
#(c)Tim Seufert 2024
title="SimplestMachine",
description="Stellen Sie mir Ihre Fragen, und ich werde versuchen, Ihnen zu helfen!-- SimplestMachine hat keinen Zugriff auf echtzeitinformationen. AI kann fehler machen.",
theme="soft",
examples=["Wie geht es dir?", "Was ist künstliche Intelligenz?", "Erkläre mir Quantenphysik einfach."],
)
# Bildupload-Bereich unten mit Accordion
with gr.Accordion("Bild hochladen", open=False):
with gr.Row():
image_input = gr.Image(type="filepath", label="Bild für die Analyse")
upload_button = gr.Button("Bild hochladen")
image_status = gr.Textbox(label="Status", interactive=False)
# Verknüpfen des Upload-Buttons mit der set_image-Funktion
upload_button.click(fn=set_image, inputs=image_input, outputs=image_status)
# Anwendung starten
if __name__ == "__main__":
demo.launch(
share=True,
server_name="0.0.0.0",
allowed_paths=["*"]
)