Vlad Bastina commited on
Commit
9466f12
·
1 Parent(s): eea7a28
Files changed (1) hide show
  1. app.py +17 -37
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  from streamlit_mic_recorder import mic_recorder
3
- import google.generativeai as genai
4
  import plotly.express as px
5
  import pandas as pd
6
  import os
@@ -44,7 +44,7 @@ Upload a WAV file, record new audio, or use the default example. The app will an
44
  """)
45
 
46
  # --- Default File Configuration ---
47
- DEFAULT_AUDIO_FILENAME = "default_audio.wav" # MAKE SURE THIS FILE EXISTS!
48
 
49
  # --- API Key Handling ---
50
  api_key = os.getenv("GOOGLE_API_KEY") or st.secrets["GOOGLE_API_KEY"]
@@ -55,15 +55,7 @@ if not api_key:
55
  st.warning("Please enter your Gemini API Key to proceed.")
56
  st.stop()
57
 
58
- try:
59
- genai.configure(api_key=api_key)
60
- # Use a model that supports audio input, like 1.5 Flash or 1.5 Pro
61
- model = genai.GenerativeModel(model_name="gemini-2.5-pro-exp-03-25") # Or gemini-1.5-pro
62
- except Exception as e:
63
- st.error(f"Error configuring Gemini SDK: {e}")
64
- st.stop()
65
-
66
-
67
 
68
 
69
  def detailed_sentiment_prompt(is_customer_support=None, customer_focus=False):
@@ -122,28 +114,23 @@ def detailed_sentiment_prompt(is_customer_support=None, customer_focus=False):
122
  def analyze_audio(audio_bytes, filename="uploaded_audio.wav"):
123
  """Sends audio to Gemini and processes the response."""
124
  temp_file_path = None
125
- uploaded_file_info = None
126
  try:
127
  # Gemini SDK works best with files. Save bytes to a temporary file.
128
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
129
  tmpfile.write(audio_bytes)
130
  temp_file_path = tmpfile.name
131
 
132
- # Optional: Ensure it's WAV format for robustness
133
- # try:
134
- # audio_segment = AudioSegment.from_file(temp_file_path)
135
- # audio_segment.export(temp_file_path, format="wav") # Re-export as WAV
136
- # except Exception as e:
137
- # st.warning(f"Could not verify/re-export as WAV using pydub: {e}. Sending as is.")
138
-
139
-
140
  # Upload the file to Gemini
141
- uploaded_file_info = genai.upload_file(path=temp_file_path, mime_type="audio/wav")
142
-
 
143
  # --- Initial Analysis Phase (Determine if Customer Support) ---
144
  initial_prompt = detailed_sentiment_prompt()
145
- initial_response = model.generate_content([initial_prompt, uploaded_file_info],
146
- request_options={"timeout": 600}) # Increased timeout
 
 
147
 
148
  # --- Process Initial Response ---
149
  try:
@@ -157,8 +144,9 @@ def analyze_audio(audio_bytes, filename="uploaded_audio.wav"):
157
  needs_second_pass = is_customer_support
158
  if needs_second_pass:
159
  refined_prompt = detailed_sentiment_prompt(is_customer_support=True, customer_focus=True)
160
- final_response = model.generate_content([refined_prompt, uploaded_file_info],
161
- request_options={"timeout": 600})
 
162
  final_text = final_response.text.strip().lstrip('```json').rstrip('```')
163
  analysis_data = json.loads(final_text)
164
  else:
@@ -187,17 +175,9 @@ def analyze_audio(audio_bytes, filename="uploaded_audio.wav"):
187
  return f"Error: {e}", [], "Error", False
188
  finally:
189
  # Clean up the uploaded file on Gemini and the local temp file
190
- if uploaded_file_info:
191
  try:
192
- # Gemini API might change; adapt if delete() method isn't available
193
- # print(f"Attempting to delete file: {uploaded_file_info.name}") # Debugging
194
- genai.delete_file(uploaded_file_info.name)
195
- except AttributeError:
196
- st.warning(f"Could not directly delete file object. Attempting delete by name: {uploaded_file_info.name}")
197
- try:
198
- genai.delete_file(uploaded_file_info.name)
199
- except Exception as del_err_name:
200
- st.warning(f"Could not delete uploaded file from Gemini by name either: {del_err_name}")
201
  except Exception as del_err:
202
  st.warning(f"Could not delete uploaded file from Gemini: {del_err}")
203
  if temp_file_path and os.path.exists(temp_file_path):
@@ -361,4 +341,4 @@ components.html(
361
  setInterval(sendHeightWhenReady, 1000);
362
  </script>
363
  """
364
- )
 
1
  import streamlit as st
2
  from streamlit_mic_recorder import mic_recorder
3
+ from google import genai
4
  import plotly.express as px
5
  import pandas as pd
6
  import os
 
44
  """)
45
 
46
  # --- Default File Configuration ---
47
+ DEFAULT_AUDIO_FILENAME = "apps/Vladp_toneAnalysis/default_audio.wav" # MAKE SURE THIS FILE EXISTS!
48
 
49
  # --- API Key Handling ---
50
  api_key = os.getenv("GOOGLE_API_KEY") or st.secrets["GOOGLE_API_KEY"]
 
55
  st.warning("Please enter your Gemini API Key to proceed.")
56
  st.stop()
57
 
58
+ client = genai.Client(api_key=api_key)
 
 
 
 
 
 
 
 
59
 
60
 
61
  def detailed_sentiment_prompt(is_customer_support=None, customer_focus=False):
 
114
  def analyze_audio(audio_bytes, filename="uploaded_audio.wav"):
115
  """Sends audio to Gemini and processes the response."""
116
  temp_file_path = None
117
+ myfile = None # Initialize myfile to None
118
  try:
119
  # Gemini SDK works best with files. Save bytes to a temporary file.
120
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
121
  tmpfile.write(audio_bytes)
122
  temp_file_path = tmpfile.name
123
 
 
 
 
 
 
 
 
 
124
  # Upload the file to Gemini
125
+ myfile = client.files.upload(file=temp_file_path)
126
+ print(f"{myfile=}")
127
+
128
  # --- Initial Analysis Phase (Determine if Customer Support) ---
129
  initial_prompt = detailed_sentiment_prompt()
130
+ initial_response = client.models.generate_content(
131
+ model="gemini-2.0-flash", contents=[myfile, initial_prompt], request_options={"timeout": 600}
132
+ )
133
+ print(f"{initial_response.text=}")
134
 
135
  # --- Process Initial Response ---
136
  try:
 
144
  needs_second_pass = is_customer_support
145
  if needs_second_pass:
146
  refined_prompt = detailed_sentiment_prompt(is_customer_support=True, customer_focus=True)
147
+ final_response = client.models.generate_content(
148
+ model="gemini-2.0-flash", contents=[myfile, refined_prompt], request_options={"timeout": 600}
149
+ )
150
  final_text = final_response.text.strip().lstrip('```json').rstrip('```')
151
  analysis_data = json.loads(final_text)
152
  else:
 
175
  return f"Error: {e}", [], "Error", False
176
  finally:
177
  # Clean up the uploaded file on Gemini and the local temp file
178
+ if myfile:
179
  try:
180
+ client.files.delete(myfile.name)
 
 
 
 
 
 
 
 
181
  except Exception as del_err:
182
  st.warning(f"Could not delete uploaded file from Gemini: {del_err}")
183
  if temp_file_path and os.path.exists(temp_file_path):
 
341
  setInterval(sendHeightWhenReady, 1000);
342
  </script>
343
  """
344
+ )