Spaces:
Runtime error
Runtime error
Commit
·
dfe6abe
1
Parent(s):
cd22e2c
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 |
-
)[0]
|
70 |
return matches
|
71 |
|
72 |
def generate_radiology_report(prompt):
|
@@ -80,30 +74,96 @@ def generate_radiology_report(prompt):
|
|
80 |
)
|
81 |
return response.choices[0].text.strip()
|
82 |
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
response = openai.Completion.create(
|
85 |
engine="text-davinci-003",
|
86 |
prompt=prompt,
|
87 |
-
max_tokens=
|
88 |
n=1,
|
89 |
stop=None,
|
90 |
-
temperature=
|
91 |
)
|
92 |
return response.choices[0].text.strip()
|
93 |
|
94 |
-
def save_as_docx(text, filename):
|
95 |
-
document = Document()
|
96 |
-
document.add_paragraph(text)
|
97 |
-
with BytesIO() as output:
|
98 |
-
document.save(output)
|
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
|
@@ -139,13 +199,20 @@ if uploaded_file is not None:
|
|
139 |
st.write(radiology_report_with_personal_info)
|
140 |
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)
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
feedback = st.text_area("Enter your feedback here...")
|
146 |
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
# Add your feedback processing logic here
|
150 |
# This could include updating the model with the new information,
|
151 |
-
# logging the feedback, or triggering an alert for manual review
|
|
|
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, second_attempt=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
matches = predict_caption(
|
62 |
+
image, model, text_embeddings, testing_df["caption"], second_attempt
|
63 |
+
)[0 if not second_attempt else 1]
|
64 |
return matches
|
65 |
|
66 |
def generate_radiology_report(prompt):
|
|
|
74 |
)
|
75 |
return response.choices[0].text.strip()
|
76 |
|
77 |
+
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
78 |
+
|
79 |
+
# Collect user's personal information
|
80 |
+
st.subheader("Personal Information")
|
81 |
+
first_name = st.text_input("First Name")
|
82 |
+
last_name = st.text_input("Last Name")
|
83 |
+
age = st.number_input("Age", min_value=0, max_value=120, value=25, step=1)
|
84 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
85 |
+
|
86 |
+
st.write("Upload Scan to get Radiological Report:")
|
87 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
88 |
+
if uploaded_file is not None:
|
89 |
+
image = Image.open(uploaded_file)
|
90 |
+
st.image(image
|
91 |
+
import streamlit as st
|
92 |
+
import pickle
|
93 |
+
import pandas as pd
|
94 |
+
import torch
|
95 |
+
from PIL import Image
|
96 |
+
import numpy as np
|
97 |
+
from main import predict_caption, CLIPModel, get_text_embeddings
|
98 |
+
import openai
|
99 |
+
import base64
|
100 |
+
from docx import Document
|
101 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
102 |
+
from io import BytesIO
|
103 |
+
|
104 |
+
# Set up OpenAI API
|
105 |
+
openai.api_key = "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"
|
106 |
+
|
107 |
+
# Custom CSS for the page
|
108 |
+
st.markdown(
|
109 |
+
"""
|
110 |
+
<style>
|
111 |
+
body {
|
112 |
+
background-color: transparent;
|
113 |
+
}
|
114 |
+
.container {
|
115 |
+
display: flex;
|
116 |
+
justify-content: center;
|
117 |
+
align-items: center;
|
118 |
+
background-color: rgba(255, 255, 255, 0.7);
|
119 |
+
border-radius: 15px;
|
120 |
+
padding: 20px;
|
121 |
+
}
|
122 |
+
.stApp {
|
123 |
+
background-color: transparent;
|
124 |
+
}
|
125 |
+
.stText, .stMarkdown, .stTextInput>label, .stButton>button>span {
|
126 |
+
color: #1c1c1c !important; /* Set the dark text color for text elements */
|
127 |
+
}
|
128 |
+
.stButton>button>span {
|
129 |
+
color: initial !important; /* Reset the text color for the 'Generate Caption' button */
|
130 |
+
}
|
131 |
+
.stMarkdown h1, .stMarkdown h2 {
|
132 |
+
color: #ff6b81 !important; /* Set the text color of h1 and h2 elements to soft red-pink */
|
133 |
+
font-weight: bold; /* Set the font weight to bold */
|
134 |
+
border: 2px solid #ff6b81; /* Add a bold border around the headers */
|
135 |
+
padding: 10px; /* Add padding to the headers */
|
136 |
+
border-radius: 5px; /* Add border-radius to the headers */
|
137 |
+
}
|
138 |
+
</style>
|
139 |
+
""",
|
140 |
+
unsafe_allow_html=True,
|
141 |
+
)
|
142 |
+
|
143 |
+
device = torch.device("cpu")
|
144 |
+
|
145 |
+
testing_df = pd.read_csv("testing_df.csv")
|
146 |
+
model = CLIPModel().to(device)
|
147 |
+
model.load_state_dict(torch.load("weights.pt", map_location=torch.device('cpu')))
|
148 |
+
text_embeddings = torch.load('saved_text_embeddings.pt', map_location=device)
|
149 |
+
|
150 |
+
def show_predicted_caption(image, second_attempt=False):
|
151 |
+
matches = predict_caption(
|
152 |
+
image, model, text_embeddings, testing_df["caption"], second_attempt
|
153 |
+
)[0 if not second_attempt else 1]
|
154 |
+
return matches
|
155 |
+
|
156 |
+
def generate_radiology_report(prompt):
|
157 |
response = openai.Completion.create(
|
158 |
engine="text-davinci-003",
|
159 |
prompt=prompt,
|
160 |
+
max_tokens=800,
|
161 |
n=1,
|
162 |
stop=None,
|
163 |
+
temperature=1,
|
164 |
)
|
165 |
return response.choices[0].text.strip()
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
st.title("RadiXGPT: An Evolution of machine doctors towards Radiology")
|
168 |
|
169 |
# Collect user's personal information
|
|
|
199 |
st.write(radiology_report_with_personal_info)
|
200 |
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)
|
201 |
|
202 |
+
st.header("Self Adaptive Feedback System")
|
203 |
+
|
204 |
+
feedback_options = st.radio("Is the radiology report satisfactory?", ["Better", "Satisfied", "Worse"])
|
|
|
205 |
|
206 |
+
if feedback_options == "Worse":
|
207 |
+
caption = show_predicted_caption(image_np, second_attempt=True)
|
208 |
+
radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this: {caption}")
|
209 |
+
radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{radiology_report}"
|
210 |
+
st.write(radiology_report_with_personal_info)
|
211 |
+
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)
|
212 |
+
elif feedback_options == "Satisfied":
|
213 |
+
st.success("Thank you for your feedback! Your input helps us improve the RadiXGPT system.")
|
214 |
+
elif feedback_options == "Better":
|
215 |
+
st.success("Thank you for your feedback! Your input helps us improve the RadiXGPT system.")
|
216 |
# Add your feedback processing logic here
|
217 |
# This could include updating the model with the new information,
|
218 |
+
# logging the feedback, or triggering an alert for manual review
|