File size: 2,491 Bytes
1380717 6bb6794 1380717 6bb6794 1380717 6bb6794 1380717 6bb6794 1380717 6bb6794 1380717 6bb6794 1380717 6bb6794 1380717 6bb6794 1380717 a0f67c7 1380717 |
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 |
import base64
import requests
from io import BytesIO
from PIL import Image
import gradio as gr
def encode_image(img):
buffered = BytesIO()
img.save(buffered, format="PNG")
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
return encoded_string
def chat_with_pixtral(uploaded_file, user_question):
if uploaded_file is not None:
# uploaded_file ist bereits ein PIL-Bildobjekt.
base64_img = encode_image(uploaded_file)
api = "https://api.hyperbolic.xyz/v1/chat/completions"
api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJyZzMyNzAyNEBnbWFpbC5jb20ifQ._frFve-BYZdb0Qo6FIj6xcDcxpY-6QlC2O-ToQxBjkc" # 여기에 API 키를 입력하세요
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
payload = {
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": user_question}, # Vom Benutzer eingegebene Fragen
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_img}"},
},
],
}
],
"model": "mistralai/Pixtral-12B-2409",
"max_tokens": 2048,
"temperature": 0.7,
"top_p": 0.9,
}
response = requests.post(api, headers=headers, json=payload)
# Prüfen der API-Antwort
if response.status_code == 200:
response_data = response.json()
if 'choices' in response_data:
assistant_response = response_data['choices'][0]['message']['content']
else:
assistant_response = "Antwortformat ist falsch."
else:
assistant_response = f"Fehlerhafte API-Anforderung: {response.status_code} - {response.text}"
return assistant_response
return "Laden Sie ein Bild hoch und geben Sie Ihre Frage ein."
# Gradio Einstellungen der Schnittstelle
iface = gr.Interface(
fn=chat_with_pixtral,
inputs=[
gr.Image(type="pil", label="Ein Bild hochladen"),
gr.Textbox(label="Ihre Frage eingeben")
],
outputs="text",
title="Pixtral Image Chat",
description="Lade ein Bild hoch und gib deine Frage ein, um mit Pixtral zu sprechen."
)
iface.launch(share=True)
|