Wall06 commited on
Commit
45a8c99
·
verified ·
1 Parent(s): 3c70ae8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -11,6 +11,7 @@ from sentinelhub import SHConfig
11
  from groq import Groq
12
  import google.generativeai as genai
13
  import tempfile
 
14
 
15
  # -------------------- ENVIRONMENT VARIABLES --------------------
16
  HF_API_KEY = os.getenv("HF_API_KEY")
@@ -78,21 +79,32 @@ def smart_summary(text):
78
  errors.append(f"HF: {err}")
79
  return "⚠ SYSTEM FAILURE. DEBUG LOG:\n" + "\n".join(errors)
80
 
81
- # -------------------- AUDIO FUNCTION (STABLE gTTS) --------------------
82
  def generate_audio_report(text):
83
  try:
84
  from gtts import gTTS
85
  except ImportError:
86
  raise gr.Error("❌ Library Missing! Add 'gTTS' to requirements.txt")
87
 
88
- if not text or "SYSTEM FAILURE" in text:
89
- raise gr.Error("❌ No valid report text found. Generate report first!")
 
 
 
90
 
91
  try:
92
- tts = gTTS(text=text[:1500], lang='en')
93
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
94
- tts.save(f.name)
95
- return f.name
 
 
 
 
 
 
 
 
96
  except Exception as e:
97
  raise gr.Error(f"Speech Generation Error: {str(e)}")
98
 
@@ -205,7 +217,7 @@ custom_css = """
205
  #title-box { background: linear-gradient(135deg, #0061ff 0%, #60efff 100%); color: white; padding: 20px; border-radius: 12px; text-align: center;}
206
  #analyze-btn { background: #0061ff; color: white; border: none; font-weight: bold; cursor: pointer; border-radius: 8px;}
207
 
208
- /* Custom Scroll Wheel for Official Report */
209
  #scroll-area textarea {
210
  overflow-y: scroll !important;
211
  scrollbar-width: thin;
@@ -220,7 +232,6 @@ custom_css = """
220
  #scroll-area textarea::-webkit-scrollbar-thumb {
221
  background-color: #0061ff;
222
  border-radius: 10px;
223
- border: 2px solid #1a1a1a;
224
  }
225
  """
226
 
@@ -258,12 +269,11 @@ with gr.Blocks(title="FlumenIntel") as demo:
258
  graph_summary_box = gr.Markdown("### Insights...")
259
 
260
  with gr.TabItem("📄 Official Report"):
261
- # Applied elem_id here to link with CSS
262
  ai_summary = gr.Textbox(
263
  label="Scientist's Assessment",
264
  lines=15,
265
  interactive=False,
266
- elem_id="scroll-area"
267
  )
268
 
269
  with gr.Row():
 
11
  from groq import Groq
12
  import google.generativeai as genai
13
  import tempfile
14
+ import time # For rate limit handling
15
 
16
  # -------------------- ENVIRONMENT VARIABLES --------------------
17
  HF_API_KEY = os.getenv("HF_API_KEY")
 
79
  errors.append(f"HF: {err}")
80
  return "⚠ SYSTEM FAILURE. DEBUG LOG:\n" + "\n".join(errors)
81
 
82
+ # -------------------- AUDIO FUNCTION (IMPROVED) --------------------
83
  def generate_audio_report(text):
84
  try:
85
  from gtts import gTTS
86
  except ImportError:
87
  raise gr.Error("❌ Library Missing! Add 'gTTS' to requirements.txt")
88
 
89
+ if not text or len(text.strip()) < 5:
90
+ raise gr.Error("❌ Report is too short or empty. Generate a report first!")
91
+
92
+ # Clean text of Markdown symbols for better speech
93
+ clean_text = text.replace("**", "").replace("#", "")
94
 
95
  try:
96
+ # Retry logic for rate limits
97
+ for i in range(3):
98
+ try:
99
+ tts = gTTS(text=clean_text[:1000], lang='en')
100
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
101
+ tts.save(f.name)
102
+ return f.name
103
+ except Exception as e:
104
+ if "429" in str(e) and i < 2:
105
+ time.sleep(2) # Wait 2 seconds and try again
106
+ continue
107
+ raise e
108
  except Exception as e:
109
  raise gr.Error(f"Speech Generation Error: {str(e)}")
110
 
 
217
  #title-box { background: linear-gradient(135deg, #0061ff 0%, #60efff 100%); color: white; padding: 20px; border-radius: 12px; text-align: center;}
218
  #analyze-btn { background: #0061ff; color: white; border: none; font-weight: bold; cursor: pointer; border-radius: 8px;}
219
 
220
+ /* Custom Scroll Wheel Styling */
221
  #scroll-area textarea {
222
  overflow-y: scroll !important;
223
  scrollbar-width: thin;
 
232
  #scroll-area textarea::-webkit-scrollbar-thumb {
233
  background-color: #0061ff;
234
  border-radius: 10px;
 
235
  }
236
  """
237
 
 
269
  graph_summary_box = gr.Markdown("### Insights...")
270
 
271
  with gr.TabItem("📄 Official Report"):
 
272
  ai_summary = gr.Textbox(
273
  label="Scientist's Assessment",
274
  lines=15,
275
  interactive=False,
276
+ elem_id="scroll-area" # Added specifically for the CSS
277
  )
278
 
279
  with gr.Row():