patrickvonplaten commited on
Commit
a0b5629
1 Parent(s): 7978c63
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import re
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)
18
+ """
19
+
20
+ API_URL = os.getenv("API_URL")
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", False, "Sample 1"],
24
+ ['A poem about the beauty of science by Alfred Edgar Brittle\nTitle: The Magic Craft\nIn the old times', 50, "Sample", False, "Sample 1"],
25
+ ['استخراج العدد العاملي في لغة بايثون:', 30, "Greedy", False, "Sample 1"],
26
+ ["Pour déguster un ortolan, il faut tout d'abord", 32, "Sample", False, "Sample 1"],
27
+ ['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"],
28
+ ['Estos ejemplos quitan vocales de las palabras\nEjemplos:\nhola - hl\nmanzana - mnzn\npapas - pps\nalacran - lcrn\npapa -', 16, "Sample",False, "Sample 1"],
29
+ ["Question: If I put cheese into the fridge, will it melt?\nAnswer:", 32, "Sample", False, "Sample 1"],
30
+ ["Math exercise - answers:\n34+10=44\n54+20=", 16, "Greedy", False, "Sample 1"],
31
+ ["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"],
32
+ ["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"],
33
+ ]
34
+
35
+ def query(payload):
36
+ print(payload)
37
+ response = requests.request("POST", API_URL, json=payload)
38
+ print(response)
39
+ return json.loads(response.content.decode("utf-8"))
40
+
41
+ def inference(input_sentence, max_length, sample_or_greedy, raw_text=False, seed=42):
42
+ if sample_or_greedy == "Sample":
43
+ parameters = {"max_new_tokens": max_length,
44
+ "top_p": 0.9,
45
+ "do_sample": True,
46
+ "seed": seed,
47
+ "early_stopping": False,
48
+ "length_penalty": 0.0,
49
+ "eos_token_id": None}
50
+ else:
51
+ parameters = {"max_new_tokens": max_length,
52
+ "do_sample": False,
53
+ "seed": seed,
54
+ "early_stopping": False,
55
+ "length_penalty": 0.0,
56
+ "eos_token_id": None}
57
+
58
+ payload = {"inputs": input_sentence,
59
+ "parameters": parameters}
60
+
61
+ data = query(
62
+ payload
63
+ )
64
+
65
+ if raw_text:
66
+ return None, data[0]['generated_text']
67
+
68
+ width, height = 3246, 3246
69
+ assets_path = "assets"
70
+ font_mapping = {
71
+ "latin characters (faster)": "DejaVuSans.ttf",
72
+ "complete alphabet (slower)":"GoNotoCurrent.ttf"
73
+ }
74
+ working_dir = Path(__file__).parent.resolve()
75
+ font_path = str(working_dir / font_mapping["complete alphabet (slower)"])
76
+ img_save_path = str(working_dir / "output.jpeg")
77
+ colors = {
78
+ BG_COMP: "#000000",
79
+ PROMPT_VAR: "#FFFFFF",
80
+ GENERATION_VAR: "#FF57A0",
81
+ BOX_COMP: "#120F25",
82
+ }
83
+
84
+ new_string = data[0]['generated_text'].split(input_sentence, 1)[1]
85
+
86
+ _, img = main(
87
+ input_sentence,
88
+ new_string,
89
+ width,
90
+ height,
91
+ assets_path=assets_path,
92
+ font_path=font_path,
93
+ colors=colors,
94
+ frame_to_box_margin=200,
95
+ text_to_text_box_margin=50,
96
+ init_font_size=150,
97
+ right_align=False,
98
+ )
99
+ return img, data[0]['generated_text']
100
+
101
+
102
+ gr.Interface(
103
+ inference,
104
+ [
105
+ gr.inputs.Textbox(label="Input"),
106
+ gr.inputs.Slider(1, 64, default=32, step=1, label="Tokens to generate"),
107
+ gr.inputs.Radio(["Sample", "Greedy"], label="Sample or greedy", default="Sample"),
108
+ gr.Checkbox(label="Just output raw text"),
109
+ 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"),
110
+ ],
111
+ ["image", "text"],
112
+ examples=examples,
113
+ # article=article,
114
+ cache_examples=False,
115
+ title=title,
116
+ description=description
117
+ ).launch()