akhaliq's picture
akhaliq HF staff
Update app.py
487ff70 verified
raw
history blame
3.17 kB
import base64
import requests
from io import BytesIO
from PIL import Image
import gradio as gr
def encode_image(img):
"""
Encodes a PIL Image to a base64 string.
"""
buffered = BytesIO()
img.save(buffered, format="PNG")
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
return encoded_string
def get_image_description(api_key, uploaded_image):
"""
Sends the uploaded image and API key to the Hyperbolic API and retrieves the response.
"""
if not api_key:
return {"error": "API key is required."}
if uploaded_image is None:
return {"error": "No image uploaded."}
try:
# Open the uploaded image
img = Image.open(uploaded_image)
base64_img = encode_image(img)
api_endpoint = "https://api.hyperbolic.xyz/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
payload = {
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What is this image?"},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{base64_img}"},
},
],
}
],
"model": "Qwen/Qwen2-VL-72B-Instruct",
"max_tokens": 2048,
"temperature": 0.7,
"top_p": 0.9,
}
response = requests.post(api_endpoint, headers=headers, json=payload)
# Check if the request was successful
if response.status_code == 200:
return response.json()
else:
return {"error": f"API Error: {response.status_code} - {response.text}"}
except Exception as e:
return {"error": str(e)}
# Define the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(
"""
# Image Description with Hyperbolic API
Upload an image and enter your Hyperbolic API key to get a description of the image.
"""
)
with gr.Row():
api_key_input = gr.Textbox(
label="Hyperbolic API Key",
type="password",
placeholder="Enter your API key here",
interactive=True
)
with gr.Row():
image_input = gr.Image(
label="Upload Image",
type="filepath"
# Removed the 'tool' parameter to ensure compatibility
)
with gr.Row():
submit_button = gr.Button("Get Description")
output = gr.JSON(label="API Response")
# Define the button click event
submit_button.click(
fn=get_image_description,
inputs=[api_key_input, image_input],
outputs=output
)
gr.Markdown(
"""
---
**Note:** Your API key is used only for this session and is not stored.
"""
)
# Launch the Gradio app
if __name__ == "__main__":
demo.launch()