Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
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:',
|
18 |
-
[
|
19 |
-
[
|
|
|
|
|
|
|
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,
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
payload = {"inputs": input_sentence,
|
48 |
-
"parameters":
|
49 |
-
|
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=
|
62 |
-
gr.inputs.
|
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,
|