import os
from dotenv import load_dotenv
import gradio as gr
from gradio.components import Textbox, Button, Slider, Checkbox
from AinaTheme import theme
from sagemaker_endpoint import invoke_endpoint
load_dotenv()
MAX_NEW_TOKENS = int(os.environ.get("MAX_NEW_TOKENS", default=100))
MAX_INPUT_CHARACTERS= int(os.environ.get("MAX_INPUT_CHARACTERS", default=100))
SHOW_MODEL_PARAMETERS_IN_UI = os.environ.get("SHOW_MODEL_PARAMETERS_IN_UI", default=True) == "True"
def submit_input(input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, num_beams, temperature):
if input_.strip() == "":
gr.Warning('Not possible to inference an empty input')
return None
model_parameters = {
"max_new_tokens": max_new_tokens,
"repetition_penalty": repetition_penalty,
"top_k": top_k,
"top_p": top_p,
"do_sample": do_sample,
"num_beams": num_beams,
"temperature": temperature
}
output = invoke_endpoint(input_, model_parameters=model_parameters)
if output is None:
gr.Warning('Inference endpoint is not available right now. Please try again later.')
return output
def change_interactive(text):
if len(text.strip()) > MAX_INPUT_CHARACTERS:
return gr.update(interactive = True), gr.update(interactive = False)
return gr.update(interactive = True), gr.update(interactive = True)
def clear():
return (
None,
None,
gr.Slider.update(value=100),
gr.Slider.update(value=1.2),
gr.Slider.update(value=50),
gr.Slider.update(value=0.95),
gr.Checkbox.update(value=True),
gr.Slider.update(value=4),
gr.Slider.update(value=0.5),
)
def gradio_app():
with gr.Blocks(theme=theme) as demo:
with gr.Row():
with gr.Column(scale=0.1):
gr.Image("ginesta_small.jpg", elem_id="flor-banner", scale=1, height=256, width=256, show_label=False, show_download_button = False, show_share_button = False)
with gr.Column():
gr.Markdown(
"""# Flor-6.3B (experimental)
馃 **[Flor](https://huggingface.co/projecte-aina/FLOR-6.3B)** is a 6.3B parameters multilingual LLM that has been trained on a massive mixture of Spanish, Catalan and English data. It is a new open-source Large Language Model (LLM), licensed for both research and commercial use. It uses the [Bloom-7b](https://huggingface.co/bigscience/bloom-7b1) model as a starting point, a state-of-the-art multilingual language model.
鈿狅笍 **Limitations**: This version is for beta testing only. The content generated by these models is unsupervised and might be judged as inappropriate or offensive. Please bear this in mind when exploring this resource.
馃憖 **Learn more about Flor:** [HF official model card](https://huggingface.co/projecte-aina/FLOR-6.3B) and the [Instruct version](https://huggingface.co/projecte-aina/FLOR_63B_Instruit).
"""
)
with gr.Row(equal_height=True):
with gr.Column(variant="panel"):
placeholder_max_token = Textbox(
visible=False,
interactive=False,
value= MAX_INPUT_CHARACTERS
)
input_ = Textbox(
lines=11,
label="Input",
placeholder="e.g. El mercat del barri 茅s fant脿stic hi pots trobar."
)
with gr.Row(variant="panel", equal_height=True):
gr.HTML("""""")
gr.HTML(f""" 0 / {MAX_INPUT_CHARACTERS}""")
with gr.Row(variant="panel"):
with gr.Accordion("Model parameters", open=False, visible=SHOW_MODEL_PARAMETERS_IN_UI):
max_new_tokens = Slider(
minimum=1,
maximum=200,
step=1,
value=MAX_NEW_TOKENS,
label="Max tokens"
)
repetition_penalty = Slider(
minimum=0.1,
maximum=10,
step=0.1,
value=1.2,
label="Repetition penalty"
)
top_k = Slider(
minimum=1,
maximum=100,
step=1,
value=50,
label="Top k"
)
top_p = Slider(
minimum=0.01,
maximum=0.99,
value=0.95,
label="Top p"
)
do_sample = Checkbox(
value=True,
label="Do sample"
)
num_beams = Slider(
minimum=1,
maximum=8,
step=1,
value=4,
label="Beams"
)
temperature = Slider(
minimum=0,
maximum=1,
value=0.5,
label="Temperature"
)
with gr.Column(variant="panel"):
output = Textbox(
lines=11,
label="Output",
interactive=False,
show_copy_button=True
)
with gr.Row(variant="panel"):
clear_btn = Button(
"Clear",
)
submit_btn = Button(
"Submit",
variant="primary",
)
with gr.Row():
with gr.Column(scale=0.5):
gr.Examples(
label="Short prompts:",
examples=[
["""La capital de Su猫cia"""],
],
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, num_beams, temperature],
outputs=output,
fn=submit_input,
)
gr.Examples(
label="Zero-shot prompts",
examples=[
["Tradueix del Castell脿 al Catal脿 la seg眉ent frase: \"Eso es pan comido.\" \nTraducci贸:"],
],
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, num_beams, temperature],
outputs=output,
fn=submit_input,
)
gr.Examples(
label="Few-Shot prompts:",
examples=[
["""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:"""],
],
inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, num_beams, temperature],
outputs=output,
fn=submit_input,
)
input_.change(fn=change_interactive, inputs=[input_], outputs=[clear_btn, submit_btn], api_name=False)
input_.change(fn=None, inputs=[input_], api_name=False, js=f"""(i) => document.getElementById('countertext').textContent = i.length > {MAX_INPUT_CHARACTERS} && 'Max length {MAX_INPUT_CHARACTERS} characters. ' || '' """)
input_.change(fn=None, inputs=[input_, placeholder_max_token], api_name=False, js="""(i, m) => {
document.getElementById('inputlenght').textContent = i.length + ' '
document.getElementById('inputlenght').style.color = (i.length > m) ? "#ef4444" : "";
}""")
clear_btn.click(fn=clear, inputs=[], outputs=[input_, output, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, num_beams, temperature], queue=False, api_name=False)
submit_btn.click(fn=submit_input, inputs=[input_, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, num_beams, temperature], outputs=[output], api_name="get-results")
demo.launch(show_api=True)
if __name__ == "__main__":
gradio_app()