Spaces:
Running
Running
Initial commit
Browse files- README.md +4 -4
- app.py +222 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title: LLMs
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.48.0
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Small Galician LLMs
|
| 3 |
+
emoji: 🍂
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.48.0
|
| 8 |
app_file: app.py
|
app.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from gradio.components import Slider
|
| 4 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Model, information and examples ----------------------------------------------
|
| 8 |
+
MODEL_NAMES = ["Carballo-bloom-1.3B","Carvalho_pt-gl","Carballo-cerebras-1.3B"]
|
| 9 |
+
markdown_description_gl = """
|
| 10 |
+
[*English below*]
|
| 11 |
+
|
| 12 |
+
Este espazo contén diferentes Grandes Modelos da Linguaxe feitos para o galego desenvolvidos polo [Proxecto Nós](https://nos.gal/en/proxecto-nos).
|
| 13 |
+
|
| 14 |
+
💐 **[Carballo-bloom-1.3B](https://huggingface.co/proxectonos/Carballo-bloom-1.3B)** é un modelo de 1.3B parámetros que é un preadestramento continuo de [FLOR-1.3B]( https://huggingface.co/projecte-aina/FLOR-1.3B), baseado a súa vez en [Bloom 1.7B](https://huggingface.co/bigscience/bloom-1b7).
|
| 15 |
+
|
| 16 |
+
👀 **Máis información sobre Carballo-bloom-1.3B:** [tarxeta modelo oficial HF](https://huggingface.co/proxectonos/Carballo-bloom-1.3B).
|
| 17 |
+
|
| 18 |
+
🧠 **[Carballo-cerebras-1.3B](https://huggingface.co/proxectonos/Carballo-cerebras-1.3B)** é un modelo de 1.3B parámetros baseado en [Cerebras-GPT 1.3B](https://huggingface.co/cerebras/Cerebras-GPT-1.3B).
|
| 19 |
+
|
| 20 |
+
👀 **Máis información sobre Carballo-cerebras-1.3B:** [tarxeta modelo oficial HF](https://huggingface.co/proxectonos/Carballo-cerebras-1.3B)
|
| 21 |
+
|
| 22 |
+
🧠 **[Carvalho_pt-gl](https://huggingface.co/Nos-PT/Carvalho_pt-gl-1.3B)** é un modelo de 1.3B parámetros baseado en [Cerebras-GPT 1.3B](https://huggingface.co/cerebras/Cerebras-GPT-1.3B) que pode traballar en galego e portugués, feito en colaboración co [VISTA Lab](https://huggingface.co/cerebras/Cerebras-GPT-1.3B) that can work in Galician and Portuguese, developed in collaboration with the [VISTA Lab](https://www.uevora.pt/en/research/R-D-Unit/vista-lab) da Universidade de Évora.
|
| 23 |
+
|
| 24 |
+
👀 **Máis información sobre Carvalho_pt-gl:** [tarxeta modelo oficial HF](https://huggingface.co/Nos-PT/Carvalho_pt-gl-1.3B)
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
markdown_description_en = """
|
| 28 |
+
## English description
|
| 29 |
+
This space contains the Galician language models developed by [Proxecto Nós](https://nos.gal/en/proxecto-nos).
|
| 30 |
+
|
| 31 |
+
💐 **[Carballo-bloom-1.3B](https://huggingface.co/proxectonos/Carballo-bloom-1.3B)** is a 1.3B parameters model which is a Continual pretraining from [FLOR-1.3B](https://huggingface.co/projecte-aina/FLOR-1.3B), which is based in [Bloom 1.7B](https://huggingface.co/bigscience/bloom-1b7).
|
| 32 |
+
|
| 33 |
+
👀 **Learn more about Carballo-bloom-1.3B:** [HF official model card](https://huggingface.co/proxectonos/Carballo-bloom-1.3B).
|
| 34 |
+
|
| 35 |
+
🧠 **[Carballo-cerebras-1.3B](https://huggingface.co/proxectonos/Carballo-cerebras-1.3B)** is a 1.3B parameters model based in [Cerebras-GPT 1.3B](https://huggingface.co/cerebras/Cerebras-GPT-1.3B).
|
| 36 |
+
|
| 37 |
+
👀 **Learn more about Carballo-cerebras-1.3B:** [HF official model card](https://huggingface.co/proxectonos/Carballo-cerebras-1.3B)
|
| 38 |
+
|
| 39 |
+
📖 **[Carvalho_pt-gl](https://huggingface.co/Nos-PT/Carvalho_pt-gl-1.3B)** is a 1.3B parameters model based in [Cerebras-GPT 1.3B](https://huggingface.co/cerebras/Cerebras-GPT-1.3B) that can work in Galician and Portuguese, developed in collaboration with the [VISTA Lab](https://www.uevora.pt/en/research/R-D-Unit/vista-lab) at the University of Évora.
|
| 40 |
+
|
| 41 |
+
👀 **More information about Carvalho_pt-gl:** [official HF model card](https://huggingface.co/Nos-PT/Carvalho_pt-gl-1.3B)
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
short_prompts_examples = [
|
| 45 |
+
["A receita tradicional das filloas é"],
|
| 46 |
+
["O neno vivía preto de"]
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
few_shot_prompts_examples = [
|
| 50 |
+
["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:"],
|
| 51 |
+
["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:"],
|
| 52 |
+
["Cualifica como Positivo ou Negativo o sentimento da seguinte frase:\n Texto: \"Estou moi feliz\"\n Polaridade: Positivo\n ---- \n Cualifica como Positivo ou Negativo o sentimento da seguinte frase:\n Texto: \"Non me gusta beber cervexa\"\n Polaridade: Negativo\n ----\n Cualifica como Positivo ou Negativo o sentimento da seguinte frase:\n Texto: \"O meu pai detesta o seu traballo\"\n Polaridade: Negativo\n ----\n Cualifica como Positivo ou Negativo o sentimento da seguinte frase:\n Texto: \"Uxía desfruta xogando ao fútbol\"\n Polaridade: Positivo\n ----\n Cualifica como Positivo ou Negativo o sentimento da seguinte frase:\n Texto: \"O neno non está contento coas notas\"\n Polaridade:"],
|
| 53 |
+
["Traduce ao galego esta frase en inglés:\n Inglés: \"my sister is studying Biology at the university.\"\n Galego: \"a miña irmá está a estudar bioloxía na universidade.\"\n ---- \nTraduce ao galego esta frase en inglés:\n Inglés: \"You are working with my mother on a very interesting project.\"\n Galego: \"Estás a traballar coa miña nai nun proxecto moi interesante\"\n ---- \n Traduce ao galego esta frase en inglés:\n Inglés: \"You have to fix the computer now\"\n Galego:"]
|
| 54 |
+
]
|
| 55 |
+
fronted_theme = 'Soft'
|
| 56 |
+
|
| 57 |
+
# Model charge ---------------------------------------------------------
|
| 58 |
+
model_id_bloom = "proxectonos/Carballo-bloom-1.3B"
|
| 59 |
+
model_id_carvalho = "Nos-PT/Carvalho_pt-gl-1.3B"
|
| 60 |
+
model_id_cerebras = "proxectonos/Carballo-cerebras-1.3B"
|
| 61 |
+
|
| 62 |
+
generator_model_gl = pipeline("text-generation", model=model_id_bloom)
|
| 63 |
+
generator_model_pt = pipeline("text-generation", model=model_id_carvalho)
|
| 64 |
+
|
| 65 |
+
# Generation functions ---------------------------------------------------------
|
| 66 |
+
def get_model(model_selection):
|
| 67 |
+
global generator_model_gl
|
| 68 |
+
if model_selection == "Carballo-bloom-1.3B":
|
| 69 |
+
if generator_model_gl.model.name_or_path != model_id_bloom:
|
| 70 |
+
generator_model_gl = pipeline("text-generation", model=model_id_bloom)
|
| 71 |
+
return generator_model_gl
|
| 72 |
+
elif model_selection == "Carballo-cerebras-1.3B":
|
| 73 |
+
if generator_model_gl.model.name_or_path != model_id_cerebras:
|
| 74 |
+
generator_model_gl = pipeline("text-generation", model=model_id_cerebras)
|
| 75 |
+
return generator_model_gl
|
| 76 |
+
else:
|
| 77 |
+
return generator_model_pt
|
| 78 |
+
|
| 79 |
+
def remove_empty_lines(text):
|
| 80 |
+
lines = text.strip().split("\n")
|
| 81 |
+
non_empty_lines = [line for line in lines if line.strip()]
|
| 82 |
+
return "\n".join(non_empty_lines)
|
| 83 |
+
|
| 84 |
+
def predict(prompt, model_select, max_length, repetition_penalty, temperature):
|
| 85 |
+
print("Dentro da xeración...")
|
| 86 |
+
generator_model = get_model(model_select)
|
| 87 |
+
prompt_length = len(generator_model.tokenizer.encode(prompt))
|
| 88 |
+
generated_text = generator_model(
|
| 89 |
+
prompt,
|
| 90 |
+
max_length=prompt_length + max_length,
|
| 91 |
+
pad_token_id=generator_model.tokenizer.eos_token_id,
|
| 92 |
+
repetition_penalty=repetition_penalty,
|
| 93 |
+
temperature=temperature,
|
| 94 |
+
do_sample=True)
|
| 95 |
+
|
| 96 |
+
generated_sequence = generated_text[0]['generated_text']
|
| 97 |
+
if generated_sequence is None:
|
| 98 |
+
gr.Warning('Inference endpoint is not available right now. Please try again later.')
|
| 99 |
+
return
|
| 100 |
+
|
| 101 |
+
generated_sequence = remove_empty_lines(generated_sequence)
|
| 102 |
+
print("Xeración completada")
|
| 103 |
+
return generated_sequence
|
| 104 |
+
|
| 105 |
+
# Gradio app ---------------------------------------------------------
|
| 106 |
+
def clear():
|
| 107 |
+
return (
|
| 108 |
+
None,
|
| 109 |
+
None,
|
| 110 |
+
gr.update(value=20),
|
| 111 |
+
gr.update(value=1.3),
|
| 112 |
+
gr.update(value=0.5)
|
| 113 |
+
)
|
| 114 |
+
def pass_to_input(generated_gl):
|
| 115 |
+
return (
|
| 116 |
+
gr.update(value=generated_gl),
|
| 117 |
+
None
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
def parameters_default(text):
|
| 121 |
+
return (
|
| 122 |
+
gr.update(value=30), # max_length
|
| 123 |
+
gr.update(value=1.3), # repetition_penalty
|
| 124 |
+
gr.update(value=0.5) # temperature
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
def parameters_fewshot_prompt(text):
|
| 128 |
+
return (
|
| 129 |
+
gr.update(value=15), # max_length
|
| 130 |
+
gr.update(value=1), # repetition_penalty
|
| 131 |
+
gr.update(value=0.5) # temperature
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
def gradio_app():
|
| 135 |
+
with gr.Blocks(theme=fronted_theme) as demo:
|
| 136 |
+
with gr.Row():
|
| 137 |
+
gr.Markdown(
|
| 138 |
+
"""
|
| 139 |
+
## 🍂 Grandes Modelos da Linguaxe en galego / Galician Language Models
|
| 140 |
+
"""
|
| 141 |
+
)
|
| 142 |
+
with gr.Row():
|
| 143 |
+
with gr.Column():
|
| 144 |
+
gr.Markdown(
|
| 145 |
+
"""
|
| 146 |
+
## <img src="https://huggingface.co/spaces/proxectonos/README/resolve/main/title-card.png" width="100%" style="border-radius: 0.75rem;">
|
| 147 |
+
"""
|
| 148 |
+
)
|
| 149 |
+
with gr.Column():
|
| 150 |
+
gr.Markdown(markdown_description_gl)
|
| 151 |
+
with gr.Row(equal_height=True):
|
| 152 |
+
model_select = gr.Dropdown(
|
| 153 |
+
label="Selecione un modelo / Select a model",
|
| 154 |
+
choices=MODEL_NAMES,
|
| 155 |
+
value=MODEL_NAMES[0],
|
| 156 |
+
interactive=True
|
| 157 |
+
)
|
| 158 |
+
with gr.Row(equal_height=True):
|
| 159 |
+
with gr.Column():
|
| 160 |
+
text_gl = gr.Textbox(label="Entrada / Input",
|
| 161 |
+
lines=6, placeholder="e.g. O neno vai a escola con ")
|
| 162 |
+
with gr.Row(variant="panel"):
|
| 163 |
+
with gr.Accordion("Parámetros do modelo / Model parameters", open=False):
|
| 164 |
+
max_length = Slider(
|
| 165 |
+
minimum=1,
|
| 166 |
+
maximum=200,
|
| 167 |
+
step=1,
|
| 168 |
+
value=30,
|
| 169 |
+
label="Max tokens"
|
| 170 |
+
)
|
| 171 |
+
repetition_penalty = Slider(
|
| 172 |
+
minimum=0.1,
|
| 173 |
+
maximum=4,
|
| 174 |
+
step=0.1,
|
| 175 |
+
value=1.3,
|
| 176 |
+
label="Penalización por repetición / Repetition penalty"
|
| 177 |
+
)
|
| 178 |
+
temperature = Slider(
|
| 179 |
+
minimum=0,
|
| 180 |
+
maximum=1,
|
| 181 |
+
value=0.5,
|
| 182 |
+
label="Temperatura / Temperature"
|
| 183 |
+
)
|
| 184 |
+
generator_btn = gr.Button(value="Xerar / Generate",variant='primary')
|
| 185 |
+
with gr.Column():
|
| 186 |
+
generated_gl = gr.Textbox(label="Saída / Output",
|
| 187 |
+
lines=6,
|
| 188 |
+
placeholder="O texto xerado aparecerá aquí...",
|
| 189 |
+
interactive=False,
|
| 190 |
+
show_copy_button=True)
|
| 191 |
+
pass_btn = gr.Button(value="Pasar texto xerado á entrada / Pass generated text to input",variant='secondary')
|
| 192 |
+
clean_btn = gr.Button(value="Limpar / Clear",variant='secondary')
|
| 193 |
+
|
| 194 |
+
generator_btn.click(predict, inputs=[text_gl, model_select, max_length, repetition_penalty, temperature], outputs=generated_gl, api_name="generate-flor-gl")
|
| 195 |
+
clean_btn.click(fn=clear, inputs=[], outputs=[text_gl, generated_gl, max_length, repetition_penalty, temperature], queue=False, api_name=False)
|
| 196 |
+
pass_btn.click(fn=pass_to_input, inputs=[generated_gl], outputs=[text_gl,generated_gl], queue=False, api_name=False)
|
| 197 |
+
|
| 198 |
+
with gr.Row():
|
| 199 |
+
with gr.Column(scale=0.5):
|
| 200 |
+
gr.Examples(
|
| 201 |
+
label = "Prompts curtos / Short prompts",
|
| 202 |
+
examples = short_prompts_examples,
|
| 203 |
+
inputs = [text_gl],
|
| 204 |
+
outputs = [max_length, repetition_penalty, temperature],
|
| 205 |
+
fn = parameters_default,
|
| 206 |
+
run_on_click = True
|
| 207 |
+
)
|
| 208 |
+
gr.Examples(
|
| 209 |
+
label = "Prompts con poucos exemplos / Few-shot prompts",
|
| 210 |
+
examples = few_shot_prompts_examples,
|
| 211 |
+
inputs = [text_gl],
|
| 212 |
+
outputs = [max_length, repetition_penalty, temperature],
|
| 213 |
+
fn = parameters_fewshot_prompt,
|
| 214 |
+
run_on_click = True
|
| 215 |
+
)
|
| 216 |
+
with gr.Row():
|
| 217 |
+
gr.Markdown(markdown_description_en)
|
| 218 |
+
|
| 219 |
+
demo.launch()
|
| 220 |
+
|
| 221 |
+
if __name__ == "__main__":
|
| 222 |
+
gradio_app()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
accelerate
|