xray-analysis / app.py
skjaini's picture
Update app.py
a5948fd verified
raw
history blame
2.55 kB
import streamlit as st
from PIL import Image
from huggingface_hub import InferenceClient
import io
def analyze_image_with_maira(image):
"""Analyzes the image using the Maira-2 model via the Hugging Face Inference API.
"""
try:
# Prepare image data - send as bytes directly to 'inputs'
image_bytes = io.BytesIO()
if image.mode == "RGBA": # Handle RGBA images (if any)
image = image.convert("RGB")
image.save(image_bytes, format="JPEG")
image_bytes = image_bytes.getvalue() # Get the bytes
client = InferenceClient() # No token needed inside the Space
result = client.post(
json={
"inputs": {
"image": image_bytes.decode(encoding="latin-1"), #Needs to be decoded
"question": "Analyze this chest X-ray image and provide detailed findings. Include any abnormalities, their locations, and potential diagnoses. Be as specific as possible.",
}
},
model="microsoft/maira-2", # Specify the model
task="visual-question-answering"
)
return result
except Exception as e:
st.error(f"An error occurred: {e}")
return None
# --- Streamlit App ---
def main():
st.title("Chest X-ray Analysis with Maira-2 (Hugging Face Spaces)")
st.write(
"Upload a chest X-ray image. This app uses the Maira-2 model within this Hugging Face Space."
)
uploaded_file = st.file_uploader("Choose a chest X-ray image (JPG, PNG)", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
with st.spinner("Analyzing image with Maira-2..."):
analysis_results = analyze_image_with_maira(image)
if analysis_results:
# --- Results Display (VQA format) ---
if isinstance(analysis_results, dict) and 'answer' in analysis_results:
st.subheader("Findings:")
st.write(analysis_results['answer'])
else:
st.warning("Unexpected API response format.")
st.write("Raw API response:", analysis_results)
else:
st.error("Failed to get analysis results.")
else:
st.write("Please upload an image.")
st.write("---")
st.write("Disclaimer: For informational purposes only. Not medical advice.")
if __name__ == "__main__":
main()