william4416 commited on
Commit
46bcc72
1 Parent(s): cd3f4b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -51
app.py CHANGED
@@ -1,14 +1,5 @@
1
  import json
2
- from transformers import pipeline
3
- import nltk
4
- from nltk.corpus import stopwords
5
- from nltk.tokenize import word_tokenize
6
- from nltk.stem import WordNetLemmatizer
7
-
8
- # Download NLTK resources
9
- nltk.download('punkt')
10
- nltk.download('wordnet')
11
- nltk.download('stopwords')
12
 
13
  # Load the JSON data from the file
14
  with open('uts_courses.json') as f:
@@ -17,48 +8,26 @@ with open('uts_courses.json') as f:
17
  # Load the question-answering pipeline
18
  qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
19
 
20
- # Define stop words and lemmatizer
21
- stop_words = set(stopwords.words('english'))
22
- lemmatizer = WordNetLemmatizer()
23
 
24
- # Function to preprocess user input
25
- def preprocess_input(user_input):
26
- tokens = word_tokenize(user_input.lower())
27
- filtered_tokens = [lemmatizer.lemmatize(word) for word in tokens if word.isalnum() and word not in stop_words]
28
- return " ".join(filtered_tokens)
29
 
30
- # Function to find courses by field of study
31
- def find_courses_by_field(field):
32
- if field in data['courses']:
33
- return data['courses'][field]
34
- else:
35
- return []
36
 
37
- # Function to handle user input and generate responses
38
- def generate_response(user_input):
39
- user_input = preprocess_input(user_input)
40
- if user_input == 'exit':
41
- return "Exiting the program."
42
- elif "courses" in user_input and "available" in user_input:
43
- field = user_input.split("in ")[1]
44
- courses = find_courses_by_field(field)
45
- if courses:
46
- response = f"Courses in {field}: {', '.join(courses)}"
47
- else:
48
- response = f"No courses found in {field}."
49
- else:
50
- answer = qa_pipeline(question=user_input, context=data)
51
- response = answer['answer']
52
- return response
53
 
54
  # Function to interactively retrieve user input
55
  def get_user_input():
56
- try:
57
- # Print prompt and retrieve user input
58
- print("You: ", end="")
59
- return input()
60
- except EOFError:
61
- return "exit"
62
 
63
  # Main function to interact with the user
64
  def main():
@@ -68,12 +37,21 @@ def main():
68
  try:
69
  while True:
70
  user_input = get_user_input()
71
- response = generate_response(user_input)
72
- print("Bot:", response)
73
- if response == "Exiting the program.":
74
  break
75
- except EOFError:
76
- print("An error occurred while processing input. Exiting the program.")
 
 
 
 
 
 
 
 
 
 
77
 
78
  if __name__ == "__main__":
79
  main()
 
1
  import json
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
 
 
 
3
 
4
  # Load the JSON data from the file
5
  with open('uts_courses.json') as f:
 
8
  # Load the question-answering pipeline
9
  qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
10
 
11
+ # Load tokenizer and model for dialog generation
12
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
13
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
14
 
15
+ # Function to generate response using dialog generation model
16
+ def generate_dialog_response(user_input):
17
+ input_text = user_input + tokenizer.eos_token
18
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
 
19
 
20
+ # Generate response
21
+ response_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
22
+ response_text = tokenizer.decode(response_ids[0], skip_special_tokens=True)
 
 
 
23
 
24
+ return response_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Function to interactively retrieve user input
27
  def get_user_input():
28
+ # Print prompt and retrieve user input
29
+ print("You: ", end="")
30
+ return input()
 
 
 
31
 
32
  # Main function to interact with the user
33
  def main():
 
37
  try:
38
  while True:
39
  user_input = get_user_input()
40
+ if user_input.lower() == 'exit':
41
+ print("Bot: Exiting the program.")
 
42
  break
43
+ elif "courses" in user_input.lower() and "available" in user_input.lower():
44
+ field = user_input.split("in ")[1]
45
+ courses = data['courses'].get(field, [])
46
+ if courses:
47
+ response = f"Courses in {field}: {', '.join(courses)}"
48
+ else:
49
+ response = f"No courses found in {field}."
50
+ else:
51
+ response = generate_dialog_response(user_input)
52
+ print("Bot:", response)
53
+ except KeyboardInterrupt:
54
+ print("\nBot: Exiting the program.")
55
 
56
  if __name__ == "__main__":
57
  main()