Spaces:
Runtime error
Runtime error
Commit
·
03d955f
1
Parent(s):
08ab8f4
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
from streamlit.session_state import SessionState
|
3 |
import pickle
|
4 |
import pandas as pd
|
5 |
import torch
|
@@ -12,6 +11,7 @@ from docx import Document
|
|
12 |
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
13 |
from io import BytesIO
|
14 |
import re
|
|
|
15 |
|
16 |
openai.api_key = "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"
|
17 |
|
@@ -57,34 +57,19 @@ 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 |
-
def handle_feedback(session_state):
|
62 |
-
if session_state.caption_index < len(session_state.generated_captions) - 1:
|
63 |
-
session_state.caption_index += 1
|
64 |
-
new_caption = session_state.generated_captions[session_state.caption_index]
|
65 |
-
return new_caption
|
66 |
-
else:
|
67 |
-
st.warning("No more captions available to regenerate the report.")
|
68 |
-
return session_state.generated_captions[session_state.caption_index]
|
69 |
-
|
70 |
-
# Initialize the session state
|
71 |
-
if "caption_index" not in st.session_state:
|
72 |
-
st.session_state.caption_index = 0
|
73 |
-
|
74 |
-
if "generated_captions" not in st.session_state:
|
75 |
-
st.session_state.generated_captions = []
|
76 |
|
77 |
def download_link(content, filename, link_text):
|
78 |
b64 = base64.b64encode(content).decode()
|
79 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
80 |
return href
|
81 |
|
82 |
-
def
|
83 |
matches = predict_caption(
|
84 |
image, model, text_embeddings, testing_df["caption"]
|
85 |
-
)
|
86 |
-
cleaned_matches = [re.sub(r'\s\(ROCO_\d+\)', '', match) for match in matches]
|
87 |
-
return cleaned_matches
|
88 |
|
89 |
def generate_radiology_report(prompt):
|
90 |
response = openai.Completion.create(
|
@@ -109,49 +94,31 @@ def save_as_docx(text, filename):
|
|
109 |
output.seek(0)
|
110 |
return output.getvalue()
|
111 |
|
112 |
-
st.title("RadiXGPT: An Evolution
|
|
|
113 |
|
114 |
-
|
115 |
-
st.subheader("Personal Information")
|
116 |
-
first_name = st.text_input("First Name")
|
117 |
-
last_name = st.text_input("Last Name")
|
118 |
-
age = st.number_input("Age", min_value=0, max_value=120, value=25, step=1)
|
119 |
-
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
if uploaded_file is not None:
|
124 |
-
image = Image.open(uploaded_file)
|
125 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
126 |
-
st.write("")
|
127 |
|
128 |
if st.button("Generate Caption"):
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
# Feedback buttons
|
148 |
-
st.header("Feedback")
|
149 |
-
if st.button("Not Satisfied"):
|
150 |
-
new_caption = handle_feedback(st.session_state)
|
151 |
-
if new_caption:
|
152 |
-
# Generate new radiology report
|
153 |
-
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 : {new_caption}")
|
154 |
-
radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{radiology_report}"
|
155 |
-
st.header("New Radiology Report")
|
156 |
-
st.write(radiology_report_with_personal_info)
|
157 |
-
st.markdown(download_link(save_as_docx(radiology_report_with_personal_info, "radiology_report.docx"), "radiology_report.docx", "Download New Report as DOCX"), unsafe_allow_html=True)
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import pickle
|
3 |
import pandas as pd
|
4 |
import torch
|
|
|
11 |
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
12 |
from io import BytesIO
|
13 |
import re
|
14 |
+
from session_state import get
|
15 |
|
16 |
openai.api_key = "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"
|
17 |
|
|
|
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 |
+
session_state = get(caption_index=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
def download_link(content, filename, link_text):
|
63 |
b64 = base64.b64encode(content).decode()
|
64 |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">{link_text}</a>'
|
65 |
return href
|
66 |
|
67 |
+
def show_predicted_caption_with_index(image, caption_index):
|
68 |
matches = predict_caption(
|
69 |
image, model, text_embeddings, testing_df["caption"]
|
70 |
+
)
|
71 |
+
cleaned_matches = [re.sub(r'\s\(ROCO_\d+\)', '', match) for match in matches]
|
72 |
+
return cleaned_matches[caption_index]
|
73 |
|
74 |
def generate_radiology_report(prompt):
|
75 |
response = openai.Completion.create(
|
|
|
94 |
output.seek(0)
|
95 |
return output.getvalue()
|
96 |
|
97 |
+
st.title("RadiXGPT: An Evolution in Radiology Reporting")
|
98 |
+
st.write("Upload a radiology image and generate a radiology report.")
|
99 |
|
100 |
+
uploaded_image = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
if uploaded_image is not None:
|
103 |
+
image = Image.open(uploaded_image)
|
|
|
|
|
104 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
|
|
105 |
|
106 |
if st.button("Generate Caption"):
|
107 |
+
if session_state.caption_index < 5:
|
108 |
+
session_state.caption_index += 1
|
109 |
+
else:
|
110 |
+
session_state.caption_index = 0
|
111 |
+
|
112 |
+
caption = show_predicted_caption_with_index(image, session_state.caption_index)
|
113 |
+
st.write("Caption: ", caption)
|
114 |
+
|
115 |
+
report_prompt = f"Generate a detailed radiology report based on this caption: {caption}"
|
116 |
+
report = generate_radiology_report(report_prompt)
|
117 |
+
st.write("Report: ", report)
|
118 |
+
|
119 |
+
# Download report as a .docx file
|
120 |
+
docx_data = save_as_docx(report, "Radiology_Report.docx")
|
121 |
+
st.markdown(
|
122 |
+
download_link(docx_data, "Radiology_Report.docx", "Download Report as .docx"),
|
123 |
+
unsafe_allow_html=True,
|
124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|