Enrique Cardoza commited on
Commit
80bdfa1
·
1 Parent(s): 3eb58d7

feat(auth): add environment variable support for GROQ_API_KEY

Browse files

- Add support for GROQ_API_KEY environment variable as primary API key source
- Fall back to user-provided API key in the UI when env var is not set
- Update error message to mention both authentication options
- Add informational note in UI when environment variable is detected
- Update placeholder text to guide users on both authentication methods
- Improve overall flexibility for different deployment scenarios

Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -42,9 +42,12 @@ def transcribe_audio(audio_file, api_key):
42
  Upload a podcast episode to get a complete text transcript.
43
  """
44
  try:
45
- # Validate inputs
46
- if not api_key:
47
- return "Error: Please provide your Groq API key"
 
 
 
48
 
49
  if audio_file is None:
50
  return "Error: Please upload an audio or video file"
@@ -55,7 +58,7 @@ def transcribe_audio(audio_file, api_key):
55
  return f"Error: {message}"
56
 
57
  # Initialize Groq client
58
- client = Groq(api_key=api_key)
59
 
60
  # Read the audio file
61
  with open(audio_file.name, "rb") as file:
@@ -86,11 +89,15 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
86
  file_count="single"
87
  )
88
 
 
 
 
89
  api_key_input = gr.Textbox(
90
  label="Groq API Key",
91
- placeholder="Enter your Groq API key here...",
92
  type="password",
93
- lines=1
 
94
  )
95
 
96
  transcribe_btn = gr.Button(
 
42
  Upload a podcast episode to get a complete text transcript.
43
  """
44
  try:
45
+ # First check for environment variable, then use provided API key
46
+ actual_api_key = os.environ.get("GROQ_API_KEY", api_key)
47
+
48
+ # Validate API key
49
+ if not actual_api_key:
50
+ return "Error: Please provide your Groq API key or set the GROQ_API_KEY environment variable"
51
 
52
  if audio_file is None:
53
  return "Error: Please upload an audio or video file"
 
58
  return f"Error: {message}"
59
 
60
  # Initialize Groq client
61
+ client = Groq(api_key=actual_api_key)
62
 
63
  # Read the audio file
64
  with open(audio_file.name, "rb") as file:
 
89
  file_count="single"
90
  )
91
 
92
+ # Show a note if env var is present
93
+ api_key_note = "API key will be used from environment variable if set" if os.environ.get("GROQ_API_KEY") else ""
94
+
95
  api_key_input = gr.Textbox(
96
  label="Groq API Key",
97
+ placeholder="Enter your Groq API key here or set GROQ_API_KEY environment variable",
98
  type="password",
99
+ lines=1,
100
+ info=api_key_note
101
  )
102
 
103
  transcribe_btn = gr.Button(