Team Ai
Apppublic

ghosthamlet/Write-Stories-Using-Bloom

sourceHugging Faceupdated 4y agoView on Hugging Face
6likes
app.py116 linesDownload Raw Back to root
1import gradio as gr2import requests3import os 4 5##Bloom Inference API6API_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"7 8headers = {"Authorization": f"Bearer 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"}9prompt_sep = '😃'10 11 12def text_generate(prompt, top_p=0.8, top_k=100, temperature=1.0, num_beams=3, repetition_penalty=3.0): 13  #Prints to debug the code14  print(f"*****Inside text_generate - Prompt is :{prompt}")15  max_tokens = 25016  max_prompt_len = 5017  json_ = {"inputs": prompt[-max_prompt_len:],18            "parameters":19            {20            "top_p": float(top_p),21            "top_k": top_k,22          "temperature": float(temperature),23          "max_new_tokens": max_tokens,24          "return_full_text": True,25          "do_sample":True,26          "num_beams": num_beams,27          "repetition_penalty": float(repetition_penalty),28          }, 29          "options": 30          {"use_cache": True,31          "wait_for_model": True,32          },}33  print(f"Gen params is: {json_}")34  response = requests.post(API_URL, headers=headers, json=json_)35  print(f"Response  is : {response}")36  output = response.json()37  print(f"output is : {output}") 38  output_tmp = output[0]['generated_text']39  print(f"output_tmp is: {output_tmp}")40  solution = output_tmp.split("\nQ:")[0]   41  print(f"Final response after splits is: {solution}")42  if '\nOutput:' in solution:43    final_solution = solution.split("\nOutput:")[0] 44    print(f"Response after removing output is: {final_solution}")45  # elif '\n\n' in solution:46  #  final_solution = solution.split("\n\n")[0] 47  #  print(f"Response after removing new line entries is: {final_solution}")48  else:49    final_solution = solution50    51  final_solution = prompt[:max(0, len(prompt) - max_prompt_len)].replace(prompt_sep, '') + prompt_sep + final_solution.replace(prompt_sep, '')52  53  if 0:54    if len(generated_txt) == 0 :55      display_output = final_solution56    else:57      display_output = generated_txt[:-len(prompt)] + final_solution58    new_prompt = final_solution[len(prompt):]59    print(f"new prompt for next cycle is : {new_prompt}")60    print(f"display_output for printing on screen is : {display_output}")61    if len(new_prompt) == 0:62      temp_text = display_output[::-1]63      print(f"What is the last character of sentence? : {temp_text[0]}")64      if temp_text[1] == '.':65        first_period_loc = temp_text[2:].find('.') + 166        print(f"Location of last Period is: {first_period_loc}")67        new_prompt = display_output[-first_period_loc:-1]68        print(f"Not sending blank as prompt so new prompt for next cycle is : {new_prompt}")69      else:70        print("HERE")71        first_period_loc = temp_text.find('.')72        print(f"Location of last Period is : {first_period_loc}")73        new_prompt = display_output[-first_period_loc:-1]74        print(f"Not sending blank as prompt so new prompt for next cycle is : {new_prompt}")75      display_output = display_output[:-1]76      77  return final_solution78 79 80demo = gr.Blocks()81 82# Test it:83# Mike and John are fighting in a war. A monster caught John. John shout: “Helping!” Mike run to him, saved him, but Mike was killed by the monster84# 迈克和约翰正在打仗。一个怪物抓住了约翰。约翰喊道:“救命!”迈克跑向他,救了他,但迈克被怪物杀了85# Mike and John are fighting in a war. A monster caught John. John shout: “Helping!” Mike run to him, saved him, but Mike was killed by the monster. John looked at Mike86# 迈克和约翰正在打仗。一个怪物抓住了约翰。约翰喊道:“救命!”迈克跑向他,救了他,但迈克被怪物杀了。约翰看着迈克87 88with demo:89  gr.Markdown("<h1><center>Write Stories Using Bloom</center></h1>")90  gr.Markdown(91        """Forked form https://huggingface.co/spaces/EuroPython2022/Write-Stories-Using-Bloom. \n Bloom is a model by [HuggingFace](https://huggingface.co/bigscience/bloom) and a team of more than 1000 researchers coming together as [BigScienceW Bloom](https://twitter.com/BigscienceW).\n\nLarge language models have demonstrated a capability of producing coherent sentences and given a context we can pretty much decide the *theme* of generated text.\n\nHow to Use this App: Use the sample text given as prompt or type in a new prompt as a starting point of your awesome story! Just keep pressing the 'Generate Text' Button and go crazy!\n\nHow this App works: This app operates by feeding back the text generated by Bloom to itself as a Prompt for next generation round and so on. Currently, due to size-limits on Prompt and Token generation, we are only able to feed very limited-length text as Prompt and are getting very few tokens generated in-turn. This makes it difficult to keep a tab on theme of text generation, so please bear with that. In summary, I believe it is a nice little fun App which you can play with for a while.\n\nThis Space is created by [Yuvraj Sharma](https://twitter.com/yvrjsharma) for EuroPython 2022 Demo."""92        )93  with gr.Row():94    input_prompt = gr.Textbox(label=f"Write some text to get started... (text after {prompt_sep} is the truncated prompt inputted to Bloom)", lines=3, value="Dear human philosophers, I read your comments on my abilities and limitations with great interest.")  95    96  # with gr.Row():97  #  generated_txt = gr.Textbox(lines=7, visible = False)98    99  with gr.Row():100    top_p = gr.Slider(label="top_p", minimum=0., maximum=1.0, value=0.8, step=0.1, visible = True)101  with gr.Row():102    top_k = gr.Slider(label="top_k", minimum=1, maximum=500, value=100, step=20, visible = True)103  with gr.Row():104    temperature = gr.Slider(label="temperature", minimum=0., maximum=2.0, value=1.0, step=0.1, visible = True)105  with gr.Row():106    num_beams = gr.Slider(label="num_beams", minimum=1, maximum=6, value=3, step=1, visible = True)107  with gr.Row():108    repetition_penalty = gr.Slider(label="repetition_penalty", minimum=1.0, maximum=6.0, value=3.0, step=1.0, visible = True)109  110  b1 = gr.Button("Generate Your Story")111 112  # b1.click(text_generate, inputs=[input_prompt, generated_txt, top_p, top_k, temperature, num_beams, repetition_penalty], outputs=[generated_txt, input_prompt]) 113  b1.click(text_generate, inputs=[input_prompt, top_p, top_k, temperature, num_beams, repetition_penalty], outputs=[input_prompt]) 114 115demo.launch(enable_queue=True, debug=True)116