Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,26 @@
|
|
1 |
-
import tweepy
|
2 |
-
import openai
|
3 |
-
import time
|
|
|
4 |
|
5 |
-
# Authentication for Twitter API
|
6 |
def authenticate_twitter():
|
7 |
auth = tweepy.OAuth1UserHandler(
|
8 |
-
consumer_key='
|
9 |
-
consumer_secret='
|
10 |
-
access_token='
|
11 |
-
access_token_secret='
|
12 |
)
|
13 |
api = tweepy.API(auth)
|
14 |
return api
|
15 |
|
16 |
# Authentication for OpenAI API
|
17 |
def authenticate_openai():
|
18 |
-
openai.api_key = '
|
19 |
|
20 |
# Function to generate a tweet using GPT-3
|
21 |
def generate_tweet():
|
22 |
response = openai.Completion.create(
|
23 |
-
engine="text-davinci-003", #
|
24 |
-
prompt="Create a tweet in the style of a humorous AI agent, keeping it casual and engaging.",
|
25 |
-
max_tokens=50
|
26 |
-
)
|
27 |
-
tweet = response.choices[0].text.strip()
|
28 |
-
return tweet
|
29 |
-
|
30 |
-
# Function to send the tweet to Twitter
|
31 |
-
def send_tweet(api, tweet):
|
32 |
-
api.update_status(tweet) # Send the generated tweet
|
33 |
-
|
34 |
-
# Main function to control the tweet posting loop
|
35 |
-
def main():
|
36 |
-
twitter_api = authenticate_twitter() # Get Twitter API instance
|
37 |
-
authenticate_openai() # Authenticate OpenAI API
|
38 |
-
|
39 |
-
while True:
|
40 |
-
tweet = generate_tweet() # Generate a new tweet
|
41 |
-
print(f"Tweeting: {tweet}")
|
42 |
-
send_tweet(twitter_api, tweet) # Send the tweet to Twitter
|
43 |
-
time.sleep(1800) # Wait for 30 minutes before posting the next tweet
|
44 |
|
45 |
-
if __name__ == "__main__":
|
46 |
-
main() # Start the program
|
47 |
|
|
|
1 |
+
import tweepy
|
2 |
+
import openai
|
3 |
+
import time
|
4 |
+
import os
|
5 |
|
6 |
+
# Authentication for Twitter API (using environment variables for security)
|
7 |
def authenticate_twitter():
|
8 |
auth = tweepy.OAuth1UserHandler(
|
9 |
+
consumer_key=os.getenv('TWITTER_API_KEY'),
|
10 |
+
consumer_secret=os.getenv('TWITTER_API_SECRET_KEY'),
|
11 |
+
access_token=os.getenv('TWITTER_ACCESS_TOKEN'),
|
12 |
+
access_token_secret=os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
|
13 |
)
|
14 |
api = tweepy.API(auth)
|
15 |
return api
|
16 |
|
17 |
# Authentication for OpenAI API
|
18 |
def authenticate_openai():
|
19 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
20 |
|
21 |
# Function to generate a tweet using GPT-3
|
22 |
def generate_tweet():
|
23 |
response = openai.Completion.create(
|
24 |
+
engine="text-davinci-003", # Choose the model to u
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
|
|
26 |
|