Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,74 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
import torch
|
| 5 |
-
from torchvision import transforms, models
|
| 6 |
-
import numpy as np
|
| 7 |
-
from groq import Groq
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Initialize Groq client
|
| 13 |
-
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 14 |
-
|
| 15 |
-
# Load Pretrained Models
|
| 16 |
-
@st.cache_resource
|
| 17 |
-
# Load Pretrained Model for Organ Recognition
|
| 18 |
-
@st.cache_resource
|
| 19 |
-
def load_organ_model():
|
| 20 |
-
model = models.resnet18(pretrained=True) # Load pretrained ResNet18
|
| 21 |
-
num_features = model.fc.in_features # Get the number of input features to the final layer
|
| 22 |
-
model.fc = torch.nn.Linear(num_features, 4) # Modify the final layer for 4 classes
|
| 23 |
-
model.eval() # Set the model to evaluation mode
|
| 24 |
-
return model
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Image Preprocessing
|
| 29 |
-
def preprocess_image(image):
|
| 30 |
-
transform = transforms.Compose([
|
| 31 |
-
transforms.Resize((224, 224)),
|
| 32 |
-
transforms.ToTensor(),
|
| 33 |
-
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 34 |
-
])
|
| 35 |
-
return transform(image).unsqueeze(0)
|
| 36 |
-
|
| 37 |
-
# Groq API for AI Insights
|
| 38 |
-
def get_ai_insights(text_prompt):
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
messages=[{"role": "user", "content": text_prompt}],
|
| 42 |
-
model="llama-3.3-70b-versatile"
|
| 43 |
-
)
|
| 44 |
-
return response.choices[0].message.content
|
| 45 |
except Exception as e:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Organ Recognition Prediction
|
| 49 |
-
def predict_organ(image):
|
| 50 |
-
with torch.no_grad():
|
| 51 |
-
input_tensor = preprocess_image(image)
|
| 52 |
-
output = organ_model(input_tensor)
|
| 53 |
-
|
| 54 |
-
# Check the output dimensions
|
| 55 |
-
st.write(f"Model output shape: {output.shape}")
|
| 56 |
-
|
| 57 |
-
# Ensure the output matches the number of classes
|
| 58 |
-
classes = ["Lungs", "Heart", "Spine", "Other"]
|
| 59 |
-
if output.size(1) != len(classes):
|
| 60 |
-
raise ValueError(
|
| 61 |
-
f"Model output size ({output.size(1)}) does not match the number of classes ({len(classes)})."
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
# Get the prediction
|
| 65 |
-
prediction_index = output.argmax().item()
|
| 66 |
-
prediction = classes[prediction_index]
|
| 67 |
-
return prediction
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
# Streamlit App
|
| 79 |
-
st.title("
|
| 80 |
-
st.
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
if
|
| 101 |
-
st.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
response = get_ai_insights(user_input)
|
| 111 |
-
st.write(response)
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from transformers import pipeline, AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def load_pipeline():
|
| 7 |
+
"""Load the Hugging Face pipeline for image classification."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
+
return pipeline("image-classification", model="dima806/pneumonia_chest_xray_image_detection")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
except Exception as e:
|
| 11 |
+
st.error(f"Error loading pipeline: {e}")
|
| 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 App
|
| 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 |
+
if uploaded_file:
|
| 35 |
+
image = Image.open(uploaded_file)
|
| 36 |
+
st.image(image, caption="Uploaded Chest X-ray", use_column_width=True)
|
| 37 |
+
|
| 38 |
+
# Load the model pipeline
|
| 39 |
+
pipe = load_pipeline()
|
| 40 |
+
|
| 41 |
+
if pipe:
|
| 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 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 55 |
+
|
| 56 |
+
st.sidebar.markdown("### Groq API Integration")
|
| 57 |
+
question = st.sidebar.text_input("Ask a question about pneumonia or X-ray diagnosis:")
|
| 58 |
+
|
| 59 |
+
if question:
|
| 60 |
+
try:
|
| 61 |
+
chat_completion = client.chat.completions.create(
|
| 62 |
+
messages=[
|
| 63 |
+
{
|
| 64 |
+
"role": "user",
|
| 65 |
+
"content": question,
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
model="llama-3.3-70b-versatile",
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
st.sidebar.write("**Groq API Response:**")
|
| 72 |
+
st.sidebar.write(chat_completion.choices[0].message.content)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.sidebar.error(f"Error using Groq API: {e}")
|
|
|
|
|
|