Spaces:
Sleeping
Sleeping
File size: 7,781 Bytes
dd9cc55 7127fcb aeb2715 3251884 7384c00 fe176a4 7127fcb 7384c00 e6a37ce 7384c00 7127fcb 7384c00 7127fcb 7384c00 e6a37ce 7127fcb e6a37ce 7384c00 e6a37ce 7127fcb 7384c00 af4dae9 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 7127fcb fe176a4 5083bb3 e6a37ce 7127fcb 5083bb3 3251884 7127fcb e6a37ce 7127fcb 5083bb3 af4dae9 5083bb3 81a43fe 7127fcb e6a37ce 7127fcb f5949bf 7127fcb e6a37ce 5083bb3 e6a37ce 7127fcb e6a37ce fe176a4 5083bb3 f3b1ac7 bdcfb66 5083bb3 3251884 5083bb3 7127fcb 5083bb3 fe176a4 7127fcb fe176a4 7384c00 7127fcb fe176a4 7127fcb 3251884 7127fcb 3251884 7127fcb 3251884 7127fcb 3251884 7127fcb 3251884 7127fcb fe176a4 3251884 7127fcb 3251884 7127fcb aeb2715 5083bb3 |
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 |
import tweepy
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
import os
import streamlit as st
from datetime import datetime
import time
from tenacity import retry, stop_after_attempt, wait_exponential
import re
from collections import Counter
def debug_print(message):
"""Função para imprimir mensagens de debug tanto no console quanto no Streamlit"""
print(message)
st.text(message)
@retry(
stop=stop_after_attempt(2), # Reduzido para 2 tentativas
wait=wait_exponential(multiplier=1, min=2, max=4), # Tempo de espera reduzido
retry=lambda e: isinstance(e, tweepy.errors.TooManyRequests)
)
def fetch_tweets(client, query, tweet_fields):
try:
tweets = client.search_recent_tweets(
query=query,
max_results=10, # Reduzido para 10 tweets
tweet_fields=tweet_fields
)
if not hasattr(tweets, 'data') or tweets.data is None:
return None
return tweets
except Exception as e:
debug_print(f"Erro na busca: {str(e)}")
return None
def post_tweet(client, text):
try:
response = client.create_tweet(text=text)
return response
except Exception as e:
debug_print(f"Erro ao postar tweet: {str(e)}")
return None
def extract_context_from_tweets(tweets_data):
"""Versão simplificada da extração de contexto"""
all_text = " ".join([tweet.text for tweet in tweets_data])
clean_text = re.sub(r'http\S+|@\S+|RT|[^\w\s]', ' ', all_text)
# Encontrar nomes capitalizados frequentes
words = clean_text.split()
capitalized_words = [word for word in words if word.istitle() and len(word) > 2]
participants = Counter(capitalized_words).most_common(3) # Reduzido para 3
# Eventos simplificados
events = []
event_keywords = ['paredão', 'prova', 'líder', 'eliminação', 'briga']
for keyword in event_keywords:
if keyword in clean_text.lower():
events.append(keyword)
return {
'participants': [p[0] for p in participants],
'events': events[:2], # Limitado a 2 eventos
'raw_text': clean_text
}
def generate_comment(context, sentiment_ratio, model, tokenizer):
"""Versão otimizada da geração de comentários"""
# Determinar o tom com base no sentimento
if sentiment_ratio['positive'] > 0.5:
tone = "clima animado"
elif sentiment_ratio['negative'] > 0.5:
tone = "clima tenso"
else:
tone = "opiniões divididas"
# Construir prompt base
prompt = f"BBB25 com {tone}"
# Adicionar contexto se disponível
if context['participants']:
prompt += f", {context['participants'][0]}"
if context['events']:
prompt += f", {context['events'][0]}"
# Gerar texto
try:
inputs = tokenizer.encode(prompt, return_tensors='pt', max_length=100, truncation=True)
outputs = model.generate(
inputs,
max_length=150, # Reduzido para melhor performance
num_return_sequences=1, # Apenas uma sequência
temperature=0.8,
top_k=40,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
text = re.sub(r'\s+', ' ', text).strip()
# Adicionar hashtags
hashtags = " #BBB25"
if len(text) + len(hashtags) > 280:
text = text[:277-len(hashtags)] + "..."
return text + hashtags
except Exception as e:
debug_print(f"Erro na geração: {str(e)}")
return f"BBB25: {tone} hoje! #BBB25" # Fallback simples
def main():
try:
st.title("Análise de Sentimentos - BBB25")
# Verificação simplificada de variáveis de ambiente
required_vars = [
'TWITTER_API_KEY',
'TWITTER_API_SECRET_KEY',
'TWITTER_ACCESS_TOKEN',
'TWITTER_ACCESS_TOKEN_SECRET',
'TWITTER_BEARER_TOKEN'
]
if any(os.getenv(var) is None for var in required_vars):
raise ValueError("Faltam variáveis de ambiente necessárias")
# Autenticação Twitter
client = tweepy.Client(
bearer_token=os.getenv('TWITTER_BEARER_TOKEN'),
consumer_key=os.getenv('TWITTER_API_KEY'),
consumer_secret=os.getenv('TWITTER_API_SECRET_KEY'),
access_token=os.getenv('TWITTER_ACCESS_TOKEN'),
access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET'),
wait_on_rate_limit=True
)
# Inicializar modelo
model_name = "pierreguillou/gpt2-small-portuguese"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
# Buscar tweets
query = 'BBB25 lang:pt -is:retweet -is:reply'
tweet_fields = ['text', 'created_at']
with st.spinner('Buscando tweets...'):
tweets = fetch_tweets(client, query, tweet_fields)
if tweets is None or not tweets.data:
st.error("Não foi possível obter tweets")
return
context = extract_context_from_tweets(tweets.data)
# Análise de sentimentos
with st.spinner('Analisando sentimentos...'):
sentiment_pipeline = pipeline(
"sentiment-analysis",
model="nlptown/bert-base-multilingual-uncased-sentiment"
)
sentiments = []
for tweet in tweets.data[:10]: # Limitado a 10 tweets
result = sentiment_pipeline(tweet.text[:512]) # Limitado a 512 caracteres
rating = int(result[0]['label'].split()[0])
if rating >= 4:
sentiments.append('positive')
elif rating <= 2:
sentiments.append('negative')
else:
sentiments.append('neutral')
# Calcular taxas
if sentiments:
total = len(sentiments)
sentiment_ratios = {
'positive': sentiments.count('positive') / total,
'negative': sentiments.count('negative') / total,
'neutral': sentiments.count('neutral') / total
}
# Gerar e postar tweet
with st.spinner('Gerando e postando tweet...'):
tweet_text = generate_comment(context, sentiment_ratios, model, tokenizer)
post_tweet(client, tweet_text)
# Interface
st.title("Resultados")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Positivo", f"{sentiment_ratios['positive']:.1%}")
with col2:
st.metric("Neutro", f"{sentiment_ratios['neutral']:.1%}")
with col3:
st.metric("Negativo", f"{sentiment_ratios['negative']:.1%}")
st.subheader("Tweet Gerado")
st.write(tweet_text)
# Log simplificado
with open('posting_log.txt', 'a') as f:
f.write(f"{datetime.now()}: {tweet_text}\n")
except Exception as e:
st.error(f"Erro: {str(e)}")
finally:
st.markdown("---")
st.markdown(
"""
<div style='text-align: center'>
<small>Desenvolvido com ❤️ usando Streamlit e Transformers</small>
</div>
""",
unsafe_allow_html=True
)
if __name__ == "__main__":
main() |