william4416 commited on
Commit
73ba3e7
·
verified ·
1 Parent(s): de823fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -55
app.py CHANGED
@@ -1,61 +1,47 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
2
- import gradio as gr
3
- import torch
4
 
5
- title = "????AI ChatBot"
6
- description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
7
- examples = [["How are you?"]]
8
-
9
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
10
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
11
 
12
  # Course information
13
  course_info = {
14
- "courses": {
15
- "Engineering": ["Civil Engineering", "Mechanical Engineering", "Electrical Engineering", "Software Engineering", "etc."],
16
- "Information Technology": ["Computer Science", "Information Systems", "Cybersecurity", "Data Science", "etc."],
17
- "Business": ["Business Administration", "Accounting", "Finance", "Marketing", "Management", "etc."],
18
- "Health Sciences": ["Nursing", "Pharmacy", "Health Information Management", "Public Health", "etc."],
19
- "Design and Architecture": ["Architecture", "Industrial Design", "Visual Communication", "etc."],
20
- "Science": ["Environmental Science", "Biotechnology", "Chemistry", "Physics", "etc."],
21
- "Law": ["Law", "Legal Studies", "etc."],
22
- "Arts and Social Sciences": ["Communication", "Education", "International Studies", "Sociology", "etc."],
23
- "Built Environment": ["Urban Planning", "Construction Management", "Property Economics", "etc."],
24
- "Creative Industries": ["Animation", "Photography", "Creative Writing", "Film and Television", "etc."]
25
- }
26
  }
27
 
28
- def predict(input, history=[]):
29
- # Check if the user is asking about courses
30
- if "course" in input.lower():
31
- # Extract the subject from the user input
32
- for subject in course_info["courses"]:
33
- if subject.lower() in input.lower():
34
- return course_info["courses"][subject], history
35
-
36
- # tokenize the new input sentence
37
- new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
38
-
39
- # append the new user input tokens to the chat history
40
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
41
-
42
- # generate a response
43
- history = model.generate(
44
- bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
45
- ).tolist()
46
-
47
- # convert the tokens to text, and then split the responses into lines
48
- response = tokenizer.decode(history[0]).split("")
49
- response = [(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)] # convert to tuples of list
50
- return response, history
51
-
52
-
53
- gr.Interface(
54
- fn=predict,
55
- title=title,
56
- description=description,
57
- examples=examples,
58
- inputs=["text", "state"],
59
- outputs=["chatbot", "state"],
60
- theme="finlaymacklon/boxy_violet",
61
- ).launch()
 
1
+ from transformers import pipeline
 
 
2
 
3
+ # Load conversational pipeline
4
+ conversation = pipeline("conversational")
 
 
 
 
5
 
6
  # Course information
7
  course_info = {
8
+ "Engineering": ["Civil Engineering", "Mechanical Engineering", "Electrical Engineering", "Software Engineering", "etc."],
9
+ "Information Technology": ["Computer Science", "Information Systems", "Cybersecurity", "Data Science", "etc."],
10
+ "Business": ["Business Administration", "Accounting", "Finance", "Marketing", "Management", "etc."],
11
+ "Health Sciences": ["Nursing", "Pharmacy", "Health Information Management", "Public Health", "etc."],
12
+ "Design and Architecture": ["Architecture", "Industrial Design", "Visual Communication", "etc."],
13
+ "Science": ["Environmental Science", "Biotechnology", "Chemistry", "Physics", "etc."],
14
+ "Law": ["Law", "Legal Studies", "etc."],
15
+ "Arts and Social Sciences": ["Communication", "Education", "International Studies", "Sociology", "etc."],
16
+ "Built Environment": ["Urban Planning", "Construction Management", "Property Economics", "etc."],
17
+ "Creative Industries": ["Animation", "Photography", "Creative Writing", "Film and Television", "etc."]
 
 
18
  }
19
 
20
+ # Function to generate response using conversational AI
21
+ def generate_response(user_input):
22
+ return conversation(user_input)[0]['generated_text']
23
+
24
+ # Function to list courses based on category
25
+ def list_courses(input_text):
26
+ for category, courses in course_info.items():
27
+ if category.lower() in input_text:
28
+ return f"Under {category}, we offer: {', '.join(courses)}"
29
+ return "I'm sorry, I couldn't find any courses matching that."
30
+
31
+ # Main function to interact with the chatbot
32
+ def chat():
33
+ print("Welcome to the Course Chatbot! Ask me about available courses or say goodbye to exit.")
34
+ while True:
35
+ user_input = input("You: ")
36
+ if user_input.lower() in ["exit", "quit", "goodbye"]:
37
+ print("Goodbye!")
38
+ break
39
+ else:
40
+ if "courses" in user_input:
41
+ bot_response = list_courses(user_input)
42
+ else:
43
+ bot_response = generate_response(user_input)
44
+ print("Bot:", bot_response)
45
+
46
+ if __name__ == "__main__":
47
+ chat()