File size: 2,533 Bytes
45add8e
f406088
45add8e
f406088
 
 
 
 
 
 
 
 
 
 
45add8e
f406088
 
 
 
 
 
 
 
 
 
 
 
45add8e
f406088
 
 
 
 
 
 
 
 
45add8e
f406088
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45add8e
f406088
 
 
 
 
 
 
45add8e
f406088
45add8e
f406088
45add8e
f406088
 
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
import os
import time
import mimetypes
import google.generativeai as genai
import gradio as gr

# Configure the Gemini API
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

def upload_to_gemini(file_path, mime_type=None):
    """Uploads the given file to Gemini, returning the URI for use in chat."""
    file = genai.upload_file(file_path, mime_type=mime_type)
    print(f"Uploaded file '{file.display_name}' as: {file.uri}")
    return file.uri

def wait_for_files_active(file_uris):
    """Ensure all files are processed and ready for use."""
    print("Waiting for file processing...")
    for uri in file_uris:
        while True:
            file = genai.get_file(uri)
            if file.state.name == "ACTIVE":
                print("File ready.")
                break
            elif file.state.name == "FAILED":
                raise Exception(f"File {uri} failed to process")
            time.sleep(10)

def handle_files(files):
    """Handle file uploads, process them, and interact with Gemini API."""
    # Save uploaded files locally
    saved_files = []
    for file in files:
        local_path = f"/tmp/{file.name}"
        with open(local_path, "wb") as f:
            f.write(file.read())
        saved_files.append((local_path, mimetypes.guess_type(local_path)[0]))

    # Upload files to Gemini and wait for them to be ready
    file_uris = [upload_to_gemini(path, mime_type) for path, mime_type in saved_files]
    wait_for_files_active(file_uris)
    
    # Start a chat session with uploaded files
    model = genai.GenerativeModel(
        model_name="gemini-2.0-flash-exp",
        generation_config={
            "temperature": 0.4,
            "top_p": 0.95,
            "top_k": 40,
            "max_output_tokens": 8192,
            "response_mime_type": "text/plain",
        }
    )
    chat_session = model.start_chat(history=[{"role": "user", "parts": [{"file_uri": uri} for uri in file_uris]}])
    response = chat_session.send_message("Describe the contents.")
    return response.text

def gradio_interface():
    """Setup Gradio UI for file uploads and processing."""
    with gr.Blocks() as app:
        with gr.Row():
            file_input = gr.File(label="Upload files", file_types=[".pdf", ".png", ".jpg", ".mp3", ".mp4"], multiple=True)
            submit_button = gr.Button("Submit")
        output = gr.Textbox(label="Response from Gemini")

        submit_button.click(fn=handle_files, inputs=[file_input], outputs=[output])

    return app

app = gradio_interface()
app.launch()