william4416 commited on
Commit
af8384f
1 Parent(s): 54a06b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -38
app.py CHANGED
@@ -1,49 +1,43 @@
 
 
 
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:
6
- data = json.load(f)
7
 
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
- # Main function to interact with the user
16
- def main():
17
- print("Welcome! I'm the UTS Course Chatbot. How can I assist you today?")
18
- print("You can ask questions about UTS courses or type 'exit' to end the conversation.")
19
-
20
- while True:
21
- user_input = input("You: ")
22
- if user_input.lower() == 'exit':
23
- print("Bot: Exiting the program.")
24
- break
 
 
 
 
 
 
25
 
26
- if "courses" in user_input.lower() and "available" in user_input.lower():
27
- field = user_input.split("in ")[-1]
28
- courses = data['courses'].get(field, [])
29
- if courses:
30
- response = f"Courses in {field}: {', '.join(courses)}"
31
- else:
32
- response = f"No courses found in {field}."
33
- else:
34
- response = generate_dialog_response(user_input)
35
- print("Bot:", response)
36
-
37
- # Function to generate response using dialog generation model
38
- def generate_dialog_response(user_input):
39
- input_text = user_input + tokenizer.eos_token
40
- input_ids = tokenizer.encode(input_text, return_tensors="pt")
41
-
42
- # Generate response
43
- response_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
44
- response_text = tokenizer.decode(response_ids[0], skip_special_tokens=True)
45
 
46
- return response_text
47
 
48
  if __name__ == "__main__":
49
  main()
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
  import json
 
5
 
6
+ title = "AI ChatBot"
7
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
8
+ examples = [["How are you?"]]
9
 
 
 
 
 
10
  tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
11
  model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
12
 
13
+ def predict(input, history=[], file_path=None):
14
+ if file_path:
15
+ json_data = read_json_file(file_path)
16
+ print(f"Contents of {file_path}:")
17
+ print(json_data)
18
+ print()
19
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
20
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
21
+ history = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id).tolist()
22
+ response = tokenizer.decode(history[0]).split("\n") # Splitting on new lines
23
+ return response[0], history
24
+
25
+ def read_json_file(file_path):
26
+ with open(file_path, 'r') as file:
27
+ data = json.load(file)
28
+ return data
29
 
30
+ def main():
31
+ gr.Interface(
32
+ fn=predict,
33
+ title=title,
34
+ description=description,
35
+ examples=examples,
36
+ inputs=[gr.inputs.Textbox(label="User Input"), gr.inputs.File(label="JSON File")],
37
+ outputs=["text", "text"],
38
+ theme="finlaymacklon/boxy_violet",
39
+ ).launch()
 
 
 
 
 
 
 
 
 
40
 
 
41
 
42
  if __name__ == "__main__":
43
  main()