Update app.py
Browse files
app.py
CHANGED
@@ -1,276 +1,92 @@
|
|
1 |
-
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
-
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
with gr.Group():
|
95 |
-
chatbot = gr.Chatbot(label='Chatbot')
|
96 |
-
with gr.Row():
|
97 |
-
textbox = gr.Textbox(
|
98 |
-
container=False,
|
99 |
-
show_label=False,
|
100 |
-
placeholder='Type a message...',
|
101 |
-
scale=10,
|
102 |
-
)
|
103 |
-
submit_button = gr.Button('Submit',
|
104 |
-
variant='primary',
|
105 |
-
scale=1,
|
106 |
-
min_width=0)
|
107 |
-
with gr.Row():
|
108 |
-
retry_button = gr.Button('π Retry', variant='secondary')
|
109 |
-
undo_button = gr.Button('β©οΈ Undo', variant='secondary')
|
110 |
-
clear_button = gr.Button('ποΈ Clear', variant='secondary')
|
111 |
-
|
112 |
-
saved_input = gr.State()
|
113 |
-
|
114 |
-
with gr.Accordion(label='Advanced options', open=False):
|
115 |
-
system_prompt = gr.Textbox(label='System prompt',
|
116 |
-
value=DEFAULT_SYSTEM_PROMPT,
|
117 |
-
lines=6)
|
118 |
-
max_new_tokens = gr.Slider(
|
119 |
-
label='Max new tokens',
|
120 |
-
minimum=1,
|
121 |
-
maximum=MAX_MAX_NEW_TOKENS,
|
122 |
-
step=1,
|
123 |
-
value=DEFAULT_MAX_NEW_TOKENS,
|
124 |
-
)
|
125 |
-
temperature = gr.Slider(
|
126 |
-
label='Temperature',
|
127 |
-
minimum=0.1,
|
128 |
-
maximum=4.0,
|
129 |
-
step=0.1,
|
130 |
-
value=1.0,
|
131 |
-
)
|
132 |
-
top_p = gr.Slider(
|
133 |
-
label='Top-p (nucleus sampling)',
|
134 |
-
minimum=0.05,
|
135 |
-
maximum=1.0,
|
136 |
-
step=0.05,
|
137 |
-
value=0.95,
|
138 |
-
)
|
139 |
-
top_k = gr.Slider(
|
140 |
-
label='Top-k',
|
141 |
-
minimum=1,
|
142 |
-
maximum=1000,
|
143 |
-
step=1,
|
144 |
-
value=50,
|
145 |
-
)
|
146 |
-
|
147 |
-
gr.Examples(
|
148 |
-
examples=[
|
149 |
-
'Hello there! How are you doing?',
|
150 |
-
'Can you explain briefly to me what is the Python programming language?',
|
151 |
-
'Explain the plot of Cinderella in a sentence.',
|
152 |
-
'How many hours does it take a man to eat a Helicopter?',
|
153 |
-
"Write a 100-word article on 'Benefits of Open-Source in AI research'",
|
154 |
-
],
|
155 |
-
inputs=textbox,
|
156 |
-
outputs=[textbox, chatbot],
|
157 |
-
fn=process_example,
|
158 |
-
cache_examples=True,
|
159 |
-
)
|
160 |
-
|
161 |
-
gr.Markdown(LICENSE)
|
162 |
-
|
163 |
-
textbox.submit(
|
164 |
-
fn=clear_and_save_textbox,
|
165 |
-
inputs=textbox,
|
166 |
-
outputs=[textbox, saved_input],
|
167 |
-
api_name=False,
|
168 |
-
queue=False,
|
169 |
-
).then(
|
170 |
-
fn=display_input,
|
171 |
-
inputs=[saved_input, chatbot],
|
172 |
-
outputs=chatbot,
|
173 |
-
api_name=False,
|
174 |
-
queue=False,
|
175 |
-
).then(
|
176 |
-
fn=check_input_token_length,
|
177 |
-
inputs=[saved_input, chatbot, system_prompt],
|
178 |
-
api_name=False,
|
179 |
-
queue=False,
|
180 |
-
).success(
|
181 |
-
fn=generate,
|
182 |
-
inputs=[
|
183 |
-
saved_input,
|
184 |
-
chatbot,
|
185 |
-
system_prompt,
|
186 |
-
max_new_tokens,
|
187 |
-
temperature,
|
188 |
-
top_p,
|
189 |
-
top_k,
|
190 |
-
],
|
191 |
-
outputs=chatbot,
|
192 |
-
api_name=False,
|
193 |
-
)
|
194 |
-
|
195 |
-
button_event_preprocess = submit_button.click(
|
196 |
-
fn=clear_and_save_textbox,
|
197 |
-
inputs=textbox,
|
198 |
-
outputs=[textbox, saved_input],
|
199 |
-
api_name=False,
|
200 |
-
queue=False,
|
201 |
-
).then(
|
202 |
-
fn=display_input,
|
203 |
-
inputs=[saved_input, chatbot],
|
204 |
-
outputs=chatbot,
|
205 |
-
api_name=False,
|
206 |
-
queue=False,
|
207 |
-
).then(
|
208 |
-
fn=check_input_token_length,
|
209 |
-
inputs=[saved_input, chatbot, system_prompt],
|
210 |
-
api_name=False,
|
211 |
-
queue=False,
|
212 |
-
).success(
|
213 |
-
fn=generate,
|
214 |
-
inputs=[
|
215 |
-
saved_input,
|
216 |
-
chatbot,
|
217 |
-
system_prompt,
|
218 |
-
max_new_tokens,
|
219 |
-
temperature,
|
220 |
-
top_p,
|
221 |
-
top_k,
|
222 |
-
],
|
223 |
-
outputs=chatbot,
|
224 |
-
api_name=False,
|
225 |
-
)
|
226 |
-
|
227 |
-
retry_button.click(
|
228 |
-
fn=delete_prev_fn,
|
229 |
-
inputs=chatbot,
|
230 |
-
outputs=[chatbot, saved_input],
|
231 |
-
api_name=False,
|
232 |
-
queue=False,
|
233 |
-
).then(
|
234 |
-
fn=display_input,
|
235 |
-
inputs=[saved_input, chatbot],
|
236 |
-
outputs=chatbot,
|
237 |
-
api_name=False,
|
238 |
-
queue=False,
|
239 |
-
).then(
|
240 |
-
fn=generate,
|
241 |
-
inputs=[
|
242 |
-
saved_input,
|
243 |
-
chatbot,
|
244 |
-
system_prompt,
|
245 |
-
max_new_tokens,
|
246 |
-
temperature,
|
247 |
-
top_p,
|
248 |
-
top_k,
|
249 |
-
],
|
250 |
-
outputs=chatbot,
|
251 |
-
api_name=False,
|
252 |
-
)
|
253 |
-
|
254 |
-
undo_button.click(
|
255 |
-
fn=delete_prev_fn,
|
256 |
-
inputs=chatbot,
|
257 |
-
outputs=[chatbot, saved_input],
|
258 |
-
api_name=False,
|
259 |
-
queue=False,
|
260 |
-
).then(
|
261 |
-
fn=lambda x: x,
|
262 |
-
inputs=[saved_input],
|
263 |
-
outputs=textbox,
|
264 |
-
api_name=False,
|
265 |
-
queue=False,
|
266 |
-
)
|
267 |
-
|
268 |
-
clear_button.click(
|
269 |
-
fn=lambda: ([], ''),
|
270 |
-
outputs=[chatbot, saved_input],
|
271 |
-
queue=False,
|
272 |
-
api_name=False,
|
273 |
-
)
|
274 |
-
|
275 |
-
demo.queue(max_size=20).launch()
|
276 |
-
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
+
from transformers import AutoModelForMultipleChoice, AutoTokenizer
|
6 |
+
from huggingface_hub import hf_hub_url, Repository
|
7 |
+
model_id="/kaggle/input/deberta-v3-large-hf-weights"
|
8 |
+
# Load the model and tokenizer
|
9 |
+
|
10 |
+
model = AutoModelForMultipleChoice.from_pretrained(model_id)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
12 |
+
|
13 |
+
# Define the preprocessing function
|
14 |
+
def preprocess(sample):
|
15 |
+
first_sentences = [sample["prompt"]] * 5
|
16 |
+
second_sentences = [sample[option] for option in "ABCDE"]
|
17 |
+
tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt")
|
18 |
+
sample["input_ids"] = tokenized_sentences["input_ids"]
|
19 |
+
sample["attention_mask"] = tokenized_sentences["attention_mask"]
|
20 |
+
return sample
|
21 |
+
|
22 |
+
# Define the prediction function
|
23 |
+
def predict(data):
|
24 |
+
inputs = torch.stack(data["input_ids"])
|
25 |
+
masks = torch.stack(data["attention_mask"])
|
26 |
+
with torch.no_grad():
|
27 |
+
logits = model(inputs, attention_mask=masks).logits
|
28 |
+
predictions_as_ids = torch.argsort(-logits, dim=1)
|
29 |
+
answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()]
|
30 |
+
return ["".join(i) for i in answers[:, :3]]
|
31 |
+
|
32 |
+
# Create the Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=predict,
|
35 |
+
inputs=gr.Interface.DataType.json,
|
36 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
37 |
+
live=True,
|
38 |
+
examples=[
|
39 |
+
{"prompt": "This is the prompt", "A": "Option A text", "B": "Option B text", "C": "Option C text", "D": "Option D text", "E": "Option E text"}
|
40 |
+
],
|
41 |
+
title="LLM Science Exam Demo",
|
42 |
+
description="Enter the prompt and options (A to E) below and get predictions.",
|
43 |
+
)
|
44 |
+
|
45 |
+
# Run the interface
|
46 |
+
iface.launch()
|
47 |
+
import pandas as pd
|
48 |
+
import numpy as np
|
49 |
+
import gradio as gr
|
50 |
+
import torch
|
51 |
+
from transformers import AutoModelForMultipleChoice, AutoTokenizer
|
52 |
+
from huggingface_hub import hf_hub_url, Repository
|
53 |
+
|
54 |
+
# Load the model and tokenizer
|
55 |
+
model_path = "my_model"
|
56 |
+
model = AutoModelForMultipleChoice.from_pretrained(model_path)
|
57 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
58 |
+
|
59 |
+
# Define the preprocessing function
|
60 |
+
def preprocess(sample):
|
61 |
+
first_sentences = [sample["prompt"]] * 5
|
62 |
+
second_sentences = [sample[option] for option in "ABCDE"]
|
63 |
+
tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt")
|
64 |
+
sample["input_ids"] = tokenized_sentences["input_ids"]
|
65 |
+
sample["attention_mask"] = tokenized_sentences["attention_mask"]
|
66 |
+
return sample
|
67 |
+
|
68 |
+
# Define the prediction function
|
69 |
+
def predict(data):
|
70 |
+
inputs = torch.stack(data["input_ids"])
|
71 |
+
masks = torch.stack(data["attention_mask"])
|
72 |
+
with torch.no_grad():
|
73 |
+
logits = model(inputs, attention_mask=masks).logits
|
74 |
+
predictions_as_ids = torch.argsort(-logits, dim=1)
|
75 |
+
answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()]
|
76 |
+
return ["".join(i) for i in answers[:, :3]]
|
77 |
+
|
78 |
+
# Create the Gradio interface
|
79 |
+
iface = gr.Interface(
|
80 |
+
fn=predict,
|
81 |
+
inputs=gr.Interface.DataType.json,
|
82 |
+
outputs=gr.outputs.Label(num_top_classes=3),
|
83 |
+
live=True,
|
84 |
+
examples=[
|
85 |
+
{"prompt": "This is the prompt", "A": "Option A text", "B": "Option B text", "C": "Option C text", "D": "Option D text", "E": "Option E text"}
|
86 |
+
],
|
87 |
+
title="LLM Science Exam Demo",
|
88 |
+
description="Enter the prompt and options (A to E) below and get predictions.",
|
89 |
+
)
|
90 |
+
|
91 |
+
# Run the interface
|
92 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|