import os import gradio as gr import google.generativeai as genai import tempfile # Ensure the API key is set api_key = os.getenv("GOOGLE_API_KEY") if not api_key: raise ValueError("GOOGLE_API_KEY not found. Please set it in your environment variables.") genai.configure(api_key=api_key) # Initialize the Gemini model model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp") def process_input(text_input, files): """Process input text and files, send them to the Gemini API, and get a response.""" contents = [] if text_input: contents.append({"text": text_input}) # Add text content for Gemini # Handle files: read and prepare for Gemini API for file in files: # Temporary file storage to read content with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(file.read()) tmp.seek(0) # Rewind file to the beginning for reading # Depending on file type and Gemini requirements, you might adjust how you read the file contents.append({ "file": tmp.name, "mime_type": file.type # MIME type is needed if specific handling is required by Gemini }) # Call Gemini API to process the collected contents try: response = model.generate_content(contents) response.resolve() return response.text except Exception as e: return f"Error communicating with Gemini API: {e}" # Create the Gradio interface def create_interface(): with gr.Blocks() as app: with gr.Row(): text_input = gr.Textbox(label="Enter your text:", placeholder="Type your query here...") file_input = gr.File(label="Upload files", file_types=["pdf", "png", "jpg", "mp3", "mp4"], file_count="multiple") submit_button = gr.Button("Process") output = gr.Textbox(placeholder="Response will appear here...") submit_button.click( fn=process_input, inputs=[text_input, file_input], outputs=output ) return app # Run the interface if __name__ == "__main__": app = create_interface() app.launch()