Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,65 @@
|
|
1 |
import os
|
2 |
-
import
|
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 |
-
#
|
15 |
-
|
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 |
-
|
36 |
-
|
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 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
organ = recognize_organ("temp_image.jpg")
|
65 |
-
st.write(f"Recognized Organ: **{organ}**")
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import numpy as np
|
3 |
import tensorflow as tf
|
4 |
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
+
import streamlit as st
|
7 |
+
from groq import Groq
|
8 |
|
9 |
# Set Groq API key in environment variable
|
10 |
os.environ['GROQ_API_KEY'] = "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"
|
11 |
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
12 |
|
13 |
+
# Initialize Groq client
|
14 |
+
client = Groq(api_key=GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Load the classification model (make sure the model is trained and saved)
|
17 |
+
classification_model = load_model('classification_model.h5') # Model for normal/abnormal classification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Function to load the image and process it
|
20 |
+
def load_image(image_file):
|
21 |
+
img = Image.open(image_file)
|
22 |
+
img = img.resize((224, 224)) # Resize image to match model input
|
23 |
+
img_array = np.array(img) / 255.0 # Normalize the image
|
24 |
+
return np.expand_dims(img_array, axis=0)
|
25 |
|
26 |
+
# Function for AI-based knowledge generation using Groq API
|
27 |
+
def generate_ai_insights(organ_name):
|
28 |
+
chat_completion = client.chat.completions.create(
|
29 |
+
messages=[
|
30 |
+
{
|
31 |
+
"role": "user",
|
32 |
+
"content": f"Explain the diseases and treatments related to {organ_name}.",
|
33 |
+
}
|
34 |
+
],
|
35 |
+
model="llama-3.3-70b-versatile",
|
36 |
+
)
|
37 |
+
return chat_completion.choices[0].message.content
|
38 |
|
39 |
+
# Streamlit UI
|
40 |
+
st.title('Medical Image Classification and Insights')
|
41 |
+
st.sidebar.title("Menu")
|
42 |
|
43 |
+
uploaded_image = st.sidebar.file_uploader("Upload X-ray or MRI Image", type=["jpg", "png", "jpeg"])
|
|
|
|
|
44 |
|
45 |
+
if uploaded_image is not None:
|
46 |
+
image = load_image(uploaded_image)
|
47 |
+
|
48 |
+
# Classify normal or abnormal
|
49 |
+
prediction = classification_model.predict(image)
|
50 |
+
if prediction[0] > 0.5:
|
51 |
+
classification_result = "Normal"
|
52 |
+
else:
|
53 |
+
classification_result = "Abnormal"
|
54 |
|
55 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
56 |
+
st.write(f"Image Classification: {classification_result}")
|
57 |
+
|
58 |
+
# Recognize the organ (You can expand the model to predict organ type)
|
59 |
+
organ_name = "Lung" # Placeholder
|
60 |
+
st.write(f"Recognized Organ: {organ_name}")
|
61 |
+
|
62 |
+
# Get AI insights for the recognized organ
|
63 |
+
ai_insights = generate_ai_insights(organ_name)
|
64 |
+
st.write("AI-Based Insights on Organ Diseases and Treatments:")
|
65 |
+
st.write(ai_insights)
|