import gradio as gr import google.generativeai as genai import os # Set up Gemini API key (use environment variable on Hugging Face) API_KEY = os.getenv("GEMINI_API_KEY") # Set this in Hugging Face secrets genai.configure(api_key=API_KEY) # Use Gemini 2.0 Flash (free-tier accessible) model = genai.GenerativeModel("gemini-2.0-flash") def vqa(image, question): """Performs Visual Question Answering (VQA) using the Gemini API.""" try: response = model.generate_content([question, image]) return response.text except Exception as e: return f"Error: {e}" # Create Gradio interfaces vqa_interface = gr.Interface( fn=vqa, inputs=[gr.Image(type="filepath"), gr.Textbox(label="Question")], outputs=gr.Textbox(label="Answer"), title="Visual Question Answering (VQA)", description="Upload an image and ask a question about it." ) if __name__ == "__main__": vqa_interface.launch()