-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv2.py
More file actions
143 lines (117 loc) · 4.98 KB
/
v2.py
File metadata and controls
143 lines (117 loc) · 4.98 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
import tkinter as tk
from tkinter import scrolledtext
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from textblob import TextBlob
import spacy
from sentence_transformers import SentenceTransformer, util
import torch
import threading
import time
# --- NLP Setup ---
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
nlp = spacy.load("en_core_web_sm")
model = SentenceTransformer('all-MiniLM-L6-v2')
# --- Knowledge Base ---
faq_responses = {
"admission": "Admissions open in July every year. You can apply online through our official website.",
"courses": "We offer B.Tech, BBA, MBA, and MCA programs. Visit our website for more details.",
"schedule": "Class schedules are shared on the student portal every semester.",
"fees": "Fee details vary by course. You can find them in the Admissions > Fee Structure section online.",
"contact": "You can reach us at contact@college.edu or call 1800-123-456.",
"hostel": "Yes, we provide separate hostel facilities for boys and girls.",
"placement": "Our top recruiters include Infosys, TCS, and Wipro with an average package of 6 LPA."
}
faq_keys = list(faq_responses.keys())
faq_embeddings = model.encode(faq_keys, convert_to_tensor=True)
# --- NLP + Spell Check ---
def correct_spelling(text):
try:
return str(TextBlob(text).correct())
except Exception:
return text
def preprocess(text):
text = text.lower()
words = word_tokenize(text)
return [w for w in words if w.isalnum() and w not in stopwords.words('english')]
def get_response(user_input):
user_input = correct_spelling(user_input)
doc = nlp(user_input.lower())
words = preprocess(user_input)
# Quick match
for keyword in faq_responses:
if keyword in words or keyword in [token.lemma_ for token in doc]:
return faq_responses[keyword]
# Semantic match
query_emb = model.encode(user_input, convert_to_tensor=True)
cos_scores = util.cos_sim(query_emb, faq_embeddings)[0]
best_idx = int(torch.argmax(cos_scores))
confidence = float(cos_scores[best_idx])
if confidence > 0.55:
return faq_responses[faq_keys[best_idx]]
else:
return "I'm not entirely sure, please check with the admin office for accurate information."
# --- Chat Display Logic ---
def insert_message(sender, message):
chat_box.config(state=tk.NORMAL)
chat_box.insert(tk.END, f"{sender}: {message}\n\n")
chat_box.config(state=tk.DISABLED)
chat_box.see(tk.END)
def show_typing():
typing_label.config(text="EduBot is typing...")
root.update_idletasks()
def hide_typing():
typing_label.config(text="")
root.update_idletasks()
def send_message(event=None):
user_input = user_entry.get().strip()
if not user_input:
return
insert_message("You", user_input)
user_entry.delete(0, tk.END)
threading.Thread(target=bot_reply, args=(user_input,)).start()
def bot_reply(user_input):
show_typing()
time.sleep(1.2)
response = get_response(user_input)
hide_typing()
insert_message("EduBot", response)
# --- GUI Setup ---
root = tk.Tk()
root.title("🎓 EduBot - AI-Powered College Assistant")
root.geometry("520x580") # slightly larger height for comfort
root.config(bg="#F7F9FC")
root.resizable(False, False)
# Header
header = tk.Label(root, text="🎓 EduBot - AI-Powered College Query Assistant",
bg="#1A73E8", fg="white", font=("Segoe UI Semibold", 14), pady=12)
header.pack(fill=tk.X)
# Chat Display
chat_box = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=65, height=20,
font=("Segoe UI", 11), relief="flat",
bg="#FFFFFF", fg="#333333", padx=10, pady=10)
chat_box.pack(padx=10, pady=(10, 5))
chat_box.config(state=tk.DISABLED)
insert_message("EduBot", "Hello! 👋 I’m EduBot, your college assistant. How can I help you today?")
# Typing Indicator
typing_label = tk.Label(root, text="", bg="#F7F9FC", fg="#777777", font=("Segoe UI Italic", 10))
typing_label.pack(pady=(2, 0))
# Input Frame
input_frame = tk.Frame(root, bg="#F7F9FC")
input_frame.pack(fill=tk.X, padx=10, pady=10)
user_entry = tk.Entry(input_frame, width=44, font=("Segoe UI", 12),
relief="flat", bg="#FFFFFF", fg="#333333",
highlightthickness=1, highlightbackground="#CCCCCC", highlightcolor="#1A73E8")
user_entry.grid(row=0, column=0, padx=(5, 5), ipady=6)
send_button = tk.Button(input_frame, text="Send ➤", command=send_message,
font=("Segoe UI Semibold", 11), bg="#1A73E8", fg="white",
activebackground="#1558B0", relief="flat", padx=15, pady=6, cursor="hand2")
send_button.grid(row=0, column=1, padx=(5, 5))
root.bind("<Return>", send_message)
# Footer
footer = tk.Label(root, text="© 2025 Galgotias University | Developed by Droun Trehan",
bg="#F7F9FC", fg="#888888", font=("Segoe UI", 9))
footer.pack(side=tk.BOTTOM, pady=5)
root.mainloop()