Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py (Smoke Test - Now with
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
import os
|
|
@@ -6,42 +6,49 @@ import os
|
|
| 6 |
# --- Config ---
|
| 7 |
# Read the BASE URL from Hugging Face secrets
|
| 8 |
BLAXEL_BASE_URL = os.getenv("BLAXEL_BACKEND_URL")
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --- Backend Client ---
|
| 11 |
def call_blaxel_backend(user_problem):
|
| 12 |
|
| 13 |
-
# 1. Check if the secret was loaded
|
| 14 |
if not BLAXEL_BASE_URL:
|
| 15 |
yield "Configuration Error: BLAXEL_BACKEND_URL secret is not set in Hugging Face."
|
| 16 |
-
yield "Please add your Blaxel URL to the 'Secrets' section of your Space settings."
|
| 17 |
return
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
full_endpoint_url = f"{BLAXEL_BASE_URL.rstrip('/')}/solve_problem"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
yield f"Attempting to connect to MudabbirAI (at {full_endpoint_url})..."
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
#
|
| 30 |
-
with httpx.stream("POST", full_endpoint_url, json={"problem": user_problem}, timeout=60) as response:
|
| 31 |
|
| 32 |
-
# ---
|
| 33 |
-
# FIX 2: Manually check for errors *before* iterating
|
| 34 |
-
# This avoids the `raise_for_status()` bug with `ResponseNotRead`
|
| 35 |
-
# ---
|
| 36 |
if response.status_code != 200:
|
| 37 |
# Manually read the error response text
|
| 38 |
error_details = response.read().decode()
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
yield f"Details: {error_details}"
|
| 42 |
-
return
|
| 43 |
|
| 44 |
-
# If we get here, the status is 200
|
| 45 |
for chunk in response.iter_text():
|
| 46 |
# Each chunk is a status update
|
| 47 |
yield chunk
|
|
|
|
| 1 |
+
# app.py (Smoke Test - Now with Authentication)
|
| 2 |
import gradio as gr
|
| 3 |
import httpx
|
| 4 |
import os
|
|
|
|
| 6 |
# --- Config ---
|
| 7 |
# Read the BASE URL from Hugging Face secrets
|
| 8 |
BLAXEL_BASE_URL = os.getenv("BLAXEL_BACKEND_URL")
|
| 9 |
+
# Read the API KEY from Hugging Face secrets
|
| 10 |
+
BLAXEL_API_KEY = os.getenv("BLAXEL_API_KEY")
|
| 11 |
|
| 12 |
# --- Backend Client ---
|
| 13 |
def call_blaxel_backend(user_problem):
|
| 14 |
|
| 15 |
+
# 1. Check if the URL secret was loaded
|
| 16 |
if not BLAXEL_BASE_URL:
|
| 17 |
yield "Configuration Error: BLAXEL_BACKEND_URL secret is not set in Hugging Face."
|
|
|
|
| 18 |
return
|
| 19 |
|
| 20 |
+
# 2. Check if the API KEY secret was loaded
|
| 21 |
+
if not BLAXEL_API_KEY:
|
| 22 |
+
yield "Configuration Error: BLAXEL_API_KEY secret is not set in Hugging Face."
|
| 23 |
+
yield "Please add your Blaxel API Key to the 'Secrets' section."
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
# 3. Construct the correct, full URL
|
| 27 |
full_endpoint_url = f"{BLAXEL_BASE_URL.rstrip('/')}/solve_problem"
|
| 28 |
|
| 29 |
+
# 4. Create the authentication headers
|
| 30 |
+
headers = {
|
| 31 |
+
"Authorization": f"Bearer {BLAXEL_API_KEY}",
|
| 32 |
+
"Content-Type": "application/json"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
yield f"Attempting to connect to MudabbirAI (at {full_endpoint_url})..."
|
| 36 |
|
| 37 |
try:
|
| 38 |
+
# 5. Pass the headers to the request
|
| 39 |
+
with httpx.stream("POST", full_endpoint_url, json={"problem": user_problem}, headers=headers, timeout=60) as response:
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if response.status_code != 200:
|
| 42 |
# Manually read the error response text
|
| 43 |
error_details = response.read().decode()
|
| 44 |
+
if response.status_code == 401 or response.status_code == 403:
|
| 45 |
+
yield f"Authentication Error (HTTP {response.status_code}): Incorrect or invalid BLAXEL_API_KEY secret."
|
| 46 |
+
else:
|
| 47 |
+
yield f"HTTP Error: Received status code {response.status_code} from Blaxel."
|
| 48 |
yield f"Details: {error_details}"
|
| 49 |
+
return
|
| 50 |
|
| 51 |
+
# If we get here, the status is 200!
|
| 52 |
for chunk in response.iter_text():
|
| 53 |
# Each chunk is a status update
|
| 54 |
yield chunk
|