shahabkahn commited on
Commit
8615772
·
verified ·
1 Parent(s): dd37d41

Update frontend.py

Browse files
Files changed (1) hide show
  1. frontend.py +70 -70
frontend.py CHANGED
@@ -1,70 +1,70 @@
1
- import requests
2
- import streamlit as st
3
- from dotenv import load_dotenv
4
- import os
5
-
6
- load_dotenv()
7
-
8
- # Define the URL of your FastAPI endpoint
9
- url = "http://localhost:8000/query"
10
-
11
- # Initialize the session state
12
- st.session_state.setdefault("chat_history", [])
13
-
14
- # Function to handle the new chat button click
15
- def new_chat():
16
- st.session_state.chat_history = [] # Clear the chat history
17
-
18
- # Streamlit app
19
- def app():
20
- st.title("Doctor's Medical Assistant")
21
- st.sidebar.button("New Chat", on_click=new_chat)
22
- st.image("2.jpg", width=300)
23
-
24
- # Display Welcome message
25
- st.write("<span style='font-size:20px; font-weight:bold;'>Welcome! How Can I Help You</span>",
26
- unsafe_allow_html=True)
27
-
28
- # Placeholder text for the input box
29
- input_placeholder = st.empty()
30
- input_text = input_placeholder.text_input("", key="user_input", help="Type your question here...")
31
-
32
- # JavaScript to handle the placeholder behavior
33
- placeholder_script = f"""
34
- <script>
35
- const inputElement = document.querySelector('input[data-baseweb="input"]');
36
- inputElement.placeholder = "Enter your question";
37
- </script>
38
- """
39
- st.markdown(placeholder_script, unsafe_allow_html=True)
40
-
41
- # Handle form submission
42
- submit_button = st.button("➡️")
43
- if submit_button:
44
- user_input = input_text.strip()
45
- if user_input:
46
- # Create the request payload
47
- payload = {"question": user_input}
48
- try:
49
- # Send the POST request to the FastAPI endpoint
50
- response = requests.post(url, json=payload)
51
- # Check if the request was successful
52
- if response.ok:
53
- # Get the answer from the FastAPI endpoint
54
- answer = response.json().get("answer")
55
- st.session_state.chat_history.append({"role": "user", "content": user_input})
56
- st.session_state.chat_history.append({"role": "assistant", "content": answer})
57
- else:
58
- st.error(f"Error: {response.status_code} {response.text}")
59
- except requests.RequestException as e:
60
- st.error(f"Error: {e}")
61
-
62
- # Display chat history
63
- for chat in st.session_state.chat_history:
64
- if chat["role"] == "user":
65
- st.write(f"**You:** {chat['content']}")
66
- else:
67
- st.write(f"**Assistant:** {chat['content']}")
68
-
69
- if __name__ == "__main__":
70
- app()
 
1
+ import requests
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ import os
5
+
6
+ load_dotenv()
7
+
8
+ # Define the URL of your FastAPI endpoint
9
+ url = "http://localhost:7860/query"
10
+
11
+ # Initialize the session state
12
+ st.session_state.setdefault("chat_history", [])
13
+
14
+ # Function to handle the new chat button click
15
+ def new_chat():
16
+ st.session_state.chat_history = [] # Clear the chat history
17
+
18
+ # Streamlit app
19
+ def app():
20
+ st.title("Doctor's Medical Assistant")
21
+ st.sidebar.button("New Chat", on_click=new_chat)
22
+ st.image("2.jpg", width=300)
23
+
24
+ # Display Welcome message
25
+ st.write("<span style='font-size:20px; font-weight:bold;'>Welcome! How Can I Help You</span>",
26
+ unsafe_allow_html=True)
27
+
28
+ # Placeholder text for the input box
29
+ input_placeholder = st.empty()
30
+ input_text = input_placeholder.text_input("", key="user_input", help="Type your question here...")
31
+
32
+ # JavaScript to handle the placeholder behavior
33
+ placeholder_script = f"""
34
+ <script>
35
+ const inputElement = document.querySelector('input[data-baseweb="input"]');
36
+ inputElement.placeholder = "Enter your question";
37
+ </script>
38
+ """
39
+ st.markdown(placeholder_script, unsafe_allow_html=True)
40
+
41
+ # Handle form submission
42
+ submit_button = st.button("➡️")
43
+ if submit_button:
44
+ user_input = input_text.strip()
45
+ if user_input:
46
+ # Create the request payload
47
+ payload = {"question": user_input}
48
+ try:
49
+ # Send the POST request to the FastAPI endpoint
50
+ response = requests.post(url, json=payload)
51
+ # Check if the request was successful
52
+ if response.ok:
53
+ # Get the answer from the FastAPI endpoint
54
+ answer = response.json().get("answer")
55
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
56
+ st.session_state.chat_history.append({"role": "assistant", "content": answer})
57
+ else:
58
+ st.error(f"Error: {response.status_code} {response.text}")
59
+ except requests.RequestException as e:
60
+ st.error(f"Error: {e}")
61
+
62
+ # Display chat history
63
+ for chat in st.session_state.chat_history:
64
+ if chat["role"] == "user":
65
+ st.write(f"**You:** {chat['content']}")
66
+ else:
67
+ st.write(f"**Assistant:** {chat['content']}")
68
+
69
+ if __name__ == "__main__":
70
+ app()