Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
from huggingface_hub import
|
4 |
-
from huggingface_hub import InferenceClient # Use InferenceClient
|
5 |
import io
|
6 |
import base64
|
7 |
|
@@ -9,12 +8,14 @@ import base64
|
|
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 |
-
|
|
|
|
|
|
|
18 |
img_str = base64.b64encode(buffered.getvalue()).decode()
|
19 |
return img_str
|
20 |
|
@@ -30,12 +31,12 @@ def analyze_image_with_maira(image):
|
|
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"
|
34 |
)
|
35 |
return result
|
36 |
|
37 |
except Exception as e:
|
38 |
-
st.error(f"An error occurred: {e}")
|
39 |
return None
|
40 |
|
41 |
|
@@ -65,7 +66,7 @@ def main():
|
|
65 |
st.warning("Unexpected API response format.")
|
66 |
st.write("Raw API response:", analysis_results)
|
67 |
else:
|
68 |
-
|
69 |
|
70 |
else:
|
71 |
st.write("Please upload an image.")
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
+
from huggingface_hub import InferenceClient
|
|
|
4 |
import io
|
5 |
import base64
|
6 |
|
|
|
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 |
|
|
|
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=encoded_image, # Pass the encoded image directly
|
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}") # General exception handling is sufficient here
|
40 |
return None
|
41 |
|
42 |
|
|
|
66 |
st.warning("Unexpected API response format.")
|
67 |
st.write("Raw API response:", analysis_results)
|
68 |
else:
|
69 |
+
st.error("Failed to get analysis results.")
|
70 |
|
71 |
else:
|
72 |
st.write("Please upload an image.")
|