Spaces:
Sleeping
Sleeping
File size: 2,491 Bytes
8615772 6bb2809 8615772 6bb2809 8615772 6bb2809 8615772 6bb2809 8615772 6bb2809 8615772 |
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 |
import requests
import streamlit as st
from dotenv import load_dotenv
import os
load_dotenv()
# Define the URL of your FastAPI endpoint
url = "https://shahabkahn-medical-assistant.hf.space/query"
# Initialize the session state
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "user_input" not in st.session_state:
st.session_state.user_input = ""
# Function to handle the new chat button click
def new_chat():
st.session_state.chat_history = [] # Clear the chat history
st.session_state.user_input = "" # Clear the user input
# Streamlit app
def app():
st.title("Doctor's Medical Assistant")
st.sidebar.button("New Chat", on_click=new_chat)
st.image("2.jpg", width=300)
# Display Welcome message
st.write("<span style='font-size:20px; font-weight:bold;'>Welcome! How Can I Help You</span>",
unsafe_allow_html=True)
# Placeholder text for the input box
input_text = st.text_input("Your Question", placeholder="Enter your question here...",
key="user_input", help="Type your question here...")
# Handle form submission
submit_button = st.button("➡️")
if submit_button:
user_input = input_text.strip()
if user_input:
# Create the request payload
payload = {"question": user_input}
try:
# Send the POST request to the FastAPI endpoint
response = requests.post(url, json=payload)
# Check if the request was successful
if response.ok:
# Get the answer from the FastAPI endpoint
answer = response.json().get("answer")
st.session_state.chat_history.append({"role": "user", "content": user_input})
st.session_state.chat_history.append({"role": "assistant", "content": answer})
# Clear the user input after submission
st.session_state.user_input = ""
else:
st.error(f"Error: {response.status_code} {response.text}")
except requests.RequestException as e:
st.error(f"Error: {e}")
# Display chat history
for chat in st.session_state.chat_history:
if chat["role"] == "user":
st.write(f"**You:** {chat['content']}")
else:
st.write(f"**Assistant:** {chat['content']}")
if __name__ == "__main__":
app()
|