-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
170 lines (151 loc) · 7.22 KB
/
Copy pathapp.py
File metadata and controls
170 lines (151 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from flask import Flask, render_template, request, jsonify
import re
from nltk.chat.util import Chat, reflections
# Define reflections for handling common variations in user input
reflections = {
"i am": "you are",
"i'll": "you'll",
"you": "me",
"me": "you",
}
# Correctly define the list of pairs that map patterns to responses
patterns_and_responses = [
# Greetings and basic questions
(r'hello|hi|hey', [
"Hello! How can I assist you today?",
"Hi there! What brings you here today?",
"Hey! How's your day going so far?",
"Salam! Aap kaise hain?",
"Hi! Aap kaise ho? Kya madad chahiye?"
]),
(r'how are you|kese ho', [
"I'm doing great! How about you?",
"I'm fine, thank you! What's new with you?",
"I'm good! Anything exciting happening today?",
"Main theek hoon! Aap kaise hain?",
"Sab theek hai, shukriya! Aap kaise hain?"
]),
(r'what is your name|Apka name?', [
"I don't have a name, but you can call me your virtual assistant. What's your name?",
"I'm just a chatbot, but I'm here to help! What's your name?",
"Mera koi naam nahi, aap mujhe apna assistant keh sakte hain. Aap ka naam kya hai?",
"Mujhe naam nahi mila, lekin aap mujhe assistant keh sakte hain. Aapka naam kya hai?"
]),
# User introduction
(r'(.*) my name is (.*)', [
"Hello, %2! Nice to meet you! What can I do for you today?",
"Hi %2! How can I assist you today?",
"Aapka naam %2 hai, acha laga aapse milke! Aapko kis cheez mein madad chahiye?",
"Hello %2! Kaise madad kar sakta hoon aapki?"
]),
# Farewells
(r'bye|goodbye|see you', [
"Goodbye! Have a great day!",
"See you later! Take care!",
"Goodbye! Looking forward to our next chat!",
"Allah Hafiz! Aapka din accha guzre!",
"Khuda Hafiz! Agli mulaqat tak ke liye!"
]),
# Offers of help
(r'(.*) help (.*)', [
"Sure, I can assist you. What do you need help with?",
"I'm here to help! What can I do for you?",
"I'd be happy to help! Tell me more about what you need.",
"Bilkul, madad karne ke liye yahan hoon. Aapko kis cheez mein madad chahiye?",
"Main madad ke liye yahan hoon. Batayein, kis mein madad chahiye?"
]),
# Questions about chatbot capabilities
(r'what can you do', [
"I can chat with you, answer questions, and keep you company. What would you like to talk about?",
"I'm here to assist with any questions or just to have a friendly chat. What's on your mind?",
"Main aapke sawalon ka jawab de sakta hoon aur aap se baat kar sakta hoon. Aap kya baat karna chahenge?",
"Main aapki madad karne aur baat karne ke liye yahan hoon. Aapke dil mein kya hai?"
]),
# Casual conversations
(r'(.*) (weather|rain|sunny)', [
"The weather is always a topic of interest! Is it raining where you are?",
"Sunny days are the best, aren't they? How's the weather where you are?",
"Mausam kaafi interesting hota hai! Kya aapke yahan barish ho rahi hai?",
"Sunny din toh best hote hain, hai na? Aapke yahan mausam kaisa hai?"
]),
(r'what (.*) your favorite (.*)', [
"I don't have preferences like humans, but I do enjoy helping people! What's your favorite %2?",
"I don't have favorites, but I'd love to hear about yours! What's your favorite %2?",
"Mujhe pasand nahi hota lekin aapki pasand sunna achha lagega. Aapki favorite %2 kya hai?",
"Mujhe koi pasand nahi, lekin aapka favorite %2 kya hai? Bataiye!"
]),
# Roman Urdu integration
(r'kaisa hai|kesi ho|kya haal hai', [
"Main theek hoon, aap batao?",
"Main bilkul theek, aap sunao kesa chal raha hai?",
"Main theek hoon! Aap kaise hain? Kya chal raha hai?",
"Sab kuch theek hai, aap kaise hain? Kya new hai aapke zindagi mein?"
]),
(r'naam kya hai|tumhara naam', [
"Mera koi naam nahi, aap mujhe apna assistant keh sakte hain. Aap ka naam kya hai?",
"Mujhe naam nahi mila, lekin aap mujhe assistant keh sakte hain. Aapka naam kya hai?",
"Main naam se waqif nahi hoon, lekin aap mujhe apna assistant keh sakte hain. Aapka naam kya hai?",
"Mujhe koi specific naam nahi, aap mujhe assistant keh sakte hain. Aapka naam kya hai?"
]),
(r'shukriya|thanks|thank you', [
"Aapka shukriya! Aapko aur kisi cheez mein madad chahiye?",
"Koi baat nahi! Kuch aur madad karun?",
"Shukriya! Aapko kisi aur cheez ki zarurat hai?",
"Aapka shukriya! Kya kuch aur chahiye aapko?"
]),
# Daily conversations between friends
(r'(.*) what did you do today |kya kiya aj ', [
"I was here helping people like you! What did you do today?",
"I spent my day assisting users. How about you?",
"Main yahan tha logon ki madad karne ke liye. Aapne aaj kya kiya?",
"Maine apna waqt yahan guzara. Aapka din kaisa raha?"
]),
(r'(.*) how was your day', [
"My day has been good, thank you for asking! How was yours?",
"I'm just a program, but it's been a busy day helping people. How about you?",
"Mera din acha raha, aapka din kaisa tha?",
"Din to theek raha, aapka din kaisa guzra?"
]),
# Random person conversation
(r'(.*) where are you from', [
"I'm a virtual assistant, so I don't have a physical location. Where are you from?",
"I don't have a specific place, but I'm here to assist you! Where are you from?",
"Main ek virtual assistant hoon, mujhe koi physical jagah nahi. Aap kahan se hain?",
"Mujhe specific jagah nahi hai, lekin main yahan aapki madad ke liye hoon. Aap kahan se hain?"
]),
(r'(.*) do you have hobbies', [
"As a chatbot, I don't have hobbies, but I enjoy helping people! What about you?",
"I don't have hobbies, but I love interacting with people. Do you have any hobbies?",
"Mere paas hobbies nahi hain, lekin logon ki madad karna acha lagta hai. Aapki hobbies kya hain?",
"Mujhe hobbies nahi hain, lekin baat karna pasand hai. Aapki hobbies kya hain?"
]),
# Default response
(r'.*', [
"I'm sorry, I don't quite understand. Could you clarify what you mean?",
"That's interesting! Tell me more.",
"Hmm, I'm not sure I get that. Can you explain?",
"Mujhe samajh nahi aaya. Kya aap thoda aur wazeh kar sakte hain?",
"Ye thoda ajeeb lag raha hai. Kya aap isay thoda aur explain kar sakte hain?"
]),
]
# Create a chatbot instance with the specified patterns and reflections
chatbot = Chat(patterns_and_responses, reflections)
app = Flask(__name__)
@app.route("/")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get_response", methods=["POST"])
def get_response():
try:
data = request.get_json()
user_input = data.get("message")
response = chatbot.respond(user_input)
return jsonify({"response": response})
except Exception as e:
return jsonify({"response": "Sorry, there was an error. Please try again."})
@app.route("/test")
def test():
return "Test route working!"
if __name__ == "__main__":
app.run(debug=True)