Spaces:
Sleeping
Sleeping
Update AKSHAYRAJAA/inference.py
Browse files- AKSHAYRAJAA/inference.py +35 -29
AKSHAYRAJAA/inference.py
CHANGED
@@ -10,11 +10,12 @@ from utils import (
|
|
10 |
)
|
11 |
from PIL import Image
|
12 |
import torchvision.transforms as transforms
|
|
|
13 |
|
14 |
# Define device
|
15 |
DEVICE = 'cpu'
|
16 |
|
17 |
-
# Define image transformations
|
18 |
TRANSFORMS = transforms.Compose([
|
19 |
transforms.Resize((224, 224)), # Replace with your model's expected input size
|
20 |
transforms.ToTensor(),
|
@@ -26,21 +27,21 @@ def load_model():
|
|
26 |
"""
|
27 |
Loads the model with the vocabulary and checkpoint.
|
28 |
"""
|
29 |
-
|
30 |
-
dataset = load_dataset()
|
31 |
-
vocabulary = dataset.vocab
|
32 |
|
33 |
-
|
34 |
-
model = get_model_instance(vocabulary)
|
35 |
|
36 |
if can_load_checkpoint():
|
37 |
-
|
38 |
load_checkpoint(model)
|
39 |
else:
|
40 |
-
|
41 |
|
42 |
model.eval() # Set the model to evaluation mode
|
43 |
-
|
44 |
return model
|
45 |
|
46 |
|
@@ -48,41 +49,46 @@ def preprocess_image(image_path):
|
|
48 |
"""
|
49 |
Preprocess the input image for the model.
|
50 |
"""
|
51 |
-
|
52 |
-
image = Image.open(image_path).convert("RGB")
|
53 |
-
image = TRANSFORMS(image).unsqueeze(0)
|
54 |
return image.to(DEVICE)
|
55 |
|
56 |
|
57 |
-
def generate_report(model,
|
58 |
"""
|
59 |
Generates a report for a given image using the model.
|
60 |
"""
|
61 |
-
|
62 |
-
|
63 |
-
print("Generating report...")
|
64 |
with torch.no_grad():
|
65 |
-
# Assuming the model has a 'generate_caption' method
|
66 |
output = model.generate_caption(image, max_length=25)
|
67 |
report = " ".join(output)
|
68 |
|
69 |
-
|
70 |
return report
|
71 |
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
79 |
|
80 |
# Load the model
|
81 |
model = load_model()
|
82 |
|
83 |
-
#
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
10 |
)
|
11 |
from PIL import Image
|
12 |
import torchvision.transforms as transforms
|
13 |
+
import streamlit as st
|
14 |
|
15 |
# Define device
|
16 |
DEVICE = 'cpu'
|
17 |
|
18 |
+
# Define image transformations
|
19 |
TRANSFORMS = transforms.Compose([
|
20 |
transforms.Resize((224, 224)), # Replace with your model's expected input size
|
21 |
transforms.ToTensor(),
|
|
|
27 |
"""
|
28 |
Loads the model with the vocabulary and checkpoint.
|
29 |
"""
|
30 |
+
st.write("Loading dataset and vocabulary...")
|
31 |
+
dataset = load_dataset()
|
32 |
+
vocabulary = dataset.vocab
|
33 |
|
34 |
+
st.write("Initializing the model...")
|
35 |
+
model = get_model_instance(vocabulary)
|
36 |
|
37 |
if can_load_checkpoint():
|
38 |
+
st.write("Loading checkpoint...")
|
39 |
load_checkpoint(model)
|
40 |
else:
|
41 |
+
st.write("No checkpoint found, starting with untrained model.")
|
42 |
|
43 |
model.eval() # Set the model to evaluation mode
|
44 |
+
st.write("Model is ready for inference.")
|
45 |
return model
|
46 |
|
47 |
|
|
|
49 |
"""
|
50 |
Preprocess the input image for the model.
|
51 |
"""
|
52 |
+
st.write(f"Preprocessing image: {image_path}")
|
53 |
+
image = Image.open(image_path).convert("RGB")
|
54 |
+
image = TRANSFORMS(image).unsqueeze(0)
|
55 |
return image.to(DEVICE)
|
56 |
|
57 |
|
58 |
+
def generate_report(model, image):
|
59 |
"""
|
60 |
Generates a report for a given image using the model.
|
61 |
"""
|
62 |
+
st.write("Generating report...")
|
|
|
|
|
63 |
with torch.no_grad():
|
|
|
64 |
output = model.generate_caption(image, max_length=25)
|
65 |
report = " ".join(output)
|
66 |
|
67 |
+
st.write(f"Generated report: {report}")
|
68 |
return report
|
69 |
|
70 |
|
71 |
+
# Streamlit App
|
72 |
+
st.title("Medical Image Report Generator")
|
73 |
+
st.write("Upload an X-ray image to generate a report.")
|
74 |
+
|
75 |
+
# File uploader
|
76 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
|
77 |
|
78 |
+
if uploaded_file is not None:
|
79 |
+
# Save uploaded file to disk
|
80 |
+
image_path = os.path.join("temp", uploaded_file.name)
|
81 |
+
with open(image_path, "wb") as f:
|
82 |
+
f.write(uploaded_file.getbuffer())
|
83 |
|
84 |
# Load the model
|
85 |
model = load_model()
|
86 |
|
87 |
+
# Preprocess and generate the report
|
88 |
+
image = preprocess_image(image_path)
|
89 |
+
report = generate_report(model, image)
|
90 |
+
|
91 |
+
# Display the image and the report
|
92 |
+
st.image(image_path, caption="Uploaded Image", use_column_width=True)
|
93 |
+
st.write("Generated Report:")
|
94 |
+
st.write(report)
|