Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
|
@@ -57,13 +87,41 @@ chatbot = gr.ChatInterface(
|
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
title = "Generador y clasificador de coloquialismos cubanos"
|
| 6 |
+
description = "Genera texto con el dialecto cubano y detecta expresiones coloquiales cubanas."
|
| 7 |
|
| 8 |
+
article = """
|
| 9 |
+
## Modelo:
|
| 10 |
+
Se usa el somosnlp-hackathon-2026/patriae-cuban-dialect-model.
|
| 11 |
+
|
| 12 |
+
## Fine-tuning:
|
| 13 |
+
Se utiliz贸 el dataset somosnlp-hackathon-2026/patriae-cuba-literature-dataset para hacer fine-tuning del modelo gia-uh/cecilia-2b-instruct-v1.
|
| 14 |
+
|
| 15 |
+
## Participantes del equipo Patriae en SomosNLP 2026:
|
| 16 |
+
* **Carlos Luis Barn茅s Infante (https://huggingface.co/blacknoize404)**
|
| 17 |
+
* **Yisel Clavel Quintero (https://huggingface.co/clavel)**
|
| 18 |
+
* **Dionis Lopez (https://huggingface.co/inoid)**
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
# Cargar el modelo para clasificaci贸n
|
| 22 |
+
classifier = pipeline(
|
| 23 |
+
"text-classification",
|
| 24 |
+
model='somosnlp-hackathon-2026/patriae-cuban-dialect-model'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 28 |
"""
|
| 29 |
+
Funci贸n para generar respuestas usando el modelo de chat
|
| 30 |
"""
|
| 31 |
+
# Usar el mismo modelo para generaci贸n (si es compatible)
|
| 32 |
+
# Nota: Si el modelo no soporta chat, necesitas usar un endpoint diferente
|
| 33 |
+
client = InferenceClient(
|
| 34 |
+
model="somosnlp-hackathon-2026/patriae-cuban-dialect-model"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 38 |
messages.extend(history)
|
|
|
|
| 39 |
messages.append({"role": "user", "content": message})
|
| 40 |
+
|
| 41 |
response = ""
|
| 42 |
+
try:
|
| 43 |
+
for message in client.chat_completion(
|
| 44 |
+
messages,
|
| 45 |
+
max_tokens=max_tokens,
|
| 46 |
+
stream=True,
|
| 47 |
+
temperature=temperature,
|
| 48 |
+
top_p=top_p,
|
| 49 |
+
):
|
| 50 |
+
if message.choices and message.choices[0].delta.content:
|
| 51 |
+
token = message.choices[0].delta.content
|
| 52 |
+
response += token
|
| 53 |
+
yield response
|
| 54 |
+
except Exception as e:
|
| 55 |
+
yield f"Error: {str(e)}"
|
| 56 |
|
| 57 |
+
def classify_text(text):
|
| 58 |
+
"""
|
| 59 |
+
Funci贸n para clasificar texto y detectar coloquialismos cubanos
|
| 60 |
+
"""
|
| 61 |
+
if not text or text.strip() == "":
|
| 62 |
+
return "Por favor, ingresa un texto para clasificar."
|
| 63 |
+
|
| 64 |
+
results = classifier(text)
|
| 65 |
+
# Ordenar por score (de mayor a menor)
|
| 66 |
+
results.sort(key=lambda x: x['score'], reverse=True)
|
| 67 |
+
|
| 68 |
+
# Mostrar el resultado m谩s probable
|
| 69 |
+
top_result = results[0]
|
| 70 |
+
return f"**Clasificaci贸n:** {top_result['label']}\n**Confianza:** {top_result['score']:.2%}"
|
|
|
|
| 71 |
|
| 72 |
+
# Interfaz de Chat
|
| 73 |
+
chat_interface = gr.ChatInterface(
|
|
|
|
|
|
|
| 74 |
respond,
|
| 75 |
additional_inputs=[
|
| 76 |
+
gr.Textbox(
|
| 77 |
+
value="Eres un chatbot cubano que ayuda a entender y usar el dialecto cubano.",
|
| 78 |
+
label="System message"
|
| 79 |
+
),
|
| 80 |
+
gr.Slider(minimum=1, maximum=1024, value=150, step=1, label="Max new tokens"),
|
| 81 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 82 |
gr.Slider(
|
| 83 |
minimum=0.1,
|
|
|
|
| 87 |
label="Top-p (nucleus sampling)",
|
| 88 |
),
|
| 89 |
],
|
| 90 |
+
title=title,
|
| 91 |
+
description=description,
|
| 92 |
+
article=article
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Interfaz de Clasificaci贸n
|
| 96 |
+
classifier_interface = gr.Interface(
|
| 97 |
+
fn=classify_text,
|
| 98 |
+
inputs=gr.Textbox(
|
| 99 |
+
label="Texto a clasificar",
|
| 100 |
+
placeholder="Escribe una frase en cubano...",
|
| 101 |
+
lines=3
|
| 102 |
+
),
|
| 103 |
+
outputs=gr.Markdown(label="Resultado"),
|
| 104 |
+
title="Clasificador de coloquialismos cubanos",
|
| 105 |
+
description="Detecta si un texto contiene expresiones coloquiales cubanas.",
|
| 106 |
+
examples=[
|
| 107 |
+
["Nos march谩bamos sin ti, creyendo que se te hab铆an pegado las s谩banas."],
|
| 108 |
+
["De Varadero ven铆a para ac谩 porque en su casa las broncas con el padre eran de ampanga."],
|
| 109 |
+
["Los pueblos que no se conocen han de darse prisa para conocerse, como quienes van a pelear juntos."],
|
| 110 |
+
]
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
# Combinar ambas interfaces en una sola app
|
| 114 |
+
with gr.Blocks() as demo:
|
| 115 |
+
gr.Markdown(f"# {title}")
|
| 116 |
+
gr.Markdown(description)
|
| 117 |
+
|
| 118 |
+
with gr.Tab("馃挰 Chat"):
|
| 119 |
+
chat_interface.render()
|
| 120 |
+
|
| 121 |
+
with gr.Tab("馃攳 Clasificador"):
|
| 122 |
+
classifier_interface.render()
|
| 123 |
+
|
| 124 |
+
gr.Markdown(article)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
+
demo.launch()
|