Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,107 +1,74 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
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 |
-
|
59 |
-
|
60 |
-
font-size: 22px;
|
61 |
-
font-weight: bold;
|
62 |
-
text-align: center;
|
63 |
-
}
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
}
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
text-align: center;
|
75 |
-
}
|
76 |
-
"""
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
"""
|
90 |
|
91 |
-
#
|
92 |
-
|
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 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
105 |
|
106 |
-
#
|
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)
|