Spaces:
Runtime error
Runtime error
Singularity666
commited on
Commit
·
d259aad
1
Parent(s):
534753c
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import pandas as pd
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
import numpy as np
|
7 |
-
from main import predict_caption, CLIPModel, get_text_embeddings
|
8 |
import openai
|
9 |
import base64
|
10 |
from docx import Document
|
@@ -56,12 +56,6 @@ model = CLIPModel().to(device)
|
|
56 |
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
57 |
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
58 |
|
59 |
-
def reset_session_state():
|
60 |
-
st.session_state.caption = None
|
61 |
-
st.session_state.report = None
|
62 |
-
st.session_state.feedback = None
|
63 |
-
st.session_state.regenerated_report = None
|
64 |
-
|
65 |
def download_link(content, filename, link_text):
|
66 |
b64 = base64.b64encode(content).decode()
|
67 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
@@ -71,8 +65,8 @@ def show_predicted_caption(image, top_k=8):
|
|
71 |
matches = predict_caption(
|
72 |
image, model, text_embeddings, testing_df["caption"]
|
73 |
)[:top_k]
|
74 |
-
cleaned_matches = [re.sub(r'\s\(ROCO_\d+\)', '', match) for match in matches]
|
75 |
-
return cleaned_matches
|
76 |
|
77 |
def generate_radiology_report(prompt):
|
78 |
response = openai.Completion.create(
|
@@ -84,9 +78,11 @@ def generate_radiology_report(prompt):
|
|
84 |
temperature=1,
|
85 |
)
|
86 |
report = response.choices[0].text.strip()
|
|
|
87 |
report = re.sub(r'\(ROCO_\d+\)', '', report).strip()
|
88 |
return report
|
89 |
|
|
|
90 |
def save_as_docx(text, filename):
|
91 |
document = Document()
|
92 |
document.add_paragraph(text)
|
@@ -95,9 +91,6 @@ def save_as_docx(text, filename):
|
|
95 |
output.seek(0)
|
96 |
return output.getvalue()
|
97 |
|
98 |
-
# Call reset_session_state() at the beginning of the script
|
99 |
-
reset_session_state()
|
100 |
-
|
101 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
102 |
|
103 |
# Collect user's personal information
|
@@ -114,38 +107,34 @@ if uploaded_file is not None:
|
|
114 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
115 |
st.write("")
|
116 |
|
117 |
-
if st.button("Generate Caption")
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
feedback_options = ["Satisfied", "Not Satisfied"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
if st.session_state.feedback == "Not Satisfied":
|
141 |
-
if st.button("Regenerate Report") or st.session_state.regenerated_report:
|
142 |
-
if not st.session_state.regenerated_report:
|
143 |
-
with st.spinner("Regenerating report..."):
|
144 |
-
alternative_caption = get_alternative_caption(image_np, model, text_embeddings, testing_df["caption"])
|
145 |
-
st.session_state.regenerated_report = generate_radiology_report(f"Write Complete Radiology Report for this with clinical info, subjective, Assessment, Finding, Impressions, Conclusion and more in proper order : {alternative_caption}")
|
146 |
-
|
147 |
-
regenerated_radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{st.session_state.regenerated_report}"
|
148 |
-
|
149 |
-
st.header("Regenerated Radiology Report")
|
150 |
-
st.write(regenerated_radiology_report_with_personal_info)
|
151 |
-
st.markdown(download_link(save_as_docx(regenerated_radiology_report_with_personal_info, "regenerated_radiology_report.docx"), "regenerated_radiology_report.docx", "Download Regenerated Report as DOCX"), unsafe_allow_html=True)
|
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
import numpy as np
|
7 |
+
from main import predict_caption, CLIPModel, get_text_embeddings, get_alternative_caption
|
8 |
import openai
|
9 |
import base64
|
10 |
from docx import Document
|
|
|
56 |
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
57 |
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def download_link(content, filename, link_text):
|
60 |
b64 = base64.b64encode(content).decode()
|
61 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
|
|
65 |
matches = predict_caption(
|
66 |
image, model, text_embeddings, testing_df["caption"]
|
67 |
)[:top_k]
|
68 |
+
cleaned_matches = [re.sub(r'\s\(ROCO_\d+\)', '', match) for match in matches] # Add this line to clean the matches
|
69 |
+
return cleaned_matches # Return the cleaned_matches instead of matches
|
70 |
|
71 |
def generate_radiology_report(prompt):
|
72 |
response = openai.Completion.create(
|
|
|
78 |
temperature=1,
|
79 |
)
|
80 |
report = response.choices[0].text.strip()
|
81 |
+
# Remove reference string from the report
|
82 |
report = re.sub(r'\(ROCO_\d+\)', '', report).strip()
|
83 |
return report
|
84 |
|
85 |
+
|
86 |
def save_as_docx(text, filename):
|
87 |
document = Document()
|
88 |
document.add_paragraph(text)
|
|
|
91 |
output.seek(0)
|
92 |
return output.getvalue()
|
93 |
|
|
|
|
|
|
|
94 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
95 |
|
96 |
# Collect user's personal information
|
|
|
107 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
108 |
st.write("")
|
109 |
|
110 |
+
if st.button("Generate Caption"):
|
111 |
+
with st.spinner("Generating caption..."):
|
112 |
+
image_np = np.array(image)
|
113 |
+
caption = show_predicted_caption(image_np)[0]
|
114 |
+
|
115 |
+
st.success(f"Caption: {caption}")
|
116 |
+
|
117 |
+
# Generate the radiology report
|
118 |
+
radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this with clinical info, subjective, Assessment, Finding, Impressions, Conclusion and more in proper order : {caption}")
|
119 |
+
|
120 |
+
# Add personal information to the radiology report
|
121 |
+
radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{radiology_report}"
|
122 |
+
|
123 |
+
st.header("Radiology Report")
|
124 |
+
st.write(radiology_report_with_personal_info)
|
125 |
+
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)
|
126 |
+
|
127 |
feedback_options = ["Satisfied", "Not Satisfied"]
|
128 |
+
selected_feedback = st.radio("Please provide feedback on the generated report:", feedback_options)
|
129 |
+
|
130 |
+
if selected_feedback == "Not Satisfied":
|
131 |
+
if st.button("Regenerate Report"):
|
132 |
+
with st.spinner("Regenerating report..."):
|
133 |
+
alternative_caption = get_alternative_caption(image_np, model, text_embeddings, testing_df["caption"])
|
134 |
+
regenerated_radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this with clinical info, subjective, Assessment, Finding, Impressions, Conclusion and more in proper order : {alternative_caption}")
|
135 |
+
|
136 |
+
regenerated_radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{regenerated_radiology_report}"
|
137 |
|
138 |
+
st.header("Regenerated Radiology Report")
|
139 |
+
st.write(regenerated_radiology_report_with_personal_info)
|
140 |
+
st.markdown(download_link(save_as_docx(regenerated_radiology_report_with_personal_info, "regenerated_radiology_report.docx"), "regenerated_radiology_report.docx", "Download Regenerated Report as DOCX"), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|