william4416 commited on
Commit
2e7fa77
1 Parent(s): 23c5a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -47
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
- # Known question-answer pairs, you can add more as per your requirement
15
- known_questions_answers = {
16
- "How are you?": "I'm fine, thank you for asking.",
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 predict(input_text, chatbot_state):
23
- response = None
24
- history = chatbot_state
25
-
26
- # Check if the input question is in the known question-answer pairs
27
- if input_text in known_questions_answers:
28
- response = known_questions_answers[input_text]
 
 
 
29
  else:
30
- # Tokenize the new user input sentence
31
- new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
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
- history = model.generate(
36
- bot_input_ids, max_length=400, pad_token_id=tokenizer.eos_token_id
37
- ).tolist()
38
- # Convert tokens to text, and split the response into lines
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
- gr.Interface(
52
- fn=predict,
53
- title=title,
54
- description=description,
55
- examples=examples,
56
- inputs=["text", state_input],
57
- outputs=[textbox_output, state_output],
58
- theme="finlaymacklon/boxy_violet",
59
- ).launch()
 
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