skjaini commited on
Commit
c20fb92
·
verified ·
1 Parent(s): 30f4ace

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from huggingface_hub import HfApi, HfFolder, HfApi, ModelCard, whoami
4
+ from huggingface_hub import InferenceClient # Use InferenceClient
5
+ import io
6
+ import base64
7
+
8
+ # --- Configuration (Simplified for Spaces) ---
9
+
10
+ # No need for API token if running *within* a Space
11
+ # The Space's environment will handle authentication
12
+ # The model ID is implicitly available if the Space is built around that model
13
+
14
+ # --- Image Encoding ---
15
+ def encode_image(image):
16
+ buffered = io.BytesIO()
17
+ image.save(buffered, format="JPEG")
18
+ img_str = base64.b64encode(buffered.getvalue()).decode()
19
+ return img_str
20
+
21
+
22
+ # --- Model Interaction (using InferenceClient) ---
23
+
24
+ def analyze_image_with_maira(image):
25
+ """Analyzes the image using the Maira-2 model via the Hugging Face Inference API.
26
+ """
27
+ try:
28
+ encoded_image = encode_image(image)
29
+ client = InferenceClient() # No token needed inside the Space
30
+ result = client.question_answering(
31
+ question="Analyze this chest X-ray image and provide detailed findings. Include any abnormalities, their locations, and potential diagnoses. Be as specific as possible.",
32
+ image=encoded_image, # Pass the encoded image directly
33
+ model="microsoft/maira-2" # Specify the model
34
+ )
35
+ return result
36
+
37
+ except Exception as e:
38
+ st.error(f"An error occurred: {e}") # General exception handling is sufficient here
39
+ return None
40
+
41
+
42
+ # --- Streamlit App ---
43
+
44
+ def main():
45
+ st.title("Chest X-ray Analysis with Maira-2 (Hugging Face Spaces)")
46
+ st.write(
47
+ "Upload a chest X-ray image. This app uses the Maira-2 model within this Hugging Face Space."
48
+ )
49
+
50
+ uploaded_file = st.file_uploader("Choose a chest X-ray image (JPG, PNG)", type=["jpg", "jpeg", "png"])
51
+
52
+ if uploaded_file is not None:
53
+ image = Image.open(uploaded_file)
54
+ st.image(image, caption="Uploaded Image", use_column_width=True)
55
+
56
+ with st.spinner("Analyzing image with Maira-2..."):
57
+ analysis_results = analyze_image_with_maira(image)
58
+
59
+ if analysis_results:
60
+ # --- Results Display (VQA format) ---
61
+ if isinstance(analysis_results, dict) and 'answer' in analysis_results:
62
+ st.subheader("Findings:")
63
+ st.write(analysis_results['answer'])
64
+ else:
65
+ st.warning("Unexpected API response format.")
66
+ st.write("Raw API response:", analysis_results)
67
+ else:
68
+ st.error("Failed to get analysis results.")
69
+
70
+ else:
71
+ st.write("Please upload an image.")
72
+
73
+ st.write("---")
74
+ st.write("Disclaimer: For informational purposes only. Not medical advice.")
75
+
76
+ if __name__ == "__main__":
77
+ main()