Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ def encode_image(img):
|
|
12 |
|
13 |
def chat_with_pixtral(uploaded_file, user_question):
|
14 |
if uploaded_file is not None:
|
15 |
-
# uploaded_file
|
16 |
base64_img = encode_image(uploaded_file)
|
17 |
|
18 |
api = "https://api.hyperbolic.xyz/v1/chat/completions"
|
@@ -28,7 +28,7 @@ def chat_with_pixtral(uploaded_file, user_question):
|
|
28 |
{
|
29 |
"role": "user",
|
30 |
"content": [
|
31 |
-
{"type": "text", "text": user_question}, #
|
32 |
{
|
33 |
"type": "image_url",
|
34 |
"image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
|
@@ -44,25 +44,25 @@ def chat_with_pixtral(uploaded_file, user_question):
|
|
44 |
|
45 |
response = requests.post(api, headers=headers, json=payload)
|
46 |
|
47 |
-
#
|
48 |
if response.status_code == 200:
|
49 |
response_data = response.json()
|
50 |
if 'choices' in response_data:
|
51 |
assistant_response = response_data['choices'][0]['message']['content']
|
52 |
else:
|
53 |
-
assistant_response = "
|
54 |
else:
|
55 |
-
assistant_response = f"API
|
56 |
|
57 |
return assistant_response
|
58 |
-
return "
|
59 |
|
60 |
-
# Gradio
|
61 |
iface = gr.Interface(
|
62 |
fn=chat_with_pixtral,
|
63 |
inputs=[
|
64 |
-
gr.Image(type="pil", label="
|
65 |
-
gr.Textbox(label="
|
66 |
],
|
67 |
outputs="text",
|
68 |
title="Pixtral Image Chat",
|
|
|
12 |
|
13 |
def chat_with_pixtral(uploaded_file, user_question):
|
14 |
if uploaded_file is not None:
|
15 |
+
# uploaded_file ist bereits ein PIL-Bildobjekt.
|
16 |
base64_img = encode_image(uploaded_file)
|
17 |
|
18 |
api = "https://api.hyperbolic.xyz/v1/chat/completions"
|
|
|
28 |
{
|
29 |
"role": "user",
|
30 |
"content": [
|
31 |
+
{"type": "text", "text": user_question}, # Vom Benutzer eingegebene Fragen
|
32 |
{
|
33 |
"type": "image_url",
|
34 |
"image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
|
|
|
44 |
|
45 |
response = requests.post(api, headers=headers, json=payload)
|
46 |
|
47 |
+
# Prรผfen der API-Antwort
|
48 |
if response.status_code == 200:
|
49 |
response_data = response.json()
|
50 |
if 'choices' in response_data:
|
51 |
assistant_response = response_data['choices'][0]['message']['content']
|
52 |
else:
|
53 |
+
assistant_response = "Antwortformat ist falsch."
|
54 |
else:
|
55 |
+
assistant_response = f"Fehlerhafte API-Anforderung: {response.status_code} - {response.text}"
|
56 |
|
57 |
return assistant_response
|
58 |
+
return "Laden Sie ein Bild hoch und geben Sie Ihre Frage ein."
|
59 |
|
60 |
+
# Gradio Einstellungen der Schnittstelle
|
61 |
iface = gr.Interface(
|
62 |
fn=chat_with_pixtral,
|
63 |
inputs=[
|
64 |
+
gr.Image(type="pil", label="Ein Bild hochladen"),
|
65 |
+
gr.Textbox(label="Ihre Frage eingeben")
|
66 |
],
|
67 |
outputs="text",
|
68 |
title="Pixtral Image Chat",
|