Spaces:
Runtime error
Runtime error
Add application file
Browse files- app.py +436 -0
- sortinghat.py +42 -0
app.py
ADDED
@@ -0,0 +1,436 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sortinghat import SortingHat
|
2 |
+
import random
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
from peft import AutoPeftModelForCausalLM
|
8 |
+
|
9 |
+
from transformers import AutoTokenizer
|
10 |
+
from time import sleep
|
11 |
+
|
12 |
+
class HPChatBot:
|
13 |
+
def __init__(self,
|
14 |
+
model_path: str = 'rnltls/harrypotter_lexicon_finetune',
|
15 |
+
device_map: str = 'auto',
|
16 |
+
load_in_4_bit: bool = True,
|
17 |
+
**quant_kwargs) -> None:
|
18 |
+
self.model = None
|
19 |
+
self.tokenizer = None
|
20 |
+
self.image_processor = None
|
21 |
+
self.conv = None
|
22 |
+
self.conv_img = []
|
23 |
+
self.img_tensor = []
|
24 |
+
self.roles = None
|
25 |
+
self.stop_key = None
|
26 |
+
self.is_chat = False
|
27 |
+
self.is_waldo = False
|
28 |
+
self.load_models(model_path,
|
29 |
+
device_map=device_map,
|
30 |
+
load_in_4_bit=load_in_4_bit,
|
31 |
+
**quant_kwargs)
|
32 |
+
|
33 |
+
def load_models(self, model_path: str,
|
34 |
+
device_map: str,
|
35 |
+
load_in_4_bit: bool,
|
36 |
+
**quant_kwargs) -> None:
|
37 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
38 |
+
model_path, # YOUR MODEL YOU USED FOR TRAINING
|
39 |
+
load_in_4bit = load_in_4_bit,
|
40 |
+
)
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
42 |
+
|
43 |
+
self.model = AutoPeftModelForCausalLM.from_pretrained(
|
44 |
+
"rnltls/harrypotter_lexicon_finetune", # YOUR MODEL YOU USED FOR TRAINING
|
45 |
+
load_in_4bit = load_in_4_bit,
|
46 |
+
)
|
47 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path,
|
48 |
+
use_fast=False)
|
49 |
+
|
50 |
+
def generate_answer(self, prompt):
|
51 |
+
output = self.model.generate(**prompt, max_new_tokens = 256)
|
52 |
+
generated_text = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
53 |
+
return generated_text
|
54 |
+
|
55 |
+
HogwartGemma = "https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/HogwartGemma.jpg?raw=true"
|
56 |
+
SpellProf = ["https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Spell_stand.jpg?raw=true",
|
57 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Spell_think.jpg?raw=true",
|
58 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Spell_show.jpg?raw=true"]
|
59 |
+
PotionProf = ["https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Potion_stand.jpg?raw=true",
|
60 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Potion_think.jpg?raw=true",
|
61 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Potion_show.jpg?raw=true"]
|
62 |
+
OtherProf = ["https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Other_stand.jpg?raw=true",
|
63 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Other_think.jpg?raw=true",
|
64 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/Other_show.jpg?raw=true"]
|
65 |
+
HouseHat = ["https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/sorting_hat.jpg?raw=true",
|
66 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/sorting_hat_think.jpg?raw=true",
|
67 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/sorting_hat_gryffindor.png?raw=true",
|
68 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/sorting_hat_hufflepuff.png?raw=true",
|
69 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/sorting_hat_ravenclaw.png?raw=true",
|
70 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/sorting_hat_slytherin.png?raw=true"]
|
71 |
+
Icons = ["https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/wand_icon.png?raw=true",
|
72 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/cauldron.png?raw=true",
|
73 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/golden-snitch.png?raw=true",
|
74 |
+
"https://github.com/kangshwan/hogwarts_chatbot/blob/main/IMG/hat_icon.png?raw=true"]
|
75 |
+
enable_btn = gr.Button(interactive=True)
|
76 |
+
disable_btn = gr.Button(interactive=False)
|
77 |
+
spell_chat, potion_chat, other_chat, house_chat = False, False, False, False
|
78 |
+
question_counter = 0
|
79 |
+
quit_counter = 0
|
80 |
+
|
81 |
+
question_list = None
|
82 |
+
quote_list = None
|
83 |
+
answer_list = []
|
84 |
+
question_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
85 |
+
|
86 |
+
### Instruction:
|
87 |
+
{}
|
88 |
+
|
89 |
+
### Input:
|
90 |
+
{}
|
91 |
+
|
92 |
+
### Response:
|
93 |
+
{}"""
|
94 |
+
|
95 |
+
def show_image(path):
|
96 |
+
# Convert To PIL Image
|
97 |
+
image = Image.open(path)
|
98 |
+
return image
|
99 |
+
|
100 |
+
def spell_chatting(chat_history, request: gr.Request):
|
101 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, answer_list
|
102 |
+
answer_list = []
|
103 |
+
quit_counter=0
|
104 |
+
question_counter = 0
|
105 |
+
chat_history.clear()
|
106 |
+
spell_chat, potion_chat, other_chat, house_chat = True, False, False, False
|
107 |
+
bot_message = "Welcome to Magic Spell Class!\nTell me what you want to achieve, and I’ll suggest the perfect spell for it!\nIf you ask in the format: 'What spell can I use when I ~?', I can give you even better suggestions!"
|
108 |
+
txt_box = gr.Textbox(value="What spell can I use when I ", interactive=True)
|
109 |
+
prof_IMG = SpellProf[0]
|
110 |
+
chat_history.append([None, bot_message])
|
111 |
+
return chat_history, prof_IMG, txt_box, enable_btn, enable_btn, enable_btn
|
112 |
+
|
113 |
+
def potion_chatting(chat_history, request: gr.Request):
|
114 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, answer_list
|
115 |
+
answer_list = []
|
116 |
+
quit_counter=0
|
117 |
+
question_counter = 0
|
118 |
+
chat_history.clear()
|
119 |
+
spell_chat, potion_chat, other_chat, house_chat = False, True, False, False
|
120 |
+
bot_message = "Welcome to Potion Class!\nTell me what you want to achieve, and I’ll suggest the perfect potion for it!\nIf you ask in the format: 'What potion can I make when I ~?', I can give you even better suggestions!"
|
121 |
+
txt_box = gr.Textbox(value="What potion can I make when I ", interactive=True)
|
122 |
+
prof_IMG = PotionProf[0]
|
123 |
+
chat_history.append([None,bot_message])
|
124 |
+
return chat_history, prof_IMG, txt_box, enable_btn, enable_btn, enable_btn
|
125 |
+
|
126 |
+
def other_chatting(chat_history, request: gr.Request):
|
127 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, answer_list
|
128 |
+
answer_list = []
|
129 |
+
quit_counter=0
|
130 |
+
question_counter = 0
|
131 |
+
chat_history.clear()
|
132 |
+
spell_chat, potion_chat, other_chat, house_chat = False, False, True, False
|
133 |
+
bot_message = "Welcome to Library!\nWelcome to the library! Feel free to ask me anything if you're curious!"
|
134 |
+
txt_box = gr.Textbox(placeholder="Ask anything you're curious about in the Wizarding World!", value="", interactive=True)
|
135 |
+
prof_IMG = OtherProf[0]
|
136 |
+
chat_history.append([None,bot_message])
|
137 |
+
return chat_history, prof_IMG, txt_box, enable_btn, enable_btn, enable_btn
|
138 |
+
|
139 |
+
def house_chatting(chat_history, request: gr.Request):
|
140 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, question_counter, question_list, quote_list, answer_list
|
141 |
+
answer_list = []
|
142 |
+
question_list = sortinghat.choose_questions()
|
143 |
+
quote_list = sortinghat.choose_quotes()
|
144 |
+
quit_counter=0
|
145 |
+
question_counter = 0
|
146 |
+
chat_history.clear()
|
147 |
+
spell_chat, potion_chat, other_chat, house_chat = False, False, False, True
|
148 |
+
bot_message = "Hmm, let's see… where shall I place you?\n" + question_list[question_counter]
|
149 |
+
txt_box = gr.Textbox(placeholder="Answer the question", value="", interactive=True)
|
150 |
+
prof_IMG = HouseHat[0]
|
151 |
+
chat_history.append([None,bot_message])
|
152 |
+
return chat_history, prof_IMG, txt_box, enable_btn, enable_btn, enable_btn
|
153 |
+
|
154 |
+
def ask_question(chat_history, text_data, request: gr.Request):
|
155 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, answer_list
|
156 |
+
if spell_chat:
|
157 |
+
print(text_data)
|
158 |
+
prof_IMG = SpellProf[1]
|
159 |
+
chat_history.append([text_data, None])
|
160 |
+
text_data = gr.Textbox(value="What spell can I use when I ", interactive=True)
|
161 |
+
return chat_history, prof_IMG, text_data
|
162 |
+
|
163 |
+
if potion_chat:
|
164 |
+
print(text_data)
|
165 |
+
prof_IMG = PotionProf[1]
|
166 |
+
chat_history.append([text_data, None])
|
167 |
+
text_data = gr.Textbox(value="What potion can I make when I ", interactive=True)
|
168 |
+
return chat_history, prof_IMG, text_data
|
169 |
+
|
170 |
+
if other_chat:
|
171 |
+
print(text_data)
|
172 |
+
prof_IMG = OtherProf[1]
|
173 |
+
chat_history.append([text_data, None])
|
174 |
+
text_data = gr.Textbox(placeholder="Ask anything you're curious about in the Wizarding World!", value="", interactive=True)
|
175 |
+
return chat_history, prof_IMG, text_data
|
176 |
+
|
177 |
+
if house_chat:
|
178 |
+
print(text_data)
|
179 |
+
prof_IMG = HouseHat[1]
|
180 |
+
chat_history.append([text_data, None])
|
181 |
+
answer_list.append(text_data)
|
182 |
+
text_data = gr.Textbox(placeholder="Answer the question", value = "", interactive=True)
|
183 |
+
return chat_history, prof_IMG, text_data
|
184 |
+
|
185 |
+
def clean_chatting(chat_history, request: gr.Request):
|
186 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, question_counter, question_list, quote_list, answer_list
|
187 |
+
quit_counter = 0
|
188 |
+
question_counter = 0
|
189 |
+
question_list=sortinghat.choose_questions()
|
190 |
+
quote_list=sortinghat.choose_quotes()
|
191 |
+
answer_list=[]
|
192 |
+
chat_history.clear()
|
193 |
+
if spell_chat:
|
194 |
+
bot_message = "Welcome to Magic Spell Class!\nTell me what you want to achieve, and I’ll suggest the perfect spell for it!\nIf you ask in the format: 'What spell can I use when I ~?', I can give you even better suggestions!"
|
195 |
+
prof_IMG = SpellProf[0]
|
196 |
+
if potion_chat:
|
197 |
+
bot_message = "Welcome to Potion Class!\nTell me what you want to achieve, and I’ll suggest the perfect potion for it!\nIf you ask in the format: 'What potion can I make when I ~?', I can give you even better suggestions!"
|
198 |
+
prof_IMG = PotionProf[0]
|
199 |
+
if other_chat:
|
200 |
+
bot_message = "Welcome to Library!\nWelcome to the library! Feel free to ask me anything if you're curious!"
|
201 |
+
prof_IMG = OtherProf[0]
|
202 |
+
if house_chat:
|
203 |
+
bot_message = "Hmm, let's see… where shall I place you?\n" + question_list[question_counter]
|
204 |
+
prof_IMG = HouseHat[0]
|
205 |
+
chat_history.append([None, bot_message])
|
206 |
+
return chat_history, prof_IMG
|
207 |
+
|
208 |
+
def run_model(chat_history, request: gr.Request):
|
209 |
+
global spell_chat, potion_chat, other_chat, house_chat, question_counter, question_list, quote_list, answer_list
|
210 |
+
|
211 |
+
if question_counter < 4 and house_chat:
|
212 |
+
bot_message = quote_list[question_counter] + '\n' + question_list[question_counter+1]
|
213 |
+
question_counter += 1
|
214 |
+
chat_history[-1][1] = bot_message
|
215 |
+
return chat_history
|
216 |
+
|
217 |
+
elif house_chat:
|
218 |
+
print("Choosing the house")
|
219 |
+
print(question_list)
|
220 |
+
print(answer_list)
|
221 |
+
user_responses = "\n".join([f"{i+1}. {q}\nAnswer: {a}" for i, (q,a) in enumerate(zip(question_list, answer_list))])
|
222 |
+
|
223 |
+
prompt = f"""
|
224 |
+
You are the Sorting Hat at Hogwarts. Based on the student's answers to the following questions, assign them to the most suitable house (Gryffindor, Hufflepuff, Ravenclaw, or Slytherin) and provide three reasons for your decision.
|
225 |
+
Gryffindor values courage, nerve, and chivalry.
|
226 |
+
Hufflepuff values hard work, patience, justice, and loyalty.
|
227 |
+
Ravenclaw values intelligence, learning, wisdom, and wit.
|
228 |
+
Slytherin values ambition, cunning, leadership, and resourcefulness.
|
229 |
+
|
230 |
+
### Instruction:
|
231 |
+
Please format the response like this:
|
232 |
+
'Your house is [house]!
|
233 |
+
Here's why:
|
234 |
+
1. [reason 1]
|
235 |
+
2. [reason 2]
|
236 |
+
3. [reason 3]
|
237 |
+
Welcome to your new house at Hogwarts!'
|
238 |
+
|
239 |
+
### Input:
|
240 |
+
{user_responses}
|
241 |
+
|
242 |
+
### Response:
|
243 |
+
"""
|
244 |
+
inputs = HP_chatbot.tokenizer([prompt], return_tensors="pt").to("cuda")
|
245 |
+
output = HP_chatbot.generate_answer(inputs)
|
246 |
+
output = output.split("Response:")[-1]
|
247 |
+
print(output)
|
248 |
+
chat_history[-1][1] = output
|
249 |
+
return chat_history
|
250 |
+
|
251 |
+
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
252 |
+
|
253 |
+
### Instruction:
|
254 |
+
{}
|
255 |
+
|
256 |
+
### Input:
|
257 |
+
{}
|
258 |
+
|
259 |
+
### Response:
|
260 |
+
{}"""
|
261 |
+
|
262 |
+
inputs = HP_chatbot.tokenizer(
|
263 |
+
[
|
264 |
+
alpaca_prompt.format(
|
265 |
+
chat_history[-1][0], # instruction
|
266 |
+
"", # input
|
267 |
+
"", # output - leave this blank for generation!
|
268 |
+
)
|
269 |
+
], return_tensors = "pt").to("cuda")
|
270 |
+
output = HP_chatbot.generate_answer(inputs)
|
271 |
+
|
272 |
+
output = output.split("Response:")[-1]
|
273 |
+
chat_history[-1][1] = output
|
274 |
+
return chat_history
|
275 |
+
|
276 |
+
def end_chatting(chat_history, textbox, request: gr.Request):
|
277 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, question_counter
|
278 |
+
if not house_chat or quit_counter > 0:
|
279 |
+
quit_counter = 0
|
280 |
+
question_counter = 0
|
281 |
+
chat_history.clear()
|
282 |
+
chat_history.append([None, "Welcome to Hogwarts!"])
|
283 |
+
textbox = gr.Textbox(show_label=False, placeholder="Welcome to Hogwart!", value= "", container=False, interactive = False)
|
284 |
+
return chat_history, HogwartGemma, textbox, disable_btn, disable_btn, disable_btn
|
285 |
+
else:
|
286 |
+
quit_counter += 1
|
287 |
+
chat_history.append([None, "Quit, you say? Oh, I see your plight, But the housing question’s still in sight.\
|
288 |
+
\nThough weary you may be, don't yet retreat,For there's still a task you must complete!"])
|
289 |
+
return chat_history, HouseHat[0], textbox, enable_btn, enable_btn, enable_btn
|
290 |
+
|
291 |
+
def result_img(chat_history, textbox, submit_btn, request: gr.Request):
|
292 |
+
global spell_chat, potion_chat, other_chat, house_chat, quit_counter, question_counter
|
293 |
+
if spell_chat:
|
294 |
+
return SpellProf[2], textbox, submit_btn
|
295 |
+
if potion_chat:
|
296 |
+
return PotionProf[2], textbox, submit_btn
|
297 |
+
if other_chat:
|
298 |
+
return OtherProf[2], textbox, submit_btn
|
299 |
+
if house_chat:
|
300 |
+
if question_counter == 4:
|
301 |
+
quit_counter = 1
|
302 |
+
house = chat_history[-1][1]
|
303 |
+
# print(house)
|
304 |
+
if "gryffindor" in house.lower():
|
305 |
+
insignia = HouseHat[2]
|
306 |
+
elif "hufflepuff" in house.lower():
|
307 |
+
insignia = HouseHat[3]
|
308 |
+
elif "ravenclaw" in house.lower():
|
309 |
+
insignia = HouseHat[4]
|
310 |
+
elif "slytherin" in house.lower():
|
311 |
+
insignia = HouseHat[5]
|
312 |
+
else:
|
313 |
+
return HouseHat[0], textbox, disable_btn
|
314 |
+
textbox = gr.Textbox(show_label=False, placeholder="Housing Done.", value= "", container=False, interactive = False)
|
315 |
+
return insignia, textbox, disable_btn
|
316 |
+
else:
|
317 |
+
return HouseHat[0], textbox, submit_btn
|
318 |
+
|
319 |
+
def build_gradio(concurrency_count=10):
|
320 |
+
textbox = gr.Textbox(show_label=False, placeholder="Welcome to Hogwart!", container=False, interactive = False)
|
321 |
+
with gr.Blocks(
|
322 |
+
theme='gstaff/xkcd',
|
323 |
+
) as demo:
|
324 |
+
state = gr.State()
|
325 |
+
with gr.Row():
|
326 |
+
with gr.Column(scale=3):
|
327 |
+
|
328 |
+
imagebox = gr.Image(value=HogwartGemma, type="pil", interactive=False, show_label=False, show_download_button=False, show_fullscreen_button=False)
|
329 |
+
|
330 |
+
spell_btn = gr.Button(icon=Icons[0], value="Spell", interactive = True, scale = 2)
|
331 |
+
potion_btn = gr.Button(icon=Icons[1], value="Potion", interactive = True, scale = 2)
|
332 |
+
other_btn = gr.Button(icon=Icons[2], value="Others", interactive = True, scale = 2)
|
333 |
+
house_btn = gr.Button(icon=Icons[3], value="Find your House", interactive = True, scale = 2)
|
334 |
+
|
335 |
+
with gr.Column(scale=8):
|
336 |
+
initial_message = [None,"Welcome to Hogwarts!"]
|
337 |
+
chatbot = gr.Chatbot(
|
338 |
+
label='Free to ask!',
|
339 |
+
value= [initial_message],
|
340 |
+
height=600,
|
341 |
+
)
|
342 |
+
with gr.Row():
|
343 |
+
with gr.Column(scale=8):
|
344 |
+
textbox.render()
|
345 |
+
with gr.Column(scale=1, min_width=50):
|
346 |
+
submit_btn = gr.Button(value="Send", variant="primary", interactive = False)
|
347 |
+
|
348 |
+
with gr.Row(elem_id="buttons") as button_row:
|
349 |
+
finish_btn = gr.Button(value="🏁 End Talking", interactive = False)
|
350 |
+
clear_btn = gr.Button(value="🗑️ Clear", interactive=False)
|
351 |
+
|
352 |
+
box_list = [spell_btn, potion_btn, other_btn, house_btn, textbox, submit_btn, finish_btn, clear_btn]
|
353 |
+
|
354 |
+
spell_btn.click(
|
355 |
+
spell_chatting,
|
356 |
+
inputs=[chatbot],
|
357 |
+
outputs=[chatbot, imagebox, textbox, submit_btn, finish_btn, clear_btn]
|
358 |
+
)
|
359 |
+
potion_btn.click(
|
360 |
+
potion_chatting,
|
361 |
+
inputs=[chatbot],
|
362 |
+
outputs=[chatbot, imagebox, textbox, submit_btn, finish_btn, clear_btn]
|
363 |
+
)
|
364 |
+
other_btn.click(
|
365 |
+
other_chatting,
|
366 |
+
inputs=[chatbot],
|
367 |
+
outputs=[chatbot, imagebox, textbox, submit_btn, finish_btn, clear_btn]
|
368 |
+
)
|
369 |
+
house_btn.click(
|
370 |
+
house_chatting,
|
371 |
+
inputs=[chatbot],
|
372 |
+
outputs=[chatbot, imagebox, textbox, submit_btn, finish_btn, clear_btn]
|
373 |
+
)
|
374 |
+
textbox.submit(
|
375 |
+
ask_question,
|
376 |
+
inputs = [chatbot, textbox],
|
377 |
+
outputs = [chatbot, imagebox, textbox]
|
378 |
+
).then(
|
379 |
+
run_model,
|
380 |
+
inputs = [chatbot],
|
381 |
+
outputs = [chatbot]
|
382 |
+
).then(
|
383 |
+
result_img,
|
384 |
+
inputs=[chatbot, textbox, submit_btn],
|
385 |
+
outputs=[imagebox, textbox, submit_btn]
|
386 |
+
)
|
387 |
+
|
388 |
+
submit_btn.click(
|
389 |
+
ask_question,
|
390 |
+
inputs = [chatbot, textbox],
|
391 |
+
outputs = [chatbot, imagebox, textbox]
|
392 |
+
).then(
|
393 |
+
run_model,
|
394 |
+
inputs = [chatbot],
|
395 |
+
outputs = [chatbot]
|
396 |
+
).then(
|
397 |
+
result_img,
|
398 |
+
inputs=[chatbot, textbox, submit_btn],
|
399 |
+
outputs=[imagebox, textbox, submit_btn]
|
400 |
+
)
|
401 |
+
|
402 |
+
finish_btn.click(
|
403 |
+
end_chatting,
|
404 |
+
inputs=[chatbot, textbox],
|
405 |
+
outputs=[chatbot, imagebox, textbox, submit_btn, finish_btn, clear_btn]
|
406 |
+
)
|
407 |
+
|
408 |
+
clear_btn.click(
|
409 |
+
clean_chatting,
|
410 |
+
inputs=[chatbot],
|
411 |
+
outputs=[chatbot, imagebox]
|
412 |
+
)
|
413 |
+
|
414 |
+
return demo
|
415 |
+
|
416 |
+
import argparse
|
417 |
+
if __name__ == "__main__":
|
418 |
+
|
419 |
+
# 컨트롤러에서 사용가능한 모델 가져옴. 나는 모델 하나만 쓸 것이기 때문에, get_model_list()에서 해당 모델이 동작하고 있는 url을 넘겨주면 된다!
|
420 |
+
# models = [args.model_url]
|
421 |
+
|
422 |
+
# HP_chatbot = HPChatBot(load_in_8bit=True,
|
423 |
+
# bnb_8bit_compute_dtype=torch.float16,
|
424 |
+
# bnb_8bit_use_double_quant=True,
|
425 |
+
# bnb_8bit_quant_type='nf8')
|
426 |
+
|
427 |
+
sortinghat = SortingHat()
|
428 |
+
|
429 |
+
# Gradio를 이용해서 데모 만들기
|
430 |
+
demo = build_gradio()
|
431 |
+
demo.queue(
|
432 |
+
api_open=False
|
433 |
+
).launch(
|
434 |
+
server_port=7861,
|
435 |
+
share=True
|
436 |
+
)
|
sortinghat.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
class SortingHat:
|
3 |
+
def __init__(self):
|
4 |
+
self.questions = [
|
5 |
+
"How do you approach challenges and obstacles, and what do you think defines bravery?",
|
6 |
+
"When working with others, do you prefer to lead, follow, or collaborate, and why?",
|
7 |
+
"In a difficult situation, would you prioritize being fair or being kind, and how would you balance the two?",
|
8 |
+
"How do you stay motivated when pursuing your goals, and what role does ambition play in your life?",
|
9 |
+
"When faced with rules or expectations, do you tend to follow them, question them, or create your own? Why?",
|
10 |
+
"Which do you value more: being honest or being kind? Why?",
|
11 |
+
"Do you rely more on intuition or analysis when making decisions? Why?",
|
12 |
+
"How do you make sure your ideas and opinions are heard in group discussions?",
|
13 |
+
"What role do you usually take in group projects or collaborations? Why?",
|
14 |
+
"How do you respond to failure, and what does it teach you?",
|
15 |
+
"What principles guide you in making difficult decisions?",
|
16 |
+
"How do you stay grounded while striving for excellence in your work or studies?"
|
17 |
+
]
|
18 |
+
self.answers = [
|
19 |
+
"So many minds, so many dreams, each one yearning for greatness in their own way.",
|
20 |
+
"Such complexity in this one—more layers than a potion brewed at midnight",
|
21 |
+
"Curious, this mind holds secrets even they haven’t discovered yet.",
|
22 |
+
"I sense a fire within, simmering beneath a calm surface.",
|
23 |
+
"The potential here is boundless, but will they see it themselves?",
|
24 |
+
"Resilient, adaptable, yet a hint of doubt clouds their path.",
|
25 |
+
"A mind as sharp as a blade, cutting through the fog of uncertainty.",
|
26 |
+
"So many roads they could walk, each with its own unique challenge.",
|
27 |
+
"A heart full of dreams and a spirit unbroken—what a combination.",
|
28 |
+
"There’s a quiet strength in this one, hidden but undeniable.",
|
29 |
+
"The choices ahead will shape their destiny more than they know.",
|
30 |
+
"Determined, but with a trace of uncertainty—an intriguing combination.",
|
31 |
+
"They carry a spark of mischief, hidden beneath a composed exterior.",
|
32 |
+
"A mind that seeks adventure, but a heart that yearns for belonging."
|
33 |
+
]
|
34 |
+
|
35 |
+
def choose_questions(self):
|
36 |
+
self.random_samples = random.sample(self.questions, 5)
|
37 |
+
return self.random_samples
|
38 |
+
|
39 |
+
def choose_quotes(self):
|
40 |
+
self.random_quotes = random.sample(self.answers, 5)
|
41 |
+
return self.random_quotes
|
42 |
+
|