Spaces:
Runtime error
Runtime error
Commit
·
bb0de7f
1
Parent(s):
d5291cb
Update app.py
Browse files
app.py
CHANGED
@@ -80,95 +80,98 @@ with colT2:
|
|
80 |
font-size:16px ; font-family: 'Times New Roman'; color: #3358ff;}
|
81 |
</style> """, unsafe_allow_html=True)
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
def analizar_tweets(search_words, number_of_tweets ):
|
88 |
tweets = api.user_timeline(screen_name = search_words,tweet_mode="extended", count= number_of_tweets)
|
89 |
-
tweet_list = [i.full_text for i in tweets]
|
90 |
-
text= pd.DataFrame(tweet_list)
|
91 |
-
text[0] = text[0].apply(preprocess)
|
92 |
-
text_list = text[0].tolist()
|
93 |
result = []
|
94 |
-
for
|
95 |
-
if (
|
|
|
|
|
96 |
continue
|
97 |
else:
|
98 |
-
|
|
|
99 |
for predic in prediction:
|
100 |
-
etiqueta = {'Tweets':
|
101 |
result.append(etiqueta)
|
102 |
|
103 |
df = pd.DataFrame(result)
|
104 |
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
|
|
|
|
|
105 |
tabla = st.table(df.reset_index(drop=True).head(30).style.applymap(color_survived, subset=['Prediccion']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
return tabla
|
108 |
|
109 |
-
def analizar_frase(frase):
|
110 |
-
|
111 |
-
predictions = pipeline_nlp(frase)
|
112 |
-
# convierte las predicciones en una lista de diccionarios
|
113 |
-
data = [{'Texto': frase, 'Prediccion': prediction['label'], 'Probabilidad': prediction['score']} for prediction in predictions]
|
114 |
-
# crea un DataFrame a partir de la lista de diccionarios
|
115 |
-
df = pd.DataFrame(data)
|
116 |
-
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
|
117 |
-
# muestra el DataFrame
|
118 |
-
#st.table(df.reset_index(drop=True).head(30).style.applymap(color_survived, subset=['Prediccion']))
|
119 |
-
tabla = st.table(df.reset_index(drop=True).head(30).style.applymap(color_survived, subset=['Prediccion']))
|
120 |
-
|
121 |
-
return tabla
|
122 |
-
|
123 |
def tweets_localidad(buscar_localidad):
|
124 |
tabla = []
|
125 |
try:
|
126 |
geolocator = Nominatim(user_agent="nombre_del_usuario")
|
127 |
location = geolocator.geocode(buscar_localidad)
|
128 |
radius = "10km"
|
129 |
-
tweets = api.search_tweets(q="",lang="es",geocode=f"{location.latitude},{location.longitude},{radius}", count =
|
130 |
-
tweet_list = [i.full_text for i in tweets]
|
131 |
-
text= pd.DataFrame(tweet_list)
|
132 |
-
text[0] = text[0].apply(preprocess_tweet)
|
133 |
-
text_list = text[0].tolist()
|
134 |
result = []
|
135 |
-
for
|
136 |
-
if (
|
137 |
continue
|
138 |
-
elif not
|
139 |
continue
|
140 |
else:
|
|
|
141 |
prediction = pipeline_nlp(text)
|
142 |
for predic in prediction:
|
143 |
etiqueta = {'Tweets': text,'Prediccion': predic['label'], 'Probabilidad': predic['score']}
|
144 |
result.append(etiqueta)
|
145 |
df = pd.DataFrame(result)
|
146 |
-
df
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
except AttributeError:
|
168 |
st.text("No existe ninguna localidad con ese nombre")
|
169 |
|
170 |
-
return tabla
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
def run():
|
173 |
with st.form("my_form"):
|
174 |
col,buff1, buff2 = st.columns([2,2,1])
|
|
|
80 |
font-size:16px ; font-family: 'Times New Roman'; color: #3358ff;}
|
81 |
</style> """, unsafe_allow_html=True)
|
82 |
|
|
|
|
|
|
|
|
|
83 |
def analizar_tweets(search_words, number_of_tweets ):
|
84 |
tweets = api.user_timeline(screen_name = search_words,tweet_mode="extended", count= number_of_tweets)
|
|
|
|
|
|
|
|
|
85 |
result = []
|
86 |
+
for tweet in tweets:
|
87 |
+
if (tweet.full_text.startswith('RT')):
|
88 |
+
continue
|
89 |
+
elif not tweet.full_text.strip():
|
90 |
continue
|
91 |
else:
|
92 |
+
datos = preprocess(tweet.full_text)
|
93 |
+
prediction = pipeline_nlp(datos)
|
94 |
for predic in prediction:
|
95 |
+
etiqueta = {'Tweets': datos,'Prediccion': predic['label'], 'Probabilidad': predic['score']}
|
96 |
result.append(etiqueta)
|
97 |
|
98 |
df = pd.DataFrame(result)
|
99 |
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
|
100 |
+
df = df[df["Prediccion"] == 'Sexista']
|
101 |
+
df = df[df["Probabilidad"] > 0.80]
|
102 |
tabla = st.table(df.reset_index(drop=True).head(30).style.applymap(color_survived, subset=['Prediccion']))
|
103 |
+
resultado=df.groupby('Prediccion')['Probabilidad'].sum()
|
104 |
+
colores=["#aae977","#EE3555"]
|
105 |
+
fig, ax = plt.subplots(figsize=(2, 2), subplotpars=None)
|
106 |
+
plt.pie(resultado,labels=resultado.index,autopct='%1.1f%%',colors=colores)
|
107 |
+
ax.set_title("Porcentajes por Categorias", fontsize=8, fontweight="bold")
|
108 |
+
plt.rcParams.update({'font.size':8, 'font.weight':'bold'})
|
109 |
+
ax.legend()
|
110 |
+
# Muestra el gráfico
|
111 |
+
plt.show()
|
112 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
113 |
+
st.pyplot()
|
114 |
|
115 |
return tabla
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
def tweets_localidad(buscar_localidad):
|
118 |
tabla = []
|
119 |
try:
|
120 |
geolocator = Nominatim(user_agent="nombre_del_usuario")
|
121 |
location = geolocator.geocode(buscar_localidad)
|
122 |
radius = "10km"
|
123 |
+
tweets = api.search_tweets(q="",lang="es",geocode=f"{location.latitude},{location.longitude},{radius}", count = 50, tweet_mode="extended")
|
|
|
|
|
|
|
|
|
124 |
result = []
|
125 |
+
for tweet in tweets:
|
126 |
+
if (tweet.full_text.startswith):
|
127 |
continue
|
128 |
+
elif not tweet.full_text.strip():
|
129 |
continue
|
130 |
else:
|
131 |
+
datos = preprocess(tweet.full_text)
|
132 |
prediction = pipeline_nlp(text)
|
133 |
for predic in prediction:
|
134 |
etiqueta = {'Tweets': text,'Prediccion': predic['label'], 'Probabilidad': predic['score']}
|
135 |
result.append(etiqueta)
|
136 |
df = pd.DataFrame(result)
|
137 |
+
if df.empty:
|
138 |
+
muestra = st.table("No se encontraron tweets sexistas dentro de la localidad")
|
139 |
+
tabla.append(muestra)
|
140 |
+
else:
|
141 |
+
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
|
142 |
+
df = df[df["Prediccion"] == 'Sexista']
|
143 |
+
df = df[df["Probabilidad"] > 0.80]
|
144 |
+
muestra = st.table(df.reset_index(drop=True).head(5).style.applymap(color_survived, subset=['Prediccion']))
|
145 |
+
tabla.append(muestra)
|
146 |
+
resultado=df.groupby('Prediccion')['Probabilidad'].sum()
|
147 |
+
colores=["#aae977","#EE3555"]
|
148 |
+
fig, ax = plt.subplots(figsize=(4, 4), subplotpars=None)
|
149 |
+
plt.pie(resultado,labels=resultado.index,autopct='%1.1f%%',colors=colores)
|
150 |
+
ax.set_title("Porcentajes por Categorias", fontsize=8, fontweight="bold")
|
151 |
+
plt.rcParams.update({'font.size':8, 'font.weight':'bold'})
|
152 |
+
ax.legend()
|
153 |
+
# Muestra el gráfico
|
154 |
+
plt.show()
|
155 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
156 |
+
st.pyplot()
|
157 |
+
|
158 |
except AttributeError:
|
159 |
st.text("No existe ninguna localidad con ese nombre")
|
160 |
|
161 |
+
return tabla
|
162 |
+
|
163 |
+
def analizar_frase(frase):
|
164 |
+
predictions = pipeline_nlp(frase)
|
165 |
+
# convierte las predicciones en una lista de diccionarios
|
166 |
+
data = [{'Texto': frase, 'Prediccion': prediction['label'], 'Probabilidad': prediction['score']} for prediction in predictions]
|
167 |
+
# crea un DataFrame a partir de la lista de diccionarios
|
168 |
+
df = pd.DataFrame(data)
|
169 |
+
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
|
170 |
+
# muestra el DataFrame
|
171 |
+
tabla = st.table(df.reset_index(drop=True).head(5).style.applymap(color_survived, subset=['Prediccion']))
|
172 |
+
|
173 |
+
return tabla
|
174 |
+
|
175 |
def run():
|
176 |
with st.form("my_form"):
|
177 |
col,buff1, buff2 = st.columns([2,2,1])
|