Wall06 commited on
Commit
005abc8
·
verified ·
1 Parent(s): 9b7322f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -18,7 +18,7 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
18
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
19
  SENTINEL_CLIENT_ID = os.getenv("SENTINEL_CLIENT_ID")
20
  SENTINEL_CLIENT_SECRET = os.getenv("SENTINEL_CLIENT_SECRET")
21
- ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
22
 
23
  # -------------------- SENTINEL CONFIG --------------------
24
  config = SHConfig()
@@ -26,15 +26,6 @@ if SENTINEL_CLIENT_ID and SENTINEL_CLIENT_SECRET:
26
  config.client_id = SENTINEL_CLIENT_ID
27
  config.client_secret = SENTINEL_CLIENT_SECRET
28
 
29
- # -------------------- ELEVENLABS SETUP --------------------
30
- try:
31
- from elevenlabs.client import ElevenLabs
32
- el_client = None
33
- if ELEVENLABS_API_KEY:
34
- el_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
35
- except ImportError:
36
- el_client = None
37
-
38
  # -------------------- AI FUNCTIONS --------------------
39
  def gemini_summary(text):
40
  try:
@@ -88,23 +79,53 @@ def smart_summary(text):
88
  errors.append(f"HF: {err}")
89
  return "⚠ SYSTEM FAILURE. DEBUG LOG:\n" + "\n".join(errors)
90
 
91
- # -------------------- AUDIO FUNCTION --------------------
92
  def generate_audio_report(text):
93
- if not text or not el_client:
94
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  try:
96
- audio_stream = el_client.generate(
97
- text=text[:500],
 
 
 
 
98
  voice="Brian",
99
  model="eleven_multilingual_v2"
100
  )
 
 
101
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
102
  for chunk in audio_stream:
103
  f.write(chunk)
104
  return f.name
 
105
  except Exception as e:
106
- print(f"ElevenLabs Error: {e}")
107
- return None
 
 
 
 
 
 
108
 
109
  # -------------------- MATH & LOGIC --------------------
110
  def calculate_wqi(pH, do, nutrients):
@@ -222,9 +243,9 @@ custom_css = """
222
  #analyze-btn { background: linear-gradient(90deg, #0061ff 0%, #60efff 100%); color: white; border: none; }
223
  """
224
 
225
- # FIXED LINE BELOW: Removed css=custom_css from gr.Blocks
226
  with gr.Blocks(title="FlumenIntel") as demo:
227
- # Inject CSS manually here
228
  gr.HTML(f"<style>{custom_css}</style>")
229
 
230
  with gr.Column(elem_id="title-box"):
 
18
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
19
  SENTINEL_CLIENT_ID = os.getenv("SENTINEL_CLIENT_ID")
20
  SENTINEL_CLIENT_SECRET = os.getenv("SENTINEL_CLIENT_SECRET")
21
+ # We will fetch ELEVENLABS_API_KEY inside the function to ensure it loads
22
 
23
  # -------------------- SENTINEL CONFIG --------------------
24
  config = SHConfig()
 
26
  config.client_id = SENTINEL_CLIENT_ID
27
  config.client_secret = SENTINEL_CLIENT_SECRET
28
 
 
 
 
 
 
 
 
 
 
29
  # -------------------- AI FUNCTIONS --------------------
30
  def gemini_summary(text):
31
  try:
 
79
  errors.append(f"HF: {err}")
80
  return "⚠ SYSTEM FAILURE. DEBUG LOG:\n" + "\n".join(errors)
81
 
82
+ # -------------------- AUDIO FUNCTION (DEBUGGING ENABLED) --------------------
83
  def generate_audio_report(text):
84
+ """
85
+ Attempts to generate audio and raises a VISIBLE ERROR if it fails.
86
+ """
87
+ # 1. Check if Library is Installed
88
+ try:
89
+ from elevenlabs.client import ElevenLabs
90
+ except ImportError:
91
+ raise gr.Error("❌ 'elevenlabs' library is missing! Check requirements.txt")
92
+
93
+ # 2. Check for Text
94
+ if not text:
95
+ raise gr.Error("❌ No text to read! Please generate the report first.")
96
+
97
+ # 3. Check for API Key
98
+ api_key = os.getenv("ELEVENLABS_API_KEY")
99
+ if not api_key:
100
+ raise gr.Error("❌ API Key Missing! Go to Settings > Secrets and add ELEVENLABS_API_KEY")
101
+
102
+ # 4. Generate Audio
103
  try:
104
+ # Connect strictly inside the function to ensure the key is fresh
105
+ client = ElevenLabs(api_key=api_key)
106
+
107
+ # Limit text to 400 chars for speed/cost
108
+ audio_stream = client.generate(
109
+ text=text[:400],
110
  voice="Brian",
111
  model="eleven_multilingual_v2"
112
  )
113
+
114
+ # Save to temp file
115
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
116
  for chunk in audio_stream:
117
  f.write(chunk)
118
  return f.name
119
+
120
  except Exception as e:
121
+ # Display the specific error from ElevenLabs
122
+ error_msg = str(e)
123
+ if "401" in error_msg:
124
+ raise gr.Error("❌ 401 Unauthorized: Your API Key is incorrect.")
125
+ elif "quota" in error_msg.lower():
126
+ raise gr.Error("❌ Quota Exceeded: You have used all your free credits.")
127
+ else:
128
+ raise gr.Error(f"❌ ElevenLabs Error: {error_msg}")
129
 
130
  # -------------------- MATH & LOGIC --------------------
131
  def calculate_wqi(pH, do, nutrients):
 
243
  #analyze-btn { background: linear-gradient(90deg, #0061ff 0%, #60efff 100%); color: white; border: none; }
244
  """
245
 
246
+ # !!! CSS FIX APPLIED HERE !!!
247
  with gr.Blocks(title="FlumenIntel") as demo:
248
+ # Inject CSS using gr.HTML instead of the 'css' argument
249
  gr.HTML(f"<style>{custom_css}</style>")
250
 
251
  with gr.Column(elem_id="title-box"):