devfire commited on
Commit
6a704db
Β·
verified Β·
1 Parent(s): dea06d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -62
app.py CHANGED
@@ -1,74 +1,107 @@
1
- import os
2
- import streamlit as st
3
- from groq import Groq
 
4
 
5
- # βœ… Fix TensorFlow CPU warnings
6
- os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
7
 
8
- # βœ… Set up the Groq API Key
9
- GROQ_API_KEY = "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"
10
- os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
 
11
 
12
- # βœ… Initialize the Groq client (Fixed 'proxies' issue)
13
- client = Groq(api_key=GROQ_API_KEY)
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # βœ… Streamlit UI setup
16
- st.set_page_config(page_title="AI Disease Detection Assistant", page_icon="🩺", layout="wide")
17
- st.title("🩺 AI Disease Detection Chatbot")
18
- st.write("Hello! I'm your AI assistant for disease-related queries. Ask me about symptoms, treatments, or general health advice.")
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # βœ… Sidebar settings
21
- st.sidebar.header("βš™οΈ Settings")
22
- chat_theme = st.sidebar.radio("Choose a theme:", ["Light", "Dark", "Blue", "Green"])
23
 
24
- # βœ… Apply themes
25
- themes = {
26
- "Dark": "#1e1e1e",
27
- "Blue": "#e3f2fd",
28
- "Green": "#e8f5e9",
29
- "Light": "#ffffff"
30
- }
31
- st.markdown(f"""
32
- <style>
33
- body {{ background-color: {themes[chat_theme]}; color: black; }}
34
- .stButton>button {{ background-color: #4CAF50; color: white; }}
35
- .chat-bubble {{ background-color: #f1f1f1; border-radius: 10px; padding: 10px; }}
36
- </style>
37
- """, unsafe_allow_html=True)
38
 
39
- # βœ… Session state for chat history
40
- if 'conversation_history' not in st.session_state:
41
- st.session_state.conversation_history = []
 
 
 
42
 
43
- # βœ… Function to generate AI response
44
- def generate_chatbot_response(user_message):
45
- # Custom responses
46
- if "who created you" in user_message.lower():
47
- return "I was created by Abdel Basit. 😊"
48
 
49
- # AI model response
50
- prompt = f"You are a medical AI assistant. The user asks: {user_message}. Provide a detailed, accurate medical response."
51
-
52
- try:
53
- chat_completion = client.chat.completions.create(
54
- messages=[{"role": "user", "content": prompt}],
55
- model="llama3-8b-8192"
56
- )
57
- return chat_completion.choices[0].message.content
58
- except Exception as e:
59
- return f"⚠️ Error: {str(e)}"
 
60
 
61
- # βœ… User chat input
62
- st.markdown("### πŸ’¬ Chat with me")
63
- user_input = st.chat_input("Ask me a health-related question:")
 
 
 
64
 
65
- if user_input:
66
- chatbot_response = generate_chatbot_response(user_input)
67
- st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- # βœ… Display chat history
70
- st.markdown("---")
71
- st.markdown("### πŸ—¨οΈ Chat History")
72
- for question, answer in st.session_state.conversation_history:
73
- st.write(f"<div class='chat-bubble'><b>{question}</b></div>", unsafe_allow_html=True)
74
- st.write(f"<div class='chat-bubble'>{answer}</div>", unsafe_allow_html=True)
 
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
19
+ img_array = np.expand_dims(img_array, axis=0)
20
+
21
+ prediction = model.predict(img_array)
22
+ predicted_class = "Pneumonia" if prediction > 0.5 else "Normal"
23
+
24
+ # Get the corresponding solution
25
+ solution = solutions.get(predicted_class, "No specific advice available.")
26
+
27
+ return predicted_class, solution
28
 
29
+ # CSS Styling
30
+ css = """
31
+ .gradio-container {
32
+ background-color: #f5f5f5;
33
+ font-family: Arial, sans-serif;
34
+ }
35
+
36
+ .gr-button {
37
+ background-color:#007bff;
38
+ color: white;
39
+ border: none;
40
+ border-radius: 5px;
41
+ font-size: 16px;
42
+ padding: 10px 20px;
43
+ cursor: pointer;
44
+ transition: background-color 0.3s ease;
45
+ }
46
 
47
+ .gr-button:hover {
48
+ background-color: #0056b3;
49
+ }
50
 
51
+ .gr-textbox, .gr-image {
52
+ border: 2px dashed #007bff;
53
+ padding: 20px;
54
+ border-radius: 10px;
55
+ background-color: #ffffff;
56
+ }
 
 
 
 
 
 
 
 
57
 
58
+ .gr-box-text {
59
+ color: #007bff;
60
+ font-size: 22px;
61
+ font-weight: bold;
62
+ text-align: center;
63
+ }
64
 
65
+ h1 {
66
+ font-size: 36px;
67
+ color: #007bff;
68
+ text-align: center;
69
+ }
70
 
71
+ p {
72
+ font-size: 20px;
73
+ color: #333;
74
+ text-align: center;
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
+
96
+ with gr.Row():
97
+ image_input = gr.Image(label="Drop Image Here", type="pil", elem_classes=["gr-image", "gr-box-text"])
98
+ output_prediction = gr.Textbox(label="Model Analysis Output", elem_classes=["gr-textbox", "gr-box-text"])
99
+ output_solution = gr.Textbox(label="Recommended Solution", elem_classes=["gr-textbox", "gr-box-text"])
100
+
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()