Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import tensorflow as tf
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
6 |
+
import numpy as np
|
7 |
+
import requests
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
# Set Groq API key in environment variable
|
11 |
+
os.environ['GROQ_API_KEY'] = "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"
|
12 |
+
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
13 |
+
|
14 |
+
# Load pre-trained models (assumed to be in the same directory or provide the correct path)
|
15 |
+
classification_model = load_model('classification_model.h5') # Model for normal/abnormal classification
|
16 |
+
organ_recognition_model = load_model('organ_recognition_model.h5') # Model for organ recognition
|
17 |
+
|
18 |
+
def classify_image(image_path):
|
19 |
+
"""Classify the image as normal or abnormal."""
|
20 |
+
image = load_img(image_path, target_size=(224, 224))
|
21 |
+
image_array = img_to_array(image) / 255.0
|
22 |
+
image_array = np.expand_dims(image_array, axis=0)
|
23 |
+
prediction = classification_model.predict(image_array)
|
24 |
+
return 'Abnormal' if prediction[0][0] > 0.5 else 'Normal'
|
25 |
+
|
26 |
+
def recognize_organ(image_path):
|
27 |
+
"""Recognize the organ in the image."""
|
28 |
+
image = load_img(image_path, target_size=(224, 224))
|
29 |
+
image_array = img_to_array(image) / 255.0
|
30 |
+
image_array = np.expand_dims(image_array, axis=0)
|
31 |
+
prediction = organ_recognition_model.predict(image_array)
|
32 |
+
organ_classes = ['Lung', 'Heart', 'Brain'] # Example classes
|
33 |
+
return organ_classes[np.argmax(prediction)]
|
34 |
+
|
35 |
+
def get_ai_insights(organ):
|
36 |
+
"""Fetch AI-based insights about the organ using Groq API."""
|
37 |
+
url = "https://api.groq.com/v1/insights"
|
38 |
+
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
39 |
+
data = {"query": f"Provide detailed insights about {organ} X-ray, its diseases, and treatments."}
|
40 |
+
response = requests.post(url, headers=headers, json=data)
|
41 |
+
if response.status_code == 200:
|
42 |
+
return response.json().get("insights", "No insights available.")
|
43 |
+
else:
|
44 |
+
return "Failed to fetch insights. Please try again later."
|
45 |
+
|
46 |
+
def main():
|
47 |
+
st.title("Medical Image Classification App")
|
48 |
+
st.sidebar.title("Navigation")
|
49 |
+
|
50 |
+
uploaded_file = st.file_uploader("Upload an X-ray or MRI image", type=["jpg", "jpeg", "png"])
|
51 |
+
|
52 |
+
if uploaded_file:
|
53 |
+
image = Image.open(uploaded_file)
|
54 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
55 |
+
|
56 |
+
with open("temp_image.jpg", "wb") as f:
|
57 |
+
f.write(uploaded_file.getbuffer())
|
58 |
+
|
59 |
+
st.write("### Classification Result")
|
60 |
+
result = classify_image("temp_image.jpg")
|
61 |
+
st.write(f"The X-ray is classified as: **{result}**")
|
62 |
+
|
63 |
+
st.write("### Organ Recognition")
|
64 |
+
organ = recognize_organ("temp_image.jpg")
|
65 |
+
st.write(f"Recognized Organ: **{organ}**")
|
66 |
+
|
67 |
+
st.write("### AI-Based Insights")
|
68 |
+
insights = get_ai_insights(organ)
|
69 |
+
st.write(insights)
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
main()
|