Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,71 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import google.generativeai as genai
|
3 |
import os
|
4 |
-
import
|
5 |
import mimetypes
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
def
|
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 |
-
"data": file_bytes
|
46 |
-
})
|
47 |
-
except Exception as e:
|
48 |
-
return f"Error processing image: {e}"
|
49 |
-
elif mime_type.startswith("video") or mime_type.startswith("audio") or mime_type == "application/pdf":
|
50 |
-
contents.append({
|
51 |
-
"mime_type": mime_type,
|
52 |
-
"data": file_bytes
|
53 |
-
})
|
54 |
-
else:
|
55 |
-
return "Unsupported file type. Please upload a valid image, video, audio, or PDF."
|
56 |
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
response.resolve()
|
62 |
-
return response.text
|
63 |
-
except Exception as e:
|
64 |
-
return f"Error generating response: {e}"
|
65 |
|
66 |
-
|
67 |
-
iface = gr.Interface(
|
68 |
-
fn=generate_response,
|
69 |
-
inputs=[
|
70 |
-
gr.Textbox(lines=3, placeholder="Enter text prompt here..."),
|
71 |
-
gr.File(label="Upload a File (Image, Video, Audio, PDF)")
|
72 |
-
],
|
73 |
-
outputs=gr.Textbox(label="Gemini Response"),
|
74 |
-
title="Multimodal Gemini Chatbot",
|
75 |
-
description="Upload an image, video, audio, or PDF, or enter text to interact with Gemini. You can provide both text and a file for multiprompt."
|
76 |
-
)
|
77 |
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
import mimetypes
|
4 |
+
import google.generativeai as genai
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Configure the Gemini API
|
8 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
9 |
+
|
10 |
+
def upload_to_gemini(file_path, mime_type=None):
|
11 |
+
"""Uploads the given file to Gemini, returning the URI for use in chat."""
|
12 |
+
file = genai.upload_file(file_path, mime_type=mime_type)
|
13 |
+
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
14 |
+
return file.uri
|
15 |
|
16 |
+
def wait_for_files_active(file_uris):
|
17 |
+
"""Ensure all files are processed and ready for use."""
|
18 |
+
print("Waiting for file processing...")
|
19 |
+
for uri in file_uris:
|
20 |
+
while True:
|
21 |
+
file = genai.get_file(uri)
|
22 |
+
if file.state.name == "ACTIVE":
|
23 |
+
print("File ready.")
|
24 |
+
break
|
25 |
+
elif file.state.name == "FAILED":
|
26 |
+
raise Exception(f"File {uri} failed to process")
|
27 |
+
time.sleep(10)
|
28 |
|
29 |
+
def handle_files(files):
|
30 |
+
"""Handle file uploads, process them, and interact with Gemini API."""
|
31 |
+
# Save uploaded files locally
|
32 |
+
saved_files = []
|
33 |
+
for file in files:
|
34 |
+
local_path = f"/tmp/{file.name}"
|
35 |
+
with open(local_path, "wb") as f:
|
36 |
+
f.write(file.read())
|
37 |
+
saved_files.append((local_path, mimetypes.guess_type(local_path)[0]))
|
38 |
|
39 |
+
# Upload files to Gemini and wait for them to be ready
|
40 |
+
file_uris = [upload_to_gemini(path, mime_type) for path, mime_type in saved_files]
|
41 |
+
wait_for_files_active(file_uris)
|
42 |
+
|
43 |
+
# Start a chat session with uploaded files
|
44 |
+
model = genai.GenerativeModel(
|
45 |
+
model_name="gemini-2.0-flash-exp",
|
46 |
+
generation_config={
|
47 |
+
"temperature": 0.4,
|
48 |
+
"top_p": 0.95,
|
49 |
+
"top_k": 40,
|
50 |
+
"max_output_tokens": 8192,
|
51 |
+
"response_mime_type": "text/plain",
|
52 |
+
}
|
53 |
+
)
|
54 |
+
chat_session = model.start_chat(history=[{"role": "user", "parts": [{"file_uri": uri} for uri in file_uris]}])
|
55 |
+
response = chat_session.send_message("Describe the contents.")
|
56 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
+
def gradio_interface():
|
59 |
+
"""Setup Gradio UI for file uploads and processing."""
|
60 |
+
with gr.Blocks() as app:
|
61 |
+
with gr.Row():
|
62 |
+
file_input = gr.File(label="Upload files", file_types=[".pdf", ".png", ".jpg", ".mp3", ".mp4"], multiple=True)
|
63 |
+
submit_button = gr.Button("Submit")
|
64 |
+
output = gr.Textbox(label="Response from Gemini")
|
65 |
|
66 |
+
submit_button.click(fn=handle_files, inputs=[file_input], outputs=[output])
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
app = gradio_interface()
|
71 |
+
app.launch()
|