Spaces:
Runtime error
Runtime error
File size: 9,474 Bytes
a86ebfe 074b087 4f600ec 123a2a5 64547bc 4c1a7b2 eb03325 edb53bb 074b087 336e489 4c1a7b2 a76f382 a86ebfe 336e489 a76f382 336e489 a86ebfe bc97bc5 9ac4891 bc97bc5 0e521b1 e5f8353 bc97bc5 074b087 f9ed481 0eaa842 d0e7d06 aa1a517 d0e7d06 868386d d0e7d06 2e8cc31 d0e7d06 2ba6811 9091676 b57da8c 2e8cc31 d0e7d06 2e8cc31 d8e3643 244d0dd d0e7d06 f9ed481 2e8cc31 d0e7d06 b57da8c 0e521b1 3e92edb 1bf00e3 dc1887d ec5e9ed 9ffd42a dad8f09 bb0de7f 485e4eb d5291cb 4f31ce9 dad8f09 bb0de7f 6de011c dad8f09 6de011c fe0b968 a7ff4ae 485e4eb 2e8cc31 485e4eb ec5e9ed b57da8c 485e4eb 6de011c b57da8c 6de011c ae4c78e 244d0dd 4581391 6de011c bb0de7f d8e3643 dc1887d dad8f09 b57da8c bb0de7f eb4e7e0 b57da8c a5ed634 89d7f96 b9decf4 bb0de7f 074b087 19aeb3f aa1a517 19aeb3f 074b087 3e92edb 074b087 3e92edb 938d354 074b087 0567ede 38110be 074b087 38110be fc3e5ca 0e521b1 1c9fb4e fc3e5ca 0e521b1 be5e499 8d37970 2f14689 074b087 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
import tweepy as tw
import streamlit as st
import pandas as pd
import regex as re
import numpy as np
import pysentimiento
import geopy
import matplotlib.pyplot as plt
from pysentimiento.preprocessing import preprocess_tweet
from geopy.geocoders import Nominatim
from transformers import pipeline
model_checkpoint = "hackathon-pln-es/twitter_sexismo-finetuned-robertuito-exist2021"
pipeline_nlp = pipeline("text-classification", model=model_checkpoint)
consumer_key = "BjipwQslVG4vBdy4qK318KnoA"
consumer_secret = "3fzL70v9faklrPgvTi3zbofw9rwk92fgGdtAslFkFYt8kGmqBJ"
access_token = "1217853705086799872-Y5zEChpTeKccuLY3XJRXDPPZhNrlba"
access_token_secret = "pqQ5aFSJxzJ2xnI6yhVtNjQO36FOu8DBOH6DtUrPAU54J"
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)
def preprocess(text):
#text=text.lower()
# remove hyperlinks
text = re.sub(r'https?:\/\/.*[\r\n]*', '', text)
text = re.sub(r'http?:\/\/.*[\r\n]*', '', text)
#Replace &, <, > with &,<,> respectively
text=text.replace(r'&?',r'and')
text=text.replace(r'<',r'<')
text=text.replace(r'>',r'>')
#remove hashtag sign
#text=re.sub(r"#","",text)
#remove mentions
text = re.sub(r"(?:\@)\w+", '', text)
#text=re.sub(r"@","",text)
#remove non ascii chars
text=text.encode("ascii",errors="ignore").decode()
#remove some puncts (except . ! ?)
text=re.sub(r'[:"#$%&\*+,-/:;<=>@\\^_`{|}~]+','',text)
text=re.sub(r'[!]+','!',text)
text=re.sub(r'[?]+','?',text)
text=re.sub(r'[.]+','.',text)
text=re.sub(r"'","",text)
text=re.sub(r"\(","",text)
text=re.sub(r"\)","",text)
text=" ".join(text.split())
return text
def highlight_survived(s):
return ['background-color: red']*len(s) if (s.Sexista == 1) else ['background-color: green']*len(s)
def color_survived(val):
color = 'red' if val=='Sexista' else 'white'
return f'background-color: {color}'
st.set_page_config(layout="wide")
st.markdown('<style>body{background-color: Blue;}</style>',unsafe_allow_html=True)
colT1,colT2 = st.columns([2,8])
with colT2:
# st.title('Analisis de comentarios sexistas en Twitter')
st.markdown(""" <style> .font {
font-size:40px ; font-family: 'Cooper Black'; color: #06bf69;}
</style> """, unsafe_allow_html=True)
st.markdown('<p class="font">An谩lisis de comentarios sexistas en Twitter</p>', unsafe_allow_html=True)
st.markdown(""" <style> .font1 {
font-size:28px ; font-family: 'Times New Roman'; color: #8d33ff;}
</style> """, unsafe_allow_html=True)
st.markdown(""" <style> .font2 {
font-size:16px ; font-family: 'Times New Roman'; color: #3358ff;}
</style> """, unsafe_allow_html=True)
def analizar_tweets(search_words, number_of_tweets):
tabla = []
if(number_of_tweets > 0 and search_words != "" ):
try:
# Buscar la informaci贸n del perfil de usuario
user = api.get_user(screen_name=search_words)
#st.text(f"La cuenta {search_words} existe.")
tweets = api.user_timeline(screen_name = search_words,tweet_mode="extended", count= number_of_tweets)
result = []
for tweet in tweets:
if (tweet.full_text.startswith('RT')):
continue
else:
datos = preprocess(tweet.full_text)
if datos == "":
continue
else:
prediction = pipeline_nlp(datos)
for predic in prediction:
etiqueta = {'Tweets': datos,'Prediccion': predic['label'], 'Probabilidad': predic['score']}
result.append(etiqueta)
df = pd.DataFrame(result)
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
df = df[df["Prediccion"] == 'Sexista']
df = df[df["Probabilidad"] > 0.5]
if df.empty:
st.text("No hay tweets a analizar")
else:
muestra = st.table(df.reset_index(drop=True).head(30).style.applymap(color_survived, subset=['Prediccion']))
#tabla.append(muestra)
#resultado=df.groupby('Prediccion')['Probabilidad'].sum()
#colores=["#aae977","#EE3555"]
#fig, ax = plt.subplots(figsize=(2, 1), subplotpars=None)
#plt.pie(resultado,labels=resultado.index,autopct='%1.1f%%',colors=colores)
#ax.set_title("Porcentajes por Categorias", fontsize=2, fontweight="bold")
#plt.rcParams.update({'font.size':2, 'font.weight':'bold'})
#ax.legend()
# Muestra el gr谩fico
#plt.show()
#st.set_option('deprecation.showPyplotGlobalUse', False)
#st.pyplot()
except Exception as e:
st.text(f"La cuenta {search_words} no existe.")
else:
st.text("Ingrese los parametros correspondientes")
return muestra
def tweets_localidad(buscar_localidad):
tabla = []
try:
geolocator = Nominatim(user_agent="nombre_del_usuario")
location = geolocator.geocode(buscar_localidad)
radius = "15km"
tweets = api.search_tweets(q="",lang="es",geocode=f"{location.latitude},{location.longitude},{radius}", count = 1000, tweet_mode="extended")
result = []
for tweet in tweets:
if (tweet.full_text.startswith('RT')):
continue
elif not tweet.full_text.strip():
continue
else:
datos = preprocess(tweet.full_text)
prediction = pipeline_nlp(datos)
for predic in prediction:
etiqueta = {'Tweets': datos,'Prediccion': predic['label'], 'Probabilidad': predic['score']}
result.append(etiqueta)
df = pd.DataFrame(result)
df['Prediccion'] = np.where(df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
df = df[df["Prediccion"] == 'Sexista']
df = df[df["Probabilidad"] > 0.5]
df = df.sort_values(by='Probabilidad', ascending=False)
#muestra = st.table(df.reset_index(drop=True).head(5).style.applymap(color_survived, subset=['Prediccion']))
if df.empty:
st.text("No se encontraron tweets sexistas dentro de la localidad")
else:
#tabla.append(muestra)
muestra = st.table(df.reset_index(drop=True).head(5).style.applymap(color_survived, subset=['Prediccion']))
resultado=df.groupby('Prediccion')['Probabilidad'].sum()
colores=["#aae977","#EE3555"]
fig, ax = plt.subplots()
fig.set_size_inches(2, 1)
plt.pie(resultado,labels=resultado.index,autopct='%1.1f%%',colors=colores, fontsize=2)
ax.set_title("Porcentajes por Categorias", fontsize=3, fontweight="bold")
plt.rcParams.update({'font.size':3, 'font.weight':'bold'})
ax.legend()
# Muestra el gr谩fico
plt.show()
st.set_option('deprecation.showPyplotGlobalUse', False)
st.pyplot()
except Exception as e:
st.text("No existe ninguna localidad con ese nombre")
return muestra
def analizar_frase(frase):
if frase == "":
#tabla = st.text("Ingrese una frase")
st.text("Ingrese una frase")
else:
predictions = pipeline_nlp(frase)
# convierte las predicciones en una lista de diccionarios
data = [{'Texto': frase, 'Prediccion': prediction['label'], 'Probabilidad': prediction['score']} for prediction in predictions]
# crea un DataFrame a partir de la lista de diccionarios
df = pd.DataFrame(data)
df['Prediccion'] = np.where( df['Prediccion'] == 'LABEL_1', 'Sexista', 'No Sexista')
# muestra el DataFrame
tabla = st.table(df.reset_index(drop=True).head(5).style.applymap(color_survived, subset=['Prediccion']))
return tabla
def run():
with st.form("my_form"):
col,buff1, buff2 = st.columns([2,2,1])
st.write("Escoja una Opci贸n")
search_words = col.text_input("Introduzca la frase, el usuario o localidad para analizar y pulse el check correspondiente")
number_of_tweets = col.number_input('Introduzca n煤mero de tweets a analizar del usuario M谩ximo 50', 0,50,0)
termino=st.checkbox('Frase')
usuario=st.checkbox('Usuario')
localidad=st.checkbox('Localidad')
submit_button = col.form_submit_button(label='Analizar')
error =False
if submit_button:
# Condici贸n para el caso de que esten dos check seleccionados
if ( termino == False and usuario == False and localidad == False):
st.text('Error no se ha seleccionado ningun check')
error=True
elif ( termino == True and usuario == True and localidad == True):
st.text('Error se han seleccionado varios check')
error=True
if (error == False):
if (termino):
analizar_frase(search_words)
elif (usuario):
analizar_tweets(search_words,number_of_tweets)
elif (localidad):
tweets_localidad(search_words)
run() |