Spaces:
Sleeping
Sleeping
mindwrapped
commited on
Commit
•
01eea82
1
Parent(s):
022cdc1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import GPT2Tokenizer, TrainingArguments, Trainer, GPT2LMHeadModel
|
4 |
+
|
5 |
+
tokenizer = GPT2Tokenizer.from_pretrained('mindwrapped/gpt2-lotr-fellowship', bos_token='<|startoftext|>',
|
6 |
+
eos_token='<|endoftext|>', pad_token='<|pad|>')
|
7 |
+
model = GPT2LMHeadModel.from_pretrained('mindwrapped/gpt2-lotr-fellowship').cuda()
|
8 |
+
|
9 |
+
|
10 |
+
def generate_text(text, temperature):
|
11 |
+
generated = tokenizer("<|startoftext|> " + text, return_tensors="pt").input_ids.cuda()
|
12 |
+
sample_outputs = model.generate(generated, do_sample=True, top_k=50,
|
13 |
+
max_length=300, top_p=0.95, temperature=float(temperature), num_return_sequences=1)
|
14 |
+
|
15 |
+
for i, sample_output in enumerate(sample_outputs):
|
16 |
+
out = tokenizer.decode(sample_output, skip_special_tokens=True)
|
17 |
+
|
18 |
+
return out
|
19 |
+
|
20 |
+
|
21 |
+
demo = gr.Interface(
|
22 |
+
fn=generate_text,
|
23 |
+
inputs=[gr.Text(),gr.Slider(minimum=0.0, maximum=5.0, value=1.0, step=0.1)],
|
24 |
+
outputs='text',
|
25 |
+
examples=[['', 1.9],['Frodo and Sam moved quietly through the night.', 2.0], ['Frodo and Sam went to the pub. ', 3.0]],
|
26 |
+
title='LOTR Generator',
|
27 |
+
description='This space uses GPT2 model fine-tuned on the "The Fellowship of the Rings" to generate text. \n\nTry inputting no text to the model and messing around with the temperature.',
|
28 |
+
article=' \n\n![visitor badge](https://visitor-badge.glitch.me/badge?page_id=mindwrapped.gpt2-lotr-fellowship-generator-space)',
|
29 |
+
live=False,
|
30 |
+
)
|
31 |
+
|
32 |
+
demo.launch(debug=True)
|