Spaces:
Sleeping
Sleeping
Commit
·
e6e702e
1
Parent(s):
978194b
Compatibilidad Gradio 3.0
Browse files- app.py +55 -49
- requirements.txt +2 -5
app.py
CHANGED
@@ -1,69 +1,75 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
model="nlptown/bert-base-multilingual-uncased-sentiment")
|
11 |
-
|
12 |
-
# Función para analizar texto
|
13 |
-
def analyze_text_sentiment(text):
|
14 |
-
result = sentiment_analysis(text)[0]
|
15 |
-
score = result['score']
|
16 |
label = result['label']
|
17 |
-
|
18 |
-
|
|
|
19 |
if label == "1 star":
|
20 |
-
|
21 |
-
literal = "Muy negativo"
|
22 |
elif label == "2 stars":
|
23 |
-
|
24 |
-
literal = "Negativo"
|
25 |
elif label == "3 stars":
|
26 |
-
|
27 |
-
literal = "Neutro"
|
28 |
elif label == "4 stars":
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
slider_value = 100
|
33 |
-
literal = "Muy positivo"
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
literal_with_confidence = f"{literal} (Confianza: {confidence}%)"
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
# Función para extraer texto de PDF y analizar
|
42 |
-
def analyze_pdf_sentiment(pdf_file):
|
43 |
-
pdf_reader = PdfReader(pdf_file.name)
|
44 |
text = ""
|
45 |
for page in pdf_reader.pages:
|
46 |
text += page.extract_text()
|
47 |
-
|
48 |
-
return
|
49 |
|
50 |
# Interfaz de Gradio
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
|
69 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import PyPDF2
|
4 |
|
5 |
+
# Cargar el modelo de análisis de sentimientos
|
6 |
+
sentiment_analysis = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
|
7 |
|
8 |
+
def analyze_text(input_text):
|
9 |
+
result = sentiment_analysis(input_text)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
label = result['label']
|
11 |
+
score = round(result['score'] * 100, 2) # Convertir a porcentaje
|
12 |
+
|
13 |
+
# Definir la categoría de sentimiento según la etiqueta
|
14 |
if label == "1 star":
|
15 |
+
sentiment_label = "Muy negativo"
|
|
|
16 |
elif label == "2 stars":
|
17 |
+
sentiment_label = "Negativo"
|
|
|
18 |
elif label == "3 stars":
|
19 |
+
sentiment_label = "Neutro"
|
|
|
20 |
elif label == "4 stars":
|
21 |
+
sentiment_label = "Positivo"
|
22 |
+
else:
|
23 |
+
sentiment_label = "Muy positivo"
|
|
|
|
|
24 |
|
25 |
+
# Devolver el resultado con el nivel de confianza
|
26 |
+
return f"{sentiment_label} ({score}%)", score
|
|
|
27 |
|
28 |
+
def analyze_pdf(pdf_file):
|
29 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
|
|
|
|
|
|
30 |
text = ""
|
31 |
for page in pdf_reader.pages:
|
32 |
text += page.extract_text()
|
33 |
+
|
34 |
+
return analyze_text(text)
|
35 |
|
36 |
# Interfaz de Gradio
|
37 |
+
text_input = gr.Textbox(label="Ingrese texto para análisis")
|
38 |
+
pdf_input = gr.File(label="Subir PDF para análisis", file_types=[".pdf"])
|
39 |
+
|
40 |
+
text_output = gr.Label(label="Resultado del análisis")
|
41 |
+
slider_output = gr.Slider(label="Nivel de sentimiento", minimum=0, maximum=100, interactive=False)
|
42 |
+
|
43 |
+
text_button = gr.Button("Analizar texto")
|
44 |
+
pdf_button = gr.Button("Analizar PDF")
|
45 |
+
|
46 |
+
def text_analysis(input_text):
|
47 |
+
sentiment, score = analyze_text(input_text)
|
48 |
+
return sentiment, score
|
49 |
+
|
50 |
+
def pdf_analysis(pdf_file):
|
51 |
+
sentiment, score = analyze_pdf(pdf_file)
|
52 |
+
return sentiment, score
|
53 |
+
|
54 |
+
with gr.Blocks() as app:
|
55 |
+
gr.Markdown("## Análisis de Sentimientos con DistilBETO")
|
56 |
+
|
57 |
+
with gr.Column():
|
58 |
+
gr.Markdown("### Análisis de Texto")
|
59 |
+
text_input.render()
|
60 |
+
text_button.render()
|
61 |
+
text_output.render()
|
62 |
+
slider_output.render()
|
63 |
|
64 |
+
text_button.click(text_analysis, inputs=text_input, outputs=[text_output, slider_output])
|
65 |
|
66 |
+
gr.Markdown("### Análisis de PDF")
|
67 |
+
pdf_input.render()
|
68 |
+
pdf_button.render()
|
69 |
+
text_output.render()
|
70 |
+
slider_output.render()
|
71 |
|
72 |
+
pdf_button.click(pdf_analysis, inputs=pdf_input, outputs=[text_output, slider_output])
|
73 |
|
74 |
+
# Ejecutar la aplicación
|
75 |
+
app.launch()
|
requirements.txt
CHANGED
@@ -1,7 +1,4 @@
|
|
1 |
-
gradio
|
2 |
transformers
|
3 |
torch
|
4 |
-
PyPDF2
|
5 |
-
fastapi==0.100.0
|
6 |
-
httpcore==0.17.3
|
7 |
-
starlette==0.27.0
|
|
|
1 |
+
gradio>=3.0
|
2 |
transformers
|
3 |
torch
|
4 |
+
PyPDF2
|
|
|
|
|
|