Daaku-C5 commited on
Commit
965fe26
·
verified ·
1 Parent(s): 4052c60

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +27 -6
src/streamlit_app.py CHANGED
@@ -20,18 +20,39 @@ TEMP_AUDIO_FILE = "temp_audio.wav"
20
  @st.cache_resource
21
  def init_openai_client():
22
  try:
23
- # Try to get API key from Streamlit secrets first (for HF Spaces)
24
- api_key = st.secrets.get("openai", None)
 
 
 
 
 
25
  if not api_key:
26
- # Fallback to environment variable
27
- api_key = os.environ.get("openai")
 
 
28
 
 
29
  if not api_key:
30
- st.error("⚠️ OpenAI API key not found. Please add OPENAI_API_KEY to your Hugging Face Spaces secrets.")
31
- st.info("Go to Settings → Repository secrets → Add OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
32
  st.stop()
33
 
34
  return OpenAI(api_key=api_key)
 
35
  except Exception as e:
36
  st.error(f"Error initializing OpenAI client: {str(e)}")
37
  st.stop()
 
20
  @st.cache_resource
21
  def init_openai_client():
22
  try:
23
+ # Check for API key in multiple places
24
+ api_key = None
25
+
26
+ # 1. Try environment variable first (HF Spaces secrets appear as env vars)
27
+ api_key = os.environ.get("OPENAI_API_KEY")
28
+
29
+ # 2. Try Streamlit secrets (for local development)
30
  if not api_key:
31
+ try:
32
+ api_key = st.secrets["OPENAI_API_KEY"]
33
+ except (KeyError, FileNotFoundError):
34
+ pass
35
 
36
+ # 3. Check if we found the key
37
  if not api_key:
38
+ st.error("⚠️ OpenAI API key not found!")
39
+ st.markdown("""
40
+ **For Hugging Face Spaces:**
41
+ 1. Go to your Space settings
42
+ 2. Click on "Repository secrets"
43
+ 3. Add a new secret with name: `OPENAI_API_KEY`
44
+ 4. Restart your Space
45
+
46
+ **For local development:**
47
+ Create `.streamlit/secrets.toml` with:
48
+ ```
49
+ OPENAI_API_KEY = "your-key-here"
50
+ ```
51
+ """)
52
  st.stop()
53
 
54
  return OpenAI(api_key=api_key)
55
+
56
  except Exception as e:
57
  st.error(f"Error initializing OpenAI client: {str(e)}")
58
  st.stop()