Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,36 @@
|
|
| 1 |
-
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return None
|
| 13 |
-
|
| 14 |
-
def classify_image_with_pipeline(pipe, image):
|
| 15 |
-
"""Classify an image using the pipeline."""
|
| 16 |
-
try:
|
| 17 |
-
results = pipe(image)
|
| 18 |
-
return results
|
| 19 |
-
except Exception as e:
|
| 20 |
-
st.error(f"Error classifying image: {e}")
|
| 21 |
-
return None
|
| 22 |
|
| 23 |
-
# Streamlit
|
| 24 |
st.title("Pneumonia Chest X-ray Image Detection")
|
| 25 |
-
st.markdown(
|
| 26 |
-
"""
|
| 27 |
-
This app detects signs of pneumonia in chest X-ray images using a pre-trained Hugging Face model.
|
| 28 |
-
"""
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# File uploader
|
| 32 |
-
uploaded_file = st.file_uploader("Upload a chest X-ray image", type=["jpg", "jpeg", "png"])
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
st.image(image, caption="Uploaded Chest X-ray", use_column_width=True)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
st.write("Classifying the image...")
|
| 43 |
-
results = classify_image_with_pipeline(pipe, image)
|
| 44 |
-
|
| 45 |
-
if results:
|
| 46 |
-
st.write("### Classification Results:")
|
| 47 |
-
for result in results:
|
| 48 |
-
st.write(f"**Label:** {result['label']} | **Score:** {result['score']:.4f}")
|
| 49 |
-
|
| 50 |
-
# Optional: Add Groq API integration if applicable
|
| 51 |
-
if os.getenv("GROQ_API_KEY"):
|
| 52 |
-
from groq import Groq
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
messages=[
|
| 63 |
-
{
|
| 64 |
-
"role": "user",
|
| 65 |
-
"content": question,
|
| 66 |
-
}
|
| 67 |
-
],
|
| 68 |
-
model="llama-3.3-70b-versatile",
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
except Exception as e:
|
| 74 |
-
st.sidebar.error(f"Error using Groq API: {e}")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Check if Groq API key is set and load it if available
|
| 7 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
+
if GROQ_API_KEY:
|
| 9 |
+
from groq import Groq
|
| 10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
+
# Use Groq API here if you want it for predictions or related tasks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Streamlit setup
|
| 14 |
st.title("Pneumonia Chest X-ray Image Detection")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Upload image
|
| 17 |
+
uploaded_image = st.file_uploader("Choose a chest X-ray image...", type=["jpg", "jpeg", "png"])
|
|
|
|
| 18 |
|
| 19 |
+
if uploaded_image is not None:
|
| 20 |
+
# Display the image
|
| 21 |
+
image = Image.open(uploaded_image)
|
| 22 |
+
st.image(image, caption="Uploaded X-ray Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Load the Hugging Face model using the pipeline
|
| 25 |
+
pipe = pipeline("image-classification", model="dima806/pneumonia_chest_xray_image_detection")
|
| 26 |
|
| 27 |
+
# Run prediction
|
| 28 |
+
with st.spinner("Classifying..."):
|
| 29 |
+
prediction = pipe(image)
|
| 30 |
|
| 31 |
+
# Display results
|
| 32 |
+
st.write(f"Prediction: {prediction[0]['label']}")
|
| 33 |
+
st.write(f"Confidence: {prediction[0]['score']:.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
else:
|
| 36 |
+
st.warning("Please upload a chest X-ray image to begin detection.")
|
|
|
|
|
|