Delete app.py
Browse files
app.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
-
from google.generativeai.types import Part
|
| 6 |
-
|
| 7 |
-
# Configure Gemini API key
|
| 8 |
-
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 9 |
-
|
| 10 |
-
def upload_to_gemini(filepath, mime_type):
|
| 11 |
-
"""Uploads a file to Gemini and returns a Part object."""
|
| 12 |
-
file = genai.upload_file(filepath, mime_type=mime_type)
|
| 13 |
-
print(f"Uploaded file '{file.display_name}' with URI: {file.uri}")
|
| 14 |
-
return Part.from_uri(file.uri, mime_type=mime_type)
|
| 15 |
-
|
| 16 |
-
def wait_for_files_active(files):
|
| 17 |
-
"""Waits until all uploaded files are processed and active."""
|
| 18 |
-
print("Waiting for files to become active...")
|
| 19 |
-
for file in files:
|
| 20 |
-
while True:
|
| 21 |
-
status = genai.get_file(file.name)
|
| 22 |
-
if status.state.name == "ACTIVE":
|
| 23 |
-
break
|
| 24 |
-
elif status.state.name == "PROCESSING":
|
| 25 |
-
time.sleep(5)
|
| 26 |
-
else:
|
| 27 |
-
raise Exception(f"File {file.name} failed to process.")
|
| 28 |
-
print("All files are active.")
|
| 29 |
-
|
| 30 |
-
def process_inputs(image, video, user_prompt):
|
| 31 |
-
"""Processes the inputs and generates a response using Gemini API."""
|
| 32 |
-
parts = []
|
| 33 |
-
|
| 34 |
-
# Convert files to Part objects with mime types
|
| 35 |
-
if image:
|
| 36 |
-
image_part = upload_to_gemini(image, mime_type="image/png")
|
| 37 |
-
parts.append(image_part)
|
| 38 |
-
|
| 39 |
-
if video:
|
| 40 |
-
video_part = upload_to_gemini(video, mime_type="video/mp4")
|
| 41 |
-
parts.append(video_part)
|
| 42 |
-
|
| 43 |
-
# Add user prompt as a text Part
|
| 44 |
-
if user_prompt:
|
| 45 |
-
text_part = Part(text=user_prompt)
|
| 46 |
-
parts.append(text_part)
|
| 47 |
-
|
| 48 |
-
# Create the `contents` list
|
| 49 |
-
contents = parts
|
| 50 |
-
|
| 51 |
-
# Ensure files are processed
|
| 52 |
-
wait_for_files_active([p for p in parts if hasattr(p, "uri")])
|
| 53 |
-
|
| 54 |
-
# Initialize Gemini model and generate content
|
| 55 |
-
model = genai.GenerativeModel("gemini-2.0-flash-exp")
|
| 56 |
-
response = model.generate_content(contents=contents)
|
| 57 |
-
|
| 58 |
-
return response.text
|
| 59 |
-
|
| 60 |
-
# Gradio Interface Setup
|
| 61 |
-
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("## Multimodal Chat with Gemini API")
|
| 63 |
-
|
| 64 |
-
with gr.Row():
|
| 65 |
-
video_input = gr.Video(label="Upload a Video")
|
| 66 |
-
image_input = gr.Image(label="Upload an Image")
|
| 67 |
-
|
| 68 |
-
user_prompt = gr.Textbox(label="Your Prompt", placeholder="Enter your question here...")
|
| 69 |
-
|
| 70 |
-
submit_button = gr.Button("Submit")
|
| 71 |
-
output = gr.Textbox(label="Gemini AI Response", interactive=False)
|
| 72 |
-
|
| 73 |
-
submit_button.click(
|
| 74 |
-
fn=process_inputs,
|
| 75 |
-
inputs=[image_input, video_input, user_prompt],
|
| 76 |
-
outputs=output
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# Run the Gradio App
|
| 80 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|