Alberto Caro commited on
Commit
e0247e5
·
1 Parent(s): 632f27a

Ejercicio difusion

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. .gradio/flagged/dataset1.csv +5 -0
  3. app.py +15 -3
  4. app2.py +29 -0
  5. app3.py +42 -0
  6. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv
.gradio/flagged/dataset1.csv ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ texto,output,timestamp
2
+ Oye eso si que no me gusta!,Tu frase es muy negativa,2025-04-21 17:42:20.806521
3
+ Oye eso si que no me gusta!,Tu frase es muy negativa,2025-04-21 17:42:33.246055
4
+ Oye eso si que no me gusta!,Tu frase es muy negativa,2025-04-21 17:42:41.177429
5
+ Oye eso si que no me gusta!,Tu frase es muy negativa,2025-04-21 17:43:11.865918
app.py CHANGED
@@ -1,7 +1,19 @@
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
7
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ clasificador = pipeline("sentiment-analysis", model="pysentimiento/robertuito-sentiment-analysis")
 
5
 
6
+ def puntuacion_sentimientos(texto):
7
+ resultado = clasificador(texto)
8
+ print(resultado)
9
+ etiqueta = resultado[0]["label"]
10
+ if etiqueta == "POS":
11
+ respuesta = "Tu frase es muy positiva"
12
+ elif etiqueta == "NEG":
13
+ respuesta = "Tu frase es muy negativa"
14
+ else:
15
+ respuesta = "ni fu ni fa"
16
+ return respuesta
17
+
18
+ demo = gr.Interface(fn=puntuacion_sentimientos, inputs="text", outputs="text")
19
  demo.launch()
app2.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Cargar el modelo de clasificación de género literario
5
+ clasificador = pipeline("text-classification", model="TheBritishLibrary/bl-books-genre")
6
+
7
+ # Función para predecir el género del texto
8
+ def clasificar_genero(texto):
9
+ resultado = clasificador(texto)
10
+ print(resultado)
11
+ etiqueta = resultado[0]["label"]
12
+ puntuacion = resultado[0]["score"]
13
+ respuesta = f"Género predicho: {etiqueta} (confianza: {puntuacion:.2f})"
14
+ return respuesta
15
+
16
+ # Interfaz Gradio con personalización
17
+ demo = gr.Interface(
18
+ fn=clasificar_genero,
19
+ inputs=gr.Textbox(placeholder="Escribe tu Frase", label="Frase para Analizar"),
20
+ outputs=gr.Textbox(label="Género del Texto"),
21
+ title="Ejemplo Clasificación de Género Literario",
22
+ description="Esta es nuestra interfaz para probar <strong>modelos de IA</strong> entrenados en textos literarios.",
23
+ article="En este modelo, escribe una frase o párrafo y pulsa en <strong>¡Clasificar!</strong> para ver el género literario estimado.",
24
+ theme="soft",
25
+ submit_btn="¡Clasificar!",
26
+ fill_width=True
27
+ )
28
+
29
+ demo.launch()
app3.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from diffusers import StableDiffusionPipeline
4
+ import torch
5
+
6
+ # Clasificador de texto (transformers)
7
+ clasificador = pipeline("text-classification", model="TheBritishLibrary/bl-books-genre")
8
+
9
+ # Generador de imágenes (diffusers)
10
+ diff_pipe = StableDiffusionPipeline.from_pretrained(
11
+ "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
12
+ ).to("cuda")
13
+
14
+ # Lógica
15
+ def analizar_y_generar(texto):
16
+ resultado = clasificador(texto)
17
+ genero = resultado[0]["label"]
18
+ confianza = resultado[0]["score"]
19
+ prompt_img = f"A dreamy illustration representing the literary genre: {genero.lower()}"
20
+
21
+ imagen = diff_pipe(prompt_img).images[0]
22
+ texto_genero = f"Género: {genero} (confianza: {confianza:.2f})"
23
+
24
+ return texto_genero, imagen
25
+
26
+ # Interfaz con diseño mejorado
27
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
28
+ gr.Markdown("# 📚 Clasificador de Género + Generador de Imagen")
29
+ gr.Markdown("Introduce un fragmento de texto para clasificar su género y generar una imagen relacionada.")
30
+
31
+ with gr.Row():
32
+ entrada = gr.Textbox(lines=4, label="Texto del Libro", placeholder="Introduce aquí tu fragmento...")
33
+
34
+ boton = gr.Button("Analizar y Generar Imagen 🎨")
35
+
36
+ with gr.Row():
37
+ salida_texto = gr.Textbox(label="Resultado del Modelo")
38
+ salida_img = gr.Image(label="Imagen Generada")
39
+
40
+ boton.click(fn=analizar_y_generar, inputs=entrada, outputs=[salida_texto, salida_img])
41
+
42
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==5.20.0
2
+ transformers==4.49.0
3
+ torch==2.6.0
4
+ diffusers==0.32.2
5
+ accelerate==1.5.2
6
+ pydantic==2.10.6