Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -172,8 +172,13 @@ QUESTION:
|
|
172 |
{user_query}
|
173 |
ANSWER:"""
|
174 |
|
|
|
|
|
|
|
|
|
|
|
175 |
headers = {
|
176 |
-
"Authorization": f"Bearer {
|
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 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
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)
|