Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
import tensorflow as tf | |
from tensorflow.keras.applications import ResNet50 | |
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions | |
from tensorflow.keras.models import Model | |
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D | |
from tensorflow.keras.preprocessing.image import img_to_array, load_img | |
import numpy as np | |
import requests | |
from PIL import Image | |
# Set Groq API key in environment variable | |
os.environ['GROQ_API_KEY'] = "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" | |
GROQ_API_KEY = os.getenv('GROQ_API_KEY') | |
# Load pre-trained ResNet50 for normal/abnormal classification | |
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) | |
x = base_model.output | |
x = GlobalAveragePooling2D()(x) | |
x = Dense(1024, activation='relu')(x) | |
predictions = Dense(1, activation='sigmoid')(x) | |
classification_model = Model(inputs=base_model.input, outputs=predictions) | |
# Load pre-trained ResNet50 for organ recognition | |
organ_model = ResNet50(weights='imagenet') | |
def classify_image(image_path): | |
"""Classify the image as normal or abnormal.""" | |
image = load_img(image_path, target_size=(224, 224)) | |
image_array = img_to_array(image) | |
image_array = preprocess_input(image_array) | |
image_array = np.expand_dims(image_array, axis=0) | |
prediction = classification_model.predict(image_array) | |
return 'Abnormal' if prediction[0][0] > 0.5 else 'Normal' | |
def recognize_organ(image_path): | |
"""Recognize the organ in the image.""" | |
image = load_img(image_path, target_size=(224, 224)) | |
image_array = img_to_array(image) | |
image_array = preprocess_input(image_array) | |
image_array = np.expand_dims(image_array, axis=0) | |
prediction = organ_model.predict(image_array) | |
decoded = decode_predictions(prediction, top=3)[0] | |
return decoded[0][1] # Top predicted class | |
def get_ai_insights(organ): | |
"""Fetch AI-based insights about the organ using Groq API.""" | |
url = "https://api.groq.com/v1/insights" | |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"} | |
data = {"query": f"Provide detailed insights about {organ} X-ray, its diseases, and treatments."} | |
response = requests.post(url, headers=headers, json=data) | |
if response.status_code == 200: | |
return response.json().get("insights", "No insights available.") | |
else: | |
return "Failed to fetch insights. Please try again later." | |
def main(): | |
st.title("Medical Image Classification App") | |
st.sidebar.title("Navigation") | |
uploaded_file = st.file_uploader("Upload an X-ray or MRI image", type=["jpg", "jpeg", "png"]) | |
if uploaded_file: | |
image = Image.open(uploaded_file) | |
st.image(image, caption="Uploaded Image", use_column_width=True) | |
with open("temp_image.jpg", "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
st.write("### Classification Result") | |
result = classify_image("temp_image.jpg") | |
st.write(f"The X-ray is classified as: **{result}**") | |
st.write("### Organ Recognition") | |
organ = recognize_organ("temp_image.jpg") | |
st.write(f"Recognized Organ: **{organ}**") | |
st.write("### AI-Based Insights") | |
insights = get_ai_insights(organ) | |
st.write(insights) | |
if __name__ == "__main__": | |
main() | |