File size: 6,331 Bytes
ad2ab25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a9d2e7
 
a463d62
 
5a9d2e7
 
ad2ab25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c96356
 
5a9d2e7
ad2ab25
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import gradio as gr
from gradio.components import Slider
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM

# Model, information and examples ----------------------------------------------
model_id = "proxectonos/FLOR-1.3B-GL"
title = "Modelo de xeración de texto FLOR-1.3B-GL"
markdown_description = """
# FLOR-1.3B-GL

🪷 **[FLOR-1.3B-GL](https://huggingface.co/proxectonos/FLOR-1.3B-GL)** is a 1.3B parameters multilingual LLM for Galician language.

👀 **Learn more about FLOR-1.3B:** [HF official model card](https://huggingface.co/proxectonos/FLOR-1.3B-GL) and the [Proxecto Nós](https://nos.gal/en/proxecto-nos).
"""

short_prompts_examples = [
    ["A receita tradicional das filloas é"], 
    ["O neno vivía preto de"]
]

few_shot_prompts_examples = [
    ["Responde á seguinte pregunta. \nPregunta: \"Cal é a capital de Noruega? \"\nResposta: \"A capital de Noruega é Oslo.\"\n---- \nResponde á seguinte pregunta.\nPregunta: \"Cal é a moeda de Portugal\" \nResposta: \"A moeda de Portugal é o euro.\" \n---- \nResponde á seguinte pregunta. \nPregunta: \"Cal é a capital de Suecia?\"\nResposta:"],
    ["Extrae as entidades nomeadas do seguinte texto: \nTexto: \"Chámome Wolfgang e vivo en Berlin\" \nEntidades: Wolfgang:PER, Berlin:LOC \n ---- \nExtrae as entidades nomeadas do seguinte texto: \nTexto: \"María e Miguel non teñen ningún problema\" \nEntidades: María:PER, Miguel:PER \n---- \nExtrae as entidades nomeadas do seguinte texto: \nTexto: \"O mellor de Barcelona é o bar do meu amigo Pablo\" \nEntidades: Pablo:PER, Barcelona:LOC \n---- \nExtrae as entidades nomeadas do seguinte texto: \nTexto: \"Carlos comparte cuarto con Marc\" \nEntidades:"]
]
fronted_theme = 'Soft'

# Model charge ---------------------------------------------------------
model_id  = "proxectonos/FLOR-1.3B-GL"
generator_model = pipeline("text-generation", model=model_id)

# Generation functions ---------------------------------------------------------
def remove_empty_lines(text):
    lines = text.strip().split("\n")
    non_empty_lines = [line for line in lines if line.strip()]
    return "\n".join(non_empty_lines)

def predict(prompt, max_length, repetition_penalty=1.3):
    print("Dentro da xeración...")
    prompt_length = len(generator_model.tokenizer.encode(prompt))
    generated_text = generator_model(
        prompt,
        max_length=prompt_length + max_length, 
        pad_token_id=generator_model.tokenizer.eos_token_id, 
        repetition_penalty=repetition_penalty)

    generated_sequence = generated_text[0]['generated_text']
    if generated_sequence is  None:
        gr.Warning('Inference endpoint is not available right now. Please try again later.')
        return
    
    generated_sequence = remove_empty_lines(generated_sequence)
    print("Xeración completada")
    return generated_sequence

# Gradio app ---------------------------------------------------------
def clear(): 
    return (
        None, 
        None,
        gr.update(value=20),
        gr.update(value=1.3)
    )
def pass_to_input(generated_gl):
    return (
        gr.update(value=generated_gl),
        None,
    )

def fewshot_prompt_parameters():
    return (
        gr.update(value=20), # max_length
        gr.update(value=1) # repetition_penalty
    )

def gradio_app():
    with gr.Blocks(theme=fronted_theme) as demo:
        with gr.Row():
            with gr.Column(scale=0.1):
                gr.HTML('<img src="https://huggingface.co/spaces/proxectonos/README/resolve/main/title-card.png" width="100%" style="border-radius: 0.75rem;">')
            with gr.Column():
                gr.Markdown(markdown_description)

        with gr.Row(equal_height=True):
            with gr.Column():
                text_gl = gr.Textbox(label="Input", 
                                     lines=6, placeholder="e.g. O neno vai a escola con ")
                with gr.Row(variant="panel"):
                        with gr.Accordion("Model parameters", open=False):
                            max_length = Slider(
                                minimum=1,
                                maximum=200,
                                step=1,
                                value=30,
                                label="Max tokens"
                            )
                            repetition_penalty = Slider(
                                minimum=0.1,
                                maximum=4,
                                step=0.1,
                                value=1.3,
                                label="Repetition penalty"
                            )
                generator_btn = gr.Button(value="Generate",variant='primary')
            with gr.Column():
                generated_gl = gr.Textbox(label="Output", 
                                          lines=6, 
                                          placeholder="Generated text will appear here",
                                          interactive=False,
                                          show_copy_button=True)
                pass_btn = gr.Button(value="Pass text to input")
                clean_btn = gr.Button(value="Clean")

        generator_btn.click(predict, inputs=[text_gl,max_length, repetition_penalty], outputs=generated_gl, api_name="generate-flor-gl")
        clean_btn.click(fn=clear, inputs=[], outputs=[text_gl, generated_gl, max_length, repetition_penalty], queue=False, api_name=False)
        pass_btn.click(fn=pass_to_input, inputs=[generated_gl], outputs=[text_gl,generated_gl], queue=False, api_name=False)
        
        with gr.Row():
            with gr.Column(scale=0.5):
                gr.Examples(
                    label = "Short prompts",
                    examples = short_prompts_examples,
                    inputs = [text_gl,max_length, repetition_penalty],
                    outputs = generated_gl,
                    fn =predict
                )
                gr.Examples(
                    label = "Few-shot prompts",
                    examples = few_shot_prompts_examples,
                    inputs = [],
                    outputs = [max_length, repetition_penalty],
                    fn =fewshot_prompt_parameters
                )

    demo.launch()

if __name__ == "__main__":
    gradio_app()