devfire commited on
Commit
7c6c8b9
·
verified ·
1 Parent(s): a44e524

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -22
app.py CHANGED
@@ -1,18 +1,28 @@
 
1
  import gradio as gr
2
- from tensorflow.keras.models import load_model
 
3
  import numpy as np
4
  from PIL import Image
 
5
 
6
- # Load model
7
  model = load_model('xray_image_classifier_model.keras')
8
 
 
 
 
 
 
 
 
9
  # Define solutions
10
  solutions = {
11
  "Pneumonia": "Consult a doctor immediately. Follow prescribed antibiotics if given, rest well, and stay hydrated.",
12
  "Normal": "Your X-ray appears normal. However, if you experience symptoms, consult a doctor for further evaluation."
13
  }
14
 
15
- # Prediction function
16
  def predict(image):
17
  img = image.resize((150, 150))
18
  img_array = np.array(img) / 255.0
@@ -26,7 +36,7 @@ def predict(image):
26
 
27
  return predicted_class, solution
28
 
29
- # CSS Styling
30
  css = """
31
  .gradio-container {
32
  background-color: #f5f5f5;
@@ -75,21 +85,8 @@ css = """
75
  }
76
  """
77
 
78
- # Description
79
- description = """
80
- **Automated Pneumonia Detection via Chest X-ray Classification**
81
-
82
- This model leverages deep learning techniques to classify chest X-ray images as either 'Pneumonia' or 'Normal.' By utilizing the InceptionV3 architecture for transfer learning, combined with data preprocessing and augmentation, the model aims to deliver powerful performance in medical image analysis. It enhances the automation of diagnostic processes, aiding in the detection of pneumonia with high accuracy.
83
-
84
- **Technologies Employed:**
85
- - TensorFlow & Keras for model development
86
- - InceptionV3 for transfer learning
87
- - Numpy, Pandas, and Matplotlib for data handling and visualization
88
- - Flask and Gradio for deployment and user interaction
89
- """
90
-
91
- # Gradio UI
92
- with gr.Blocks(css=css) as interface:
93
  gr.Markdown("<h1>Automated Pneumonia Detection via Chest X-ray Classification</h1>")
94
  gr.Markdown("<p>Submit a chest X-ray image below.</p>")
95
 
@@ -101,7 +98,68 @@ with gr.Blocks(css=css) as interface:
101
  submit_btn = gr.Button("Initiate Diagnostic Analysis", elem_classes=["gr-button"])
102
  submit_btn.click(fn=predict, inputs=image_input, outputs=[output_prediction, output_solution])
103
 
104
- gr.Markdown(description)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- # Launch the app
107
- interface.launch()
 
1
+ import os
2
  import gradio as gr
3
+ import streamlit as st
4
+ from groq import Groq
5
  import numpy as np
6
  from PIL import Image
7
+ from tensorflow.keras.models import load_model
8
 
9
+ # Load Pneumonia Detection Model
10
  model = load_model('xray_image_classifier_model.keras')
11
 
12
+ # Set up Groq API Key
13
+ GROQ_API_KEY = "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"
14
+ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
15
+
16
+ # Initialize the Groq client
17
+ client = Groq(api_key=GROQ_API_KEY)
18
+
19
  # Define solutions
20
  solutions = {
21
  "Pneumonia": "Consult a doctor immediately. Follow prescribed antibiotics if given, rest well, and stay hydrated.",
22
  "Normal": "Your X-ray appears normal. However, if you experience symptoms, consult a doctor for further evaluation."
23
  }
24
 
25
+ # Prediction Function
26
  def predict(image):
27
  img = image.resize((150, 150))
28
  img_array = np.array(img) / 255.0
 
36
 
37
  return predicted_class, solution
38
 
39
+ # CSS Styling for Gradio
40
  css = """
41
  .gradio-container {
42
  background-color: #f5f5f5;
 
85
  }
86
  """
87
 
88
+ # Gradio UI for Pneumonia Detection
89
+ with gr.Blocks(css=css) as gradio_interface:
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  gr.Markdown("<h1>Automated Pneumonia Detection via Chest X-ray Classification</h1>")
91
  gr.Markdown("<p>Submit a chest X-ray image below.</p>")
92
 
 
98
  submit_btn = gr.Button("Initiate Diagnostic Analysis", elem_classes=["gr-button"])
99
  submit_btn.click(fn=predict, inputs=image_input, outputs=[output_prediction, output_solution])
100
 
101
+ gr.Markdown("<h3>Note:</h3> <p>The AI model provides an initial assessment. Always consult a doctor for final diagnosis.</p>")
102
+
103
+ # Streamlit UI for Disease Chatbot
104
+ st.set_page_config(page_title="AI Health Assistant", page_icon="🩺", layout="wide")
105
+ st.title("🩺 AI Health Assistant")
106
+ st.write("Welcome! Upload an X-ray for pneumonia detection or ask the chatbot about diseases.")
107
+
108
+ # Sidebar Theme Settings
109
+ st.sidebar.header("⚙️ Settings")
110
+ chat_theme = st.sidebar.radio("Choose a theme:", ["Light", "Dark", "Blue", "Green"])
111
+
112
+ if chat_theme == "Dark":
113
+ st.markdown("""
114
+ <style>
115
+ body {background-color: #1e1e1e; color: white;}
116
+ .stButton>button {background-color: #4CAF50; color: white;}
117
+ .chat-bubble {background-color: #2c2c2c; border-radius: 10px; padding: 10px;}
118
+ </style>
119
+ """, unsafe_allow_html=True)
120
+ elif chat_theme == "Blue":
121
+ st.markdown("""
122
+ <style>
123
+ body {background-color: #e3f2fd; color: black;}
124
+ .stButton>button {background-color: #2196F3; color: white;}
125
+ .chat-bubble {background-color: #bbdefb; border-radius: 10px; padding: 10px;}
126
+ </style>
127
+ """, unsafe_allow_html=True)
128
+ elif chat_theme == "Green":
129
+ st.markdown("""
130
+ <style>
131
+ body {background-color: #e8f5e9; color: black;}
132
+ .stButton>button {background-color: #4CAF50; color: white;}
133
+ .chat-bubble {background-color: #c8e6c9; border-radius: 10px; padding: 10px;}
134
+ </style>
135
+ """, unsafe_allow_html=True)
136
+
137
+ # Chatbot Function
138
+ def generate_chatbot_response(user_message):
139
+ if "who created you" in user_message.lower():
140
+ return "I was created by Abdel Basit. 😊"
141
+
142
+ prompt = f"You are a helpful AI chatbot for medical guidance. The user is asking: {user_message}. Provide a detailed, professional response."
143
+
144
+ chat_completion = client.chat.completions.create(
145
+ messages=[{"role": "user", "content": prompt}],
146
+ model="llama3-8b-8192",
147
+ )
148
+
149
+ return chat_completion.choices[0].message.content
150
+
151
+ # Chatbot Interface
152
+ st.markdown("### 💬 Chat with the AI Health Assistant")
153
+ user_input = st.chat_input("Ask me a health-related question:")
154
+
155
+ if user_input:
156
+ chatbot_response = generate_chatbot_response(user_input)
157
+ st.markdown(f"**You:** {user_input}")
158
+ st.markdown(f"**AI:** {chatbot_response}")
159
+
160
+ # Launch Gradio Interface in Streamlit
161
+ st.markdown("---")
162
+ st.markdown("## 🔬 Pneumonia Detection System")
163
+ with st.expander("Click here to open the AI Pneumonia Detection System"):
164
+ gradio_interface.launch()
165