Spaces:
Runtime error
Runtime error
Commit
·
ed27ead
1
Parent(s):
a89eb44
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,119 +1,112 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import re
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
-
from screenshot import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
- Content: Please see our [content disclaimer](https://huggingface.co/spaces/bigscience/bloom-book) before using the model, as it may sometimes behave in unexpected ways.
|
| 16 |
-
|
| 17 |
-
Options:
|
| 18 |
-
- sampling: imaginative completions (may be not super accurate e.g. math/history)
|
| 19 |
-
- greedy: accurate completions (may be more boring or have repetitions)
|
| 20 |
-
"""
|
| 21 |
|
| 22 |
API_URL = os.getenv("API_URL")
|
| 23 |
|
| 24 |
-
examples = [
|
| 25 |
-
['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"],
|
| 26 |
-
['A poem about the beauty of science by Alfred Edgar Brittle\nTitle: The Magic Craft\nIn the old times', 50, "Sample", False, "Sample 1"],
|
| 27 |
-
['استخراج العدد العاملي في لغة بايثون:', 30, "Greedy", False, "Sample 1"],
|
| 28 |
-
["Pour déguster un ortolan, il faut tout d'abord", 32, "Sample", False, "Sample 1"],
|
| 29 |
-
['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"],
|
| 30 |
-
['Estos ejemplos quitan vocales de las palabras\nEjemplos:\nhola - hl\nmanzana - mnzn\npapas - pps\nalacran - lcrn\npapa -', 16, "Sample",False, "Sample 1"],
|
| 31 |
-
["Question: If I put cheese into the fridge, will it melt?\nAnswer:", 32, "Sample", False, "Sample 1"],
|
| 32 |
-
["Math exercise - answers:\n34+10=44\n54+20=", 16, "Greedy", False, "Sample 1"],
|
| 33 |
-
["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"],
|
| 34 |
-
["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"],
|
| 35 |
-
]
|
| 36 |
|
| 37 |
def query(payload):
|
| 38 |
print(payload)
|
| 39 |
response = requests.request("POST", API_URL, json=payload)
|
| 40 |
print(response)
|
| 41 |
return json.loads(response.content.decode("utf-8"))
|
| 42 |
-
|
| 43 |
-
def inference(input_sentence, max_length, sample_or_greedy,
|
| 44 |
if sample_or_greedy == "Sample":
|
| 45 |
-
parameters = {
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
-
parameters = {
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
payload = {"inputs": input_sentence,
|
| 61 |
-
"parameters": parameters}
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return None, data[0]['generated_text']
|
| 69 |
-
|
| 70 |
-
width, height = 3246, 3246
|
| 71 |
-
assets_path = "assets"
|
| 72 |
-
font_mapping = {
|
| 73 |
-
"latin characters (faster)": "DejaVuSans.ttf",
|
| 74 |
-
"complete alphabet (slower)":"GoNotoCurrent.ttf"
|
| 75 |
-
}
|
| 76 |
-
working_dir = Path(__file__).parent.resolve()
|
| 77 |
-
font_path = str(working_dir / font_mapping["complete alphabet (slower)"])
|
| 78 |
-
img_save_path = str(working_dir / "output.jpeg")
|
| 79 |
-
colors = {
|
| 80 |
-
BG_COMP: "#000000",
|
| 81 |
-
PROMPT_VAR: "#FFFFFF",
|
| 82 |
-
GENERATION_VAR: "#FF57A0",
|
| 83 |
-
BOX_COMP: "#120F25",
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
new_string = data[0]['generated_text'].split(input_sentence, 1)[1]
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
frame_to_box_margin=200,
|
| 97 |
-
text_to_text_box_margin=50,
|
| 98 |
-
init_font_size=150,
|
| 99 |
-
right_align=False,
|
| 100 |
)
|
| 101 |
-
return img, data[0]['generated_text']
|
| 102 |
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
gr.
|
| 108 |
-
|
| 109 |
-
gr.
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
from screenshot import (
|
| 6 |
+
before_prompt,
|
| 7 |
+
prompt_to_generation,
|
| 8 |
+
after_generation,
|
| 9 |
+
js_save,
|
| 10 |
+
js_load_script,
|
| 11 |
+
)
|
| 12 |
+
from spaces_info import description, examples, initial_prompt_value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
API_URL = os.getenv("API_URL")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def query(payload):
|
| 18 |
print(payload)
|
| 19 |
response = requests.request("POST", API_URL, json=payload)
|
| 20 |
print(response)
|
| 21 |
return json.loads(response.content.decode("utf-8"))
|
| 22 |
+
|
| 23 |
+
def inference(input_sentence, max_length, sample_or_greedy, seed=42):
|
| 24 |
if sample_or_greedy == "Sample":
|
| 25 |
+
parameters = {
|
| 26 |
+
"max_new_tokens": max_length,
|
| 27 |
+
"top_p": 0.9,
|
| 28 |
+
"do_sample": True,
|
| 29 |
+
"seed": seed,
|
| 30 |
+
"early_stopping": False,
|
| 31 |
+
"length_penalty": 0.0,
|
| 32 |
+
"eos_token_id": None,
|
| 33 |
+
}
|
| 34 |
else:
|
| 35 |
+
parameters = {
|
| 36 |
+
"max_new_tokens": max_length,
|
| 37 |
+
"do_sample": False,
|
| 38 |
+
"seed": seed,
|
| 39 |
+
"early_stopping": False,
|
| 40 |
+
"length_penalty": 0.0,
|
| 41 |
+
"eos_token_id": None,
|
| 42 |
+
}
|
| 43 |
|
| 44 |
+
payload = {"inputs": input_sentence, "parameters": parameters}
|
|
|
|
| 45 |
|
| 46 |
+
#if API_URL is not None:
|
| 47 |
+
data = query(payload)
|
| 48 |
+
#else: # To remove when plugged in a real API
|
| 49 |
+
# input_sentence, generation = payload["inputs"].split("\n", 1)
|
| 50 |
+
# data = [{"generated_text": input_sentence + generation}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
generation = data[0]["generated_text"].split(input_sentence, 1)[1]
|
| 53 |
+
return (
|
| 54 |
+
before_prompt
|
| 55 |
+
+ input_sentence
|
| 56 |
+
+ prompt_to_generation
|
| 57 |
+
+ generation
|
| 58 |
+
+ after_generation,
|
| 59 |
+
data[0]["generated_text"],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo = gr.Blocks()
|
| 65 |
+
with demo:
|
| 66 |
+
with gr.Row():
|
| 67 |
+
gr.Markdown(value=description)
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
text = gr.Textbox(
|
| 71 |
+
label="Input",
|
| 72 |
+
value=" ", # should be set to " " when plugged into a real API
|
| 73 |
+
)
|
| 74 |
+
tokens = gr.Slider(1, 64, value=32, step=1, label="Tokens to generate")
|
| 75 |
+
sampling = gr.Radio(
|
| 76 |
+
["Sample", "Greedy"], label="Sample or greedy", value="Sample"
|
| 77 |
+
)
|
| 78 |
+
sampling2 = gr.Radio(
|
| 79 |
+
["Sample 1", "Sample 2", "Sample 3", "Sample 4", "Sample 5"],
|
| 80 |
+
value="Sample 1",
|
| 81 |
+
label="Sample other generations (only work in 'Sample' mode)",
|
| 82 |
+
type="index",
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
submit = gr.Button("Submit")
|
| 87 |
+
load_image = gr.Button("Generate Image")
|
| 88 |
+
with gr.Column():
|
| 89 |
+
display_out = gr.HTML(
|
| 90 |
+
label="Image"
|
| 91 |
+
) # , visible=False) #interactive=True,
|
| 92 |
+
display_out.set_event_trigger(
|
| 93 |
+
"load",
|
| 94 |
+
fn=None,
|
| 95 |
+
inputs=None,
|
| 96 |
+
outputs=None,
|
| 97 |
+
no_target=True,
|
| 98 |
+
js=js_load_script,
|
| 99 |
+
)
|
| 100 |
+
text_out = gr.Textbox(label="Output")
|
| 101 |
+
with gr.Row():
|
| 102 |
+
gr.Examples(examples=examples, inputs=[text, tokens, sampling, sampling2])
|
| 103 |
+
|
| 104 |
+
submit.click(
|
| 105 |
+
inference,
|
| 106 |
+
inputs=[text, tokens, sampling, sampling2],
|
| 107 |
+
outputs=[display_out, text_out],
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
load_image.click(fn=None, inputs=None, outputs=None, _js=js_save)
|
| 111 |
+
|
| 112 |
+
demo.launch()
|