pixtral / app.py
JAMESPARK3's picture
Upload folder using huggingface_hub
1380717 verified
raw
history blame
2.52 kB
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์€ ์ด๋ฏธ PIL ์ด๋ฏธ์ง€ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.
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}, # ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์งˆ๋ฌธ
{
"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)
# API ์‘๋‹ต ํ™•์ธ
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 = "์‘๋‹ต ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค."
else:
assistant_response = f"API ์š”์ฒญ ์‹คํŒจ: {response.status_code} - {response.text}"
return assistant_response
return "์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”."
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ •
iface = gr.Interface(
fn=chat_with_pixtral,
inputs=[
gr.Image(type="pil", label="์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š”"),
gr.Textbox(label="์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”")
],
outputs="text",
title="Pixtral Image Chat",
description="์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์—ฌ Pixtral๊ณผ ๋Œ€ํ™”ํ•˜์„ธ์š”."
)
iface.launch(share=True)