Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
import os
|
4 |
+
import io
|
5 |
+
import mimetypes
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Initialize Gemini API
|
9 |
+
try:
|
10 |
+
GOOGLE_API_KEY = os.environ["GEMINI_API_KEY"]
|
11 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
12 |
+
model = genai.GenerativeModel("gemini-pro-vision")
|
13 |
+
except KeyError:
|
14 |
+
print("Error: GEMINI_API_KEY not found in environment variables. Please set it in Hugging Face Space secrets.")
|
15 |
+
exit()
|
16 |
+
except Exception as e:
|
17 |
+
print(f"Error initializing Gemini API: {e}")
|
18 |
+
exit()
|
19 |
+
|
20 |
+
def generate_response(text_input, file_input):
|
21 |
+
"""Generates a response using Gemini's multiprompt feature with various file types."""
|
22 |
+
try:
|
23 |
+
contents = []
|
24 |
+
if text_input:
|
25 |
+
contents.append(text_input)
|
26 |
+
|
27 |
+
if file_input:
|
28 |
+
file_path = file_input
|
29 |
+
mime_type, _ = mimetypes.guess_type(file_path)
|
30 |
+
if mime_type is None:
|
31 |
+
return "Unsupported file type. Please upload a valid image, video, audio, or PDF."
|
32 |
+
|
33 |
+
try:
|
34 |
+
with open(file_path, "rb") as f:
|
35 |
+
file_bytes = f.read()
|
36 |
+
except Exception as e:
|
37 |
+
return f"Error reading file: {e}"
|
38 |
+
|
39 |
+
if mime_type.startswith("image"):
|
40 |
+
try:
|
41 |
+
# Check if it's a valid image
|
42 |
+
Image.open(io.BytesIO(file_bytes))
|
43 |
+
contents.append({
|
44 |
+
"mime_type": mime_type,
|
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 |
+
if not contents:
|
58 |
+
return "Please provide either text or a file."
|
59 |
+
|
60 |
+
response = model.generate_content(contents)
|
61 |
+
response.resolve()
|
62 |
+
return response.text
|
63 |
+
except Exception as e:
|
64 |
+
return f"Error generating response: {e}"
|
65 |
+
|
66 |
+
# Gradio Interface
|
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 |
+
if __name__ == "__main__":
|
79 |
+
iface.launch(share=False)
|