Spaces:
Sleeping
Sleeping
File size: 2,125 Bytes
743067f bf950aa 743067f bf950aa e6d1128 bf950aa e6d1128 bf950aa cc37a15 bf950aa e6d1128 bf950aa e6d1128 bf950aa e6d1128 bf950aa e6d1128 bf950aa f6c0abe bf950aa f6c0abe bf950aa 9226908 |
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 |
import json
from transformers import pipeline
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
# Download NLTK resources
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')
# Load the JSON data from the file
with open('uts_courses.json') as f:
data = json.load(f)
# Load the question-answering pipeline
qa_pipeline = pipeline("question-answering")
# Define stop words and lemmatizer
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()
# Function to preprocess user input
def preprocess_input(user_input):
tokens = word_tokenize(user_input.lower())
filtered_tokens = [lemmatizer.lemmatize(word) for word in tokens if word.isalnum() and word not in stop_words]
return " ".join(filtered_tokens)
# Function to find courses by field of study
def find_courses_by_field(field):
if field in data['courses']:
return data['courses'][field]
else:
return []
# Function to handle user input and generate responses
def generate_response(user_input):
user_input = preprocess_input(user_input)
if user_input == 'exit':
return "Exiting the program."
elif "courses" in user_input and "available" in user_input:
field = user_input.split("in ")[1]
courses = find_courses_by_field(field)
if courses:
response = f"Courses in {field}: {', '.join(courses)}"
else:
response = f"No courses found in {field}."
else:
answer = qa_pipeline(question=user_input, context=data)
response = answer['answer']
return response
# Main function to interact with the user
def main():
print("Welcome! I'm the UTS Course Chatbot. How can I assist you today?")
print("You can ask questions about UTS courses or type 'exit' to end the conversation.")
while True:
user_input = input("You: ")
response = generate_response(user_input)
print("Bot:", response)
if response == "Exiting the program.":
break
if __name__ == "__main__":
main() |