Delete app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import osimport timeimport gradio as grimport google.generativeai as genai
# Configure the Gemini API keygenai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Upload files to Geminidef upload_to_gemini(file_path, mime_type): """Uploads a file to Gemini and returns its reference.""" file = genai.upload_file(file_path, mime_type=mime_type) print(f"Uploaded file '{file.display_name}' with URI: {file.uri}") return {"fileData": {"fileUri": file.uri, "mimeType": mime_type}}
# Wait for file processingdef wait_for_files_active(files): """Waits until all uploaded files are active.""" print("Waiting for file processing...") for file in files: status = genai.get_file(file["fileData"]["fileUri"]) while status.state.name == "PROCESSING": print(".", end="", flush=True) time.sleep(5) status = genai.get_file(file["fileData"]["fileUri"]) if status.state.name != "ACTIVE": raise Exception(f"File {file['fileData']['fileUri']} failed to process.") print("\nAll files are ready.")
# Process user inputs and interact with Gemini APIdef process_inputs(image_path, audio_path, pdf_file, video_path, user_prompt): """Uploads files, processes content, and queries the Gemini API.""" contents = []
# Upload files if provided if image_path: contents.append(upload_to_gemini(image_path, mime_type="image/png")) if audio_path: contents.append(upload_to_gemini(audio_path, mime_type="audio/ogg")) if pdf_file: contents.append(upload_to_gemini(pdf_file.name, mime_type="application/pdf")) if video_path: contents.append(upload_to_gemini(video_path, mime_type="video/quicktime"))
# Ensure files are ready if contents: wait_for_files_active(contents)
# Add user text prompt if user_prompt: contents.append({"text": user_prompt})
# Send content to Gemini API model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp") response = model.generate_content(contents=contents)
return response.text
# Gradio Interfacewith gr.Blocks() as demo: gr.Markdown("## Multimodal Chat Interface with Gemini API")
with gr.Row(): image_input = gr.Image(label="Upload an Image") audio_input = gr.Audio(label="Upload an Audio File") with gr.Row(): pdf_input = gr.File(label="Upload a PDF") video_input = gr.Video(label="Upload a Video File")
user_prompt = gr.Textbox(label="Enter Your Prompt", placeholder="Ask something about the files...")
submit_button = gr.Button("Submit") output_box = gr.Textbox(label="Gemini AI Response")
submit_button.click( fn=process_inputs, inputs=[image_input, audio_input, pdf_input, video_input, user_prompt], outputs=output_box )
# Launch the Gradio appdemo.launch()app.pyDeleted-82