Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from typing import List, Dict, Any
|
| 5 |
+
|
| 6 |
+
# Constants
|
| 7 |
+
API_URL = "https://arpit-bansal-healthbridge.hf.space/retrieve"
|
| 8 |
+
|
| 9 |
+
# Session state initialization
|
| 10 |
+
if "messages" not in st.session_state:
|
| 11 |
+
st.session_state.messages = []
|
| 12 |
+
if "previous_state" not in st.session_state:
|
| 13 |
+
st.session_state.previous_state = None
|
| 14 |
+
if "message_counter" not in st.session_state:
|
| 15 |
+
st.session_state.message_counter = 0
|
| 16 |
+
|
| 17 |
+
def query_api(user_query: str) -> str:
|
| 18 |
+
"""Send request to the FastAPI backend and return response"""
|
| 19 |
+
payload = {
|
| 20 |
+
"query": user_query,
|
| 21 |
+
"previous_state": st.session_state.previous_state,
|
| 22 |
+
"user_data": st.session_state.get("user_data", None)
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = requests.post(API_URL, json=payload)
|
| 27 |
+
response.raise_for_status()
|
| 28 |
+
result = response.json()
|
| 29 |
+
# Update previous state with full conversation history
|
| 30 |
+
st.session_state.previous_state = st.session_state.messages
|
| 31 |
+
return result["response"]
|
| 32 |
+
except requests.exceptions.RequestException as e:
|
| 33 |
+
return f"Error: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# App UI
|
| 36 |
+
st.title("HealthBridge")
|
| 37 |
+
|
| 38 |
+
# User info sidebar
|
| 39 |
+
with st.sidebar:
|
| 40 |
+
st.header("User Information")
|
| 41 |
+
state = st.text_input("India-State", key="state_input")
|
| 42 |
+
gender = st.selectbox("Gender", ["", "Male", "Female", "Other", "Prefer not to say"], key="gender_input")
|
| 43 |
+
|
| 44 |
+
if st.button("Save User Info"):
|
| 45 |
+
st.session_state.user_data = {
|
| 46 |
+
"state": state if state else None,
|
| 47 |
+
"gender": gender if gender else None
|
| 48 |
+
}
|
| 49 |
+
st.success("User information saved!")
|
| 50 |
+
|
| 51 |
+
st.markdown('---')
|
| 52 |
+
st.caption("It's a preliminary stage AI, for any health consult, visit your doctor.")
|
| 53 |
+
st.caption("We currently support only few states in India, will increase the support soon.")
|
| 54 |
+
st.caption("Developed by: Arpit Bansal")
|
| 55 |
+
# Display chat history
|
| 56 |
+
for message in st.session_state.messages:
|
| 57 |
+
with st.chat_message(message["role"]):
|
| 58 |
+
st.write(message["content"])
|
| 59 |
+
|
| 60 |
+
# Chat input
|
| 61 |
+
if prompt := st.chat_input("Ask something..."):
|
| 62 |
+
if st.session_state.message_counter >= 15:
|
| 63 |
+
st.session_state.messages = []
|
| 64 |
+
st.session_state.previous_state = None
|
| 65 |
+
st.session_state.message_counter = 0
|
| 66 |
+
st.info("Starting a new conversation due to message limit")
|
| 67 |
+
st.experimental_rerun()
|
| 68 |
+
# Add user message to chat history
|
| 69 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 70 |
+
st.session_state.message_counter += 1
|
| 71 |
+
with st.chat_message("user"):
|
| 72 |
+
st.write(prompt)
|
| 73 |
+
|
| 74 |
+
# Get response from API
|
| 75 |
+
with st.chat_message("assistant"):
|
| 76 |
+
with st.spinner("Thinking..."):
|
| 77 |
+
response = query_api(prompt)
|
| 78 |
+
st.write(response)
|
| 79 |
+
|
| 80 |
+
# Add assistant response to chat history
|
| 81 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 82 |
+
|
| 83 |
+
# Clear chat button
|
| 84 |
+
if st.button("Clear Chat"):
|
| 85 |
+
st.session_state.messages = []
|
| 86 |
+
st.session_state.previous_state = None
|
| 87 |
+
st.session_state.message_counter = 0
|
| 88 |
+
st.experimental_rerun()
|