Spaces:
Sleeping
Sleeping
Update src/app.py
Browse fileslets users enter an OpenAI api key if they have one
- src/app.py +23 -3
src/app.py
CHANGED
|
@@ -112,6 +112,18 @@ with st.sidebar:
|
|
| 112 |
key="model_selector_radio" # Unique Key Added for Safety
|
| 113 |
)
|
| 114 |
st.info(f"Connected to: **{model_choice}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
st.divider()
|
| 117 |
st.header("⚙️ Controls")
|
|
@@ -177,10 +189,18 @@ def query_local_model(messages, max_tokens, model_name):
|
|
| 177 |
return f"Connection Error: {e}", None
|
| 178 |
|
| 179 |
def query_openai_model(messages, max_tokens):
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
-
client = OpenAI(api_key=
|
| 184 |
|
| 185 |
try:
|
| 186 |
response = client.chat.completions.create(
|
|
|
|
| 112 |
key="model_selector_radio" # Unique Key Added for Safety
|
| 113 |
)
|
| 114 |
st.info(f"Connected to: **{model_choice}**")
|
| 115 |
+
|
| 116 |
+
# --- REGULAR USER OPENAI API KEY ---
|
| 117 |
+
user_api_key = st.text_input(
|
| 118 |
+
"OpenAI API Key (Optional)",
|
| 119 |
+
type="password",
|
| 120 |
+
help="Enter your own key to bypass limits."
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if user_api_key:
|
| 124 |
+
st.session_state.user_openai_key = user_api_key
|
| 125 |
+
else:
|
| 126 |
+
st.session_state.user_openai_key = None
|
| 127 |
|
| 128 |
st.divider()
|
| 129 |
st.header("⚙️ Controls")
|
|
|
|
| 189 |
return f"Connection Error: {e}", None
|
| 190 |
|
| 191 |
def query_openai_model(messages, max_tokens):
|
| 192 |
+
# 1. Check for User Key first
|
| 193 |
+
api_key_to_use = st.session_state.get("user_openai_key")
|
| 194 |
+
|
| 195 |
+
# 2. Fallback to System Key
|
| 196 |
+
if not api_key_to_use:
|
| 197 |
+
api_key_to_use = OPENAI_KEY
|
| 198 |
+
|
| 199 |
+
# 3. Final Safety Check
|
| 200 |
+
if not api_key_to_use:
|
| 201 |
+
return "Error: No API Key available. Please enter one in the sidebar.", None
|
| 202 |
|
| 203 |
+
client = OpenAI(api_key=api_key_to_use)
|
| 204 |
|
| 205 |
try:
|
| 206 |
response = client.chat.completions.create(
|