Spaces:
Runtime error
Runtime error
Commit ·
b84092b
1
Parent(s): edd8d6d
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,16 +57,10 @@ model = CLIPModel().to(device)
|
|
| 57 |
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
| 58 |
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
if 'user_input' not in st.session_state:
|
| 62 |
-
st.session_state.user_input = ''
|
| 63 |
-
if 'chat_history' not in st.session_state:
|
| 64 |
-
st.session_state.chat_history = []
|
| 65 |
-
|
| 66 |
-
def show_predicted_caption(image):
|
| 67 |
matches = predict_caption(
|
| 68 |
image, model, text_embeddings, testing_df["caption"]
|
| 69 |
-
)[
|
| 70 |
return matches
|
| 71 |
|
| 72 |
def generate_radiology_report(prompt):
|
|
@@ -80,17 +74,6 @@ def generate_radiology_report(prompt):
|
|
| 80 |
)
|
| 81 |
return response.choices[0].text.strip()
|
| 82 |
|
| 83 |
-
def chatbot_response(prompt):
|
| 84 |
-
response = openai.Completion.create(
|
| 85 |
-
engine="text-davinci-003",
|
| 86 |
-
prompt=prompt,
|
| 87 |
-
max_tokens=500,
|
| 88 |
-
n=1,
|
| 89 |
-
stop=None,
|
| 90 |
-
temperature=0.8,
|
| 91 |
-
)
|
| 92 |
-
return response.choices[0].text.strip()
|
| 93 |
-
|
| 94 |
def save_as_docx(text, filename):
|
| 95 |
document = Document()
|
| 96 |
document.add_paragraph(text)
|
|
@@ -99,11 +82,6 @@ def save_as_docx(text, filename):
|
|
| 99 |
output.seek(0)
|
| 100 |
return output.getvalue()
|
| 101 |
|
| 102 |
-
def download_link(content, filename, link_text):
|
| 103 |
-
b64 = base64.b64encode(content).decode()
|
| 104 |
-
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
| 105 |
-
return href
|
| 106 |
-
|
| 107 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
| 108 |
|
| 109 |
# Collect user's personal information
|
|
@@ -123,7 +101,7 @@ if uploaded_file is not None:
|
|
| 123 |
if st.button("Generate Caption"):
|
| 124 |
with st.spinner("Generating caption..."):
|
| 125 |
image_np = np.array(image)
|
| 126 |
-
caption = show_predicted_caption(image_np)
|
| 127 |
|
| 128 |
st.success(f"Caption: {caption}")
|
| 129 |
|
|
@@ -141,11 +119,21 @@ if uploaded_file is not None:
|
|
| 141 |
|
| 142 |
# Advanced Feedback System
|
| 143 |
st.header("Advanced Feedback System")
|
| 144 |
-
|
| 145 |
-
feedback = st.
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
| 58 |
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
| 59 |
|
| 60 |
+
def show_predicted_caption(image, top_k=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
matches = predict_caption(
|
| 62 |
image, model, text_embeddings, testing_df["caption"]
|
| 63 |
+
)[:top_k]
|
| 64 |
return matches
|
| 65 |
|
| 66 |
def generate_radiology_report(prompt):
|
|
|
|
| 74 |
)
|
| 75 |
return response.choices[0].text.strip()
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def save_as_docx(text, filename):
|
| 78 |
document = Document()
|
| 79 |
document.add_paragraph(text)
|
|
|
|
| 82 |
output.seek(0)
|
| 83 |
return output.getvalue()
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
| 86 |
|
| 87 |
# Collect user's personal information
|
|
|
|
| 101 |
if st.button("Generate Caption"):
|
| 102 |
with st.spinner("Generating caption..."):
|
| 103 |
image_np = np.array(image)
|
| 104 |
+
caption = show_predicted_caption(image_np)[0]
|
| 105 |
|
| 106 |
st.success(f"Caption: {caption}")
|
| 107 |
|
|
|
|
| 119 |
|
| 120 |
# Advanced Feedback System
|
| 121 |
st.header("Advanced Feedback System")
|
| 122 |
+
feedback_options = ["Better", "Satisfied", "Worse"]
|
| 123 |
+
feedback = st.radio("Rate the generated report:", feedback_options)
|
| 124 |
+
|
| 125 |
+
top_k = 1
|
| 126 |
+
while feedback == "Worse":
|
| 127 |
+
top_k += 1
|
| 128 |
+
with st.spinner("Regenerating report..."):
|
| 129 |
+
new_caption = show_predicted_caption(image_np, top_k=top_k)[-1]
|
| 130 |
+
radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this: {new_caption}")
|
| 131 |
+
|
| 132 |
+
radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{radiology_report}"
|
| 133 |
+
|
| 134 |
+
with container:
|
| 135 |
+
container.empty()
|
| 136 |
+
st.header("Radiology Report")
|
| 137 |
+
st.write(radiology_report_with_personal_info)
|
| 138 |
+
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)
|
| 139 |
+
feedback = st.radio("Rate the regenerated report:", feedback_options)
|