File size: 1,841 Bytes
743067f
46bcc72
e6d1128
bf950aa
 
 
e6d1128
bf950aa
d29ff44
cc37a15
46bcc72
 
 
e6d1128
54a06b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46bcc72
 
 
 
e6d1128
46bcc72
 
 
e6d1128
46bcc72
e6d1128
bf950aa
07afb57
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
import json
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM

# 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", model="distilbert-base-cased-distilled-squad")

# Load tokenizer and model for dialog generation
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")

# 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: ")
        if user_input.lower() == 'exit':
            print("Bot: Exiting the program.")
            break

        if "courses" in user_input.lower() and "available" in user_input.lower():
            field = user_input.split("in ")[-1]
            courses = data['courses'].get(field, [])
            if courses:
                response = f"Courses in {field}: {', '.join(courses)}"
            else:
                response = f"No courses found in {field}."
        else:
            response = generate_dialog_response(user_input)
        print("Bot:", response)

# Function to generate response using dialog generation model
def generate_dialog_response(user_input):
    input_text = user_input + tokenizer.eos_token
    input_ids = tokenizer.encode(input_text, return_tensors="pt")

    # Generate response
    response_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
    response_text = tokenizer.decode(response_ids[0], skip_special_tokens=True)

    return response_text

if __name__ == "__main__":
    main()