Spaces:
Sleeping
Sleeping
Tim Seufert commited on
Commit ·
dfe7beb
1
Parent(s): 7dc9ccd
visionbetaupdate
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import cohere
|
| 4 |
|
|
@@ -21,33 +22,52 @@ co = cohere.Client(api_key=api_key)
|
|
| 21 |
# Globale Variable für das aktuelle Bild
|
| 22 |
current_image = None
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def respond(message, history):
|
| 25 |
-
"""
|
| 26 |
global current_image
|
| 27 |
try:
|
| 28 |
-
#
|
| 29 |
-
image_context = ""
|
| 30 |
if current_image:
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
current_image = None
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Fehler: {str(e)}")
|
|
@@ -64,11 +84,10 @@ with gr.Blocks() as demo:
|
|
| 64 |
# ChatInterface oben
|
| 65 |
chat_interface = gr.ChatInterface(
|
| 66 |
fn=respond,
|
| 67 |
-
|
| 68 |
-
|
| 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?", "
|
| 72 |
)
|
| 73 |
|
| 74 |
# Bildupload-Bereich unten mit Accordion
|
|
|
|
| 1 |
import os
|
| 2 |
+
import base64
|
| 3 |
import gradio as gr
|
| 4 |
import cohere
|
| 5 |
|
|
|
|
| 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 mit Bildunterstützung"""
|
| 32 |
global current_image
|
| 33 |
try:
|
| 34 |
+
# Prüfen, ob ein Bild vorhanden ist
|
|
|
|
| 35 |
if current_image:
|
| 36 |
+
# Base64-Kodierung des Bildes
|
| 37 |
+
base64_image = encode_image_to_base64(current_image)
|
| 38 |
+
|
| 39 |
+
# Vision-Anfrage mit Bild und Text
|
| 40 |
+
response = co.chat(
|
| 41 |
+
model='c4ai-aya-vision-8b',
|
| 42 |
+
message=f"{message}",
|
| 43 |
+
temperature=0.3,
|
| 44 |
+
chat_history=[],
|
| 45 |
+
tools=[],
|
| 46 |
+
attachments=[{"source": {"type": "base64", "media_type": "image/jpeg", "data": base64_image}}]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Bild nach Verwendung zurücksetzen
|
| 50 |
current_image = None
|
| 51 |
+
#(c)Tim Seufert 2024
|
| 52 |
+
return response.text
|
| 53 |
+
else:
|
| 54 |
+
# Normale Textanfrage ohne Bild
|
| 55 |
+
stream = co.chat_stream(
|
| 56 |
+
model='c4ai-aya-vision-8b', # Wir verwenden das gleiche Modell für Konsistenz
|
| 57 |
+
message=f"{prompt} '{message}'",
|
| 58 |
+
temperature=0.3,
|
| 59 |
+
chat_history=[],
|
| 60 |
+
prompt_truncation='AUTO',
|
| 61 |
+
connectors=[{"id": "web-search"}]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
response = "".join([
|
| 65 |
+
event.text
|
| 66 |
+
for event in stream
|
| 67 |
+
if event.event_type == "text-generation"
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
return response
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Fehler: {str(e)}")
|
|
|
|
| 84 |
# ChatInterface oben
|
| 85 |
chat_interface = gr.ChatInterface(
|
| 86 |
fn=respond,
|
| 87 |
+
title="SimplestMachine Vision",
|
| 88 |
+
description="Stellen Sie mir Fragen oder laden Sie ein Bild hoch, und ich werde es analysieren!",
|
|
|
|
| 89 |
theme="soft",
|
| 90 |
+
examples=["Wie geht es dir?", "Was ist künstliche Intelligenz?", "Beschreibe dieses Bild"]
|
| 91 |
)
|
| 92 |
|
| 93 |
# Bildupload-Bereich unten mit Accordion
|