Bartix84 commited on
Commit
5892a3b
·
verified ·
1 Parent(s): d6b854e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -10
main.py CHANGED
@@ -172,8 +172,13 @@ QUESTION:
172
  {user_query}
173
  ANSWER:"""
174
 
 
 
 
 
 
175
  headers = {
176
- "Authorization": f"Bearer {st.secrets['OPENROUTER_API_KEY']}",
177
  "HTTP-Referer": "https://yourappname.streamlit.app",
178
  "X-Title": "PoliticBot"
179
  }
@@ -189,18 +194,22 @@ ANSWER:"""
189
  "top_p": 0.95
190
  }
191
 
192
- response = httpx.post(
193
- "https://openrouter.ai/api/v1/chat/completions",
194
- headers=headers,
195
- json=payload,
196
- timeout=60
197
- )
198
-
199
- if response.status_code != 200:
200
- return f"❌ Error {response.status_code}: {response.text}"
 
 
 
201
 
202
  return response.json()["choices"][0]["message"]["content"].strip()
203
 
 
204
  # ---------------------- STREAMLIT INTERFACE ----------------------
205
 
206
  st.image('portada3.jpg', use_container_width=True)
 
172
  {user_query}
173
  ANSWER:"""
174
 
175
+ # Verificar si existe la API Key como secreto
176
+ api_key = st.secrets.get("OPENROUTER_API_KEY", None)
177
+ if not api_key:
178
+ return "❌ Error: Missing `OPENROUTER_API_KEY` in Hugging Face Space secrets."
179
+
180
  headers = {
181
+ "Authorization": f"Bearer {api_key}",
182
  "HTTP-Referer": "https://yourappname.streamlit.app",
183
  "X-Title": "PoliticBot"
184
  }
 
194
  "top_p": 0.95
195
  }
196
 
197
+ try:
198
+ response = httpx.post(
199
+ "https://openrouter.ai/api/v1/chat/completions",
200
+ headers=headers,
201
+ json=payload,
202
+ timeout=60
203
+ )
204
+ response.raise_for_status()
205
+ except httpx.RequestError as e:
206
+ return f"❌ Connection error: {e}"
207
+ except httpx.HTTPStatusError as e:
208
+ return f"❌ API error: {e.response.status_code} - {e.response.text}"
209
 
210
  return response.json()["choices"][0]["message"]["content"].strip()
211
 
212
+
213
  # ---------------------- STREAMLIT INTERFACE ----------------------
214
 
215
  st.image('portada3.jpg', use_container_width=True)