-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·81 lines (67 loc) · 2.83 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st
import os
from PIL import Image
from transformers import BlipForConditionalGeneration, BlipProcessor
# Load the model and processor
processor = BlipProcessor.from_pretrained("model")
model = BlipForConditionalGeneration.from_pretrained("model")
# Streamlit app title and description
st.title("Radiology Report Generator")
st.write("Upload chest X-ray images and get radiology reports.")
st.markdown(
"""
Upload your own images or choose from the examples below to generate radiology reports.
More about this model can be found in my [medium blog](https://medium.com/@sureshnithin1729/interpret-cxrs-using-biobert-clip-456d0ce8cda2).
"""
)
# Upload images
uploaded_files = st.file_uploader("Choose images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
# Example images folder path
examples_folder = "examples"
# Process each uploaded image
if uploaded_files:
st.write("**Generated Reports:**")
# Create a row to display images
images_row = st.empty()
# Process each uploaded file
for uploaded_file in uploaded_files:
# Open image
image = Image.open(uploaded_file)
# Process the inputs
inputs = processor(
images=image,
text='', # Modify with your indication text
return_tensors="pt"
)
# Generate the report
output = model.generate(**inputs, max_length=512)
report = processor.decode(output[0], skip_special_tokens=True)
# Display the image and report
st.image(image, caption=f"Uploaded Image: {uploaded_file.name}", use_column_width=True)
st.write(f"**Generated Report for {uploaded_file.name}:**")
# st.write(report)
try :
indication=report.split('findings')[0]
fidx=report.find('findings')
impdx=report.find('impression')
findings=report[fidx:impdx]
impressions=report[impdx:]
st.write(f"{indication}\n")
st.write(f"{findings}\n")
st.write(f"{impressions}\n")
except:
st.write(report)
# Append image to the images row with a small gap
images_row.image(image, caption=f"Uploaded Image: {uploaded_file.name}", use_column_width=True)
images_row.markdown("---") # Adds a small gap between images
# Display example images from folder
st.header("Sample Images ")
st.write("Drag & Drop them in the box above or upload your own files")
example_images = os.listdir(examples_folder)
example_images = os.listdir(examples_folder)
for example_image in example_images:
# Load example image
image_path = os.path.join(examples_folder, example_image)
image = Image.open(image_path)
# Display the example image
st.image(image, caption=f"Example Image: {example_image}", use_column_width=True)