Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,55 @@
|
|
1 |
import os
|
2 |
-
import time
|
3 |
-
import mimetypes
|
4 |
-
import google.generativeai as genai
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
def
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
response =
|
|
|
56 |
return response.text
|
57 |
|
58 |
-
|
59 |
-
|
60 |
with gr.Blocks() as app:
|
61 |
with gr.Row():
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
-
submit_button.click(
|
|
|
|
|
|
|
|
|
67 |
|
68 |
return app
|
69 |
|
70 |
-
|
71 |
-
|
|
|
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
|
5 |
+
# Initialize the Gemini API
|
6 |
+
def initialize_gemini():
|
7 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
8 |
+
if not api_key:
|
9 |
+
raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
|
10 |
+
genai.configure(api_key=api_key)
|
11 |
+
return genai.GenerativeModel("gemini-2.0-flash-exp") # Assuming gemini-2.0-flash-exp is the correct model name
|
12 |
+
|
13 |
+
# Handle file inputs and text to generate a response
|
14 |
+
def generate_response(text_input, files):
|
15 |
+
contents = []
|
16 |
+
if text_input:
|
17 |
+
contents.append({"text": text_input}) # Add text input to the contents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
for file in files:
|
20 |
+
with open(file.name, "rb") as f:
|
21 |
+
data = f.read()
|
22 |
+
# Guess the MIME type from the file input
|
23 |
+
mime_type = gr.io.utils.guess_mime_type(file.name)
|
24 |
+
if mime_type:
|
25 |
+
contents.append({
|
26 |
+
"mime_type": mime_type,
|
27 |
+
"data": data
|
28 |
+
})
|
29 |
+
|
30 |
+
model = initialize_gemini()
|
31 |
+
response = model.generate_content(contents)
|
32 |
+
response.resolve()
|
33 |
return response.text
|
34 |
|
35 |
+
# Gradio Interface
|
36 |
+
def setup_interface():
|
37 |
with gr.Blocks() as app:
|
38 |
with gr.Row():
|
39 |
+
text_input = gr.Textbox(label="Enter any text:", placeholder="Type your question or comment here...")
|
40 |
+
file_input = gr.File(label="Upload your files", file_types=["image", "video", "audio", "pdf"])
|
41 |
+
submit_button = gr.Button("Generate Response")
|
42 |
+
response_output = gr.Textbox(label="Response from Gemini", placeholder="Response will appear here...")
|
43 |
|
44 |
+
submit_button.click(
|
45 |
+
fn=generate_response,
|
46 |
+
inputs=[text_input, file_input],
|
47 |
+
outputs=[response_output]
|
48 |
+
)
|
49 |
|
50 |
return app
|
51 |
|
52 |
+
# Run the interface
|
53 |
+
if __name__ == "__main__":
|
54 |
+
app = setup_interface()
|
55 |
+
app.launch(share=True) # Set `share=True` for Hugging Face Spaces or public deployment
|