Created app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("fubuki119/JokesGPT")
|
5 |
+
model = AutoModelForCausalLM.from_pretrained("fubuki119/JokesGPT")
|
6 |
+
|
7 |
+
|
8 |
+
def generate(max_length):
|
9 |
+
starting_text = "JOKE:"
|
10 |
+
end_token = "<|endoftext|>"
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model.to(device)
|
13 |
+
model.eval()
|
14 |
+
with torch.no_grad():
|
15 |
+
cur_ids = torch.tensor(tokenizer.encode("JOKE:")
|
16 |
+
).unsqueeze(0).to(device)
|
17 |
+
for i in range(max_length):
|
18 |
+
outputs = model(cur_ids)
|
19 |
+
logits, _ = outputs[:]
|
20 |
+
softmax_logits = torch.softmax(logits[0, -1], dim=0)
|
21 |
+
|
22 |
+
next_token_id = torch.multinomial(softmax_logits, 1).item()
|
23 |
+
cur_ids = torch.cat([cur_ids, torch.ones(
|
24 |
+
(1, 1)).long().to(device) * next_token_id], dim=1)
|
25 |
+
if next_token_id == tokenizer.encode(end_token)[0]:
|
26 |
+
joke = cur_ids.detach().cpu().tolist()
|
27 |
+
joke = joke[0]
|
28 |
+
return tokenizer.decode(joke[3:-1])
|
29 |
+
joke = cur_ids.detach().cpu().tolist()
|
30 |
+
joke = joke[0]
|
31 |
+
return tokenizer.decode(joke[3:])
|
32 |
+
|
33 |
+
|
34 |
+
def fn(n):
|
35 |
+
print(f"i got {n}")
|
36 |
+
return "thanks"
|
37 |
+
|
38 |
+
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=generate,
|
41 |
+
inputs=gr.Number(value=200, label="Maxlen"),
|
42 |
+
outputs="text",
|
43 |
+
)
|
44 |
+
iface.launch(share=True, debug=True)
|