Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tweepy # Import the tweepy library to interact with Twitter API
|
| 2 |
+
import openai # Import OpenAI API for language generation
|
| 3 |
+
import time # Import time library to control tweet timing
|
| 4 |
+
|
| 5 |
+
# Authentication for Twitter API
|
| 6 |
+
def authenticate_twitter():
|
| 7 |
+
auth = tweepy.OAuth1UserHandler(
|
| 8 |
+
consumer_key='YOUR_TWITTER_CONSUMER_KEY',
|
| 9 |
+
consumer_secret='YOUR_TWITTER_CONSUMER_SECRET',
|
| 10 |
+
access_token='YOUR_TWITTER_ACCESS_TOKEN',
|
| 11 |
+
access_token_secret='YOUR_TWITTER_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 = 'YOUR_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", # You can choose the model to use
|
| 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
|