Rahatara commited on
Commit
522c3ac
·
verified ·
1 Parent(s): b38b0c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -56
app.py CHANGED
@@ -1,19 +1,21 @@
 
1
  import os
2
  import time
3
  import gradio as gr
4
  import google.generativeai as genai
 
5
 
6
- # Configure the Gemini API key
7
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
8
 
9
- def upload_to_gemini(file_path, mime_type=None):
10
- """Uploads a file to the Gemini API and returns the file object."""
11
- file = genai.upload_file(file_path, mime_type=mime_type)
12
  print(f"Uploaded file '{file.display_name}' with URI: {file.uri}")
13
- return file
14
 
15
  def wait_for_files_active(files):
16
- """Waits until all uploaded files are processed and active."""
17
  print("Waiting for files to become active...")
18
  for file in files:
19
  while True:
@@ -26,71 +28,46 @@ def wait_for_files_active(files):
26
  raise Exception(f"File {file.name} failed to process.")
27
  print("All files are active.")
28
 
29
- def process_inputs(image, audio, pdf, video, user_prompt):
30
- """Processes the inputs and generates a response using the Gemini API."""
31
- files = []
 
32
  if image:
33
- files.append(upload_to_gemini(image, mime_type="image/png"))
34
- if audio:
35
- files.append(upload_to_gemini(audio, mime_type="audio/mp4"))
36
- if pdf:
37
- files.append(upload_to_gemini(pdf.name, mime_type="application/pdf"))
38
  if video:
39
- files.append(upload_to_gemini(video, mime_type="video/mp4"))
40
 
41
- if files:
42
- wait_for_files_active(files)
 
43
 
44
- # Initialize chat session with the model
45
- chat_session = model.start_chat(
46
- history=[
47
- {
48
- "role": "user",
49
- "parts": [{"fileData": {"fileUri": file.uri, "mimeType": file.mime_type}} for file in files] + [{"text": user_prompt}],
50
- }
51
- ]
52
- )
53
 
54
- # Send the user's message and get a response
55
- response = chat_session.send_message(user_prompt)
 
 
56
  return response.text
57
 
58
- # Model configuration
59
- generation_config = {
60
- "temperature": 1,
61
- "top_p": 0.95,
62
- "top_k": 40,
63
- "max_output_tokens": 8192,
64
- "response_mime_type": "text/plain",
65
- }
66
-
67
- # Initialize the Gemini model
68
- model = genai.GenerativeModel(
69
- model_name="gemini-2.0-flash-exp",
70
- generation_config=generation_config,
71
- )
72
-
73
- # Gradio interface setup
74
  with gr.Blocks() as demo:
75
- gr.Markdown("## Multimodal Chat with Gemini AI")
76
 
77
  with gr.Row():
78
- image_input = gr.Image(label="Upload an Image")
79
- audio_input = gr.Audio(label="Upload an Audio File")
80
- with gr.Row():
81
- pdf_input = gr.File(label="Upload a PDF")
82
- video_input = gr.Video(label="Upload a Video File")
83
-
84
- user_prompt = gr.Textbox(label="Your Prompt", placeholder="Enter your question or command here...")
85
-
86
  submit_button = gr.Button("Submit")
87
- output = gr.Textbox(label="Gemini AI Response", interactive=False)
88
 
89
  submit_button.click(
90
  fn=process_inputs,
91
- inputs=[image_input, audio_input, pdf_input, video_input, user_prompt],
92
  outputs=output
93
  )
94
 
95
- # Launch the Gradio app
96
  demo.launch()
 
1
+
2
  import os
3
  import time
4
  import gradio as gr
5
  import google.generativeai as genai
6
+ from google.generativeai.types import Part
7
 
8
+ # Configure Gemini API key
9
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
10
 
11
+ def upload_to_gemini(filepath, mime_type=None):
12
+ """Uploads a file to Gemini and returns a Part object."""
13
+ file = genai.upload_file(filepath, mime_type=mime_type)
14
  print(f"Uploaded file '{file.display_name}' with URI: {file.uri}")
15
+ return Part.from_uri(file.uri, mime_type=mime_type)
16
 
17
  def wait_for_files_active(files):
18
+ """Waits until all files are active."""
19
  print("Waiting for files to become active...")
20
  for file in files:
21
  while True:
 
28
  raise Exception(f"File {file.name} failed to process.")
29
  print("All files are active.")
30
 
31
+ def process_inputs(image, video, user_prompt):
32
+ """Processes inputs and returns the Gemini response."""
33
+ parts = []
34
+
35
  if image:
36
+ parts.append(upload_to_gemini(image, mime_type="image/png"))
 
 
 
 
37
  if video:
38
+ parts.append(upload_to_gemini(video, mime_type="video/mp4"))
39
 
40
+ # Add the user prompt as a Part object
41
+ if user_prompt:
42
+ parts.append(Part(text=user_prompt))
43
 
44
+ # Wait for files to process
45
+ wait_for_files_active([p for p in parts if isinstance(p, Part) and p.uri])
 
 
 
 
 
 
 
46
 
47
+ # Create a chat session and send the message
48
+ model = genai.GenerativeModel("gemini-2.0-flash-exp")
49
+ chat_session = model.start_chat()
50
+ response = chat_session.send_message(parts)
51
  return response.text
52
 
53
+ # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  with gr.Blocks() as demo:
55
+ gr.Markdown("## Multimodal Chat with Gemini API")
56
 
57
  with gr.Row():
58
+ video_input = gr.Video(label="Upload a Video", type="filepath")
59
+ image_input = gr.Image(label="Upload an Image", type="filepath")
60
+
61
+ user_prompt = gr.Textbox(label="Your Prompt", placeholder="Enter your question here...")
62
+
 
 
 
63
  submit_button = gr.Button("Submit")
64
+ output = gr.Textbox(label="Gemini Response", interactive=False)
65
 
66
  submit_button.click(
67
  fn=process_inputs,
68
+ inputs=[image_input, video_input, user_prompt],
69
  outputs=output
70
  )
71
 
72
+ # Run the app
73
  demo.launch()