thomwolf HF staff commited on
Commit
ff33526
1 Parent(s): 55bb6df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -5,7 +5,13 @@ import json
5
  import os
6
 
7
  title = "BLOOM"
8
- description = "Gradio Demo for BLOOM. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
 
 
 
 
 
 
9
 
10
  API_URL = "https://hfbloom.ngrok.io/generate"
11
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
@@ -14,39 +20,40 @@ hf_writer = gr.HuggingFaceDatasetSaver(HF_API_TOKEN, "huggingface/bloom_internal
14
 
15
 
16
  examples = [
17
- ['A "whatpu" is a small, furry animal native to Tanzania. An example of a sentence that uses the word whatpu is: We were traveling in Africa and we saw these very cute whatpus. To do a "farduddle" means to jump up and down really fast. An example of a sentence that uses the word farduddle is:', 8, 0, 0, 0.1, 0, 0.9, False],
18
- ['def quicksort(l):', 8, 0, 0, 0.1, 0, 0.9, False],
19
- ['Q: ¿Cómo te llamas? A: What is your name? Q: ¿Qué edad tienes? A: How old are you? Q: ¿Dónde vives?', 8, 0, 0, 0.1, 0, 0.9, False]
 
 
 
20
  ]
21
 
22
- def safe_text(text):
23
- text = text.replace('%', '\\%25')
24
- text = text.replace('#', '\\%23')
25
- text = text.replace('+', '\\%2B')
26
- text = text.replace('*', '\\%2A')
27
- text = text.replace('&', '\\%26')
28
- text = re.sub(r"([$_*\[\]()~`>\#\+\-=|\.!{}])", r"\\\1", text)
29
- return f"<pre>{text}</pre>"
30
-
31
-
32
  def query(payload):
33
  print(payload)
34
  response = requests.request("POST", API_URL, json=payload)
35
  print(response)
36
  return json.loads(response.content.decode("utf-8"))
37
 
38
- def inference(input_sentence, max_length, no_repeat_ngram_size, num_beams, temperature,top_k, top_p, greedy_decoding, seed=42):
39
- top_k = None if top_k == 0 else top_k
40
- do_sample = False if num_beams > 0 else not greedy_decoding
41
- num_beams = None if (greedy_decoding or num_beams == 0) else num_beams
42
- no_repeat_ngram_size = None if num_beams is None else no_repeat_ngram_size
43
- top_p = None if num_beams else top_p
44
-
45
- early_stopping = None if num_beams is None else num_beams > 0
 
 
 
 
 
 
 
 
46
 
47
  payload = {"inputs": input_sentence,
48
- "parameters": {"max_new_tokens": max_length, "top_k": top_k, "top_p": top_p, "temperature": temperature,
49
- "do_sample": do_sample, "seed": seed, "early_stopping":early_stopping, "no_repeat_ngram_size":no_repeat_ngram_size, "num_beams":num_beams}}
50
  data = query(
51
  payload
52
  )
@@ -58,13 +65,8 @@ gr.Interface(
58
  inference,
59
  [
60
  gr.inputs.Textbox(label="Input"),
61
- gr.inputs.Slider(1, 64, default=8, step=1, label="Tokens to generate"),
62
- gr.inputs.Slider(1, 10, default=2, step=1, label="No repeat N gram"),
63
- gr.inputs.Slider(0, 10, default=5, step=1, label="Num beams"),
64
- gr.inputs.Slider(0.0, 1.0, default=0.1, step=0.05, label="Temperature"),
65
- gr.inputs.Slider(0, 64, default=0, step=1, label="Top K"),
66
- gr.inputs.Slider(0.0, 10, default=0.9, step=0.05, label="Top P"),
67
- gr.inputs.Checkbox(False, label="Greedy decoding"),
68
  ],
69
  gr.outputs.Textbox(label="Output"),
70
  examples=examples,
 
5
  import os
6
 
7
  title = "BLOOM"
8
+ description = """Gradio Demo for BLOOM. To use it, simply add your text, or click one of the examples to load them.
9
+ Tips: Do NOT talk to BLOOM as an entity, it's not a chatbot but a webpage/blog/article completion model. For the best results: MIMIC a few words of a webpage similar to the content you want to generate. Start a sentence as if YOU were writing a blog, webpage, math post, coding article and BLOOPM will generate a coherent follow-up.
10
+
11
+ Generation simple selector:
12
+ - sampling: imaginative completions (may be not super accurate e.g. math/history)
13
+ - greedy: accurate completions (may be more boring or have repetitions)
14
+ """
15
 
16
  API_URL = "https://hfbloom.ngrok.io/generate"
17
  HF_API_TOKEN = os.getenv("HF_API_TOKEN")
 
20
 
21
 
22
  examples = [
23
+ ['A "whatpu" is a small, furry animal native to Tanzania. An example of a sentence that uses the word whatpu is: We were traveling in Africa and we saw these very cute whatpus. To do a "farduddle" means to jump up and down really fast. An example of a sentence that uses the word farduddle is:', 32, "Sample"],
24
+ ["Pour déguster un ortolan, il faut tout d'abord", 32, "Sample"],
25
+ ["Question: If I put cheese into the fridge, will it melt?\nAnswer:", 32, "Sample"],
26
+ ["Math exercise - answers:\n34+10=44\n54+20=", 16, "Greedy"],
27
+ ["Question: Where does the Greek Goddess Persephone spend half of the year when she is not with her mother?\nAnswer:", 24, "Greedy"],
28
+ ["spelling test answers.\nWhat are the letters in « language »?\nAnswer: l-a-n-g-u-a-g-e\nWhat are the letters in « Romanian »?\nAnswer:", 24, "Greedy"],
29
  ]
30
 
 
 
 
 
 
 
 
 
 
 
31
  def query(payload):
32
  print(payload)
33
  response = requests.request("POST", API_URL, json=payload)
34
  print(response)
35
  return json.loads(response.content.decode("utf-8"))
36
 
37
+ def inference(input_sentence, max_length, sample_or_greedy, seed=42):
38
+ if sample_or_greedy == "Sample":
39
+ parameters = {"max_new_tokens": max_length,
40
+ "top_p": 0.9,
41
+ "do_sample": True,
42
+ "seed": seed,
43
+ "early_stopping": False,
44
+ "length_penalty": 0.0,
45
+ "eos_token_id": None}
46
+ else:
47
+ parameters = {"max_new_tokens": max_length,
48
+ "do_sample": False,
49
+ "seed": seed,
50
+ "early_stopping": False,
51
+ "length_penalty": 0.0,
52
+ "eos_token_id": None}
53
 
54
  payload = {"inputs": input_sentence,
55
+ "parameters": parameters}
56
+
57
  data = query(
58
  payload
59
  )
 
65
  inference,
66
  [
67
  gr.inputs.Textbox(label="Input"),
68
+ gr.inputs.Slider(1, 64, default=32, step=1, label="Tokens to generate"),
69
+ gr.inputs.Radio(["Sample", "Greedy"]),
 
 
 
 
 
70
  ],
71
  gr.outputs.Textbox(label="Output"),
72
  examples=examples,