CoolFace
Apppublic

Wootang01/text_generator

sourceHugging Faceupdated 3y agoView on Hugging Face
12likes
app.py66 linesDownload Raw Back to root
1#import libraries and dependencies2#from gradio.mix import Parallel3 4import gradio as gr5import torch6from transformers import pipeline7 8#instantiate variables as strings9title="Text Generator"10#title1="Level 1 Text Generator"11#title2="Level 3 Text Generator"12description="This text generator has been trained to chat and to respond to natural language instructions."13#description1="This is the basic text generator all students were taught to code using an older, smaller language model. Input text, submit, and the text generator will generate one output text instance."14#description2="This is a more advanced text generator that many students were taught to code. Input text and the text generator generates three output text instances from three language models. Importantly, two of these language models were designed to process explicit instructions."15#description3="This is the most advanced text generator that a few students were taught to code. Input text and the text generator generates an output text instance. You can resubmit to include that new text as input text."16examples = [17    ["What is the capital of China?"],18    ["How do I apply for an Australian visa?"],19    ["Write a short story."],20    ["Once upon a time, "]21]22 23#instantiate variables as functions24#pipe = pipeline("text-generation", model='EleutherAI/gpt-neo-2.7B', trust_remote_code=True)25 26ans = pipeline(model="databricks/dolly-v2-3b", torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto")27 28#model1 = gr.Interface.load("huggingface/bigscience/bloom-560m")29#model2 = gr.Interface.load("huggingface/google/flan-t5-xl")30#model3 = gr.Interface.load("huggingface/bigscience/bloomz-7b1")31#model4 = gr.Interface.load("huggingface/EleutherAI/gpt-j-6B")32 33#togethercomputer/GPT-NeoXT-Chat-Base-20B34#decapoda-research/llama-7b-hf35 36#define functions37 38def answer(query):39  out=ans(query)40  return out41 42#def complete_with_gpt(text):43#    # Use the last 50 characters of the text as context44#    return text[:-50] + model4(text[-50:])45 46#with gr.Blocks() as demo:47#    with gr.Row():48#        textbox = gr.Textbox(placeholder=description3, lines=8)49#        with gr.Column():50#            btn = gr.Button("Submit")51 52#    btn.click(complete_with_gpt, textbox, textbox)53 54#tab1 = gr.Interface.load("huggingface/gpt2", title=title1, description=description1, examples=examples)55#tab2 = gr.Parallel(model1, model2, model3, inputs=gr.Textbox(lines=5, label="Input explicit or implicit instructions"), title=title2, description=description2, examples=examples)56#tab3 = demo57 58#demo1 = gr.TabbedInterface([tab1, tab2, tab3], ["Level 1", "Level 3", "Level 5"], title=title)59 60#if __name__ == "__main__":61#    demo1.launch(debug=True)62#gr.Interface.from_pipeline(pipe).launch()63 64Demo = gr.Interface(fn=answer,inputs='text',outputs='text', title=title, description=description, examples=examples)65Demo.launch()66