Spaces:
Runtime error
Runtime error
patrickvonplaten
commited on
Commit
•
4311752
1
Parent(s):
53e8cbd
up
Browse files
_
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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', 64, "Sample", False, "Sample 1"],
|
25 |
+
['استخراج العدد العاملي في لغة بايثون:', 32, "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 -', 32, "Sample", False, "Sample 1"],
|
28 |
+
['Estos ejemplos quitan vocales de las palabras\nEjemplos:\nhola - hl\nmanzana - mnzn\npapas - pps\nalacran - lcrn\npapa -', 32, "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=", 32, "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:", 32, "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:", 32, "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):
|
42 |
+
if sample_or_greedy == "Sample":
|
43 |
+
parameters = {"max_new_tokens": max_length,
|
44 |
+
"top_p": 0.9}
|
45 |
+
else:
|
46 |
+
parameters = {"max_new_tokens": max_length,
|
47 |
+
"do_sample": False}
|
48 |
+
|
49 |
+
payload = {"prompts": input_sentence, "gen_method": sample_or_greedy, "max_new_tokens": max_length}
|
50 |
+
|
51 |
+
data = query(
|
52 |
+
payload
|
53 |
+
)
|
54 |
+
|
55 |
+
if raw_text:
|
56 |
+
return None, data[0]['generated_text']
|
57 |
+
|
58 |
+
width, height = 3326, 3326
|
59 |
+
assets_path = "assets"
|
60 |
+
font_mapping = {
|
61 |
+
"latin characters (faster)": "DejaVuSans.ttf",
|
62 |
+
"complete alphabet (slower)":"GoNotoCurrent.ttf"
|
63 |
+
}
|
64 |
+
working_dir = Path(__file__).parent.resolve()
|
65 |
+
font_path = str(working_dir / font_mapping["complete alphabet (slower)"])
|
66 |
+
img_save_path = str(working_dir / "output.jpeg")
|
67 |
+
colors = {
|
68 |
+
BG_COMP: "#000000",
|
69 |
+
PROMPT_VAR: "#FFFFFF",
|
70 |
+
GENERATION_VAR: "#FF57A0",
|
71 |
+
BOX_COMP: "#120F25",
|
72 |
+
}
|
73 |
+
|
74 |
+
new_string = data[0]['generated_text'].split(input_sentence, 1)[1]
|
75 |
+
|
76 |
+
_, img = main(
|
77 |
+
input_sentence,
|
78 |
+
new_string,
|
79 |
+
width,
|
80 |
+
height,
|
81 |
+
assets_path=assets_path,
|
82 |
+
font_path=font_path,
|
83 |
+
colors=colors,
|
84 |
+
frame_to_box_margin=200,
|
85 |
+
text_to_text_box_margin=50,
|
86 |
+
init_font_size=150,
|
87 |
+
right_align=False,
|
88 |
+
)
|
89 |
+
return img, data[0]['generated_text']
|
90 |
+
|
91 |
+
|
92 |
+
gr.Interface(
|
93 |
+
inference,
|
94 |
+
[
|
95 |
+
gr.inputs.Textbox(label="Input"),
|
96 |
+
gr.inputs.Radio([32, 64], default=32, label="Tokens to generate"),
|
97 |
+
gr.inputs.Radio(["sampling", "greedy"], label="Sample or greedy", default="Sample"),
|
98 |
+
gr.Checkbox(label="Just output raw text"),
|
99 |
+
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"),
|
100 |
+
],
|
101 |
+
["image", "text"],
|
102 |
+
examples=examples,
|
103 |
+
# article=article,
|
104 |
+
cache_examples=False,
|
105 |
+
title=title,
|
106 |
+
description=description
|
107 |
+
).launch()
|
app.py
CHANGED
@@ -39,18 +39,7 @@ def query(payload):
|
|
39 |
return json.loads(response.content.decode("utf-8"))
|
40 |
|
41 |
def inference(input_sentence, max_length, sample_or_greedy, raw_text=False):
|
42 |
-
|
43 |
-
parameters = {"max_new_tokens": max_length,
|
44 |
-
"top_p": 0.9,
|
45 |
-
"do_sample": True,
|
46 |
-
"eos_token_id": None}
|
47 |
-
else:
|
48 |
-
parameters = {"max_new_tokens": max_length,
|
49 |
-
"do_sample": False,
|
50 |
-
"eos_token_id": None}
|
51 |
-
|
52 |
-
payload = {"inputs": input_sentence,
|
53 |
-
"parameters": parameters}
|
54 |
|
55 |
data = query(
|
56 |
payload
|
@@ -98,7 +87,7 @@ gr.Interface(
|
|
98 |
[
|
99 |
gr.inputs.Textbox(label="Input"),
|
100 |
gr.inputs.Radio([32, 64], default=32, label="Tokens to generate"),
|
101 |
-
gr.inputs.Radio(["
|
102 |
gr.Checkbox(label="Just output raw text"),
|
103 |
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"),
|
104 |
],
|
|
|
39 |
return json.loads(response.content.decode("utf-8"))
|
40 |
|
41 |
def inference(input_sentence, max_length, sample_or_greedy, raw_text=False):
|
42 |
+
payload = {"prompts": input_sentence, "gen_method": sample_or_greedy, "max_new_tokens": max_length}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
data = query(
|
45 |
payload
|
|
|
87 |
[
|
88 |
gr.inputs.Textbox(label="Input"),
|
89 |
gr.inputs.Radio([32, 64], default=32, label="Tokens to generate"),
|
90 |
+
gr.inputs.Radio(["sampling", "greedy"], label="Sample or greedy", default="Sample"),
|
91 |
gr.Checkbox(label="Just output raw text"),
|
92 |
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"),
|
93 |
],
|