Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
from requests_oauthlib import OAuth1Session
|
4 |
+
import re
|
5 |
+
import schedule
|
6 |
+
import time
|
7 |
+
from datetime import datetime
|
8 |
+
|
9 |
+
# Define your credentials here
|
10 |
+
GROQ_API_KEY = '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' # Replace with your Groq API key
|
11 |
+
TWITTER_CONSUMER_KEY = 'oZd1HFFLyGCqrsEo70nQCatMX'
|
12 |
+
TWITTER_CONSUMER_SECRET = 'Ytbx3P6xWhAqcs7cSoDZYlbNuaiC0u9ECe2vjaMIw5tVNafo5M'
|
13 |
+
TWITTER_ACCESS_TOKEN = '1557747240776892417-3WRco08xNgdBWsYhV90eqHNhUuwXOE'
|
14 |
+
TWITTER_ACCESS_TOKEN_SECRET = '3iu8LMLjnObnaqHpLS0QzSx2MqibVrr091qp3khadDasS'
|
15 |
+
|
16 |
+
# Groq API endpoint and parameters for content generation
|
17 |
+
GROQ_URL = 'https://api.groq.com/openai/v1/chat/completions'
|
18 |
+
GROQ_MODEL = 'llama3-8b-8192' # Replace with the model you want to use
|
19 |
+
|
20 |
+
# Twitter's character limit for a tweet
|
21 |
+
TWITTER_CHAR_LIMIT = 280
|
22 |
+
|
23 |
+
def generate_content():
|
24 |
+
"""
|
25 |
+
Generate content using the Groq API, ensuring the content is concise.
|
26 |
+
"""
|
27 |
+
headers = {
|
28 |
+
'Content-Type': 'application/json',
|
29 |
+
'Authorization': f'Bearer {GROQ_API_KEY}'
|
30 |
+
}
|
31 |
+
|
32 |
+
# Define the prompt for generating concise content about ReactJS
|
33 |
+
payload = {
|
34 |
+
'model': GROQ_MODEL,
|
35 |
+
'messages': [
|
36 |
+
{
|
37 |
+
'role': 'user',
|
38 |
+
'content': 'Crypto tip or joke, limited to 280 characters. Keep it concise, fun, and relevant to cryptocurrency concepts like market trends, tokens, or crypto memes. Include emojis if appropriate. Make sure not to exceed the character limit.'
|
39 |
+
}
|
40 |
+
]
|
41 |
+
}
|
42 |
+
|
43 |
+
|
44 |
+
try:
|
45 |
+
print("Sending request to Groq API...")
|
46 |
+
response = requests.post(GROQ_URL, headers=headers, json=payload)
|
47 |
+
print(f"Groq API response status: {response.status_code}")
|
48 |
+
|
49 |
+
if response.status_code == 200:
|
50 |
+
generated_text = response.json()['choices'][0]['message']['content']
|
51 |
+
print("Successfully generated content from Groq API.")
|
52 |
+
return generated_text
|
53 |
+
else:
|
54 |
+
print(f"Failed to generate content. Groq API response: {response.text}")
|
55 |
+
return None
|
56 |
+
|
57 |
+
except requests.exceptions.RequestException as e:
|
58 |
+
print(f"Error generating content: {e}")
|
59 |
+
return None
|
60 |
+
|
61 |
+
def sanitize_content(content):
|
62 |
+
"""
|
63 |
+
Sanitize the response by removing unwanted characters and cleaning up the text.
|
64 |
+
"""
|
65 |
+
# Remove unwanted characters such as extra spaces, newlines, and special characters
|
66 |
+
content = re.sub(r'\s+', ' ', content) # Replace multiple spaces with a single space
|
67 |
+
content = content.strip() # Remove leading/trailing spaces
|
68 |
+
content = content.replace("\n", " ") # Replace newlines with space
|
69 |
+
return content
|
70 |
+
|
71 |
+
def truncate_content(content):
|
72 |
+
"""
|
73 |
+
This function truncates the content to fit Twitter's character limit.
|
74 |
+
If the content is too long, it adds '...' at the end to indicate truncation.
|
75 |
+
"""
|
76 |
+
if len(content) > TWITTER_CHAR_LIMIT:
|
77 |
+
print(f"Content exceeds {TWITTER_CHAR_LIMIT} characters. Truncating...")
|
78 |
+
content = content[:TWITTER_CHAR_LIMIT - 3] + "..." # Truncate and add '...' to indicate cut-off
|
79 |
+
return content
|
80 |
+
|
81 |
+
def post_to_twitter(content):
|
82 |
+
"""
|
83 |
+
Post the sanitized and truncated content to Twitter using OAuth1 authentication.
|
84 |
+
"""
|
85 |
+
twitter_url = 'https://api.twitter.com/2/tweets'
|
86 |
+
|
87 |
+
# Create OAuth1Session instance for Twitter authentication
|
88 |
+
oauth = OAuth1Session(
|
89 |
+
TWITTER_CONSUMER_KEY,
|
90 |
+
client_secret=TWITTER_CONSUMER_SECRET,
|
91 |
+
resource_owner_key=TWITTER_ACCESS_TOKEN,
|
92 |
+
resource_owner_secret=TWITTER_ACCESS_TOKEN_SECRET
|
93 |
+
)
|
94 |
+
|
95 |
+
# Sanitize and truncate the content to ensure it fits within Twitter's character limit
|
96 |
+
content = sanitize_content(content)
|
97 |
+
content = truncate_content(content)
|
98 |
+
|
99 |
+
# Define the data payload for the tweet
|
100 |
+
tweet_data = {
|
101 |
+
'text': content # 'status' changed to 'text' for Twitter API v2
|
102 |
+
}
|
103 |
+
|
104 |
+
try:
|
105 |
+
print("Sending request to Twitter API...")
|
106 |
+
response = oauth.post(twitter_url, json=tweet_data)
|
107 |
+
print(f"Twitter API response status: {response.status_code}")
|
108 |
+
|
109 |
+
if response.status_code == 201:
|
110 |
+
print("Successfully posted the tweet!")
|
111 |
+
else:
|
112 |
+
print(f"Error posting tweet. Twitter API response: {response.status_code}, {response.text}")
|
113 |
+
|
114 |
+
except requests.exceptions.RequestException as e:
|
115 |
+
print(f"Error posting tweet: {e}")
|
116 |
+
|
117 |
+
def generate_and_post():
|
118 |
+
"""
|
119 |
+
Generate content from Groq and post it to Twitter.
|
120 |
+
"""
|
121 |
+
content = generate_content()
|
122 |
+
if content:
|
123 |
+
print("Generated content:", content)
|
124 |
+
post_to_twitter(content)
|
125 |
+
else:
|
126 |
+
print("Failed to generate content.")
|
127 |
+
|
128 |
+
# Schedule the job for 9 AM and 9 PM
|
129 |
+
# schedule.every().day.at("09:00").do(generate_and_post)
|
130 |
+
|
131 |
+
# Schedule the job to run every 2 minutes for testing
|
132 |
+
schedule.every(59).minutes.do(generate_and_post)
|
133 |
+
|
134 |
+
|
135 |
+
schedule.every().day.at("21:00").do(generate_and_post)
|
136 |
+
|
137 |
+
# Run the schedule loop
|
138 |
+
if __name__ == "__main__":
|
139 |
+
print(f"Scheduler started at {datetime.now()}")
|
140 |
+
while True:
|
141 |
+
schedule.run_pending() # Run pending scheduled tasks
|
142 |
+
time.sleep(60) # Wait for one minute before checking again
|