Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,6 +65,7 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 65 |
with st.spinner("Generating response..."):
|
| 66 |
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 67 |
|
|
|
|
| 68 |
response = requests.post(
|
| 69 |
API_URL,
|
| 70 |
headers=headers,
|
|
@@ -77,12 +78,34 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 77 |
"return_full_text": False
|
| 78 |
}
|
| 79 |
}
|
| 80 |
-
)
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
with st.chat_message("assistant"):
|
| 88 |
st.markdown(assistant_response)
|
|
@@ -90,4 +113,4 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 90 |
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
-
st.error(f"
|
|
|
|
| 65 |
with st.spinner("Generating response..."):
|
| 66 |
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 67 |
|
| 68 |
+
# Make API request
|
| 69 |
response = requests.post(
|
| 70 |
API_URL,
|
| 71 |
headers=headers,
|
|
|
|
| 78 |
"return_full_text": False
|
| 79 |
}
|
| 80 |
}
|
| 81 |
+
)
|
| 82 |
|
| 83 |
+
# Handle API errors
|
| 84 |
+
if response.status_code != 200:
|
| 85 |
+
error_msg = response.json().get('error', 'Unknown API error')
|
| 86 |
+
st.error(f"API Error: {error_msg}")
|
| 87 |
+
if "loading" in error_msg.lower():
|
| 88 |
+
st.info("Please wait a moment and try again. The model might be loading.")
|
| 89 |
+
return
|
| 90 |
+
|
| 91 |
+
# Process successful response
|
| 92 |
+
result = response.json()
|
| 93 |
+
|
| 94 |
+
if isinstance(result, list):
|
| 95 |
+
# Handle normal response format
|
| 96 |
+
assistant_response = result[0].get('generated_text', 'No response generated')
|
| 97 |
+
|
| 98 |
+
# Clean up response
|
| 99 |
+
if "Assistant:" in assistant_response:
|
| 100 |
+
assistant_response = assistant_response.split("Assistant:")[-1].strip()
|
| 101 |
+
|
| 102 |
+
elif isinstance(result, dict) and 'error' in result:
|
| 103 |
+
# Handle error format
|
| 104 |
+
st.error(f"API Error: {result['error']}")
|
| 105 |
+
return
|
| 106 |
else:
|
| 107 |
+
st.error("Unexpected response format from API")
|
| 108 |
+
return
|
| 109 |
|
| 110 |
with st.chat_message("assistant"):
|
| 111 |
st.markdown(assistant_response)
|
|
|
|
| 113 |
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
+
st.error(f"Application Error: {str(e)}")
|