-
Notifications
You must be signed in to change notification settings - Fork 1
/
db-setup.py
147 lines (117 loc) · 4.36 KB
/
db-setup.py
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
import sqlite3
from datetime import datetime
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
import nltk
from nltk.corpus import stopwords
# Ensure NLTK resources are downloaded
nltk.download('stopwords')
# Database Setup (SQLite)
def setup_database():
# Connect to SQLite database
conn = sqlite3.connect('education_counseling.db')
c = conn.cursor()
# Create table for courses
c.execute('''
CREATE TABLE IF NOT EXISTS courses (
id INTEGER PRIMARY KEY,
name TEXT,
course TEXT,
requirement TEXT,
price TEXT,
offering_university TEXT
)
''')
# Create table for user interactions
c.execute('''
CREATE TABLE IF NOT EXISTS user_interactions (
id INTEGER PRIMARY KEY,
timestamp TEXT,
user_input TEXT,
bot_response TEXT
)
''')
# Insert sample data (if needed)
c.execute("INSERT INTO courses (name, course, requirement, price, offering_university) VALUES ('Computer Science', 'B.Sc. in Computer Science', 'High School Diploma', '10000', 'University A')")
c.execute("INSERT INTO courses (name, course, requirement, price, offering_university) VALUES ('Business Administration', 'BBA in Business Administration', 'High School Diploma', '8000', 'University B')")
# Commit the changes
conn.commit()
# Close the connection
conn.close()
# Function to fetch course information
def fetch_courses():
# Connect to SQLite database
conn = sqlite3.connect('education_counseling.db')
c = conn.cursor()
# Fetch all courses
c.execute("SELECT name, course, requirement, price, offering_university FROM courses")
courses = c.fetchall()
# Close the connection
conn.close()
# Format the result for display
if courses:
response = "Here are the courses we offer:\n"
for course in courses:
response += f"\nName: {course[0]}\nDetails: {course[1]}\nRequirement: {course[2]}\nPrice: {course[3]}\nOffering University: {course[4]}\n"
return response
else:
return "No courses available."
# Function to log user interactions
def log_interaction(user_input, bot_response):
# Connect to SQLite database
conn = sqlite3.connect('education_counseling.db')
c = conn.cursor()
# Insert the interaction log
c.execute('''
INSERT INTO user_interactions (timestamp, user_input, bot_response)
VALUES (?, ?, ?)
''', (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), user_input, bot_response))
# Commit the changes
conn.commit()
# Close the connection
conn.close()
# Function to fetch logged interactions for ML training
def fetch_logged_interactions():
# Connect to SQLite database
conn = sqlite3.connect('education_counseling.db')
# Read logged interactions into a DataFrame
df = pd.read_sql_query("SELECT user_input, bot_response FROM user_interactions", conn)
# Close the connection
conn.close()
return df
# ML Model class
class MLModel:
def __init__(self):
self.vectorizer = TfidfVectorizer(stop_words=stopwords.words('english'))
self.model = SGDClassifier()
def train(self, user_inputs, bot_responses):
if user_inputs.empty:
raise ValueError("Training data is empty. Ensure there are logged interactions for training.")
X = self.vectorizer.fit_transform(user_inputs)
self.model.fit(X, bot_responses)
def predict(self, user_input):
X = self.vectorizer.transform([user_input])
return self.model.predict(X)[0]
# Function to train the ML model
def train_ml_model():
df = fetch_logged_interactions()
ml_model = MLModel()
ml_model.train(df['user_input'], df['bot_response'])
return ml_model
# Example usage
if __name__ == "__main__":
# Setup database (if not already setup)
setup_database()
# Example user input and fetching courses
user_input = "What courses do you offer?"
bot_response = fetch_courses()
print(bot_response)
# Log the interaction
log_interaction(user_input, bot_response)
# Train the ML model from logged interactions
ml_model = train_ml_model()
# Example predictions
test_input = "How much does the Computer Science course cost?"
prediction = ml_model.predict(test_input)
print(f"Prediction: {prediction}")