Spaces:
Sleeping
Sleeping
File size: 2,395 Bytes
7d6a656 | 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 | """
Simple VQA using Hugging Face API with error handling
"""
import gradio as gr
import requests
from PIL import Image
import io
import os
HF_TOKEN = os.environ.get("HF_TOKEN", "YOUR_TOKEN_HERE")
def answer_question(image, question):
if image is None:
return "Please upload an image first."
if not question.strip():
return "Please ask a question."
try:
# Convert image to bytes
buffered = io.BytesIO()
image.save(buffered, format="PNG")
img_bytes = buffered.getvalue()
# API call with timeout
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
}
# For VQA models, use this format
payload = {
"inputs": {
"image": img_bytes.hex(),
"question": question
}
}
response = requests.post(
"https://api-inference.huggingface.co/models/Salesforce/blip-vqa-base",
headers=headers,
json=payload,
timeout=30 # 30 second timeout
)
if response.status_code == 200:
result = response.json()
return result.get("answer", "No answer found")
elif response.status_code == 503:
return "Model is loading. Please try again in a few seconds."
else:
return f"Error: {response.status_code} - {response.text[:100]}"
except requests.exceptions.Timeout:
return "Request timed out. Please try again."
except requests.exceptions.ConnectionError:
return "Connection error. Check your internet and try again."
except Exception as e:
return f"Error: {str(e)}"
# Create interface
with gr.Blocks(title="VQA") as demo:
gr.Markdown("# 🖼️ Visual Question Answering")
with gr.Row():
with gr.Column():
image_input = gr.Image(label="Upload Image", type="pil", height=300)
question_input = gr.Textbox(label="Question", placeholder="What's in this image?")
submit_btn = gr.Button("Answer", variant="primary")
with gr.Column():
answer_output = gr.Textbox(label="Answer", lines=4, interactive=False)
submit_btn.click(answer_question, [image_input, question_input], answer_output)
demo.launch(share=True)
|