abuzarAli commited on
Commit
8066bd0
·
verified ·
1 Parent(s): e8686dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -56
app.py CHANGED
@@ -1,72 +1,65 @@
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()
 
 
 
 
 
 
 
 
 
 
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)