File size: 3,487 Bytes
7ed2f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
009bc18
7ed2f0f
 
 
 
009bc18
 
7ed2f0f
 
 
6b31376
 
 
7ed2f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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()