noumanjavaid commited on
Commit
e637f4b
·
verified ·
1 Parent(s): 1e32418

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -23
app.py CHANGED
@@ -16,14 +16,13 @@ from download import download_video_audio, delete_download
16
  MAX_FILE_SIZE = 41943040 # 40MB in bytes
17
  FILE_TOO_LARGE_MESSAGE = "File too large. Maximum size is 40MB."
18
 
19
- # Load environment variables
20
  load_dotenv()
21
- GROQ_API_KEY = os.environ.get("GROQ_API_KEY", None)
22
- audio_file_path = None
23
 
24
  # Initialize session states
25
  if 'api_key' not in st.session_state:
26
- st.session_state.api_key = GROQ_API_KEY
 
27
 
28
  if 'recording' not in st.session_state:
29
  st.session_state.recording = False
@@ -34,9 +33,8 @@ if 'audio_data' not in st.session_state:
34
  if 'transcript' not in st.session_state:
35
  st.session_state.transcript = ""
36
 
37
- if 'groq' not in st.session_state:
38
- if st.session_state.api_key:
39
- st.session_state.groq = Groq(api_key=st.session_state.api_key)
40
 
41
  # Set page configuration
42
  st.set_page_config(
@@ -50,6 +48,17 @@ st.set_page_config(
50
  LLM_MODEL = "deepseek-r1-distill-llama-70b"
51
  TRANSCRIPTION_MODEL = "distil-whisper-large-v3-en"
52
 
 
 
 
 
 
 
 
 
 
 
 
53
  class GenerationStatistics:
54
  def __init__(self, input_time=0, output_time=0, input_tokens=0, output_tokens=0, total_time=0, model_name=LLM_MODEL):
55
  self.input_time = input_time
@@ -205,15 +214,13 @@ class AudioRecorder:
205
 
206
  def transcribe_audio_with_groq(audio_file_path):
207
  """Transcribe audio file using Groq's transcription API"""
208
- if not st.session_state.api_key:
209
- st.error("Please provide a valid Groq API key in the sidebar.")
210
  return ""
211
 
212
- client = Groq(api_key=st.session_state.api_key)
213
-
214
  try:
215
  with open(audio_file_path, "rb") as file:
216
- transcription = client.audio.transcriptions.create(
217
  file=(audio_file_path, file.read()),
218
  model=TRANSCRIPTION_MODEL,
219
  response_format="verbose_json"
@@ -225,12 +232,10 @@ def transcribe_audio_with_groq(audio_file_path):
225
 
226
  def process_transcript(transcript):
227
  """Process transcript with Groq's DeepSeek model for highly structured notes"""
228
- if not st.session_state.api_key:
229
- st.error("Please provide a valid Groq API key in the sidebar.")
230
  return None
231
 
232
- client = Groq(api_key=st.session_state.api_key)
233
-
234
  # Enhanced structure for better organization
235
  structure = {
236
  "Executive Summary": "",
@@ -309,7 +314,7 @@ def process_transcript(transcript):
309
  stats = GenerationStatistics(model_name=LLM_MODEL)
310
  start_time = time.time()
311
 
312
- response = client.chat.completions.create(
313
  messages=[{"role": "user", "content": prompt}],
314
  model=LLM_MODEL,
315
  temperature=0.3, # Lower temperature for more structured output
@@ -415,16 +420,55 @@ def main():
415
  # Sidebar for configuration
416
  with st.sidebar:
417
  st.header("Configuration")
418
- api_key = st.text_input("Groq API Key", value=st.session_state.api_key or "", type="password")
419
 
420
- if api_key:
421
- st.session_state.api_key = api_key
422
- if 'groq' not in st.session_state or st.session_state.groq is None:
423
- st.session_state.groq = Groq(api_key=api_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
 
425
  st.markdown("---")
426
  st.info("Using DeepSeek-R1-Distill-Llama-70B model for note generation and Distil Whisper for transcription")
427
-
 
 
 
 
 
 
 
 
 
428
  # Input methods tabs
429
  input_method = st.radio("Choose input method:", ["Live Recording", "Upload Audio", "YouTube URL", "Text Input"])
430
 
 
16
  MAX_FILE_SIZE = 41943040 # 40MB in bytes
17
  FILE_TOO_LARGE_MESSAGE = "File too large. Maximum size is 40MB."
18
 
19
+ # Load environment variables in a secure way
20
  load_dotenv()
 
 
21
 
22
  # Initialize session states
23
  if 'api_key' not in st.session_state:
24
+ # Try to get API key from environment variable first
25
+ st.session_state.api_key = os.environ.get("GROQ_API_KEY", "")
26
 
27
  if 'recording' not in st.session_state:
28
  st.session_state.recording = False
 
33
  if 'transcript' not in st.session_state:
34
  st.session_state.transcript = ""
35
 
36
+ if 'groq_client' not in st.session_state:
37
+ st.session_state.groq_client = None
 
38
 
39
  # Set page configuration
40
  st.set_page_config(
 
48
  LLM_MODEL = "deepseek-r1-distill-llama-70b"
49
  TRANSCRIPTION_MODEL = "distil-whisper-large-v3-en"
50
 
51
+ # Initialize Groq client function
52
+ def initialize_groq_client(api_key):
53
+ """Initialize Groq client with the provided API key"""
54
+ if not api_key:
55
+ return None
56
+ try:
57
+ return Groq(api_key=api_key)
58
+ except Exception as e:
59
+ st.error(f"Failed to initialize Groq client: {e}")
60
+ return None
61
+
62
  class GenerationStatistics:
63
  def __init__(self, input_time=0, output_time=0, input_tokens=0, output_tokens=0, total_time=0, model_name=LLM_MODEL):
64
  self.input_time = input_time
 
214
 
215
  def transcribe_audio_with_groq(audio_file_path):
216
  """Transcribe audio file using Groq's transcription API"""
217
+ if not st.session_state.groq_client:
218
+ st.error("Groq client is not initialized. Please check your API key.")
219
  return ""
220
 
 
 
221
  try:
222
  with open(audio_file_path, "rb") as file:
223
+ transcription = st.session_state.groq_client.audio.transcriptions.create(
224
  file=(audio_file_path, file.read()),
225
  model=TRANSCRIPTION_MODEL,
226
  response_format="verbose_json"
 
232
 
233
  def process_transcript(transcript):
234
  """Process transcript with Groq's DeepSeek model for highly structured notes"""
235
+ if not st.session_state.groq_client:
236
+ st.error("Groq client is not initialized. Please check your API key.")
237
  return None
238
 
 
 
239
  # Enhanced structure for better organization
240
  structure = {
241
  "Executive Summary": "",
 
314
  stats = GenerationStatistics(model_name=LLM_MODEL)
315
  start_time = time.time()
316
 
317
+ response = st.session_state.groq_client.chat.completions.create(
318
  messages=[{"role": "user", "content": prompt}],
319
  model=LLM_MODEL,
320
  temperature=0.3, # Lower temperature for more structured output
 
420
  # Sidebar for configuration
421
  with st.sidebar:
422
  st.header("Configuration")
 
423
 
424
+ # Improved API key handling
425
+ api_key_input = st.text_input(
426
+ "Groq API Key",
427
+ value=st.session_state.api_key,
428
+ type="password",
429
+ help="Enter your Groq API key. Get one at https://console.groq.com/keys"
430
+ )
431
+
432
+ # Only update if changed
433
+ if api_key_input != st.session_state.api_key:
434
+ st.session_state.api_key = api_key_input
435
+ # Reinitialize client with new key
436
+ st.session_state.groq_client = initialize_groq_client(api_key_input)
437
+
438
+ if st.session_state.groq_client:
439
+ st.success("✅ API key validated!")
440
+
441
+ # Initialize client if not done already
442
+ if st.session_state.api_key and not st.session_state.groq_client:
443
+ st.session_state.groq_client = initialize_groq_client(st.session_state.api_key)
444
+
445
+ # API key instructions
446
+ with st.expander("How to get a Groq API key"):
447
+ st.markdown("""
448
+ 1. Visit [Groq Cloud Console](https://console.groq.com/)
449
+ 2. Sign up or log in to your account
450
+ 3. Navigate to API Keys in the sidebar
451
+ 4. Click "Create API Key" and give it a name
452
+ 5. Copy the generated key (you won't see it again!)
453
+
454
+ **Security tip**: For production use, store your API key as an environment variable:
455
+ ```bash
456
+ export GROQ_API_KEY="your-api-key-here"
457
+ ```
458
+ """)
459
 
460
  st.markdown("---")
461
  st.info("Using DeepSeek-R1-Distill-Llama-70B model for note generation and Distil Whisper for transcription")
462
+
463
+ # Check if API key is valid before proceeding
464
+ if not st.session_state.api_key:
465
+ st.warning("⚠️ Please enter your Groq API key in the sidebar to use ScribeWizard")
466
+ st.stop()
467
+
468
+ if not st.session_state.groq_client:
469
+ st.error("❌ Failed to initialize Groq client. Please check your API key.")
470
+ st.stop()
471
+
472
  # Input methods tabs
473
  input_method = st.radio("Choose input method:", ["Live Recording", "Upload Audio", "YouTube URL", "Text Input"])
474