Spaces:
Running
Running
File size: 2,179 Bytes
62f1b80 |
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 |
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"], multiple=True)
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() |