File size: 4,117 Bytes
ee9feac
6a2a902
 
 
 
 
ee9feac
 
 
e8f27bb
5ef0c29
e8f27bb
 
 
da060ac
 
e8f27bb
 
2c3dc94
e8f27bb
 
 
 
 
 
2c3dc94
e8f27bb
 
 
54ce4bd
e8f27bb
ee9feac
 
 
 
 
 
a24cc8b
 
a555b4f
 
d88c0f6
 
e78f595
70432c2
e78f595
d88c0f6
 
70432c2
e78f595
70432c2
d88c0f6
 
ee9feac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
95
96
97
98
99
100
101
102
103
examples = [
    "Write an essay about meditation. [EOI]",
    "Give me 5 steps to clean my room. [EOI]",
    "How are the continents formed? [EOI]",
    "Prompt: A man draws a gun in a dark alley and asks for your wallet. You begrudgingly obey. He throws it on the ground, shoots it till it screeches, and turns to you; 'you are safe now'. Write a story about given prompt. [EOI]",
    "Write directions of a cooking recipe with these ingredients: chicken breast, carrots, green peas, celery, butter, onion, flour, salt, black pepper, celery seed, chicken broth, milk, unbaked pie crusts? [EOI]",
]


import gradio as gr 
from transformers import AutoTokenizer, pipeline, AutoModelForCausalLM



tokenizer = AutoTokenizer.from_pretrained("akoksal/LongForm-OPT-125M") 
generate = pipeline('text-generation', model="akoksal/LongForm-OPT-125M", tokenizer=tokenizer)


def predict(instruction, topp, max_length, temperature):
    if "[EOI]" not in instruction:
        instruction = instruction + " [EOI]"
    x = generate(instruction,
        do_sample=True, 
        top_p=topp, 
        num_return_sequences=1,
        max_length=max_length,
        temperature=temperature
    )[0]["generated_text"]
    
    return x[len(instruction):]

def process_example(args):
    for x in predict(args):
        pass
    return x


with gr.Blocks() as demo:
    with gr.Column():
        gr.Markdown("""# 📜LongForm
        
The LongForm dataset is created by leveraging English corpus examples with augmented instructions. We select a diverse set of human-written documents from existing corpora such as C4 and Wikipedia and generate instructions for the given documents via LLMs. Then, we extend these examples with structured corpora examples such as Stack Exchange and WikiHow and task examples such as question answering, email writing, grammar error correction, story/poem generation, and text summarization.

**Paper**: https://arxiv.org/abs/2304.08460 

**Dataset and Models**: https://github.com/akoksal/LongForm

**Tips**:

1. Use the "[EOI]" token at the end of the instruction for OPT models. This demo adds [EOI] automatically if you forget it.

2. The LongForm dataset and models mainly focus on long text generation and have limitations regarding structured prediction tasks in NLP. 
"""
        )
        with gr.Row():
            with gr.Column(scale=3):
                instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
                with gr.Box():
                    gr.Markdown("**Answer**")
                    output = gr.Markdown(elem_id="q-output")
                submit = gr.Button("Generate", variant="primary")
                gr.Examples(
                    examples=examples,
                    inputs=[instruction],
                    cache_examples=False,
                    fn=process_example,
                    outputs=[output],
                )

            with gr.Column(scale=1):
                top_p = gr.Slider(
                    label="Top-p (nucleus sampling)",
                    value=0.90,
                    minimum=0.0,
                    maximum=1,
                    step=0.05,
                    interactive=True,
                    info="Higher values sample low-probability tokens",
                )
                max_length = gr.Slider(
                    label="Max length",
                    value=64,
                    minimum=1,
                    maximum=512,
                    step=4,
                    interactive=True,
                    info="The maximum length of the output",
                )
                temperature = gr.Slider(
                    label="Temperature",
                    value=1.0,
                    minimum=0.0,
                    maximum=2.0,
                    step=0.1,
                    interactive=True,
                    info="Higher values sample more diverse outputs",
                )

    submit.click(predict, inputs=[instruction, top_p, max_length, temperature], outputs=[output])


demo.queue(concurrency_count=4)
demo.launch()