Singularity666 commited on
Commit
b0cd6ca
·
1 Parent(s): 391d6d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -30
app.py CHANGED
@@ -11,7 +11,6 @@ from docx import Document
11
  from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
12
  from io import BytesIO
13
  import re
14
- from SessionState import get
15
 
16
  openai.api_key = "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"
17
 
@@ -57,19 +56,17 @@ 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
- 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,31 +91,46 @@ def save_as_docx(text, filename):
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
- )
 
 
 
 
 
 
 
 
 
11
  from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
12
  from io import BytesIO
13
  import re
 
14
 
15
  openai.api_key = "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"
16
 
 
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>'
62
  return href
63
 
64
+ def show_predicted_caption(image, top_k=8):
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(
 
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
97
+ st.subheader("Personal Information")
98
+ first_name = st.text_input("First Name")
99
+ last_name = st.text_input("Last Name")
100
+ age = st.number_input("Age", min_value=0, max_value=120, value=25, step=1)
101
+ gender = st.selectbox("Gender", ["Male", "Female", "Other"])
102
 
103
+ st.write("Upload Scan to get Radiological Report:")
104
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
105
+ if uploaded_file is not None:
106
+ image = Image.open(uploaded_file)
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 buttons
128
+ st.header("Thanks for your feedback!")
129
+ feedback_options = ["Better", "Satisfied", "Worse"]
130
+ selected_feedback = st.radio("Please provide feedback on the generated report:", feedback_options)
131
+
132
+ if st.button("Submit Feedback"):
133
+ st.success("Thanks for providing feedback!")
134
+ # Implement your feedback handling logic here based on the `selected_feedback` value
135
+
136
+ Tell me how to solve this problem like