Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,32 @@ import streamlit as st
|
|
2 |
from PIL import Image
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import io
|
5 |
-
import base64
|
6 |
|
7 |
# --- Configuration (Simplified for Spaces) ---
|
8 |
|
9 |
-
# No need for API token if running *within* a Space
|
10 |
-
# The Space's environment will handle authentication
|
11 |
-
|
12 |
-
# --- Image Encoding ---
|
13 |
-
def encode_image(image):
|
14 |
-
buffered = io.BytesIO()
|
15 |
-
# Convert to RGB *before* saving as JPEG
|
16 |
-
if image.mode == "RGBA":
|
17 |
-
image = image.convert("RGB")
|
18 |
-
image.save(buffered, format="JPEG") # Save as JPEG
|
19 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
20 |
-
return img_str
|
21 |
-
|
22 |
-
|
23 |
# --- Model Interaction (using InferenceClient) ---
|
24 |
|
25 |
def analyze_image_with_maira(image):
|
26 |
"""Analyzes the image using the Maira-2 model via the Hugging Face Inference API.
|
27 |
"""
|
28 |
try:
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
client = InferenceClient() # No token needed inside the Space
|
31 |
result = client.question_answering(
|
32 |
question="Analyze this chest X-ray image and provide detailed findings. Include any abnormalities, their locations, and potential diagnoses. Be as specific as possible.",
|
33 |
-
image=
|
34 |
model="microsoft/maira-2" # Specify the model
|
35 |
)
|
36 |
return result
|
37 |
|
38 |
except Exception as e:
|
39 |
-
st.error(f"An error occurred: {e}")
|
40 |
return None
|
41 |
|
42 |
|
|
|
2 |
from PIL import Image
|
3 |
from huggingface_hub import InferenceClient
|
4 |
import io
|
|
|
5 |
|
6 |
# --- Configuration (Simplified for Spaces) ---
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# --- Model Interaction (using InferenceClient) ---
|
9 |
|
10 |
def analyze_image_with_maira(image):
|
11 |
"""Analyzes the image using the Maira-2 model via the Hugging Face Inference API.
|
12 |
"""
|
13 |
try:
|
14 |
+
# Prepare image data - no need to encode for InferenceClient if sending bytes directly
|
15 |
+
image_bytes = io.BytesIO()
|
16 |
+
if image.mode == "RGBA": # Handle RGBA images (if any)
|
17 |
+
image = image.convert("RGB")
|
18 |
+
image.save(image_bytes, format="JPEG")
|
19 |
+
image_bytes = image_bytes.getvalue() # Get the bytes
|
20 |
+
|
21 |
client = InferenceClient() # No token needed inside the Space
|
22 |
result = client.question_answering(
|
23 |
question="Analyze this chest X-ray image and provide detailed findings. Include any abnormalities, their locations, and potential diagnoses. Be as specific as possible.",
|
24 |
+
image=image_bytes, # Pass the image bytes directly
|
25 |
model="microsoft/maira-2" # Specify the model
|
26 |
)
|
27 |
return result
|
28 |
|
29 |
except Exception as e:
|
30 |
+
st.error(f"An error occurred: {e}")
|
31 |
return None
|
32 |
|
33 |
|