Spaces:
Running
Running
import streamlit as st | |
import requests | |
import json | |
from typing import List, Dict, Any | |
# Constants | |
API_URL = "https://arpit-bansal-healthbridge.hf.space/retrieve" | |
# Session state initialization | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
if "previous_state" not in st.session_state: | |
st.session_state.previous_state = None | |
if "message_counter" not in st.session_state: | |
st.session_state.message_counter = 0 | |
def query_api(user_query: str) -> str: | |
"""Send request to the FastAPI backend and return response""" | |
payload = { | |
"query": user_query, | |
"previous_state": st.session_state.previous_state, | |
"user_data": st.session_state.get("user_data", None) | |
} | |
try: | |
response = requests.post(API_URL, json=payload) | |
response.raise_for_status() | |
result = response.json() | |
# Update previous state with full conversation history | |
st.session_state.previous_state = st.session_state.messages | |
return result["response"] | |
except requests.exceptions.RequestException as e: | |
return f"Error: {str(e)}" | |
# App UI | |
st.title("HealthBridge") | |
# User info sidebar | |
with st.sidebar: | |
st.header("User Information") | |
state = st.text_input("India-State", key="state_input") | |
gender = st.selectbox("Gender", ["", "Male", "Female", "Other", "Prefer not to say"], key="gender_input") | |
style = st.selectbox("Style", ["Normal", "concise", "explanatory"], key="style_input") | |
if st.button("Save User Info"): | |
st.session_state.user_data = { | |
"state": state if state else None, | |
"gender": gender if gender else None, | |
"style": style if style else "Normal" | |
} | |
st.success("User information saved!") | |
st.markdown('---') | |
st.caption("tells the information about healthcare and wellbeing schemes in India, based on your state") | |
st.caption("currently support : National schemes, Madhya Pradesh, Himachal Pradesh, Uttar Pradesh, 8 Union Territories.") | |
st.markdown('---') | |
st.caption("It's a preliminary stage AI, for any health consult, visit your doctor.") | |
st.caption("We currently support only few states in India, will increase the support soon.") | |
st.caption("Developed by: Arpit Bansal") | |
# Display chat history | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.write(message["content"]) | |
# Chat input | |
if prompt := st.chat_input("Ask something..."): | |
if st.session_state.message_counter >= 15: | |
st.session_state.messages = [] | |
st.session_state.previous_state = None | |
st.session_state.message_counter = 0 | |
st.info("Starting a new conversation due to message limit") | |
st.experimental_rerun() | |
# Add user message to chat history | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
st.session_state.message_counter += 1 | |
with st.chat_message("user"): | |
st.write(prompt) | |
# Get response from API | |
with st.chat_message("assistant"): | |
with st.spinner("Thinking..."): | |
response = query_api(prompt) | |
st.write(response) | |
# Add assistant response to chat history | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
# Clear chat button | |
if st.button("Clear Chat"): | |
st.session_state.messages = [] | |
st.session_state.previous_state = None | |
st.session_state.message_counter = 0 | |
st.experimental_rerun() |