""" Visual Question Answering with Hugging Face API """ import gradio as gr import requests from PIL import Image import io import base64 import os # Your Hugging Face token HF_TOKEN = os.environ.get("HF_TOKEN", "") 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 base64 buffered = io.BytesIO() image.save(buffered, format="PNG") img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8') # API request headers = { "Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json" } payload = { "inputs": { "image": img_base64, "question": question } } response = requests.post( "https://api-inference.huggingface.co/models/Salesforce/blip-vqa-base", headers=headers, json=payload, timeout=60 ) if response.status_code == 200: result = response.json() if isinstance(result, list): return result[0].get("answer", "No answer") return result.get("answer", "No answer") else: return f"Model is loading (or error). Please try again. Error: {response.status_code}" except Exception as e: return f"Error: {str(e)[:100]}" with gr.Blocks() as demo: gr.Markdown("# 🖼️ Visual Question Answering (API)") with gr.Row(): with gr.Column(): img = gr.Image(type="pil", label="Upload Image", height=300) q = gr.Textbox(label="Question", placeholder="What's in this image?") btn = gr.Button("Ask", variant="primary") with gr.Column(): out = gr.Textbox(label="Answer", lines=4) btn.click(answer_question, [img, q], out) demo.launch()