Spaces:
Runtime error
Runtime error
Commit
·
b5ebf36
1
Parent(s):
f9f1b17
Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,36 @@ import pandas as pd
|
|
4 |
import torch
|
5 |
from PIL import Image
|
6 |
import numpy as np
|
7 |
-
from main import predict_caption, CLIPModel
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
9 |
|
|
|
10 |
st.markdown(
|
11 |
"""
|
12 |
<style>
|
13 |
body {
|
14 |
background-color: transparent;
|
15 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
</style>
|
17 |
""",
|
18 |
unsafe_allow_html=True,
|
19 |
)
|
20 |
|
21 |
-
|
22 |
device = torch.device("cpu")
|
23 |
|
24 |
testing_df = pd.read_csv("testing_df.csv")
|
@@ -33,8 +48,41 @@ def show_predicted_caption(image):
|
|
33 |
)[0]
|
34 |
return matches
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
40 |
if uploaded_file is not None:
|
@@ -42,10 +90,32 @@ if uploaded_file is not None:
|
|
42 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
43 |
st.write("")
|
44 |
|
45 |
-
if st.button("Generate
|
46 |
-
with st.spinner("Generating
|
47 |
image_np = np.array(image)
|
48 |
caption = show_predicted_caption(image_np)
|
49 |
st.success(f"Caption: {caption}")
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 io import BytesIO
|
11 |
+
from reportlab.lib.pagesizes import letter
|
12 |
+
from reportlab.pdfgen import canvas
|
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 |
</style>
|
33 |
""",
|
34 |
unsafe_allow_html=True,
|
35 |
)
|
36 |
|
|
|
37 |
device = torch.device("cpu")
|
38 |
|
39 |
testing_df = pd.read_csv("testing_df.csv")
|
|
|
48 |
)[0]
|
49 |
return matches
|
50 |
|
51 |
+
|
52 |
+
def generate_radiology_report(prompt):
|
53 |
+
response = openai.Completion.create(
|
54 |
+
engine="text-davinci-003",
|
55 |
+
prompt=prompt,
|
56 |
+
max_tokens=800,
|
57 |
+
n=1,
|
58 |
+
stop=None,
|
59 |
+
temperature=0.9,
|
60 |
+
)
|
61 |
+
return response.choices[0].text.strip()
|
62 |
+
|
63 |
+
|
64 |
+
def chatbot_response(prompt):
|
65 |
+
response = openai.Completion.create(
|
66 |
+
engine="text-davinci-003",
|
67 |
+
prompt=prompt,
|
68 |
+
max_tokens=500,
|
69 |
+
n=1,
|
70 |
+
stop=None,
|
71 |
+
temperature=0.8,
|
72 |
+
)
|
73 |
+
return response.choices[0].text.strip()
|
74 |
+
|
75 |
+
|
76 |
+
def create_pdf(caption, buffer):
|
77 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
78 |
+
c.drawString(50, 750, caption)
|
79 |
+
c.save()
|
80 |
+
buffer.seek(0)
|
81 |
+
return buffer
|
82 |
+
|
83 |
+
|
84 |
+
st.title("RadiXGPT: An Evolution of machine doctors towrads Radiology")
|
85 |
+
st.write("Upload Scan to get Radiological Report:")
|
86 |
|
87 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
88 |
if uploaded_file is not None:
|
|
|
90 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
91 |
st.write("")
|
92 |
|
93 |
+
if st.button("Generate Report"):
|
94 |
+
with st.spinner("Generating Report...hold on"):
|
95 |
image_np = np.array(image)
|
96 |
caption = show_predicted_caption(image_np)
|
97 |
st.success(f"Caption: {caption}")
|
98 |
|
99 |
+
prompt = f"Write Complete Radiology Report for this: {caption}"
|
100 |
+
report = generate_radiology_report(prompt)
|
101 |
+
|
102 |
+
st.markdown("<h2>Generated Radiology Report</h2>", unsafe_allow_html=True)
|
103 |
+
st.markdown(f"<div class='container'>{report}</div>", unsafe_allow_html=True)
|
104 |
+
|
105 |
+
# PDF download
|
106 |
+
buffer = BytesIO()
|
107 |
+
pdf_buffer = create_pdf(report, buffer)
|
108 |
+
b64 = base64.b64encode(pdf_buffer.getvalue()).decode()
|
109 |
+
href = f'<a href="data:application/octet-stream;base64,{b64}" download="Radiology_Report.pdf">Download Radiology Report as PDF</a>'
|
110 |
+
st.markdown(href, unsafe_allow_html=True)
|
111 |
+
|
112 |
+
# Chatbot for consultation
|
113 |
+
st.markdown("<h2>Need help understanding the Radiological Report?</h2>", unsafe_allow_html=True)
|
114 |
+
st.write("Ask your queries here:")
|
115 |
+
user_question = st.text_input("")
|
116 |
+
|
117 |
+
if st.button("Get Help"):
|
118 |
+
chat_prompt = f"The generated radiology report is: {report}. User asks: {user_question}"
|
119 |
+
chat_answer = chatbot_response(chat_prompt)
|
120 |
+
st.markdown("<h3>Answer:</h3>", unsafe_allow_html=True)
|
121 |
+
st.write(chat_answer)
|