NavyDevilDoc commited on
Commit
cae8a68
·
verified ·
1 Parent(s): ff85e7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -1,16 +1,23 @@
1
  import streamlit as st
2
  from openai import OpenAI
 
3
 
4
  # 1. Configuration
5
  st.set_page_config(page_title="High School Writing Coach", layout="wide")
6
 
7
- # Initialize client (ensure you have your API key in .streamlit/secrets.toml)
8
- # or set it directly for testing: client = OpenAI(api_key="sk-...")
9
- if "OPENAI_API_KEY" in st.secrets:
10
- client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
11
- else:
12
- st.error("Please set your OpenAI API Key in .streamlit/secrets.toml")
13
- st.stop()
 
 
 
 
 
 
14
 
15
  # 2. System Prompt
16
  # This enforces the "No Rewrite" rule.
 
1
  import streamlit as st
2
  from openai import OpenAI
3
+ import os
4
 
5
  # 1. Configuration
6
  st.set_page_config(page_title="High School Writing Coach", layout="wide")
7
 
8
+ # Get the API key from Environment Variables (HF Spaces) OR st.secrets (Local)
9
+ api_key = os.environ.get("OPENAI_API_KEY")
10
+
11
+ if not api_key:
12
+ try:
13
+ # Fallback for local development if using secrets.toml
14
+ api_key = st.secrets["OPENAI_API_KEY"]
15
+ except (FileNotFoundError, KeyError):
16
+ st.error("OpenAI API Key not found. Please add 'OPENAI_API_KEY' to your Hugging Face Space secrets.")
17
+ st.stop()
18
+
19
+ # Initialize client with the found key
20
+ client = OpenAI(api_key=api_key)
21
 
22
  # 2. System Prompt
23
  # This enforces the "No Rewrite" rule.