Spaces:
Running
Running
william4416
commited on
Commit
•
2e7fa77
1
Parent(s):
23c5a04
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,40 @@
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
-
import gradio as gr
|
3 |
import torch
|
4 |
import json
|
5 |
|
6 |
-
title = "Smart AI ChatBot"
|
7 |
-
description = "A conversational model capable of intelligently answering questions (DialoGPT)"
|
8 |
-
examples = [["How are you?"], ["What's the weather like?"]]
|
9 |
-
|
10 |
# Load DialoGPT model and tokenizer
|
11 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
12 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
"What's the weather like?": "The weather is nice today, sunny and warm.",
|
18 |
-
"What's your name?": "I am Smart AI ChatBot.",
|
19 |
-
"Do you speak English?": "I can understand and respond to English questions.",
|
20 |
-
}
|
21 |
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
else:
|
30 |
-
# Tokenize the
|
31 |
-
|
32 |
-
# Append the new user input tokens to the chat history
|
33 |
-
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
34 |
# Generate a response
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
response = tokenizer.decode(history[0], skip_special_tokens=True)
|
40 |
-
|
41 |
-
return response, history
|
42 |
-
|
43 |
-
def main():
|
44 |
-
# You can add logic here to read known question-answer pairs, for example, from a JSON file
|
45 |
-
pass
|
46 |
-
|
47 |
-
textbox_output = gr.outputs.Textbox(label="Chatbot Response")
|
48 |
-
state_input = "text"
|
49 |
-
state_output = "state"
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
2 |
import torch
|
3 |
import json
|
4 |
|
|
|
|
|
|
|
|
|
5 |
# Load DialoGPT model and tokenizer
|
6 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
7 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
8 |
|
9 |
+
# Load courses data from JSON file
|
10 |
+
with open("uts_courses.json", "r") as file:
|
11 |
+
courses_data = json.load(file)
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
def generate_response(user_input):
|
14 |
+
if user_input.lower() == "help":
|
15 |
+
return "I can help you with information about UTS courses. Feel free to ask!"
|
16 |
+
elif user_input.lower() == "exit":
|
17 |
+
return "Goodbye!"
|
18 |
+
elif user_input.lower() == "list courses":
|
19 |
+
course_list = "\n".join([f"{category}: {', '.join(courses)}" for category, courses in courses_data["courses"].items()])
|
20 |
+
return f"Here are the available courses:\n{course_list}"
|
21 |
+
elif user_input.lower() in courses_data["courses"]:
|
22 |
+
return f"The courses in {user_input} are: {', '.join(courses_data['courses'][user_input])}"
|
23 |
else:
|
24 |
+
# Tokenize the user input
|
25 |
+
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
|
|
|
|
26 |
# Generate a response
|
27 |
+
response_ids = model.generate(input_ids, max_length=100, pad_token_id=tokenizer.eos_token_id)
|
28 |
+
# Decode the response
|
29 |
+
response = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
30 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
if __name__ == "__main__":
|
33 |
+
print("Welcome to the UTS Course Chatbot!")
|
34 |
+
print("Type 'help' to see available commands and 'exit' to quit.")
|
35 |
+
while True:
|
36 |
+
user_input = input("You: ")
|
37 |
+
response = generate_response(user_input)
|
38 |
+
print("Chatbot:", response)
|
39 |
+
if user_input.lower() == "exit":
|
40 |
+
break
|