File size: 2,288 Bytes
8a359f9
8066bd0
8a359f9
 
 
8066bd0
 
8a359f9
 
 
 
 
8066bd0
 
8a359f9
8066bd0
 
8a359f9
8066bd0
 
 
 
 
 
8a359f9
8066bd0
 
 
 
 
 
 
 
 
 
 
 
8a359f9
8066bd0
 
 
8a359f9
8066bd0
8a359f9
8066bd0
 
 
 
 
 
 
 
 
8a359f9
8066bd0
 
 
 
 
 
 
 
 
 
 
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
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image
import streamlit as st
from groq import Groq

# Set Groq API key in environment variable
os.environ['GROQ_API_KEY'] = "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"
GROQ_API_KEY = os.getenv('GROQ_API_KEY')

# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)

# Load the classification model (make sure the model is trained and saved)
classification_model = load_model('classification_model.h5')  # Model for normal/abnormal classification

# Function to load the image and process it
def load_image(image_file):
    img = Image.open(image_file)
    img = img.resize((224, 224))  # Resize image to match model input
    img_array = np.array(img) / 255.0  # Normalize the image
    return np.expand_dims(img_array, axis=0)

# Function for AI-based knowledge generation using Groq API
def generate_ai_insights(organ_name):
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"Explain the diseases and treatments related to {organ_name}.",
            }
        ],
        model="llama-3.3-70b-versatile",
    )
    return chat_completion.choices[0].message.content

# Streamlit UI
st.title('Medical Image Classification and Insights')
st.sidebar.title("Menu")

uploaded_image = st.sidebar.file_uploader("Upload X-ray or MRI Image", type=["jpg", "png", "jpeg"])

if uploaded_image is not None:
    image = load_image(uploaded_image)
    
    # Classify normal or abnormal
    prediction = classification_model.predict(image)
    if prediction[0] > 0.5:
        classification_result = "Normal"
    else:
        classification_result = "Abnormal"

    st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
    st.write(f"Image Classification: {classification_result}")
    
    # Recognize the organ (You can expand the model to predict organ type)
    organ_name = "Lung"  # Placeholder
    st.write(f"Recognized Organ: {organ_name}")
    
    # Get AI insights for the recognized organ
    ai_insights = generate_ai_insights(organ_name)
    st.write("AI-Based Insights on Organ Diseases and Treatments:")
    st.write(ai_insights)