File size: 1,627 Bytes
6e21dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9026f71
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
import streamlit as st
import requests  # Standard library for sending API requests

st.set_page_config(page_title="Medical AI Assistant", layout="centered")
st.title("⚕️ General Medicine Chatbot")

# This is the address of your FastAPI "Brain"
API_URL = "http://0.0.0.0:8000/ask"

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display previous messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("Ask a medical question..."):
    # 1. Display user message
    st.chat_message("user").markdown(prompt)
    st.session_state.messages.append({"role": "user", "content": prompt})

    # 2. Call the API
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            try:
                # We send the query to your FastAPI server
                # Note: 'query' must match the Pydantic model in main.py
                payload = {"query": prompt}
                response = requests.post(API_URL, json=payload)
                
                if response.status_code == 200:
                    answer = response.json().get("answer", "No answer found.")
                    st.markdown(answer)
                    st.session_state.messages.append({"role": "assistant", "content": answer})
                else:
                    st.error(f"API Error {response.status_code}: {response.text}")
            
            except Exception as e:
                st.error(f"Could not connect to the API. Is Uvicorn running? Error: {e}")