Singularity666 commited on
Commit
83eab98
·
1 Parent(s): 5602714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -92
app.py CHANGED
@@ -1,85 +1,29 @@
1
  import streamlit as st
2
- import pickle
3
- 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
11
- from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
12
- from io import BytesIO
13
-
14
- # Set up OpenAI API
15
- openai.api_key = "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"
16
-
17
- # Custom CSS for the page
18
- st.markdown(
19
- """
20
- <style>
21
- body {
22
- background-color: transparent;
23
- }
24
- .container {
25
- display: flex;
26
- justify-content: center;
27
- align-items: center;
28
- background-color: rgba(255, 255, 255, 0.7);
29
- border-radius: 15px;
30
- padding: 20px;
31
- }
32
- .stApp {
33
- background-color: transparent;
34
- }
35
- .stText, .stMarkdown, .stTextInput>label, .stButton>button>span {
36
- color: #1c1c1c !important; /* Set the dark text color for text elements */
37
- }
38
- .stButton>button>span {
39
- color: initial !important; /* Reset the text color for the 'Generate Caption' button */
40
- }
41
- .stMarkdown h1, .stMarkdown h2 {
42
- color: #ff6b81 !important; /* Set the text color of h1 and h2 elements to soft red-pink */
43
- font-weight: bold; /* Set the font weight to bold */
44
- border: 2px solid #ff6b81; /* Add a bold border around the headers */
45
- padding: 10px; /* Add padding to the headers */
46
- border-radius: 5px; /* Add border-radius to the headers */
47
- }
48
- </style>
49
- """,
50
- unsafe_allow_html=True,
51
- )
52
-
53
- device = torch.device("cpu")
54
-
55
- testing_df = pd.read_csv("testing_df.csv")
56
- 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
- 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):
67
- response = openai.Completion.create(
68
- engine="text-davinci-003",
69
- prompt=prompt,
70
- max_tokens=800,
71
- n=1,
72
- stop=None,
73
- temperature=1,
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
 
@@ -93,7 +37,7 @@ if uploaded_file is not None:
93
  if st.button("Generate Caption"):
94
  with st.spinner("Generating caption..."):
95
  image_np = np.array(image)
96
- caption = show_predicted_caption(image_np)
97
 
98
  st.success(f"Caption: {caption}")
99
 
@@ -109,20 +53,24 @@ if uploaded_file is not None:
109
  st.write(radiology_report_with_personal_info)
110
  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)
111
 
112
- st.header("Self Adaptive Feedback System")
 
 
 
113
 
114
- feedback_options = st.radio("Is the radiology report satisfactory?", ["Better", "Satisfied", "Worse"])
 
 
 
 
 
115
 
116
- if feedback_options == "Worse":
117
- caption = show_predicted_caption(image_np, second_attempt=True)
118
- radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this: {caption}")
119
  radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{radiology_report}"
 
 
 
 
120
  st.write(radiology_report_with_personal_info)
121
  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)
122
- elif feedback_options == "Satisfied":
123
- st.success("Thank you for your feedback! Your input helps us improve the RadiXGPT system.")
124
- elif feedback_options == "Better":
125
- st.success("Thank you for your feedback! Your input helps us improve the RadiXGPT system.")
126
- # Add your feedback processing logic here
127
- # This could include updating the model with the new information,
128
- # logging the feedback, or triggering an alert for manual review
 
1
  import streamlit as st
 
 
 
2
  from PIL import Image
3
  import numpy as np
4
+ import main
5
+ import pandas as pd
6
+ import cv2
7
+
8
+ def get_image_np(uploaded_file):
9
+ image = Image.open(uploaded_file)
10
+ return np.array(image)
11
+
12
+ def show_predicted_caption(image_np, top_k=1):
13
+ image = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
14
+ captions = predict_caption(image, model, text_embeddings, valid_df['caption'].values, n=top_k)
15
+ return captions
16
+
17
+ # Load the model and text embeddings
18
+ valid_df = pd.read_csv('testing_df.csv')
19
+ model, text_embeddings = main.get_text_embeddings(valid_df)
20
+
21
+ # App code
22
+ st.title("Medical Radiology Report Generator")
23
+
24
+ st.header("Personal Information")
25
+ first_name = st.text_input("First Name", "John")
26
+ last_name = st.text_input("Last Name", "Doe")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  age = st.number_input("Age", min_value=0, max_value=120, value=25, step=1)
28
  gender = st.selectbox("Gender", ["Male", "Female", "Other"])
29
 
 
37
  if st.button("Generate Caption"):
38
  with st.spinner("Generating caption..."):
39
  image_np = np.array(image)
40
+ caption = show_predicted_caption(image_np)[0]
41
 
42
  st.success(f"Caption: {caption}")
43
 
 
53
  st.write(radiology_report_with_personal_info)
54
  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)
55
 
56
+ # Advanced Feedback System
57
+ st.header("Advanced Feedback System")
58
+ feedback_options = ["Better", "Satisfied", "Worse"]
59
+ feedback = st.radio("Rate the generated report:", feedback_options)
60
 
61
+ top_k = 1
62
+ while feedback == "Worse":
63
+ top_k += 1
64
+ with st.spinner("Regenerating report..."):
65
+ new_caption = show_predicted_caption(image_np, top_k=top_k)[-1]
66
+ radiology_report = generate_radiology_report(f"Write Complete Radiology Report for this: {new_caption}")
67
 
 
 
 
68
  radiology_report_with_personal_info = f"Patient Name: {first_name} {last_name}\nAge: {age}\nGender: {gender}\n\n{radiology_report}"
69
+
70
+ with container:
71
+ container.empty()
72
+ st.header("Radiology Report")
73
  st.write(radiology_report_with_personal_info)
74
  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)
75
+
76
+ feedback = st.radio("Rate the generated report:", feedback_options)