Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,29 +3,25 @@ import gradio as gr
|
|
| 3 |
import torch
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
|
| 7 |
title = "AI ChatBot"
|
| 8 |
description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
|
| 9 |
examples = [["How are you?"]]
|
| 10 |
|
| 11 |
-
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
|
| 13 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
|
| 14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
-
|
| 16 |
model.to(device)
|
| 17 |
|
| 18 |
# Load courses data from JSON file
|
| 19 |
with open("uts_courses.json", "r") as f:
|
| 20 |
courses_data = json.load(f)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
def predict(input, history=[]):
|
| 24 |
# Check if the input question is about courses
|
| 25 |
-
if "courses" in
|
| 26 |
# Check if the input question contains a specific field (e.g., Engineering, Information Technology, etc.)
|
| 27 |
for field in courses_data["courses"]:
|
| 28 |
-
if field.lower() in
|
| 29 |
# Get the list of courses for the specified field
|
| 30 |
courses_list = courses_data["courses"][field]
|
| 31 |
# Format the response
|
|
@@ -35,7 +31,7 @@ def predict(input, history=[]):
|
|
| 35 |
# If the input question is not about courses, use the dialogue model to generate a response
|
| 36 |
# tokenize the new input sentence
|
| 37 |
new_user_input_ids = tokenizer.encode(
|
| 38 |
-
|
| 39 |
).to(device)
|
| 40 |
|
| 41 |
# append the new user input tokens to the chat history
|
|
@@ -43,37 +39,30 @@ def predict(input, history=[]):
|
|
| 43 |
|
| 44 |
# generate a response
|
| 45 |
history = model.generate(
|
| 46 |
-
bot_input_ids, max_length=
|
| 47 |
).tolist()
|
| 48 |
|
| 49 |
# convert the tokens to text, and then split the responses into lines
|
| 50 |
-
response = tokenizer.decode(history[0]).split(
|
| 51 |
-
response
|
| 52 |
-
(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
|
| 53 |
-
] # convert to tuples of list
|
| 54 |
-
return response, history
|
| 55 |
-
|
| 56 |
|
| 57 |
def main():
|
| 58 |
# Load courses data from JSON file
|
| 59 |
with open("uts_courses.json", "r") as f:
|
| 60 |
courses_data = json.load(f)
|
| 61 |
-
|
| 62 |
print("Contents of uts_courses.json:")
|
| 63 |
print(courses_data)
|
| 64 |
print()
|
| 65 |
|
| 66 |
-
|
| 67 |
if __name__ == "__main__":
|
| 68 |
main()
|
| 69 |
|
| 70 |
-
|
| 71 |
gr.Interface(
|
| 72 |
fn=predict,
|
| 73 |
title=title,
|
| 74 |
description=description,
|
| 75 |
examples=examples,
|
| 76 |
-
inputs=["text", "
|
| 77 |
-
outputs=["
|
| 78 |
-
theme="finlaymacklon/boxy_violet"
|
| 79 |
).launch()
|
|
|
|
| 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 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
# Load courses data from JSON file
|
| 16 |
with open("uts_courses.json", "r") as f:
|
| 17 |
courses_data = json.load(f)
|
| 18 |
|
| 19 |
+
def predict(input_text, history=[]):
|
|
|
|
| 20 |
# Check if the input question is about courses
|
| 21 |
+
if "courses" in input_text.lower():
|
| 22 |
# Check if the input question contains a specific field (e.g., Engineering, Information Technology, etc.)
|
| 23 |
for field in courses_data["courses"]:
|
| 24 |
+
if field.lower() in input_text.lower():
|
| 25 |
# Get the list of courses for the specified field
|
| 26 |
courses_list = courses_data["courses"][field]
|
| 27 |
# Format the response
|
|
|
|
| 31 |
# If the input question is not about courses, use the dialogue model to generate a response
|
| 32 |
# tokenize the new input sentence
|
| 33 |
new_user_input_ids = tokenizer.encode(
|
| 34 |
+
input_text + tokenizer.eos_token, return_tensors="pt"
|
| 35 |
).to(device)
|
| 36 |
|
| 37 |
# append the new user input tokens to the chat history
|
|
|
|
| 39 |
|
| 40 |
# generate a response
|
| 41 |
history = model.generate(
|
| 42 |
+
bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id
|
| 43 |
).tolist()
|
| 44 |
|
| 45 |
# convert the tokens to text, and then split the responses into lines
|
| 46 |
+
response = tokenizer.decode(history[0]).split()
|
| 47 |
+
return " ".join(response), history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def main():
|
| 50 |
# Load courses data from JSON file
|
| 51 |
with open("uts_courses.json", "r") as f:
|
| 52 |
courses_data = json.load(f)
|
|
|
|
| 53 |
print("Contents of uts_courses.json:")
|
| 54 |
print(courses_data)
|
| 55 |
print()
|
| 56 |
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
main()
|
| 59 |
|
|
|
|
| 60 |
gr.Interface(
|
| 61 |
fn=predict,
|
| 62 |
title=title,
|
| 63 |
description=description,
|
| 64 |
examples=examples,
|
| 65 |
+
inputs=["text", "text"], # Changed input from "state" to "text"
|
| 66 |
+
outputs=["text", "state"], # Changed output to match the tuple return type
|
| 67 |
+
theme="finlaymacklon/boxy_violet"
|
| 68 |
).launch()
|