CoolFace
Apppublic

EuroPython2022/Scratchpad-w-BLOOM

sourceHugging Faceupdated 4y agoView on Hugging Face
14likes
app.py63 linesDownload Raw Back to root
1import gradio as gr2import requests3import os4 5##Bloom6API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"7HF_TOKEN = os.environ["HF_TOKEN"]8headers = {"Authorization": f"Bearer {HF_TOKEN}"}9 10def text_generate(prompt):11  print(f"Prompt is :{prompt}")12  p =  prompt + " Solution: " 13  print(f"Final prompt is : {p}")14  json_ = {"inputs": p,15            "parameters":16            {17            "top_p": 0.9,18          "temperature": 1.1,19          "max_new_tokens": 250,20          "return_full_text": True21          }, "options": 22              {23              "use_cache": True,24              "wait_for_model":True25              },}26  response = requests.post(API_URL, headers=headers, json=json_)27  print(f"Response  is : {response}")28  output = response.json()29  print(f"output is : {output}")30  output_tmp = output[0]['generated_text']31  print(f"output_tmp is: {output_tmp}")32  solution = output_tmp.split("\nQ:")[0]33  print(f"Final response after splits is: {solution}") 34  return solution 35 36demo = gr.Blocks()37 38with demo:39    gr.Markdown("<h1><center>Length generalization (LG) With BLOOM🌸 </center></h1>")40    gr.Markdown(41            """42            We will examine large language models ability to extrapolate to longer problems! \n43            Length generalization (LG) is important: Often, long examples are rare and intrinsically more difficult, yet are the ones we care more about.  \n44            Recent paper [Exploring Length Generalization in Large Language Models](https://arxiv.org/pdf/2207.04901) found that using few-shot  [scratchpad](https://arxiv.org/abs/2112.00114), a combo behind many strong LLM results (eg. #Minerva ) \n45            leads to **substantial improvements in length generalization!** \n46            In-context learning enables variable length pattern matching, producing solutions of correct lengths. \n47            This space is an attempt at inspecting this LLM behavior/capability in the new HuggingFace BigScienceW [Bloom](https://huggingface.co/bigscience/bloom) model. \n48            This Space is created by [Muhtasham Oblokulov](https://twitter.com/muhtasham9) for EuroPython 2022 Demo. \n49            This Space is work in progress, BLOOM doesn't support inference on long sequencess so you may try with shorter sequences. \n50            """51            )52    with gr.Row(): 53        input_prompt = gr.Textbox(value="Q:The coin is heads up.(1) Then Austin flips. Is the coin still heads up? Solution: Coin is initially heads up. (1) After Austin flips, coin turns to heads. Q: The coin is heads up. (2) Then Austin doesn't flip. (1) Then Kara flips. Is the coin still heads up?",54        label="Enter your examples zero-shot (few-shot is not supported due to API limit) followed by Query :")55        generated_txt = gr.Textbox(lines=10, label="Generated Solution:")56 57    b1 = gr.Button("Generate Text")58    b1.click(text_generate,inputs=[input_prompt], outputs=[generated_txt])59    60    with gr.Row(): 61            gr.Markdown("![visitor badge](https://visitor-badge.glitch.me/badge?page_id=europython2022_scratchpad-w-bloom)")62 63demo.launch(enable_queue=True, debug=True)