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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
8
+ if not api_key:
9
+ raise ValueError("GOOGLE_API_KEY not found. Please set it in your environment variables.")
10
+ genai.configure(api_key=api_key)
11
+
12
+ # Initialize the Gemini model
13
+ model = genai.GenerativeModel(model_name="gemini-2.0-flash-exp")
14
+
15
+ def process_input(text_input, files):
16
+ """Process input text and files, send them to the Gemini API, and get a response."""
17
+ contents = []
18
+ if text_input:
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:
36
+ response = model.generate_content(contents)
37
+ response.resolve()
38
+ return response.text
39
+ except Exception as e:
40
+ return f"Error communicating with Gemini API: {e}"
41
+
42
+ # Create the Gradio interface
43
+ 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"], multiple=True)
48
+ submit_button = gr.Button("Process")
49
+
50
+ output = gr.Textbox(placeholder="Response will appear here...")
51
+
52
+ submit_button.click(
53
+ fn=process_input,
54
+ inputs=[text_input, file_input],
55
+ outputs=output
56
+ )
57
+
58
+ return app
59
+
60
+ # Run the interface
61
+ if __name__ == "__main__":
62
+ app = create_interface()
63
+ app.launch()