Spaces:
Sleeping
Sleeping
Commit
·
995cf05
1
Parent(s):
62bb4d2
Add Gradio application for Transformers and dependencies
Browse files- app.py +130 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
# Configurar logging para ver información de Transformers (opcional, pero útil para depuración)
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
| 7 |
+
|
| 8 |
+
# --- Funciones para las Pipelines de Transformers ---
|
| 9 |
+
|
| 10 |
+
def sentiment_analysis(text):
|
| 11 |
+
try:
|
| 12 |
+
classifier = pipeline("sentiment-analysis")
|
| 13 |
+
result = classifier(text)
|
| 14 |
+
return result
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return str(e)
|
| 17 |
+
|
| 18 |
+
def text_generation(prompt, max_length=50):
|
| 19 |
+
try:
|
| 20 |
+
generator = pipeline("text-generation")
|
| 21 |
+
result = generator(prompt, max_length=int(max_length), num_return_sequences=1)
|
| 22 |
+
return result[0]['generated_text'] if result else "No text generated."
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return str(e)
|
| 25 |
+
|
| 26 |
+
def zero_shot_classification(text, candidate_labels_str):
|
| 27 |
+
try:
|
| 28 |
+
candidate_labels = [label.strip() for label in candidate_labels_str.split(',')]
|
| 29 |
+
if not candidate_labels or all(not label for label in candidate_labels):
|
| 30 |
+
return "Por favor, ingresa etiquetas candidatas válidas separadas por comas."
|
| 31 |
+
classifier = pipeline("zero-shot-classification")
|
| 32 |
+
result = classifier(text, candidate_labels=candidate_labels)
|
| 33 |
+
return result
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return str(e)
|
| 36 |
+
|
| 37 |
+
def fill_mask(text_with_mask, top_k=2):
|
| 38 |
+
try:
|
| 39 |
+
unmasker = pipeline("fill-mask")
|
| 40 |
+
result = unmasker(text_with_mask, top_k=int(top_k))
|
| 41 |
+
return result
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return str(e)
|
| 44 |
+
|
| 45 |
+
def named_entity_recognition(text):
|
| 46 |
+
try:
|
| 47 |
+
ner_pipeline = pipeline("ner", grouped_entities=True)
|
| 48 |
+
result = ner_pipeline(text)
|
| 49 |
+
return result
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return str(e)
|
| 52 |
+
|
| 53 |
+
def summarization(text, min_length=30, max_length=130):
|
| 54 |
+
try:
|
| 55 |
+
summarizer = pipeline("summarization")
|
| 56 |
+
result = summarizer(text, min_length=int(min_length), max_length=int(max_length), do_sample=False)
|
| 57 |
+
return result[0]['summary_text'] if result else "No summary generated."
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return str(e)
|
| 60 |
+
|
| 61 |
+
def translation_fr_en(text):
|
| 62 |
+
try:
|
| 63 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
|
| 64 |
+
result = translator(text)
|
| 65 |
+
return result[0]['translation_text'] if result else "No translation."
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return str(e)
|
| 68 |
+
|
| 69 |
+
# --- Creación de la Interfaz Gradio ---
|
| 70 |
+
|
| 71 |
+
with gr.Blocks(title="Demo de Transformers con Gradio") as demo:
|
| 72 |
+
gr.Markdown("# Prueba varios modelos de Hugging Face Transformers")
|
| 73 |
+
gr.Markdown("Recuerda: La primera vez que ejecutes una tarea, el modelo se descargará y puede tardar.")
|
| 74 |
+
|
| 75 |
+
with gr.Tabs():
|
| 76 |
+
with gr.TabItem("Análisis de Sentimiento"):
|
| 77 |
+
with gr.Row():
|
| 78 |
+
sa_input = gr.Textbox(label="Ingresa texto para análisis de sentimiento")
|
| 79 |
+
sa_button = gr.Button("Analizar Sentimiento")
|
| 80 |
+
sa_output = gr.JSON(label="Resultado del Análisis")
|
| 81 |
+
sa_button.click(sentiment_analysis, inputs=sa_input, outputs=sa_output)
|
| 82 |
+
|
| 83 |
+
with gr.TabItem("Generación de Texto"):
|
| 84 |
+
with gr.Row():
|
| 85 |
+
tg_input_prompt = gr.Textbox(label="Ingresa un prompt para iniciar la generación")
|
| 86 |
+
tg_max_length = gr.Number(label="Longitud Máxima", value=50)
|
| 87 |
+
tg_button = gr.Button("Generar Texto")
|
| 88 |
+
tg_output = gr.Textbox(label="Texto Generado", lines=5)
|
| 89 |
+
tg_button.click(text_generation, inputs=[tg_input_prompt, tg_max_length], outputs=tg_output)
|
| 90 |
+
|
| 91 |
+
with gr.TabItem("Clasificación Zero-Shot"):
|
| 92 |
+
with gr.Row():
|
| 93 |
+
zsc_input_text = gr.Textbox(label="Texto a clasificar")
|
| 94 |
+
zsc_input_labels = gr.Textbox(label="Etiquetas candidatas (separadas por comas)", placeholder="ej: educación, política, negocios")
|
| 95 |
+
zsc_button = gr.Button("Clasificar (Zero-Shot)")
|
| 96 |
+
zsc_output = gr.JSON(label="Resultado de la Clasificación")
|
| 97 |
+
zsc_button.click(zero_shot_classification, inputs=[zsc_input_text, zsc_input_labels], outputs=zsc_output)
|
| 98 |
+
|
| 99 |
+
with gr.TabItem("Rellenar Máscara (Fill-Mask)"):
|
| 100 |
+
with gr.Row():
|
| 101 |
+
fm_input_text = gr.Textbox(label="Texto con <mask> para rellenar", placeholder="This course will teach you all about <mask> models.")
|
| 102 |
+
fm_top_k = gr.Number(label="Top K resultados", value=2)
|
| 103 |
+
fm_button = gr.Button("Rellenar Máscara")
|
| 104 |
+
fm_output = gr.JSON(label="Posibles Rellenos")
|
| 105 |
+
fm_button.click(fill_mask, inputs=[fm_input_text, fm_top_k], outputs=fm_output)
|
| 106 |
+
|
| 107 |
+
with gr.TabItem("Reconocimiento de Entidades Nombradas (NER)"):
|
| 108 |
+
ner_input = gr.Textbox(label="Texto para NER")
|
| 109 |
+
ner_button = gr.Button("Reconocer Entidades")
|
| 110 |
+
ner_output = gr.JSON(label="Entidades Reconocidas")
|
| 111 |
+
ner_button.click(named_entity_recognition, inputs=ner_input, outputs=ner_output)
|
| 112 |
+
|
| 113 |
+
with gr.TabItem("Resumen de Texto (Summarization)"):
|
| 114 |
+
with gr.Row():
|
| 115 |
+
summ_input_text = gr.TextArea(label="Texto largo para resumir", lines=7)
|
| 116 |
+
with gr.Row():
|
| 117 |
+
summ_min_len = gr.Number(label="Longitud Mínima del Resumen", value=30)
|
| 118 |
+
summ_max_len = gr.Number(label="Longitud Máxima del Resumen", value=130)
|
| 119 |
+
summ_button = gr.Button("Generar Resumen")
|
| 120 |
+
summ_output = gr.Textbox(label="Resumen Generado", lines=5)
|
| 121 |
+
summ_button.click(summarization, inputs=[summ_input_text, summ_min_len, summ_max_len], outputs=summ_output)
|
| 122 |
+
|
| 123 |
+
with gr.TabItem("Traducción (Francés a Inglés)"):
|
| 124 |
+
tr_input_text = gr.Textbox(label="Texto en Francés para traducir a Inglés", placeholder="Ce cours est produit par Hugging Face.")
|
| 125 |
+
tr_button = gr.Button("Traducir")
|
| 126 |
+
tr_output = gr.Textbox(label="Texto Traducido (Inglés)")
|
| 127 |
+
tr_button.click(translation_fr_en, inputs=tr_input_text, outputs=tr_output)
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
sentencepiece
|
| 4 |
+
gradio
|