Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,69 @@ st.set_page_config(layout="wide")
|
|
| 8 |
API_URL = "https://virtual-eng-expert-api-v1.greensea-b20be511.northeurope.azurecontainerapps.io/api"
|
| 9 |
headers = {"Content-Type": "application/json"}
|
| 10 |
|
| 11 |
-
def query(payload):
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def generate_response(user,prompt):
|
| 16 |
output = query({"namespace":"virtualEngineer","version":1,"messages":[{"role":user, "content": prompt}]})
|
|
|
|
| 8 |
API_URL = "https://virtual-eng-expert-api-v1.greensea-b20be511.northeurope.azurecontainerapps.io/api"
|
| 9 |
headers = {"Content-Type": "application/json"}
|
| 10 |
|
| 11 |
+
def query(payload, retries=2):
|
| 12 |
+
"""Query the API with retry logic"""
|
| 13 |
+
for attempt in range(retries + 1):
|
| 14 |
+
try:
|
| 15 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
| 16 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 17 |
+
|
| 18 |
+
# Check if response is actually JSON before parsing
|
| 19 |
+
content_type = response.headers.get('Content-Type', '')
|
| 20 |
+
if 'application/json' not in content_type:
|
| 21 |
+
# Response is not JSON - likely HTML error page
|
| 22 |
+
error_text = response.text[:1000]
|
| 23 |
+
st.error(f"API returned non-JSON response (Content-Type: {content_type})")
|
| 24 |
+
st.error(f"Response preview: {error_text[:500]}")
|
| 25 |
+
with st.expander("Debug: View full response"):
|
| 26 |
+
st.text(error_text)
|
| 27 |
+
with st.expander("Debug: View request payload"):
|
| 28 |
+
st.json(payload)
|
| 29 |
+
raise ValueError(f"API returned non-JSON response. Status: {response.status_code}")
|
| 30 |
+
|
| 31 |
+
# Try to parse JSON
|
| 32 |
+
try:
|
| 33 |
+
return response.json()
|
| 34 |
+
except ValueError as json_error:
|
| 35 |
+
# Response claims to be JSON but isn't valid
|
| 36 |
+
st.error(f"API returned invalid JSON. Response text: {response.text[:500]}")
|
| 37 |
+
with st.expander("Debug: View request payload"):
|
| 38 |
+
st.json(payload)
|
| 39 |
+
raise ValueError(f"Invalid JSON response: {str(json_error)}")
|
| 40 |
+
|
| 41 |
+
except requests.exceptions.HTTPError as e:
|
| 42 |
+
if attempt < retries:
|
| 43 |
+
st.warning(f"Attempt {attempt + 1} failed with {e.response.status_code}. Retrying...")
|
| 44 |
+
continue
|
| 45 |
+
error_msg = f"API Error {e.response.status_code}"
|
| 46 |
+
try:
|
| 47 |
+
# Try to get JSON error if available
|
| 48 |
+
error_json = e.response.json()
|
| 49 |
+
if isinstance(error_json, dict):
|
| 50 |
+
error_msg += f": {error_json.get('error', error_json.get('message', str(error_json)))}"
|
| 51 |
+
else:
|
| 52 |
+
error_msg += f": {error_json}"
|
| 53 |
+
except:
|
| 54 |
+
# Not JSON, show text response
|
| 55 |
+
error_text = e.response.text[:500]
|
| 56 |
+
if error_text:
|
| 57 |
+
error_msg += f": {error_text}"
|
| 58 |
+
st.error(error_msg)
|
| 59 |
+
with st.expander("Debug: View request payload"):
|
| 60 |
+
st.json(payload)
|
| 61 |
+
raise
|
| 62 |
+
except requests.exceptions.Timeout:
|
| 63 |
+
if attempt < retries:
|
| 64 |
+
st.warning(f"Attempt {attempt + 1} timed out. Retrying...")
|
| 65 |
+
continue
|
| 66 |
+
st.error("Request timed out. The API may be slow or unavailable. Please try again.")
|
| 67 |
+
raise
|
| 68 |
+
except requests.exceptions.RequestException as e:
|
| 69 |
+
if attempt < retries:
|
| 70 |
+
st.warning(f"Attempt {attempt + 1} failed: {str(e)[:100]}. Retrying...")
|
| 71 |
+
continue
|
| 72 |
+
st.error(f"Connection Error: {str(e)}")
|
| 73 |
+
raise
|
| 74 |
|
| 75 |
def generate_response(user,prompt):
|
| 76 |
output = query({"namespace":"virtualEngineer","version":1,"messages":[{"role":user, "content": prompt}]})
|