File size: 4,172 Bytes
0d39371
dfe7beb
b9b646c
 
0d39371
b9b646c
 
 
 
 
 
 
 
 
0d39371
f787549
 
0d39371
f787549
 
0d39371
9dd2467
 
 
dfe7beb
 
 
 
 
c6ff117
dfe7beb
9dd2467
b9b646c
dfe7beb
9dd2467
dfe7beb
 
 
3dd810e
dfe7beb
 
 
 
 
 
 
 
 
9dd2467
3dd810e
dfe7beb
 
 
 
3dd810e
dfe7beb
 
 
 
 
 
 
 
 
 
 
 
 
 
fecb9ae
b9b646c
f787549
 
ef46ac6
9dd2467
 
 
 
 
 
 
 
3dd810e
9dd2467
3dd810e
9dd2467
dfe7beb
 
9dd2467
3dd810e
 
9dd2467
 
 
 
 
 
 
 
 
 
 
b9b646c
f787549
8af3f9e
b9b646c
 
 
8af3f9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import base64
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 encode_image_to_base64(image_path):
    """Konvertiert ein Bild in Base64-Format"""
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def respond(message, history):
    """Antwortfunktion für das Gradio-Chatinterface mit Bildunterstützung"""
    global current_image
    try:
        # Prüfen, ob ein Bild vorhanden ist
        if current_image:
            # Base64-Kodierung des Bildes
            base64_image = encode_image_to_base64(current_image)
            
            # Vision-Anfrage mit Bild und Text - OHNE tools Parameter
            response = co.chat(
                model='c4ai-aya-vision-8b',
                message=f"{message}",
                temperature=0.3,
                chat_history=[],
                attachments=[{"source": {"type": "base64", "media_type": "image/jpeg", "data": base64_image}}]
            )
            
            # Bild nach Verwendung zurücksetzen
            current_image = None
            
            return response.text
        else:
            # Normale Textanfrage ohne Bild
            stream = co.chat_stream(
                model='c4ai-aya-vision-8b',
                message=f"{prompt} '{message}'",
                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 mit message-Type
    chat_interface = gr.ChatInterface(
        #(c)Tim Seufert 2024
        fn=respond,
        title="SimplestMachine Vision",
        description="Stellen Sie mir Fragen oder laden Sie ein Bild hoch, und ich werde es analysieren!",
        theme="soft",
        examples=["Wie geht es dir?", "Was ist künstliche Intelligenz?", "Beschreibe dieses Bild"],
        type="messages"  # Moderneres Nachrichtenformat verwenden
    )
    
    # 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(
        server_name="0.0.0.0",
        allowed_paths=["*"]
    )