Tim Seufert commited on
Commit
fa3ed48
·
1 Parent(s): 287c6a0

updateback

Browse files
Files changed (1) hide show
  1. app.py +28 -45
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 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 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
- return response.text
51
- else:
52
- # Normale Textanfrage ohne Bild
53
- stream = co.chat_stream(
54
- model='c4ai-aya-vision-8b',
55
- message=f"{prompt} '{message}'",
56
- temperature=0.3,
57
- prompt_truncation='AUTO',
58
- connectors=[{"id": "web-search"}]
59
- )
60
-
61
- response = "".join([
62
- event.text
63
- for event in stream
64
- if event.event_type == "text-generation"
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 mit message-Type
82
  chat_interface = gr.ChatInterface(
83
  fn=respond,
84
- #(c)Tim Seufert 2024
85
- title="SimplestMachine Vision",
86
- description="Stellen Sie mir Fragen oder laden Sie ein Bild hoch, und ich werde es analysieren!",
87
  theme="soft",
88
- examples=["Wie geht es dir?", "Was ist künstliche Intelligenz?", "Beschreibe dieses Bild"],
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
  )