Rahatara commited on
Commit
5113bc3
·
verified ·
1 Parent(s): b25e2f5

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -79
app.py DELETED
@@ -1,79 +0,0 @@
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
- api_key = os.getenv("GOOGLE_API_KEY")
9
- if not api_key:
10
- raise ValueError("GEMINI_API_KEY not found. Please set it in your environment variables.")
11
- genai.configure(api_key=api_key)
12
-
13
- def upload_to_gemini(file_data, mime_type):
14
- """Uploads the given file data to Gemini and returns the file object if successful."""
15
- try:
16
- file = genai.upload_file(file_data, mime_type=mime_type)
17
- print(f"Uploaded file '{file.display_name}' as: {file.uri}")
18
- return file
19
- except Exception as e:
20
- print(f"Error uploading file: {e}")
21
- return None
22
-
23
- def wait_for_files_active(files):
24
- """Waits for the uploaded files to be processed and become active."""
25
- for file in files:
26
- while file.state.name == "PROCESSING":
27
- print(".", end="", flush=True)
28
- time.sleep(10)
29
- file = genai.get_file(file.name)
30
- if file.state.name != "ACTIVE":
31
- raise Exception(f"File {file.display_name} failed to process, current state: {file.state.name}")
32
- print("All files ready.")
33
-
34
- def gemini_interaction(input_text, *file_args):
35
- """Handles the interaction with Gemini API for both text and file inputs."""
36
- files = []
37
- for i in range(0, len(file_args), 2):
38
- if file_args[i] is not None:
39
- file_data = file_args[i].read()
40
- mime_type = file_args[i + 1]
41
- uploaded_file = upload_to_gemini(file_data, mime_type)
42
- if uploaded_file:
43
- files.append(uploaded_file)
44
-
45
- # Wait for all files to be ready before proceeding
46
- if files:
47
- wait_for_files_active(files)
48
-
49
- # Prepare the content for the Gemini API
50
- contents = []
51
- if input_text:
52
- contents.append({"text": input_text})
53
- contents.extend([{"file": file.uri} for file in files if file])
54
-
55
- # Initialize model and generate content
56
- model = genai.GenerativeModel("gemini-2.0-flash-exp")
57
- try:
58
- response_stream = model.generate_content(contents, stream=True)
59
- return ''.join([chunk.text for chunk in response_stream])
60
- except Exception as e:
61
- return f"Gemini API error: {e}"
62
-
63
- # Define the Gradio interface
64
- iface = gr.Interface(
65
- fn=gemini_interaction,
66
- inputs=[
67
- gr.Textbox(label="Enter your Prompt"),
68
- gr.File(label="Upload File 1"), gr.Dropdown(label="MIME Type for File 1", choices=["image/png", "audio/ogg", "application/pdf", "video/mp4"]),
69
- gr.File(label="Upload File 2"), gr.Dropdown(label="MIME Type for File 2", choices=["image/png", "audio/ogg", "application/pdf", "video/mp4"]),
70
- gr.File(label="Upload File 3"), gr.Dropdown(label="MIME Type for File 3", choices=["image/png", "audio/ogg", "application/pdf", "video/mp4"]),
71
- gr.File(label="Upload File 4"), gr.Dropdown(label="MIME Type for File 4", choices=["image/png", "audio/ogg", "application/pdf", "video/mp4"])
72
- ],
73
- outputs=gr.Textbox(),
74
- title="Interact with Gemini 2.0",
75
- description="Upload files and enter text to interact with the Gemini 2.0 model."
76
- )
77
-
78
- if __name__ == "__main__":
79
- iface.launch(debug=True)