Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,45 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
from
|
| 4 |
import io
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
image_bytes = io.BytesIO()
|
| 12 |
-
if image.mode == "RGBA": # Handle RGBA images (if any)
|
| 13 |
-
image = image.convert("RGB")
|
| 14 |
-
image.save(image_bytes, format="JPEG")
|
| 15 |
-
image_bytes = image_bytes.getvalue() # Get the bytes
|
| 16 |
-
|
| 17 |
-
client = InferenceClient() # No token needed inside the Space
|
| 18 |
-
result = client.post(
|
| 19 |
-
json={
|
| 20 |
-
"inputs": {
|
| 21 |
-
"image": image_bytes.decode(encoding="latin-1"), #Needs to be decoded
|
| 22 |
-
"question": "Analyze this chest X-ray image and provide detailed findings. Include any abnormalities, their locations, and potential diagnoses. Be as specific as possible.",
|
| 23 |
-
}
|
| 24 |
-
},
|
| 25 |
-
model="microsoft/maira-2", # Specify the model
|
| 26 |
-
task="visual-question-answering"
|
| 27 |
-
)
|
| 28 |
-
return result
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
-
st.error(f"
|
| 32 |
return None
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# --- Streamlit App ---
|
| 36 |
|
| 37 |
def main():
|
| 38 |
-
st.title("Chest X-ray Analysis with Maira-2 (
|
| 39 |
-
st.write(
|
| 40 |
-
|
| 41 |
-
)
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
uploaded_file = st.file_uploader("Choose a chest X-ray image (JPG, PNG)", type=["jpg", "jpeg", "png"])
|
| 44 |
|
|
@@ -47,18 +48,26 @@ def main():
|
|
| 47 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 48 |
|
| 49 |
with st.spinner("Analyzing image with Maira-2..."):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
else:
|
| 64 |
st.write("Please upload an image.")
|
|
@@ -66,5 +75,6 @@ def main():
|
|
| 66 |
st.write("---")
|
| 67 |
st.write("Disclaimer: For informational purposes only. Not medical advice.")
|
| 68 |
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
+
from transformers import pipeline
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
# --- Configuration ---
|
| 7 |
+
# Specify the model
|
| 8 |
+
MODEL_NAME = "microsoft/maira-2"
|
| 9 |
+
|
| 10 |
+
# --- Model Loading (using pipeline) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
@st.cache_resource # Cache the pipeline for performance
|
| 13 |
+
def load_pipeline():
|
| 14 |
+
"""Loads the VQA pipeline."""
|
| 15 |
+
try:
|
| 16 |
+
# Explicitly set device if CUDA is available, otherwise use CPU
|
| 17 |
+
device = 0 if any(tf.config.list_physical_devices('GPU')) else -1
|
| 18 |
+
vqa_pipeline = pipeline("visual-question-answering", model=MODEL_NAME, device=device) # Add device
|
| 19 |
+
return vqa_pipeline
|
| 20 |
except Exception as e:
|
| 21 |
+
st.error(f"Error loading pipeline: {e}")
|
| 22 |
return None
|
| 23 |
|
| 24 |
+
# --- Image Preprocessing (Keep as bytes) ---
|
| 25 |
+
def prepare_image(image):
|
| 26 |
+
"""Prepares the PIL Image object for the pipeline (handles RGBA)."""
|
| 27 |
+
image_bytes = io.BytesIO()
|
| 28 |
+
if image.mode == "RGBA":
|
| 29 |
+
image = image.convert("RGB")
|
| 30 |
+
image.save(image_bytes, format="JPEG")
|
| 31 |
+
return image_bytes.getvalue() # Return bytes directly
|
| 32 |
|
| 33 |
# --- Streamlit App ---
|
| 34 |
|
| 35 |
def main():
|
| 36 |
+
st.title("Chest X-ray Analysis with Maira-2 (Transformers Pipeline)")
|
| 37 |
+
st.write("Upload a chest X-ray image. This app uses the Maira-2 model via the Transformers library.")
|
| 38 |
+
|
| 39 |
+
vqa_pipeline = load_pipeline()
|
| 40 |
+
if vqa_pipeline is None:
|
| 41 |
+
st.warning("Pipeline not loaded. Predictions will not be available.")
|
| 42 |
+
return
|
| 43 |
|
| 44 |
uploaded_file = st.file_uploader("Choose a chest X-ray image (JPG, PNG)", type=["jpg", "jpeg", "png"])
|
| 45 |
|
|
|
|
| 48 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 49 |
|
| 50 |
with st.spinner("Analyzing image with Maira-2..."):
|
| 51 |
+
image_data = prepare_image(image)
|
| 52 |
+
try:
|
| 53 |
+
results = vqa_pipeline(
|
| 54 |
+
image=image_data, # Pass the image bytes
|
| 55 |
+
question="Analyze this chest X-ray image and provide detailed findings. Include any abnormalities, their locations, and potential diagnoses. Be as specific as possible.",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if results: # Handle results (list of dicts)
|
| 59 |
+
if isinstance(results, list) and len(results) > 0:
|
| 60 |
+
best_answer = max(results, key=lambda x: x.get('score', 0))
|
| 61 |
+
if 'answer' in best_answer:
|
| 62 |
+
st.subheader("Findings:")
|
| 63 |
+
st.write(best_answer['answer'])
|
| 64 |
+
else:
|
| 65 |
+
st.warning("Could not find 'answer' in results.")
|
| 66 |
+
else:
|
| 67 |
+
st.warning("Unexpected result format.")
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
st.error(f"An error occurred during analysis: {e}")
|
| 71 |
|
| 72 |
else:
|
| 73 |
st.write("Please upload an image.")
|
|
|
|
| 75 |
st.write("---")
|
| 76 |
st.write("Disclaimer: For informational purposes only. Not medical advice.")
|
| 77 |
|
| 78 |
+
|
| 79 |
if __name__ == "__main__":
|
| 80 |
main()
|