Spaces:
Running
Running
Tim Seufert
commited on
Commit
·
fa3ed48
1
Parent(s):
287c6a0
updateback
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import os
|
2 |
-
import base64
|
3 |
import gradio as gr
|
4 |
import cohere
|
5 |
|
@@ -22,49 +21,33 @@ co = cohere.Client(api_key=api_key)
|
|
22 |
# Globale Variable für das aktuelle Bild
|
23 |
current_image = None
|
24 |
|
25 |
-
def encode_image_to_base64(image_path):
|
26 |
-
"""Konvertiert ein Bild in Base64-Format"""
|
27 |
-
with open(image_path, "rb") as image_file:
|
28 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
29 |
-
|
30 |
def respond(message, history):
|
31 |
-
"""Antwortfunktion für das Gradio-Chatinterface
|
32 |
global current_image
|
33 |
try:
|
34 |
-
#
|
|
|
35 |
if current_image:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# Vision-Anfrage mit minimalen Parametern
|
40 |
-
response = co.chat(
|
41 |
-
model='c4ai-aya-vision-8b',
|
42 |
-
message=message,
|
43 |
-
temperature=0.3,
|
44 |
-
attachments=[{"source": {"type": "base64", "media_type": "image/jpeg", "data": base64_image}}]
|
45 |
-
)
|
46 |
-
|
47 |
-
# Bild nach Verwendung zurücksetzen
|
48 |
current_image = None
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
return response
|
68 |
|
69 |
except Exception as e:
|
70 |
print(f"Fehler: {str(e)}")
|
@@ -78,15 +61,14 @@ def set_image(image):
|
|
78 |
|
79 |
# Kombiniertes Interface mit Blocks
|
80 |
with gr.Blocks() as demo:
|
81 |
-
# ChatInterface
|
82 |
chat_interface = gr.ChatInterface(
|
83 |
fn=respond,
|
84 |
-
|
85 |
-
title="SimplestMachine
|
86 |
-
description="Stellen Sie mir Fragen
|
87 |
theme="soft",
|
88 |
-
examples=["Wie geht es dir?", "Was ist künstliche Intelligenz?", "
|
89 |
-
type="messages"
|
90 |
)
|
91 |
|
92 |
# Bildupload-Bereich unten mit Accordion
|
@@ -102,6 +84,7 @@ with gr.Blocks() as demo:
|
|
102 |
# Anwendung starten
|
103 |
if __name__ == "__main__":
|
104 |
demo.launch(
|
|
|
105 |
server_name="0.0.0.0",
|
106 |
allowed_paths=["*"]
|
107 |
)
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import cohere
|
4 |
|
|
|
21 |
# Globale Variable für das aktuelle Bild
|
22 |
current_image = None
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
def respond(message, history):
|
25 |
+
"""Einfache Antwortfunktion für das Gradio-Chatinterface"""
|
26 |
global current_image
|
27 |
try:
|
28 |
+
# Bildkontext hinzufügen, wenn ein Bild vorhanden ist
|
29 |
+
image_context = ""
|
30 |
if current_image:
|
31 |
+
image_context = "\nEin Bild wurde hochgeladen. Bitte beschreibe und analysiere dieses Bild: " + current_image
|
32 |
+
# Bild zurücksetzen nach Verwendung
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
current_image = None
|
34 |
+
|
35 |
+
stream = co.chat_stream(
|
36 |
+
model='command-r-plus-08-2024', # Hier ggf. ein Vision-Modell verwenden
|
37 |
+
message=f"{prompt} '{message}{image_context}'",
|
38 |
+
temperature=0.3,
|
39 |
+
chat_history=[],
|
40 |
+
prompt_truncation='AUTO',
|
41 |
+
connectors=[{"id": "web-search"}]
|
42 |
+
)
|
43 |
+
|
44 |
+
response = "".join([
|
45 |
+
event.text
|
46 |
+
for event in stream
|
47 |
+
if event.event_type == "text-generation"
|
48 |
+
])
|
49 |
+
|
50 |
+
return response
|
|
|
|
|
51 |
|
52 |
except Exception as e:
|
53 |
print(f"Fehler: {str(e)}")
|
|
|
61 |
|
62 |
# Kombiniertes Interface mit Blocks
|
63 |
with gr.Blocks() as demo:
|
64 |
+
# ChatInterface oben
|
65 |
chat_interface = gr.ChatInterface(
|
66 |
fn=respond,
|
67 |
+
#(c)Tim Seufert 2024
|
68 |
+
title="SimplestMachine",
|
69 |
+
description="Stellen Sie mir Ihre Fragen, und ich werde versuchen, Ihnen zu helfen!-- SimplestMachine hat keinen Zugriff auf echtzeitinformationen. AI kann fehler machen.",
|
70 |
theme="soft",
|
71 |
+
examples=["Wie geht es dir?", "Was ist künstliche Intelligenz?", "Erkläre mir Quantenphysik einfach."],
|
|
|
72 |
)
|
73 |
|
74 |
# Bildupload-Bereich unten mit Accordion
|
|
|
84 |
# Anwendung starten
|
85 |
if __name__ == "__main__":
|
86 |
demo.launch(
|
87 |
+
share=True,
|
88 |
server_name="0.0.0.0",
|
89 |
allowed_paths=["*"]
|
90 |
)
|