Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,25 +12,44 @@ FLOW_ID = "0853dfd7-558c-4958-9ca4-dc9ca8c69302"
|
|
| 12 |
APPLICATION_TOKEN = os.environ.get("APP_TOKEN")
|
| 13 |
ENDPOINT = "materialssciencebot"
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{ENDPOINT}"
|
| 17 |
-
|
| 18 |
payload = {
|
| 19 |
"input_value": message,
|
| 20 |
"output_type": "chat",
|
| 21 |
"input_type": "chat",
|
| 22 |
}
|
| 23 |
-
|
| 24 |
-
headers = {"Authorization": "Bearer "
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
st.markdown("""
|
| 29 |
<style>
|
| 30 |
.footer {
|
| 31 |
position: fixed;
|
| 32 |
bottom: 0;
|
| 33 |
-
|
| 34 |
padding: 10px;
|
| 35 |
font-size: 16px;
|
| 36 |
color: #333;
|
|
@@ -45,21 +64,30 @@ st.markdown("""
|
|
| 45 |
def main():
|
| 46 |
st.title("Materials Science Bot")
|
| 47 |
st.markdown("<h4 style='font-size: 20px;'> Ask anything related to the world of materials! 😉</h4>", unsafe_allow_html=True)
|
| 48 |
-
|
| 49 |
|
| 50 |
if st.button("Run"):
|
| 51 |
-
if not
|
| 52 |
-
st.error("Please enter a
|
| 53 |
return
|
| 54 |
|
| 55 |
try:
|
| 56 |
with st.spinner("Running flow..."):
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
response = response['outputs'][0]['outputs'][0]['results']['type']['text']
|
| 60 |
-
st.markdown(response)
|
| 61 |
except Exception as e:
|
| 62 |
-
st.error(str(e))
|
|
|
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
main()
|
|
|
|
| 12 |
APPLICATION_TOKEN = os.environ.get("APP_TOKEN")
|
| 13 |
ENDPOINT = "materialssciencebot"
|
| 14 |
|
| 15 |
+
# Function to call API with improved error handling
|
| 16 |
+
def run_flow(message: str):
|
| 17 |
api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{ENDPOINT}"
|
| 18 |
+
|
| 19 |
payload = {
|
| 20 |
"input_value": message,
|
| 21 |
"output_type": "chat",
|
| 22 |
"input_type": "chat",
|
| 23 |
}
|
| 24 |
+
|
| 25 |
+
headers = {"Authorization": f"Bearer {APPLICATION_TOKEN}", "Content-Type": "application/json"}
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = requests.post(api_url, json=payload, headers=headers, timeout=60, stream=True)
|
| 29 |
+
response.raise_for_status() # Raise error for HTTP failures
|
| 30 |
+
|
| 31 |
+
collected_response = ""
|
| 32 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 33 |
+
if chunk:
|
| 34 |
+
collected_response += chunk.decode()
|
| 35 |
+
yield collected_response # Stream output gradually
|
| 36 |
+
|
| 37 |
+
except requests.exceptions.RequestException as e:
|
| 38 |
+
yield f"Error: {str(e)}"
|
| 39 |
+
except json.JSONDecodeError:
|
| 40 |
+
yield "Error: Invalid JSON response from API"
|
| 41 |
+
|
| 42 |
+
# Cache previous API responses to improve performance
|
| 43 |
+
@st.cache_data
|
| 44 |
+
def get_response(message: str):
|
| 45 |
+
return list(run_flow(message))[-1] # Get final response
|
| 46 |
|
| 47 |
st.markdown("""
|
| 48 |
<style>
|
| 49 |
.footer {
|
| 50 |
position: fixed;
|
| 51 |
bottom: 0;
|
| 52 |
+
left: 0;
|
| 53 |
padding: 10px;
|
| 54 |
font-size: 16px;
|
| 55 |
color: #333;
|
|
|
|
| 64 |
def main():
|
| 65 |
st.title("Materials Science Bot")
|
| 66 |
st.markdown("<h4 style='font-size: 20px;'> Ask anything related to the world of materials! 😉</h4>", unsafe_allow_html=True)
|
| 67 |
+
message = st.text_area("Message", placeholder="What is oxidation?...")
|
| 68 |
|
| 69 |
if st.button("Run"):
|
| 70 |
+
if not message.strip():
|
| 71 |
+
st.error("Please enter a message")
|
| 72 |
return
|
| 73 |
|
| 74 |
try:
|
| 75 |
with st.spinner("Running flow..."):
|
| 76 |
+
response_placeholder = st.empty()
|
| 77 |
+
full_response = ""
|
| 78 |
+
|
| 79 |
+
for chunk in run_flow(message): # Stream response
|
| 80 |
+
full_response += chunk # Append the latest chunk
|
| 81 |
+
try:
|
| 82 |
+
json_response = json.loads(full_response) # Convert to JSON
|
| 83 |
+
extracted_text = json_response["outputs"][0]["outputs"][0]["results"]["message"]["text"]
|
| 84 |
+
response_placeholder.markdown(extracted_text) # Display only AI response
|
| 85 |
+
except (json.JSONDecodeError, KeyError):
|
| 86 |
+
response_placeholder.markdown(full_response) # Show raw response if parsing fails
|
| 87 |
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
+
st.error(f"⚠️ Error: {str(e)}")
|
| 90 |
+
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
main()
|