File size: 2,596 Bytes
dd9cc55
 
aeb2715
d665c22
dd9cc55
 
987baef
dd9cc55
 
 
 
 
 
fcb0322
dd9cc55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcb0322
aeb2715
fcb0322
 
 
 
 
 
 
 
 
 
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
import tweepy
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer
import os

# Autenticação com Twitter para leitura
client = tweepy.Client(bearer_token=os.environ.get('AAAAAAAAAAAAAAAAAAAAAFhdyAEAAAAAIG7CES2Ej98j9NGylgrb0yOrvvA%3DoSuAx9ZWUbyofrjM5VYiQBqjZA0oXXs4Ik21py9BZEn8mrAvzW'))

# Autenticação com Twitter para postagem
auth = tweepy.OAuth1UserHandler(
    os.environ.get('lXr9PmhTdm2hxC20AmNIguxsn'),
    os.environ.get('LMsDCue1BSBGydpC9ykw0MJTzJ35rbWwcIjkVbgp8PW1apjmFo'),
    os.environ.get('69748110-yMiyeul89rwhUDJmgCNW0vfAVm3hIJRdFhOlVUc6d'),
    os.environ.get('ioExZSIT0LZ5pvUXxDRtTuNUp8lCaoIP2H5vCJsphvFbk')
)
api = tweepy.API(auth)

# Coletar tweets
query = 'BBB25 -filter:retweets'
tweets = client.search_recent_tweets(query=query, lang='pt', max_results=100)

# Análise de sentimentos
sentiment_pipeline = pipeline('sentiment-analysis', model='cardiffnlp/twitter-xlm-roberta-base-sentiment')

sentiments = []
for tweet in tweets.data:
    result = sentiment_pipeline(tweet.text)
    sentiments.append(result[0]['label'])

# Calcular taxas
positive = sentiments.count('positive')
negative = sentiments.count('negative')
total = len(sentiments)

positive_ratio = positive / total
negative_ratio = negative / total

# Gerar mensagem com IA
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2')

if positive_ratio > 0.6:
    prompt = "Write an exciting tweet about BBB25 with a positive tone in Portuguese."
elif negative_ratio > 0.6:
    prompt = "Write an informative tweet about BBB25 with a neutral tone in Portuguese."
else:
    prompt = "Write a buzzing tweet about BBB25 with an engaging tone in Portuguese."

input_ids = tokenizer.encode(prompt, return_tensors='pt')

# Gerar texto com limite de tokens correspondente a 280 caracteres
outputs = model.generate(input_ids, max_length=25, do_sample=True)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)

# Limitar o tweet a 280 caracteres
generated_text = generated_text[:280]

# Postar no Twitter
try:
    api.update_status(status=generated_text)
    print(f"Postado: {generated_text}")
except Exception as e:
    print(f"Erro ao postar: {e}")

# Logging (opcional)
with open('posting_log.txt', 'a') as f:
    f.write(f"Positive Ratio: {positive_ratio}, Negative Ratio: {negative_ratio}, Posted: {generated_text}\n")
    


# Footer
st.markdown("---")
st.markdown(
    """
    <div style='text-align: center'>
        <small>Desenvolvido com ❤️ usando Streamlit e Transformers</small>
    </div>
    """,
    unsafe_allow_html=True
)