osanseviero HF staff commited on
Commit
480cb62
1 Parent(s): 2ac6c4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -22
app.py CHANGED
@@ -3,15 +3,15 @@ import re
3
  import requests
4
  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.
9
-
10
  Tips:
11
  - Do NOT talk to BLOOM as an entity, it's not a chatbot but a webpage/blog/article completion model.
12
  - For the best results: MIMIC a few sentences of a webpage similar to the content you want to generate.
13
  Start a paragraph as if YOU were writing a blog, webpage, math post, coding article and BLOOM will generate a coherent follow-up. Longer prompts usually give more interesting results.
14
-
15
  Options:
16
  - sampling: imaginative completions (may be not super accurate e.g. math/history)
17
  - greedy: accurate completions (may be more boring or have repetitions)
@@ -23,21 +23,17 @@ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
23
  hf_writer = gr.HuggingFaceDatasetSaver(HF_API_TOKEN, "huggingface/bloom_internal_prompts", organization="huggingface")
24
 
25
 
26
-
27
-
28
-
29
-
30
  examples = [
31
- ['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", "Sample 1"],
32
- ['A poem about the beauty of science by Alfred Edgar Brittle\nTitle: The Magic Craft\nIn the old times', 50, "Sample", "Sample 1"],
33
- ['استخراج العدد العاملي في لغة بايثون:', 30, "Greedy", "Sample 1"],
34
- ["Pour déguster un ortolan, il faut tout d'abord", 32, "Sample", "Sample 1"],
35
- ['Traduce español de España a español de Argentina\nEl coche es rojo - el auto es rojo\nEl ordenador es nuevo - la computadora es nueva\nel boligrafo es negro -', 16, "Sample", "Sample 1"],
36
- ['Estos ejemplos quitan vocales de las palabras\nEjemplos:\nhola - hl\nmanzana - mnzn\npapas - pps\nalacran - lcrn\npapa -', 16, "Sample", "Sample 1"],
37
- ["Question: If I put cheese into the fridge, will it melt?\nAnswer:", 32, "Sample", "Sample 1"],
38
- ["Math exercise - answers:\n34+10=44\n54+20=", 16, "Greedy", "Sample 1"],
39
- ["Question: Where does the Greek Goddess Persephone spend half of the year when she is not with her mother?\nAnswer:", 24, "Greedy", "Sample 1"],
40
- ["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", "Sample 1"],
41
  ]
42
 
43
  def query(payload):
@@ -46,7 +42,7 @@ def query(payload):
46
  print(response)
47
  return json.loads(response.content.decode("utf-8"))
48
 
49
- def inference(input_sentence, max_length, sample_or_greedy, seed=42):
50
  if sample_or_greedy == "Sample":
51
  parameters = {"max_new_tokens": max_length,
52
  "top_p": 0.9,
@@ -69,8 +65,42 @@ def inference(input_sentence, max_length, sample_or_greedy, seed=42):
69
  data = query(
70
  payload
71
  )
72
- print(data)
73
- return data[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
 
76
  gr.Interface(
@@ -78,10 +108,11 @@ gr.Interface(
78
  [
79
  gr.inputs.Textbox(label="Input"),
80
  gr.inputs.Slider(1, 64, default=32, step=1, label="Tokens to generate"),
81
- gr.inputs.Radio(["Sample", "Greedy"], label="Sample or greedy"),
82
- gr.inputs.Radio(["Sample 1", "Sample 2", "Sample 3", "Sample 4", "Sample 5"], label="Sample other generations (only work in 'Sample' mode", type="index"),
 
83
  ],
84
- gr.outputs.Textbox(label="Output"),
85
  examples=examples,
86
  # article=article,
87
  title=title,
 
3
  import requests
4
  import json
5
  import os
6
+ from screenshot import BG_COMP, BOX_COMP, GENERATION_VAR, PROMPT_VAR, main
7
+ from pathlib import Path
8
 
9
  title = "BLOOM"
10
  description = """Gradio Demo for BLOOM. To use it, simply add your text, or click one of the examples to load them.
 
11
  Tips:
12
  - Do NOT talk to BLOOM as an entity, it's not a chatbot but a webpage/blog/article completion model.
13
  - For the best results: MIMIC a few sentences of a webpage similar to the content you want to generate.
14
  Start a paragraph as if YOU were writing a blog, webpage, math post, coding article and BLOOM will generate a coherent follow-up. Longer prompts usually give more interesting results.
 
15
  Options:
16
  - sampling: imaginative completions (may be not super accurate e.g. math/history)
17
  - greedy: accurate completions (may be more boring or have repetitions)
 
23
  hf_writer = gr.HuggingFaceDatasetSaver(HF_API_TOKEN, "huggingface/bloom_internal_prompts", organization="huggingface")
24
 
25
 
 
 
 
 
26
  examples = [
27
+ ['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", False, "Sample 1"],
28
+ ['A poem about the beauty of science by Alfred Edgar Brittle\nTitle: The Magic Craft\nIn the old times', 50, "Sample", False, "Sample 1"],
29
+ ['استخراج العدد العاملي في لغة بايثون:', 30, "Greedy", False, "Sample 1"],
30
+ ["Pour déguster un ortolan, il faut tout d'abord", 32, "Sample", False, "Sample 1"],
31
+ ['Traduce español de España a español de Argentina\nEl coche es rojo - el auto es rojo\nEl ordenador es nuevo - la computadora es nueva\nel boligrafo es negro -', 16, "Sample", False, "Sample 1"],
32
+ ['Estos ejemplos quitan vocales de las palabras\nEjemplos:\nhola - hl\nmanzana - mnzn\npapas - pps\nalacran - lcrn\npapa -', 16, "Sample",False, "Sample 1"],
33
+ ["Question: If I put cheese into the fridge, will it melt?\nAnswer:", 32, "Sample", False, "Sample 1"],
34
+ ["Math exercise - answers:\n34+10=44\n54+20=", 16, "Greedy", False, "Sample 1"],
35
+ ["Question: Where does the Greek Goddess Persephone spend half of the year when she is not with her mother?\nAnswer:", 24, "Greedy", False, "Sample 1"],
36
+ ["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", False, "Sample 1"],
37
  ]
38
 
39
  def query(payload):
 
42
  print(response)
43
  return json.loads(response.content.decode("utf-8"))
44
 
45
+ def inference(input_sentence, max_length, sample_or_greedy, raw_text=False, seed=42):
46
  if sample_or_greedy == "Sample":
47
  parameters = {"max_new_tokens": max_length,
48
  "top_p": 0.9,
 
65
  data = query(
66
  payload
67
  )
68
+
69
+ if raw_text:
70
+ return None, data[0]['generated_text']
71
+
72
+ width, height = 3246, 3246
73
+ assets_path = "assets"
74
+ font_mapping = {
75
+ "latin characters (faster)": "DejaVuSans.ttf",
76
+ "complete alphabet (slower)":"GoNotoCurrent.ttf"
77
+ }
78
+ working_dir = Path(__file__).parent.resolve()
79
+ font_path = str(working_dir / font_mapping["complete alphabet (slower)"])
80
+ img_save_path = str(working_dir / "output.jpeg")
81
+ colors = {
82
+ BG_COMP: "#000000",
83
+ PROMPT_VAR: "#FFFFFF",
84
+ GENERATION_VAR: "#FF57A0",
85
+ BOX_COMP: "#120F25",
86
+ }
87
+
88
+ new_string = data[0]['generated_text'].split(input_sentence, 1)[1]
89
+
90
+ _, img = main(
91
+ input_sentence,
92
+ new_string,
93
+ width,
94
+ height,
95
+ assets_path=assets_path,
96
+ font_path=font_path,
97
+ colors=colors,
98
+ frame_to_box_margin=200,
99
+ text_to_text_box_margin=50,
100
+ init_font_size=150,
101
+ right_align=False,
102
+ )
103
+ return img, data[0]['generated_text']
104
 
105
 
106
  gr.Interface(
 
108
  [
109
  gr.inputs.Textbox(label="Input"),
110
  gr.inputs.Slider(1, 64, default=32, step=1, label="Tokens to generate"),
111
+ gr.inputs.Radio(["Sample", "Greedy"], label="Sample or greedy", default="Sample"),
112
+ gr.Checkbox(label="Just output raw text"),
113
+ gr.inputs.Radio(["Sample 1", "Sample 2", "Sample 3", "Sample 4", "Sample 5"], default="Sample 1", label="Sample other generations (only work in 'Sample' mode", type="index"),
114
  ],
115
+ ["image", "text"],
116
  examples=examples,
117
  # article=article,
118
  title=title,