Spaces:
Running
Running
import os | |
import gradio as gr | |
import google.generativeai as genai | |
# 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_info in files: | |
file_content = file_info['content'] # Access the file content from the Gradio file dictionary | |
mime_type = file_info['metadata']['mime_type'] # Access MIME type from the metadata | |
contents.append({ | |
"file": file_content, | |
"mime_type": mime_type # MIME type is used 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", type="file", 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 | |
if __name__ == "__main__": | |
app = create_interface() | |
app.launch(debug=True) |