Adapt to HF inference endpoints
Browse files
app.py
CHANGED
|
@@ -3,31 +3,40 @@ from dotenv import load_dotenv
|
|
| 3 |
import gradio as gr
|
| 4 |
from gradio.components import Textbox, Button, Slider, Checkbox
|
| 5 |
from AinaTheme import theme
|
| 6 |
-
from
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
MAX_NEW_TOKENS = int(os.environ.get("MAX_NEW_TOKENS", default=100))
|
| 11 |
MAX_INPUT_CHARACTERS= int(os.environ.get("MAX_INPUT_CHARACTERS", default=100))
|
| 12 |
SHOW_MODEL_PARAMETERS_IN_UI = os.environ.get("SHOW_MODEL_PARAMETERS_IN_UI", default=True) == "True"
|
| 13 |
|
| 14 |
|
| 15 |
-
def submit_input(input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample,
|
| 16 |
if input_.strip() == "":
|
| 17 |
gr.Warning('Not possible to inference an empty input')
|
| 18 |
return None
|
|
|
|
| 19 |
model_parameters = {
|
| 20 |
"max_new_tokens": max_new_tokens,
|
| 21 |
"repetition_penalty": repetition_penalty,
|
| 22 |
"top_k": top_k,
|
| 23 |
"top_p": top_p,
|
| 24 |
"do_sample": do_sample,
|
| 25 |
-
"num_beams": num_beams,
|
| 26 |
"temperature": temperature
|
| 27 |
}
|
| 28 |
|
| 29 |
-
output =
|
| 30 |
-
|
| 31 |
|
| 32 |
if output is None:
|
| 33 |
gr.Warning('Inference endpoint is not available right now. Please try again later.')
|
|
@@ -42,13 +51,12 @@ def clear():
|
|
| 42 |
return (
|
| 43 |
None,
|
| 44 |
None,
|
| 45 |
-
gr.
|
| 46 |
-
gr.
|
| 47 |
-
gr.
|
| 48 |
-
gr.
|
| 49 |
-
gr.
|
| 50 |
-
gr.
|
| 51 |
-
gr.Slider.update(value=0.5),
|
| 52 |
)
|
| 53 |
|
| 54 |
def gradio_app():
|
|
@@ -117,13 +125,6 @@ def gradio_app():
|
|
| 117 |
value=True,
|
| 118 |
label="Do sample"
|
| 119 |
)
|
| 120 |
-
num_beams = Slider(
|
| 121 |
-
minimum=1,
|
| 122 |
-
maximum=8,
|
| 123 |
-
step=1,
|
| 124 |
-
value=4,
|
| 125 |
-
label="Beams"
|
| 126 |
-
)
|
| 127 |
temperature = Slider(
|
| 128 |
minimum=0,
|
| 129 |
maximum=1,
|
|
@@ -153,7 +154,7 @@ def gradio_app():
|
|
| 153 |
examples=[
|
| 154 |
["""La capital de Su猫cia"""],
|
| 155 |
],
|
| 156 |
-
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample,
|
| 157 |
outputs=output,
|
| 158 |
fn=submit_input,
|
| 159 |
)
|
|
@@ -163,7 +164,7 @@ def gradio_app():
|
|
| 163 |
examples=[
|
| 164 |
["Tradueix del Castell脿 al Catal脿 la seg眉ent frase: \"Eso es pan comido.\" \nTraducci贸:"],
|
| 165 |
],
|
| 166 |
-
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample,
|
| 167 |
outputs=output,
|
| 168 |
fn=submit_input,
|
| 169 |
)
|
|
@@ -172,7 +173,7 @@ def gradio_app():
|
|
| 172 |
examples=[
|
| 173 |
["""Oraci贸: Els sons mel貌dics produeixen una sensaci贸 de calma i benestar en l'individu. \nPar脿frasi: La m煤sica 茅s molt relaxant i reconfortant.\n----\nOraci贸: L'animal dom猫stic mostra una gran alegria i satisfacci贸. \nPar脿frasi: El gos 茅s molt feli莽. \n----\nOraci贸: El vehicle es va trencar i vaig haver de contactar amb el servei de remolc perqu猫 el transport茅s. \nPar脿frasi: El cotxe es va trencar i vaig haver de trucar la grua. \n----\nOraci贸: El professor va explicar els conceptes de manera clara i concisa. \nPar脿frasi:"""],
|
| 174 |
],
|
| 175 |
-
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample,
|
| 176 |
outputs=output,
|
| 177 |
fn=submit_input,
|
| 178 |
)
|
|
@@ -188,8 +189,8 @@ def gradio_app():
|
|
| 188 |
document.getElementById('inputlenght').style.color = (i.length > m) ? "#ef4444" : "";
|
| 189 |
}""")
|
| 190 |
|
| 191 |
-
clear_btn.click(fn=clear, inputs=[], outputs=[input_, output, max_new_tokens, repetition_penalty, top_k, top_p, do_sample,
|
| 192 |
-
submit_btn.click(fn=submit_input, inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample,
|
| 193 |
|
| 194 |
demo.launch(show_api=True)
|
| 195 |
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from gradio.components import Textbox, Button, Slider, Checkbox
|
| 5 |
from AinaTheme import theme
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
+
def generate(prompt, model_parameters):
|
| 11 |
+
output = client.text_generation(prompt, **model_parameters, return_full_text=True)
|
| 12 |
+
return output
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
client = InferenceClient(
|
| 16 |
+
os.environ.get("HF_INFERENCE_ENDPOINT_URL"),
|
| 17 |
+
token=os.environ.get("HF_INFERENCE_ENDPOINT_TOKEN")
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
MAX_NEW_TOKENS = int(os.environ.get("MAX_NEW_TOKENS", default=100))
|
| 21 |
MAX_INPUT_CHARACTERS= int(os.environ.get("MAX_INPUT_CHARACTERS", default=100))
|
| 22 |
SHOW_MODEL_PARAMETERS_IN_UI = os.environ.get("SHOW_MODEL_PARAMETERS_IN_UI", default=True) == "True"
|
| 23 |
|
| 24 |
|
| 25 |
+
def submit_input(input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature):
|
| 26 |
if input_.strip() == "":
|
| 27 |
gr.Warning('Not possible to inference an empty input')
|
| 28 |
return None
|
| 29 |
+
|
| 30 |
model_parameters = {
|
| 31 |
"max_new_tokens": max_new_tokens,
|
| 32 |
"repetition_penalty": repetition_penalty,
|
| 33 |
"top_k": top_k,
|
| 34 |
"top_p": top_p,
|
| 35 |
"do_sample": do_sample,
|
|
|
|
| 36 |
"temperature": temperature
|
| 37 |
}
|
| 38 |
|
| 39 |
+
output = generate(input_, model_parameters)
|
|
|
|
| 40 |
|
| 41 |
if output is None:
|
| 42 |
gr.Warning('Inference endpoint is not available right now. Please try again later.')
|
|
|
|
| 51 |
return (
|
| 52 |
None,
|
| 53 |
None,
|
| 54 |
+
gr.update(value=MAX_NEW_TOKENS),
|
| 55 |
+
gr.update(value=1.2),
|
| 56 |
+
gr.update(value=50),
|
| 57 |
+
gr.update(value=0.95),
|
| 58 |
+
gr.update(value=True),
|
| 59 |
+
gr.update(value=0.5),
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
def gradio_app():
|
|
|
|
| 125 |
value=True,
|
| 126 |
label="Do sample"
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
temperature = Slider(
|
| 129 |
minimum=0,
|
| 130 |
maximum=1,
|
|
|
|
| 154 |
examples=[
|
| 155 |
["""La capital de Su猫cia"""],
|
| 156 |
],
|
| 157 |
+
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature],
|
| 158 |
outputs=output,
|
| 159 |
fn=submit_input,
|
| 160 |
)
|
|
|
|
| 164 |
examples=[
|
| 165 |
["Tradueix del Castell脿 al Catal脿 la seg眉ent frase: \"Eso es pan comido.\" \nTraducci贸:"],
|
| 166 |
],
|
| 167 |
+
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature],
|
| 168 |
outputs=output,
|
| 169 |
fn=submit_input,
|
| 170 |
)
|
|
|
|
| 173 |
examples=[
|
| 174 |
["""Oraci贸: Els sons mel貌dics produeixen una sensaci贸 de calma i benestar en l'individu. \nPar脿frasi: La m煤sica 茅s molt relaxant i reconfortant.\n----\nOraci贸: L'animal dom猫stic mostra una gran alegria i satisfacci贸. \nPar脿frasi: El gos 茅s molt feli莽. \n----\nOraci贸: El vehicle es va trencar i vaig haver de contactar amb el servei de remolc perqu猫 el transport茅s. \nPar脿frasi: El cotxe es va trencar i vaig haver de trucar la grua. \n----\nOraci贸: El professor va explicar els conceptes de manera clara i concisa. \nPar脿frasi:"""],
|
| 175 |
],
|
| 176 |
+
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature],
|
| 177 |
outputs=output,
|
| 178 |
fn=submit_input,
|
| 179 |
)
|
|
|
|
| 189 |
document.getElementById('inputlenght').style.color = (i.length > m) ? "#ef4444" : "";
|
| 190 |
}""")
|
| 191 |
|
| 192 |
+
clear_btn.click(fn=clear, inputs=[], outputs=[input_, output, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature], queue=False, api_name=False)
|
| 193 |
+
submit_btn.click(fn=submit_input, inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature], outputs=[output], api_name="get-results")
|
| 194 |
|
| 195 |
demo.launch(show_api=True)
|
| 196 |
|