duhitsayu commited on
Commit
f58f6eb
·
verified ·
1 Parent(s): d12f535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
app.py CHANGED
@@ -3,74 +3,73 @@ import google.generativeai as genai
3
  import gradio as gr
4
  from PIL import Image
5
 
6
- # Configure the API key
7
- API_KEY = "AIzaSyDeQ5VIcPbgzSG6QfFvn4tjP4G-V-frpwM"
8
- try:
9
- genai.configure(api_key=API_KEY)
10
- except Exception as e:
11
- print(f"Error configuring API key: {e}")
12
- exit()
13
 
14
- # Initialize the model (using gemini-1.5-pro for multimodal support)
15
  try:
16
- model = genai.GenerativeModel("gemini-2.0-flash")
 
17
  chat = model.start_chat(history=[])
18
  except Exception as e:
19
- print(f"Error initializing model: {e}")
20
  exit()
21
 
 
22
  def chatbot_interface(text_input, image_input=None, audio_input=None):
23
  try:
24
-
25
  if not text_input:
26
  return "Please enter a message."
27
 
28
- content = [text_input]
29
 
30
  # Handle image input
31
- if image_input is not None:
32
  try:
33
- # Image is already a PIL object from Gradio
34
- content.append({"image": image_input})
 
 
35
  except Exception as e:
36
  return f"Error processing image: {e}"
37
 
38
- # Handle audio input (filepath from Gradio)
39
- if audio_input is not None:
40
- if os.path.exists(audio_input):
41
- try:
42
- with open(audio_input, "rb") as audio_file:
43
- audio_data = audio_file.read()
44
- content.append({"audio": audio_data})
45
- except Exception as e:
46
- return f"Error processing audio: {e}"
47
- else:
48
- return "Audio file not found."
49
 
50
- # Send the combined input to Gemini
51
  response = chat.send_message(content)
52
- return response.text
53
 
54
  except Exception as e:
55
  return f"An error occurred: {e}"
56
 
57
- # Create Gradio interface
58
  interface = gr.Interface(
59
  fn=chatbot_interface,
60
  inputs=[
61
  gr.Textbox(label="Your Message", placeholder="Type your question here..."),
62
  gr.Image(label="Upload an Image", type="pil"),
63
- gr.Audio(label="Upload or Record Audio", type="filepath") # Removed source="upload"
64
  ],
65
  outputs=gr.Textbox(label="Bot Response"),
66
- title="FINANCE",
67
- description="Chat with text, images, and audio! Ensure your API key is valid and files are accessible.",
68
- allow_flagging="never" # Disable flagging for simplicity
69
  )
70
 
71
- # Launch the interface
72
  if __name__ == "__main__":
73
  try:
74
- interface.launch(debug=True, share=True) # Debug mode to see errors in the console
75
  except Exception as e:
76
  print(f"Error launching interface: {e}")
 
3
  import gradio as gr
4
  from PIL import Image
5
 
6
+ # Load API key securely from environment variables
7
+ API_KEY = os.getenv("AIzaSyDeQ5VIcPbgzSG6QfFvn4tjP4G-V-frpwM") # Set this in your environment
8
+ if not API_KEY:
9
+ raise ValueError("API Key is missing! Set GEMINI_API_KEY as an environment variable.")
 
 
 
10
 
11
+ # Configure Gemini API
12
  try:
13
+ genai.configure(api_key=API_KEY)
14
+ model = genai.GenerativeModel("gemini-1.5-flash") # Corrected model name
15
  chat = model.start_chat(history=[])
16
  except Exception as e:
17
+ print(f"Error initializing Gemini model: {e}")
18
  exit()
19
 
20
+ # Function to process chatbot interaction
21
  def chatbot_interface(text_input, image_input=None, audio_input=None):
22
  try:
 
23
  if not text_input:
24
  return "Please enter a message."
25
 
26
+ content = [{"text": text_input}]
27
 
28
  # Handle image input
29
+ if image_input:
30
  try:
31
+ image_input.save("temp_image.png") # Save and load as a file
32
+ with open("temp_image.png", "rb") as img_file:
33
+ image_data = img_file.read()
34
+ content.append({"image": image_data})
35
  except Exception as e:
36
  return f"Error processing image: {e}"
37
 
38
+ # Handle audio input
39
+ if audio_input and os.path.exists(audio_input):
40
+ try:
41
+ with open(audio_input, "rb") as audio_file:
42
+ audio_data = audio_file.read()
43
+ content.append({"audio": audio_data})
44
+ except Exception as e:
45
+ return f"Error processing audio: {e}"
46
+ elif audio_input:
47
+ return "Audio file not found."
 
48
 
49
+ # Send message to Gemini model
50
  response = chat.send_message(content)
51
+ return response.text if response else "No response from the model."
52
 
53
  except Exception as e:
54
  return f"An error occurred: {e}"
55
 
56
+ # Create Gradio UI
57
  interface = gr.Interface(
58
  fn=chatbot_interface,
59
  inputs=[
60
  gr.Textbox(label="Your Message", placeholder="Type your question here..."),
61
  gr.Image(label="Upload an Image", type="pil"),
62
+ gr.Audio(label="Upload or Record Audio", type="filepath")
63
  ],
64
  outputs=gr.Textbox(label="Bot Response"),
65
+ title="FINANCE Chatbot",
66
+ description="Chat using text, images, and audio! Ensure your API key is valid.",
67
+ allow_flagging="never"
68
  )
69
 
70
+ # Launch the Gradio interface
71
  if __name__ == "__main__":
72
  try:
73
+ interface.launch(debug=True, share=True)
74
  except Exception as e:
75
  print(f"Error launching interface: {e}")