sahar0 commited on
Commit
b87cb04
·
verified ·
1 Parent(s): a859051

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +23 -0
  2. app.py +118 -0
  3. chatbot_model.h5 +3 -0
  4. intents.json +229 -0
  5. requirements.txt +0 -0
  6. training_data.pkl +3 -0
Dockerfile ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # استخدام نسخة بايثون رسمية
2
+ FROM python:3.9
3
+
4
+ # إعداد مجلد العمل
5
+ WORKDIR /code
6
+
7
+ # نسخ ملف المتطلبات وتثبيت المكتبات
8
+ COPY ./requirements.txt /code/requirements.txt
9
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
10
+
11
+ # إنشاء مجلد لتحميلات NLTK وتحديد الصلاحيات
12
+ RUN mkdir -p /code/nltk_data
13
+ ENV NLTK_DATA=/code/nltk_data
14
+ RUN chmod -R 777 /code/nltk_data
15
+
16
+ # نسخ باقي ملفات المشروع
17
+ COPY . .
18
+
19
+ # إعطاء صلاحيات للكتابة (مهم جداً في Hugging Face)
20
+ RUN chmod -R 777 /code
21
+
22
+ # أمر تشغيل التطبيق عبر Gunicorn على المنفذ 7860
23
+ CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import json
3
+ import numpy as np
4
+ import pickle
5
+ import nltk
6
+ from nltk.stem import PorterStemmer
7
+ from tensorflow.keras.models import load_model
8
+ import random
9
+ import os
10
+
11
+ # ========================================================
12
+ # إعداد NLTK للسيرفر (Hugging Face)
13
+ # ========================================================
14
+ # تحديد مسار التحميل داخل مجلد المشروع
15
+ nltk_data_path = os.path.join(os.getcwd(), 'nltk_data')
16
+
17
+ # إنشاء المجلد إذا لم يكن موجوداً
18
+ if not os.path.exists(nltk_data_path):
19
+ os.makedirs(nltk_data_path, exist_ok=True)
20
+
21
+ # إضافة المسار لقائمة بحث NLTK
22
+ nltk.data.path.append(nltk_data_path)
23
+
24
+ # تحميل المكتبات الضرورية (يتم التحميل مرة واحدة عند بدء التشغيل)
25
+ print("Downloading NLTK data...")
26
+ nltk.download('punkt', download_dir=nltk_data_path, quiet=True)
27
+ nltk.download('wordnet', download_dir=nltk_data_path, quiet=True)
28
+ nltk.download('punkt_tab', download_dir=nltk_data_path, quiet=True)
29
+ nltk.download('omw-1.4', download_dir=nltk_data_path, quiet=True)
30
+ print("✅ NLTK data downloaded successfully.")
31
+ # ========================================================
32
+
33
+ app = Flask(__name__)
34
+ stemmer = PorterStemmer()
35
+ ignore_words = ['?', '!', '.', ',']
36
+
37
+ # تحميل النموذج والبيانات
38
+ print("Loading model and data...")
39
+ try:
40
+ model = load_model('chatbot_model.h5')
41
+ data = pickle.load(open('training_data.pkl', 'rb'))
42
+ words = data['words']
43
+ classes = data['classes']
44
+ intents = data['data']
45
+ print("✅ Model loaded successfully.")
46
+ except Exception as e:
47
+ print(f"🛑 Error loading model: {e}")
48
+ # قيم افتراضية لمنع توقف التطبيق في حالة الخطأ
49
+ words = []
50
+ classes = []
51
+ intents = {"intents": []}
52
+
53
+ # دوال المعالجة الذكية
54
+ def clean_up_sentence(sentence):
55
+ sentence_words = nltk.word_tokenize(sentence)
56
+ sentence_words = [stemmer.stem(word.lower()) for word in sentence_words if word not in ignore_words]
57
+ return sentence_words
58
+
59
+ def bag_of_words(sentence, words):
60
+ sentence_words = clean_up_sentence(sentence)
61
+ bag = [0] * len(words)
62
+ for s in sentence_words:
63
+ for i, w in enumerate(words):
64
+ if w == s:
65
+ bag[i] = 1
66
+ return np.array(bag)
67
+
68
+ def predict_class(sentence):
69
+ if not words: return []
70
+
71
+ bow = bag_of_words(sentence, words)
72
+ res = model.predict(np.array([bow]), verbose=0)[0]
73
+ ERROR_THRESHOLD = 0.25
74
+ results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
75
+
76
+ results.sort(key=lambda x: x[1], reverse=True)
77
+ return_list = []
78
+ for r in results:
79
+ return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
80
+ return return_list
81
+
82
+ def get_response(ints, intents_json):
83
+ if not ints:
84
+ return "I'm sorry, I didn't understand that. Could you rephrase?"
85
+
86
+ tag = ints[0]['intent']
87
+ list_of_intents = intents_json['intents']
88
+ for i in list_of_intents:
89
+ if i['tag'] == tag:
90
+ result = random.choice(i['responses'])
91
+ break
92
+ else:
93
+ result = "I'm sorry, I don't have an answer for that."
94
+
95
+ return result
96
+
97
+ # --- صفحات الموقع ---
98
+
99
+ @app.route("/")
100
+ def home():
101
+ return render_template("index.html")
102
+
103
+ @app.route("/get_response", methods=["POST"])
104
+ def chatbot_response():
105
+ try:
106
+ msg = request.json.get('msg')
107
+ if msg:
108
+ ints = predict_class(msg)
109
+ res = get_response(ints, intents)
110
+ return jsonify({"response": res})
111
+ else:
112
+ return jsonify({"response": "Please type a message."})
113
+ except Exception as e:
114
+ return jsonify({"response": f"Error: {str(e)}"})
115
+
116
+ if __name__ == "__main__":
117
+ # إعدادات المنفذ الخاصة بـ Hugging Face
118
+ app.run(host='0.0.0.0', port=7860)
chatbot_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:57c1b738a5498bafe8e77ef6886148875f1e8226136be9c127a9ac0e07b487c0
3
+ size 330016
intents.json ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "intents": [
3
+ {
4
+ "tag": "greeting",
5
+ "patterns": ["Hi", "Hello", "Hey", "Good morning", "Is anyone there?"],
6
+ "responses": ["Hello! I am your medical assistant.", "Hi there, how can I help you?", "Good day! Ask me about any medical issue."]
7
+ },
8
+ {
9
+ "tag": "goodbye",
10
+ "patterns": ["Bye", "See you", "Goodbye", "Thanks", "Thank you"],
11
+ "responses": ["See you later, stay healthy!", "Goodbye!", "You are welcome."]
12
+ },
13
+ {
14
+ "tag": "Cuts",
15
+ "patterns": ["What to do if Cuts?", "How to cure Cuts?", "Which medicine to apply for Cuts?", "what to apply on cuts?", "Cuts"],
16
+ "responses": ["Wash the cut properly to prevent infection and stop the bleeding by applying pressure for 1-2minutes until bleeding stops. Apply Petroleum Jelly to make sure that the wound is moist for quick healing. Finally cover the cut with a sterile bandage. Pain relievers such as acetaminophen can be applied."]
17
+ },
18
+ {
19
+ "tag": "Abrasions",
20
+ "patterns": ["how do you treat abrasions?", "Do Abrasions cause scars?", "Abrasions", "what to do if abrasions?", "Which medicine to apply for abrasions?", "How to cure abrasions?"],
21
+ "responses": ["Begin with washed hands. Gently clean the area with cool to lukewarm water and mild soap. Remove dirt or other particles from the wound using sterilized tweezers. For a mild scrape that’s not bleeding, leave the wound uncovered. If the wound is bleeding, use a clean cloth or bandage, and apply gentle pressure to the area to stop any bleeding."]
22
+ },
23
+ {
24
+ "tag": "stings",
25
+ "patterns": ["How do you treat Sting?", "Stings", "What to do if you get a sting?", "Which medicine to apply if sting?"],
26
+ "responses": ["Remove any stingers immediately. Some experts recommend scraping out the stinger with a credit card. Applying ice to the site may provide some mild relief. Apply ice for 20 minutes once every hour as needed. Wash the sting site with soap and water."]
27
+ },
28
+ {
29
+ "tag": "Splinter",
30
+ "patterns": ["How to remove Splinters", "How to cure Splinters?", "What to do if I have splinters?", "How do you bring a splinter to the surface?"],
31
+ "responses": ["1. SOAK IT IN EPSOM SALTS. Dissolve a cup of the salts into a warm bath and soak whatever part of the body has the splinter. 2. VINEGAR OR OIL. Soak the affected area in oil (olive or corn) or white vinegar for around 20 to 30 minutes."]
32
+ },
33
+ {
34
+ "tag": "Sprains",
35
+ "patterns": ["How do you treat a sprain?", "what to do if i get a sprain?", "Which cream to apply if i get a sprain?", "Which medicine to apply if I get a sprain?"],
36
+ "responses": ["Use an ice pack or ice slush bath immediately for 15 to 20 minutes and repeat every two to three hours while you're awake. To help stop swelling, compress the ankle with an elastic bandage until the swelling stops."]
37
+ },
38
+ {
39
+ "tag": "Strains",
40
+ "patterns": ["How do you treat a strain?", "what to do if i get a strain?", "Which cream to apply if i get a strain?", "Which medicine to apply if I get a strain?"],
41
+ "responses": ["Rest, Ice, Compression and Elevation can be used to cure strains. Avoid using your muscle for a few days. Apply ice immediately after injuring your muscle."]
42
+ },
43
+ {
44
+ "tag": "Fever",
45
+ "patterns": ["How do you treat a mild Fever?", "what to do if i get a mild fever?", "Which medicine to take if I get a mild fever?", "fever"],
46
+ "responses": ["To treat a fever at home: 1)Drink plenty of fluids. 2)Dress in lightweight clothing. 3)Take acetaminophen (Tylenol) or ibuprofen. 5) Get medical help if the fever lasts more than five days."]
47
+ },
48
+ {
49
+ "tag": "Nasal Congestion",
50
+ "patterns": ["How do you treat nasal Congestion?", "what to do if i get a nasal congestion?", "Which medicine to take if I have a nasal congestion?"],
51
+ "responses": ["Focus on keeping your nasal passages and sinuses moist. 1)Use a humidifier. 2)Drink lots of fluids. 3)Place a warm, wet towel on your face."]
52
+ },
53
+ {
54
+ "tag": "Cough",
55
+ "patterns": ["How to cure cough?", "How do you treat cough?", "what to do if i get a cough?", "Which medicine to take if I get cough?"],
56
+ "responses": ["1) Honey: mix 2 teaspoons with warm water. 2) Ginger tea. 3) Fluids: Staying hydrated is vital."]
57
+ },
58
+ {
59
+ "tag": "Sore Throat",
60
+ "patterns": ["How do you treat sore throat?", "what to do if i get a sore throat?", "Which medicine to take if I get a sore throat?"],
61
+ "responses": ["1) Make sure you get plenty of rest and drink a lot of fluids. 2)Inhale steam. 3)Sip chicken broth or warm tea with honey."]
62
+ },
63
+ {
64
+ "tag": "Gastrointestinal problems",
65
+ "patterns": ["How do you treat gas problems?", "what to do if i have Gastrointestinal problems?", "Gas problems"],
66
+ "responses": ["1) Replenish body fluids. 2)Do not take antidiarrheal drugs unless specified by a professional. 3)Taking antacids may help."]
67
+ },
68
+ {
69
+ "tag": "Skin problems",
70
+ "patterns": ["How do you treat Skin problems?", "what to do if i get a skin allergy?", "Skin allergy"],
71
+ "responses": ["1)Hydrocortisone cream. 2)Ointments like calamine lotion. 3)Antihistamines. 4)Cold compresses."]
72
+ },
73
+ {
74
+ "tag": "Abdonominal Pain",
75
+ "patterns": ["How do you treat Abdonominal Pain?", "what to do if i get a Abdonominal Pain?", "Stomach pain"],
76
+ "responses": ["1)Provide clear fluids to sip, such as water or broth. 2)Serve bland foods, such as saltine crackers, plain bread, rice. 3)Avoid spicy or greasy foods."]
77
+ },
78
+ {
79
+ "tag": "Bruises",
80
+ "patterns": ["How do you treat Bruises?", "what to do if i get a Bruise?", "Bruise treatment"],
81
+ "responses": ["1)Ice the bruise with an ice pack wrapped in a towel for 10-20 minutes. 2)Compress the bruised area if it is swelling, using an elastic bandage."]
82
+ },
83
+ {
84
+ "tag": "Broken Toe",
85
+ "patterns": ["How do you treat a Broken Toe?", "what to do if i get a Broken Toe?", "Broken toe"],
86
+ "responses": ["1)To help decrease pain and swelling, elevate the foot, ice the injury, and stay off the foot. 3) Most broken toes heal without complications in six weeks."]
87
+ },
88
+ {
89
+ "tag": "Choking",
90
+ "patterns": ["How do you treat Choking?", "what to do if i get a Choke?", "Choking"],
91
+ "responses": ["1)Encourage them to keep coughing. 2)Ask them to try to spit out the object. 3)If coughing doesn't work, start back blows and abdominal thrusts."]
92
+ },
93
+ {
94
+ "tag": "Wound",
95
+ "patterns": ["How do you treat a wound?", "what to do if i get a Wound?", "Wound"],
96
+ "responses": ["1)Rinse the cut or wound with water and apply pressure with sterile gauze. 2)Raise the injured body part to slow bleeding. 4)Cover the wound with a new, clean bandage."]
97
+ },
98
+ {
99
+ "tag": "Diarrhea",
100
+ "patterns": ["How do you treat Diarrhea?", "what to do if i get Diarrhea?", "Diarrhea"],
101
+ "responses": ["1)Hydrating the body is essential. 2)Avoid dairy products. 3)If diarrhea lasts for more than 2 days, seek medical advice."]
102
+ },
103
+ {
104
+ "tag": "Frost bite",
105
+ "patterns": ["How do you treat a Frost bite?", "what to do if i get a Frost bite?", "Frost bite"],
106
+ "responses": ["1)Get out of the cold. 2) Gently rewarm frostbitten areas in warm water (not hot). Soak for 20 to 30 minutes."]
107
+ },
108
+ {
109
+ "tag": "Heat Exhaustion",
110
+ "patterns": ["How do you treat Heat Exhaustion?", "what to do if i feel Exhausted due to heat?", "Heat exhaustion"],
111
+ "responses": ["1)Move the person out of the heat into a shady place. Lay the person down and elevate the legs. Have the person drink cool water."]
112
+ },
113
+ {
114
+ "tag": "Heat Stroke",
115
+ "patterns": ["How do you treat Heat Stroke?", "what to do if i get a Heat Stroke?", "Heat stroke"],
116
+ "responses": ["1)Cool the person's entire body by sponging or spraying cold water. Watch for signs of rapidly progressing heatstroke, such as seizure."]
117
+ },
118
+ {
119
+ "tag": "Insect Bites",
120
+ "patterns": ["How do you treat a Insect Bite?", "what to do if a insect bites me?", "Insect bite"],
121
+ "responses": ["1) Wash the affected area with soap and water. Apply a cold compress or an ice pack to any swelling for at least 10 minutes."]
122
+ },
123
+ {
124
+ "tag": "nose bleed",
125
+ "patterns": ["How do you treat a bleeding nose?", "what to do if i my nose is bleeding?", "Nose bleed"],
126
+ "responses": ["Sit upright and lean forward. Pinch your nose using your thumb and index finger and breathe through your mouth. Continue for 10 to 15 minutes."]
127
+ },
128
+ {
129
+ "tag": "Pulled Muscle",
130
+ "patterns": ["How do you treat a Pulled Muscle?", "what to do if my muscle is pulled?", "Pulled muscle"],
131
+ "responses": ["1) Apply ice for 10 to 15 minutes every 1 hour for the first day. Rest the pulled muscle for at least a day."]
132
+ },
133
+ {
134
+ "tag": "Rectal bleeding",
135
+ "patterns": ["How do you treat Rectal Bleeding?", "Rectal bleeding"],
136
+ "responses": ["For minimal bleeding, home treatment with lots of water and fiber. If severe, seek emergency treatment."]
137
+ },
138
+ {
139
+ "tag": "Sun Burn",
140
+ "patterns": ["How do you treat Sun Burn?", "Sun burn"],
141
+ "responses": ["1) Cool the skin. 2) Drink water to prevent dehydration. 3) Take a pain reliever such as ibuprofen."]
142
+ },
143
+ {
144
+ "tag": "Testicle Pain",
145
+ "patterns": ["How do you treat Testicle Pain?", "Testicle pain"],
146
+ "responses": ["1) For pain, take over-the-counter medication such as acetaminophen. For swelling, apply an ice pack to the scrotum."]
147
+ },
148
+ {
149
+ "tag": "Vertigo",
150
+ "patterns": ["How do you treat a Vertigo?", "Vertigo"],
151
+ "responses": ["1) Have the person lie down and rest. 2) Help the person avoid falls. 3) Any new signs of vertigo should be checked by a doctor."]
152
+ },
153
+ {
154
+ "tag": "Eye Injury",
155
+ "patterns": ["How do you treat an eye Injury?", "Eye injury"],
156
+ "responses": ["1) DO NOT rub the eye. 2)Blink several times. 3) Use eyewash or saline solution to flush the eye out."]
157
+ },
158
+ {
159
+ "tag": "Chemical Burn",
160
+ "patterns": ["How do you treat a chemical burn?", "Chemical burn"],
161
+ "responses": ["1) Flush the area with water for at least 20 minutes. 2) Remove contaminated clothing. 3) Don't put antibiotic ointment on the burn."]
162
+ },
163
+ {
164
+ "tag": "Poison",
165
+ "patterns": ["How do you treat a Poison?", "Poison"],
166
+ "responses": ["1) Get to fresh air right away. 2) Rinse skin with running water for 15 to 20 minutes. 3) Call the Poison Help line immediately."]
167
+ },
168
+ {
169
+ "tag": "Teeth",
170
+ "patterns": ["How do you treat broken Teeth ?", "Broken teeth"],
171
+ "responses": ["1) Rinse the tooth in water. Put it back into position if possible (adult teeth only). Go to see a dentist as an emergency."]
172
+ },
173
+ {
174
+ "tag": "seizure",
175
+ "patterns": ["How do you treat a seizure?", "Seizure"],
176
+ "responses": ["1) Stay calm and remain with the person. 2) If they have food in their mouth, roll them onto their side. 3) Place something soft under their head."]
177
+ },
178
+ {
179
+ "tag": "Head Injury",
180
+ "patterns": ["How do you treat a head Injury?", "Head injury"],
181
+ "responses": ["1) Apply firm pressure to the wound with sterile gauze. But don't apply direct pressure if you suspect a skull fracture."]
182
+ },
183
+ {
184
+ "tag": "Fainting",
185
+ "patterns": ["How do you treat Faint?", "Fainting"],
186
+ "responses": ["1) Lie down or sit down. 2) Place your head between your knees if you sit down. 3) Raise the person's legs above heart level."]
187
+ },
188
+ {
189
+ "tag": "Headache",
190
+ "patterns": ["How do you treat a Headache?", "Headache"],
191
+ "responses": ["Give ibuprofen (Advil) or acetaminophen (Tylenol) for pain. Drink water and rest in a dark room."]
192
+ },
193
+ {
194
+ "tag": "Cold",
195
+ "patterns": ["How do you treat a Cold?", "Cold"],
196
+ "responses": ["1)Keeping hydrated is vital. 2)Vitamin C is extremely helpful. 3)Rest well."]
197
+ },
198
+ {
199
+ "tag": "Rash",
200
+ "patterns": ["How do you treat Rashes?", "Rash"],
201
+ "responses": ["1)Olive oil soothes the skin. 2)Baking soda is useful in drying skin rashes. 3)Aloe Vera is excellent for soothing."]
202
+ },
203
+ {
204
+ "tag": "snake bite",
205
+ "patterns": ["How do you treat a snake bite?", "Snake bite"],
206
+ "responses": ["1)Move the person beyond striking distance. 2)Keep the person calm and still. 3)Remove jewelry. 4)Seek medical help immediately."]
207
+ },
208
+ {
209
+ "tag": "animal bite",
210
+ "patterns": ["How do you treat a animal bite?", "Dog bite"],
211
+ "responses": ["1)Wash the wound with soap and warm water. 2)Apply an antibacterial ointment. 3)Cover with a sterile bandage. 4)Seek medical help."]
212
+ },
213
+ {
214
+ "tag": "Drowning",
215
+ "patterns": ["What to do if someone is Drowning?", "Drowning"],
216
+ "responses": ["1)Check if the person is breathing. 2)Check the person's pulse for 10 seconds. 3)If There is No Pulse, Start CPR."]
217
+ },
218
+ {
219
+ "tag": "CPR",
220
+ "patterns": ["How to give CPR??", "CPR"],
221
+ "responses": ["1)Push hard and fast on the center of the chest. 2)Do chest compressions at the rate of 100-120 per minute."]
222
+ },
223
+ {
224
+ "tag": "Fracture",
225
+ "patterns": ["How do you treat a Fracture?", "Fracture"],
226
+ "responses": ["1)Stop any bleeding. 2)Immobilize the injured area using a splint. 3)Apply ice packs to limit swelling."]
227
+ }
228
+ ]
229
+ }
requirements.txt ADDED
File without changes
training_data.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b26fefcf0028af4ca94fe2b7cf96cfbe244b6b9ed632c744a239c4eda4b5a1b6
3
+ size 12064