Rahatara commited on
Commit
7931675
·
verified ·
1 Parent(s): 39237c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import gradio as gr
3
  import google.generativeai as genai
4
- import tempfile
5
 
6
  # Ensure the API key is set
7
  api_key = os.getenv("GOOGLE_API_KEY")
@@ -19,17 +18,14 @@ def process_input(text_input, files):
19
  contents.append({"text": text_input}) # Add text content for Gemini
20
 
21
  # Handle files: read and prepare for Gemini API
22
- for file in files:
23
- # Temporary file storage to read content
24
- with tempfile.NamedTemporaryFile(delete=False) as tmp:
25
- tmp.write(file.read())
26
- tmp.seek(0) # Rewind file to the beginning for reading
27
-
28
- # Depending on file type and Gemini requirements, you might adjust how you read the file
29
- contents.append({
30
- "file": tmp.name,
31
- "mime_type": file.type # MIME type is needed if specific handling is required by Gemini
32
- })
33
 
34
  # Call Gemini API to process the collected contents
35
  try:
@@ -44,7 +40,7 @@ def create_interface():
44
  with gr.Blocks() as app:
45
  with gr.Row():
46
  text_input = gr.Textbox(label="Enter your text:", placeholder="Type your query here...")
47
- file_input = gr.File(label="Upload files", file_types=["pdf", "png", "jpg", "mp3", "mp4"], file_count="multiple")
48
  submit_button = gr.Button("Process")
49
 
50
  output = gr.Textbox(placeholder="Response will appear here...")
@@ -57,7 +53,6 @@ def create_interface():
57
 
58
  return app
59
 
60
- # Run the interface
61
  if __name__ == "__main__":
62
  app = create_interface()
63
- app.launch()
 
1
  import os
2
  import gradio as gr
3
  import google.generativeai as genai
 
4
 
5
  # Ensure the API key is set
6
  api_key = os.getenv("GOOGLE_API_KEY")
 
18
  contents.append({"text": text_input}) # Add text content for Gemini
19
 
20
  # Handle files: read and prepare for Gemini API
21
+ for file_info in files:
22
+ file_content = file_info['content'] # Access the file content from the Gradio file dictionary
23
+ mime_type = file_info['metadata']['mime_type'] # Access MIME type from the metadata
24
+
25
+ contents.append({
26
+ "file": file_content,
27
+ "mime_type": mime_type # MIME type is used if specific handling is required by Gemini
28
+ })
 
 
 
29
 
30
  # Call Gemini API to process the collected contents
31
  try:
 
40
  with gr.Blocks() as app:
41
  with gr.Row():
42
  text_input = gr.Textbox(label="Enter your text:", placeholder="Type your query here...")
43
+ file_input = gr.File(label="Upload files", type="file", file_types=["pdf", "png", "jpg", "mp3", "mp4"], count="multiple")
44
  submit_button = gr.Button("Process")
45
 
46
  output = gr.Textbox(placeholder="Response will appear here...")
 
53
 
54
  return app
55
 
 
56
  if __name__ == "__main__":
57
  app = create_interface()
58
+ app.launch(debug=True)