File size: 2,805 Bytes
45add8e
 
 
 
 
 
 
 
 
f3ee818
45add8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import google.generativeai as genai
import os
import io
import mimetypes
from PIL import Image

# Initialize Gemini API
try:
    GOOGLE_API_KEY = os.environ["GOOGLE_API_KEY"]
    genai.configure(api_key=GOOGLE_API_KEY)
    model = genai.GenerativeModel("gemini-pro-vision")
except KeyError:
    print("Error: GEMINI_API_KEY not found in environment variables. Please set it in Hugging Face Space secrets.")
    exit()
except Exception as e:
    print(f"Error initializing Gemini API: {e}")
    exit()

def generate_response(text_input, file_input):
    """Generates a response using Gemini's multiprompt feature with various file types."""
    try:
        contents = []
        if text_input:
            contents.append(text_input)

        if file_input:
            file_path = file_input
            mime_type, _ = mimetypes.guess_type(file_path)
            if mime_type is None:
                return "Unsupported file type. Please upload a valid image, video, audio, or PDF."
            
            try:
                with open(file_path, "rb") as f:
                    file_bytes = f.read()
            except Exception as e:
                return f"Error reading file: {e}"
            
            if mime_type.startswith("image"):
                try:
                    # Check if it's a valid image
                    Image.open(io.BytesIO(file_bytes))
                    contents.append({
                        "mime_type": mime_type,
                        "data": file_bytes
                    })
                except Exception as e:
                     return f"Error processing image: {e}"
            elif mime_type.startswith("video") or mime_type.startswith("audio") or mime_type == "application/pdf":
                contents.append({
                    "mime_type": mime_type,
                    "data": file_bytes
                })
            else:
                return "Unsupported file type. Please upload a valid image, video, audio, or PDF."

        if not contents:
            return "Please provide either text or a file."

        response = model.generate_content(contents)
        response.resolve()
        return response.text
    except Exception as e:
        return f"Error generating response: {e}"

# Gradio Interface
iface = gr.Interface(
    fn=generate_response,
    inputs=[
        gr.Textbox(lines=3, placeholder="Enter text prompt here..."),
        gr.File(label="Upload a File (Image, Video, Audio, PDF)")
    ],
    outputs=gr.Textbox(label="Gemini Response"),
    title="Multimodal Gemini Chatbot",
    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."
)

if __name__ == "__main__":
    iface.launch(share=False)