Spaces:
Running
Running
File size: 2,548 Bytes
c20fb92 5abb257 c20fb92 76acf42 2ee8b8c c20fb92 a5948fd 76acf42 a5948fd c20fb92 2ee8b8c c20fb92 5abb257 c20fb92 |
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 |
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() |