Spaces:
Runtime error
Runtime error
Commit
·
07f2c26
1
Parent(s):
9ad06d5
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import pickle
|
3 |
import pandas as pd
|
4 |
import torch
|
@@ -58,6 +59,14 @@ model = CLIPModel().to(device)
|
|
58 |
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
59 |
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def show_predicted_caption(image):
|
62 |
matches = predict_caption(
|
63 |
image, model, text_embeddings, testing_df["caption"]
|
@@ -99,6 +108,9 @@ def download_link(content, filename, link_text):
|
|
99 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
100 |
return href
|
101 |
|
|
|
|
|
|
|
102 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
103 |
|
104 |
# Collect user's personal information
|
@@ -134,26 +146,28 @@ if uploaded_file is not None:
|
|
134 |
st.write(radiology_report_with_personal_info)
|
135 |
st.markdown(download_link(save_as_docx(radiology_report_with_personal_info, "radiology_report.docx"), "radiology_report.docx", "Download Report as DOCX"), unsafe_allow_html=True)
|
136 |
|
137 |
-
#
|
138 |
st.header("1-to-1 Consultation")
|
139 |
st.write("Ask any questions you have about the radiology report:")
|
140 |
-
|
141 |
-
user_input = st.text_input("Enter your question:")
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
if user_input.lower() == "thank you":
|
148 |
st.write("Bot: You're welcome! If you have any more questions, feel free to ask.")
|
|
|
|
|
149 |
else:
|
150 |
# Generate the answer to the user's question
|
151 |
-
prompt = f"Answer to the user's question based on the generated radiology report: {user_input}"
|
152 |
-
for history_item in chat_history:
|
153 |
prompt += f"\nUser: {history_item['user']}"
|
154 |
if 'bot' in history_item:
|
155 |
prompt += f"\nBot: {history_item['bot']}"
|
156 |
-
|
157 |
answer = chatbot_response(prompt)
|
158 |
-
chat_history[-1]["bot"] = answer
|
159 |
st.write(f"Bot: {answer}")
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from streamlit.state import session_state
|
3 |
import pickle
|
4 |
import pandas as pd
|
5 |
import torch
|
|
|
59 |
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
60 |
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
61 |
|
62 |
+
# Add this function to initialize the session state
|
63 |
+
def init_state():
|
64 |
+
if 'user_input' not in session_state:
|
65 |
+
session_state.user_input = ''
|
66 |
+
if 'chat_history' not in session_state:
|
67 |
+
session_state.chat_history = []
|
68 |
+
|
69 |
+
|
70 |
def show_predicted_caption(image):
|
71 |
matches = predict_caption(
|
72 |
image, model, text_embeddings, testing_df["caption"]
|
|
|
108 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
109 |
return href
|
110 |
|
111 |
+
# Initialize the session state
|
112 |
+
init_state()
|
113 |
+
|
114 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
115 |
|
116 |
# Collect user's personal information
|
|
|
146 |
st.write(radiology_report_with_personal_info)
|
147 |
st.markdown(download_link(save_as_docx(radiology_report_with_personal_info, "radiology_report.docx"), "radiology_report.docx", "Download Report as DOCX"), unsafe_allow_html=True)
|
148 |
|
149 |
+
# Modify the 1-to-1 consultation section
|
150 |
st.header("1-to-1 Consultation")
|
151 |
st.write("Ask any questions you have about the radiology report:")
|
152 |
+
|
153 |
+
session_state.user_input = st.text_input("Enter your question:", value=session_state.user_input)
|
154 |
+
|
155 |
+
if session_state.user_input:
|
156 |
+
session_state.chat_history.append({"user": session_state.user_input})
|
157 |
+
|
158 |
+
if session_state.user_input.lower() == "thank you":
|
|
|
159 |
st.write("Bot: You're welcome! If you have any more questions, feel free to ask.")
|
160 |
+
session_state.user_input = ''
|
161 |
+
session_state.chat_history = []
|
162 |
else:
|
163 |
# Generate the answer to the user's question
|
164 |
+
prompt = f"Answer to the user's question based on the generated radiology report: {session_state.user_input}"
|
165 |
+
for history_item in session_state.chat_history:
|
166 |
prompt += f"\nUser: {history_item['user']}"
|
167 |
if 'bot' in history_item:
|
168 |
prompt += f"\nBot: {history_item['bot']}"
|
169 |
+
|
170 |
answer = chatbot_response(prompt)
|
171 |
+
session_state.chat_history[-1]["bot"] = answer
|
172 |
st.write(f"Bot: {answer}")
|
173 |
+
session_state.user_input = ''
|