william4416 commited on
Commit
fa62174
·
verified ·
1 Parent(s): 8ffb2d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -67
app.py CHANGED
@@ -1,70 +1,73 @@
1
- # Import required libraries
2
- from fastapi import FastAPI, HTTPException
3
- from pydantic import BaseModel
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
5
  import json
6
 
7
- # Create FastAPI app instance
8
- app = FastAPI()
9
-
10
- class ChatBot:
11
- def __init__(self):
12
- # Load DialoGPT model and tokenizer
13
- try:
14
- self.tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
15
- self.model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
16
- except Exception as e:
17
- raise HTTPException(status_code=500, detail=f"Model loading failed: {e}")
18
-
19
- # Load courses data
20
- try:
21
- with open("uts_courses.json", "r") as file:
22
- self.courses_data = json.load(file)
23
- except Exception as e:
24
- raise HTTPException(status_code=500, detail=f"Courses data loading failed: {e}")
25
-
26
- def generate_response(self, user_input: str):
27
- """
28
- Generate response based on user input
29
- Args:
30
- user_input: User input text
31
- Returns:
32
- Generated response text
33
- """
34
- if user_input.lower() == "help":
35
- return "I can help you with UTS courses information, feel free to ask!"
36
- elif user_input.lower() == "exit":
37
- return "Goodbye!"
38
- elif user_input.lower() == "list courses":
39
- # Generate course list
40
- course_list = "\n".join([f"{category}: {', '.join(courses)}" for category, courses in self.courses_data["courses"].items()])
41
- return f"Here are the available courses:\n{course_list}"
42
- elif user_input.lower() in self.courses_data["courses"]:
43
- # List courses under the specified course category
44
- return f"The courses in {user_input} category are: {', '.join(self.courses_data['courses'][user_input])}"
45
- else:
46
- # Use DialoGPT model to generate response
47
- input_ids = self.tokenizer.encode(user_input + self.tokenizer.eos_token, return_tensors="pt")
48
- response_ids = self.model.generate(input_ids, max_length=100, pad_token_id=self.tokenizer.eos_token_id)
49
- response = self.tokenizer.decode(response_ids[0], skip_special_tokens=True)
50
- return response
51
-
52
- # Define user input model
53
- class UserInput(BaseModel):
54
- user_input: str
55
-
56
- # Create chatbot instance
57
- chatbot = ChatBot()
58
-
59
- # Define API route
60
- @app.post("/")
61
- async def chat(user_input: UserInput):
62
- """
63
- Process user input and return response
64
- Args:
65
- user_input: User input JSON data
66
- Returns:
67
- JSON data containing the response text
68
- """
69
- response = chatbot.generate_response(user_input.user_input)
70
- return {"response": response}
 
 
 
 
 
 
 
 
1
  from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ 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 = [["What courses are available in Engineering?"]]
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 input.lower():
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 input.lower():
29
+ # Get the list of courses for the specified field
30
+ courses_list = courses_data["courses"][field]
31
+ # Format the response
32
+ response = f"The available courses in {field} are: {', '.join(courses_list)}."
33
+ return response, history
34
+
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
+ input + tokenizer.eos_token, return_tensors="pt"
39
+ ).to(device)
40
+
41
+ # append the new user input tokens to the chat history
42
+ bot_input_ids = torch.cat([torch.tensor(history).to(device), new_user_input_ids], dim=-1)
43
+
44
+ # generate a response
45
+ history = model.generate(
46
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
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
+ pass
59
+
60
+
61
+ if __name__ == "__main__":
62
+ main()
63
+
64
+
65
+ gr.Interface(
66
+ fn=predict,
67
+ title=title,
68
+ description=description,
69
+ examples=examples,
70
+ inputs=["text", "state"],
71
+ outputs=["chatbot", "state"],
72
+ theme="finlaymacklon/boxy_violet",
73
+ ).launch()