Spaces:
Sleeping
Sleeping
File size: 2,956 Bytes
8a359f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import os
import streamlit as st
import tensorflow as tf
from tensorflow.keras.models import load_model
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 models (assumed to be in the same directory or provide the correct path)
classification_model = load_model('classification_model.h5') # Model for normal/abnormal classification
organ_recognition_model = load_model('organ_recognition_model.h5') # Model for organ recognition
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) / 255.0
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) / 255.0
image_array = np.expand_dims(image_array, axis=0)
prediction = organ_recognition_model.predict(image_array)
organ_classes = ['Lung', 'Heart', 'Brain'] # Example classes
return organ_classes[np.argmax(prediction)]
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()
|