bridget-20 commited on
Commit
4b178aa
·
verified ·
1 Parent(s): 987e906

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -322
app.py DELETED
@@ -1,322 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 1,
6
- "id": "eda1960f-1ff3-460c-8632-0b595a1406c3",
7
- "metadata": {},
8
- "outputs": [
9
- {
10
- "name": "stderr",
11
- "output_type": "stream",
12
- "text": [
13
- "C:\\Users\\BT\\anaconda3\\Lib\\site-packages\\gradio\\components\\chatbot.py:223: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style 'role' and 'content' keys.\n",
14
- " warnings.warn(\n"
15
- ]
16
- },
17
- {
18
- "name": "stdout",
19
- "output_type": "stream",
20
- "text": [
21
- "* Running on local URL: http://127.0.0.1:7860\n",
22
- "* Running on public URL: https://82cc2e5002966d288b.gradio.live\n",
23
- "\n",
24
- "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
25
- ]
26
- },
27
- {
28
- "data": {
29
- "text/html": [
30
- "<div><iframe src=\"https://82cc2e5002966d288b.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
31
- ],
32
- "text/plain": [
33
- "<IPython.core.display.HTML object>"
34
- ]
35
- },
36
- "metadata": {},
37
- "output_type": "display_data"
38
- }
39
- ],
40
- "source": [
41
- "from datetime import datetime, timedelta\n",
42
- "import random\n",
43
- "import gradio as gr\n",
44
- "\n",
45
- "class EnhancedStudentPlatformBot:\n",
46
- " def __init__(self):\n",
47
- " self.student_data = {}\n",
48
- " self.conversations = {}\n",
49
- " self.custom_prompts = {}\n",
50
- " self.conversation_history = {}\n",
51
- " \n",
52
- " # Default FAQ responses\n",
53
- " self.faq_responses = {\n",
54
- " \"attachment\": \"You can find available attachments under the 'Opportunities' tab. Navigate to the tab and click 'Apply' for positions that interest you.\",\n",
55
- " \"submit_project\": \"To submit a project: 1. Go to 'My Projects' 2. Click 'New Submission' 3. Fill the form and upload your files 4. Click 'Submit'\",\n",
56
- " \"portfolio\": \"To update your portfolio: 1. Go to 'My Profile' 2. Click 'Edit Portfolio' 3. Add your projects and skills 4. Save changes\",\n",
57
- " \"register\": \"To register, go to the homepage, click on 'Sign Up', and fill in your details. Make sure to verify your email to complete the registration process.\"\n",
58
- " }\n",
59
- " \n",
60
- " # Conversation starters and follow-ups\n",
61
- " self.conversation_starters = [\n",
62
- " \"How's your learning journey going today?\",\n",
63
- " \"What's on your mind regarding your studies?\",\n",
64
- " \"Need any help with your current projects?\",\n",
65
- " \"How can I assist you with your learning goals today?\"\n",
66
- " ]\n",
67
- " \n",
68
- " self.follow_up_questions = {\n",
69
- " \"project_recommendation\": [\n",
70
- " \"Would you like more details about any of these projects?\",\n",
71
- " \"Shall I help you set up a project timeline?\",\n",
72
- " \"Would you like to see learning resources for these projects?\"\n",
73
- " ],\n",
74
- " \"progress_check\": [\n",
75
- " \"Would you like specific tips to improve in any area?\",\n",
76
- " \"Should we set some learning goals for the next week?\",\n",
77
- " \"Would you like to see related project opportunities?\"\n",
78
- " ]\n",
79
- " }\n",
80
- "\n",
81
- " def initialize_student(self, student_id, name, skills=None):\n",
82
- " \"\"\"Initialize a new student profile with conversation tracking\"\"\"\n",
83
- " if student_id not in self.student_data:\n",
84
- " self.student_data[student_id] = {\n",
85
- " \"name\": name,\n",
86
- " \"skills\": skills or [],\n",
87
- " \"projects\": [],\n",
88
- " \"deadlines\": [],\n",
89
- " \"progress\": {\n",
90
- " \"web_development\": 0,\n",
91
- " \"python\": 0,\n",
92
- " \"data_science\": 0\n",
93
- " },\n",
94
- " \"last_interaction\": datetime.now(),\n",
95
- " \"interaction_count\": 0\n",
96
- " }\n",
97
- " self.conversation_history[student_id] = []\n",
98
- " return self.generate_welcome_message(student_id)\n",
99
- "\n",
100
- " def add_custom_prompt(self, keyword, response, follow_up=None):\n",
101
- " \"\"\"Allow adding custom prompts and responses\"\"\"\n",
102
- " self.custom_prompts[keyword.lower()] = {\n",
103
- " \"response\": response,\n",
104
- " \"follow_up\": follow_up\n",
105
- " }\n",
106
- " return f\"Custom prompt added for keyword: {keyword}\"\n",
107
- "\n",
108
- " def generate_welcome_message(self, student_id):\n",
109
- " \"\"\"Generate personalized welcome message based on interaction history\"\"\"\n",
110
- " student = self.student_data[student_id]\n",
111
- " current_time = datetime.now()\n",
112
- " time_diff = current_time - student[\"last_interaction\"]\n",
113
- " \n",
114
- " if time_diff.days == 0 and student[\"interaction_count\"] > 0:\n",
115
- " return f\"Welcome back {student['name']}! Continuing from our previous conversation. How can I help you now?\"\n",
116
- " \n",
117
- " # First interaction of the day\n",
118
- " greeting = self._get_time_based_greeting()\n",
119
- " return f\"{greeting} {student['name']}! 👋\\n\\n\" \\\n",
120
- " f\"I'm your learning assistant and I notice you're skilled in {', '.join(student['skills'])}. \" \\\n",
121
- " f\"I can help you with:\\n\\n\" \\\n",
122
- " \"🎯 Platform guidance and personalized recommendations\\n\" \\\n",
123
- " \"📚 Project suggestions and resources\\n\" \\\n",
124
- " \"❓ Common questions and support\\n\" \\\n",
125
- " \"⏰ Deadline management\\n\" \\\n",
126
- " \"📊 Progress tracking\\n\\n\" \\\n",
127
- " f\"{random.choice(self.conversation_starters)}\"\n",
128
- "\n",
129
- " def _get_time_based_greeting(self):\n",
130
- " \"\"\"Return appropriate greeting based on time of day\"\"\"\n",
131
- " hour = datetime.now().hour\n",
132
- " if 5 <= hour < 12:\n",
133
- " return \"Good morning\"\n",
134
- " elif 12 <= hour < 17:\n",
135
- " return \"Good afternoon\"\n",
136
- " else:\n",
137
- " return \"Good evening\"\n",
138
- "\n",
139
- " def process_message(self, student_id, message):\n",
140
- " \"\"\"Enhanced message processing with context awareness\"\"\"\n",
141
- " # Initialize the student if not already initialized\n",
142
- " if student_id not in self.student_data:\n",
143
- " return \"Please register first to use the chatbot.\"\n",
144
- " \n",
145
- " # Update interaction metrics\n",
146
- " self.student_data[student_id][\"last_interaction\"] = datetime.now()\n",
147
- " self.student_data[student_id][\"interaction_count\"] += 1\n",
148
- " \n",
149
- " # Store message in conversation history\n",
150
- " self.conversation_history[student_id].append({\n",
151
- " \"timestamp\": datetime.now(),\n",
152
- " \"message\": message,\n",
153
- " \"type\": \"user\"\n",
154
- " })\n",
155
- " \n",
156
- " # Process message\n",
157
- " response = self._generate_response(student_id, message)\n",
158
- " \n",
159
- " # Store response in conversation history\n",
160
- " self.conversation_history[student_id].append({\n",
161
- " \"timestamp\": datetime.now(),\n",
162
- " \"message\": response,\n",
163
- " \"type\": \"bot\"\n",
164
- " })\n",
165
- " \n",
166
- " return response\n",
167
- "\n",
168
- " def _generate_response(self, student_id, message):\n",
169
- " \"\"\"Generate contextual response based on message and conversation history\"\"\"\n",
170
- " message = message.lower()\n",
171
- " \n",
172
- " # Check custom prompts first\n",
173
- " for keyword, prompt_data in self.custom_prompts.items():\n",
174
- " if keyword in message:\n",
175
- " response = prompt_data[\"response\"]\n",
176
- " if prompt_data[\"follow_up\"]:\n",
177
- " response += f\"\\n\\n{prompt_data['follow_up']}\"\n",
178
- " return response\n",
179
- " \n",
180
- " # Handle different types of queries with context\n",
181
- " if any(word in message for word in [\"help\", \"guide\", \"how\"]):\n",
182
- " if \"register\" in message:\n",
183
- " return self.faq_responses.get(\"register\", \"You can register by visiting the registration page.\")\n",
184
- " elif \"project\" in message and \"submit\" in message:\n",
185
- " return self.faq_responses.get(\"submit_project\", \"\")\n",
186
- " \n",
187
- " elif any(word in message for word in [\"recommend\", \"project\", \"idea\"]):\n",
188
- " response = self.recommend_projects(student_id)\n",
189
- " return f\"{response}\\n\\n{random.choice(self.follow_up_questions['project_recommendation'])}\"\n",
190
- " \n",
191
- " elif \"deadline\" in message:\n",
192
- " return self.check_deadlines(student_id)\n",
193
- " \n",
194
- " elif \"progress\" in message:\n",
195
- " response = self.track_progress(student_id)\n",
196
- " return f\"{response}\\n\\n{random.choice(self.follow_up_questions['progress_check'])}\"\n",
197
- " \n",
198
- " elif any(keyword in message for keyword in self.faq_responses.keys()):\n",
199
- " return self.handle_faq(message)\n",
200
- " \n",
201
- " # If no specific intent is matched, provide a conversational response\n",
202
- " return self._generate_fallback_response(student_id)\n",
203
- "\n",
204
- " def _generate_fallback_response(self, student_id):\n",
205
- " \"\"\"Generate a contextual fallback response\"\"\"\n",
206
- " student = self.student_data[student_id]\n",
207
- " \n",
208
- " # Check if student has any upcoming deadlines\n",
209
- " has_deadlines = len(student[\"deadlines\"]) > 0\n",
210
- " \n",
211
- " responses = [\n",
212
- " f\"I notice you're working on improving your {', '.join(student['skills'])}. Would you like some project recommendations in these areas?\",\n",
213
- " \"I can help you track your progress or manage deadlines. What would you like to focus on?\",\n",
214
- " \"Would you like to explore some learning resources or get project recommendations?\"\n",
215
- " ]\n",
216
- " \n",
217
- " if has_deadlines:\n",
218
- " responses.append(\"I see you have some upcoming deadlines. Would you like to review them?\")\n",
219
- " \n",
220
- " return random.choice(responses)\n",
221
- "\n",
222
- " def recommend_projects(self, student_id):\n",
223
- " \"\"\"Dummy method to recommend projects based on student skills\"\"\"\n",
224
- " student = self.student_data[student_id]\n",
225
- " skills = student['skills']\n",
226
- " projects = [f\"Build a {skill}-based project\" for skill in skills]\n",
227
- " return f\"Here are some project ideas based on your skills: {', '.join(projects)}\"\n",
228
- "\n",
229
- " def check_deadlines(self, student_id):\n",
230
- " \"\"\"Check upcoming deadlines for the student\"\"\"\n",
231
- " student = self.student_data[student_id]\n",
232
- " deadlines = student.get(\"deadlines\", [])\n",
233
- " \n",
234
- " if not deadlines:\n",
235
- " return \"You don't have any upcoming deadlines.\"\n",
236
- " \n",
237
- " deadline_messages = [f\"{d['task']} - Due by {d['date'].strftime('%Y-%m-%d')}\" for d in deadlines]\n",
238
- " return \"Here are your upcoming deadlines:\\n\" + \"\\n\".join(deadline_messages)\n",
239
- "\n",
240
- " def track_progress(self, student_id):\n",
241
- " \"\"\"Track the student's progress in various skills\"\"\"\n",
242
- " student = self.student_data[student_id]\n",
243
- " progress = student[\"progress\"]\n",
244
- " progress_report = [f\"{skill}: {percent}% complete\" for skill, percent in progress.items()]\n",
245
- " return \"Here's your current progress:\\n\" + \"\\n\".join(progress_report)\n",
246
- "\n",
247
- " def handle_faq(self, message):\n",
248
- " \"\"\"Handle FAQ-related queries\"\"\"\n",
249
- " for keyword, response in self.faq_responses.items():\n",
250
- " if keyword in message:\n",
251
- " return response\n",
252
- " return \"I couldn't find an answer to your query.\"\n",
253
- "\n",
254
- "\n",
255
- "# Gradio Interface\n",
256
- "def chatbot_interface(user_input, history=[]):\n",
257
- " student_id = \"12345\"\n",
258
- " \n",
259
- " # Initialize the bot (ensures student is set up)\n",
260
- " bot = EnhancedStudentPlatformBot()\n",
261
- " if len(history) == 0:\n",
262
- " bot.initialize_student(student_id=student_id, name=\"John\", skills=[\"Python\", \"Web Development\"])\n",
263
- " \n",
264
- " # Process message\n",
265
- " bot_response = bot.process_message(student_id, user_input)\n",
266
- " \n",
267
- " # Append both user input and bot response to history\n",
268
- " history.append((user_input, bot_response))\n",
269
- " \n",
270
- " return history, history # Return updated history to display\n",
271
- "\n",
272
- "\n",
273
- "# Gradio Chatbot UI\n",
274
- "with gr.Blocks() as chatbot_app:\n",
275
- " gr.Markdown(\"<h1><center>CollabHub Student Platform Bot</center></h1>\")\n",
276
- " \n",
277
- " chatbot = gr.Chatbot(label=\"Chat with the bot\")\n",
278
- " message = gr.Textbox(placeholder=\"Type your message here...\", label=\"Your Message\")\n",
279
- " \n",
280
- " # Define the submit action\n",
281
- " def handle_input(user_input, history):\n",
282
- " return chatbot_interface(user_input, history)\n",
283
- " \n",
284
- " # Action: on submit (send message)\n",
285
- " message.submit(handle_input, [message, chatbot], [message, chatbot])\n",
286
- "\n",
287
- "# Launch the Gradio app\n",
288
- "if __name__ == \"__main__\":\n",
289
- " chatbot_app.launch(share=True)"
290
- ]
291
- },
292
- {
293
- "cell_type": "code",
294
- "execution_count": None,
295
- "id": "1d1832ad-c286-48a0-9d34-fb093f997a07",
296
- "metadata": {},
297
- "outputs": [],
298
- "source": []
299
- }
300
- ],
301
- "metadata": {
302
- "kernelspec": {
303
- "display_name": "Python 3 (ipykernel)",
304
- "language": "python",
305
- "name": "python3"
306
- },
307
- "language_info": {
308
- "codemirror_mode": {
309
- "name": "ipython",
310
- "version": 3
311
- },
312
- "file_extension": ".py",
313
- "mimetype": "text/x-python",
314
- "name": "python",
315
- "nbconvert_exporter": "python",
316
- "pygments_lexer": "ipython3",
317
- "version": "3.12.7"
318
- }
319
- },
320
- "nbformat": 4,
321
- "nbformat_minor": 5
322
- }