Adedoyinjames commited on
Commit
0e9d266
·
verified ·
1 Parent(s): 3f5d6f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -31,7 +31,7 @@ topics = ['AI', 'Tech', 'Business Startups', 'Entrepreneurship']
31
  def generate_post(topic=None):
32
  if topic is None:
33
  topic = random.choice(topics)
34
- prompt = f"Generate a high-quality, educational, and informative X (Twitter) post under 270 characters showcasing your expertise in {topic}. Make it engaging, insightful, and professional."
35
  response = query({
36
  "messages": [
37
  {
@@ -41,24 +41,28 @@ def generate_post(topic=None):
41
  ],
42
  "model": "deepseek-ai/DeepSeek-V3.2:novita",
43
  "max_tokens": 200,
44
- "temperature": 0.7
45
  })
46
  post_content = response["choices"][0]["message"]["content"].strip()
47
- # Ensure under 270 chars
48
  if len(post_content) > 280:
49
  post_content = post_content[:277] + "..."
50
  return post_content
51
 
52
- # Tweepy setup for X API
53
- auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
54
- auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
55
- api = tweepy.API(auth)
 
 
 
56
 
57
- # Function to post to X
58
  def post_to_x(content):
59
  try:
60
- api.update_status(status=content)
61
- return f"Posted successfully: {content}"
 
62
  except Exception as e:
63
  return f"Error posting: {str(e)}"
64
 
@@ -73,13 +77,15 @@ def run_scheduler():
73
  schedule.every(2).hours.do(auto_post)
74
  while True:
75
  schedule.run_pending()
76
- time.sleep(60) # Check every minute
77
 
78
  # Start scheduler in background
79
  threading.Thread(target=run_scheduler, daemon=True).start()
80
 
81
- # Gradio interface for manual generation and posting
82
  def manual_generate(topic):
 
 
83
  content = generate_post(topic)
84
  return content
85
 
@@ -88,16 +94,16 @@ def manual_post(content):
88
 
89
  with gr.Blocks() as demo:
90
  gr.Markdown("# AI/Tech/Startups/Entrepreneurship X Post Generator")
91
- gr.Markdown("Use this interface to manually generate and post high-quality content. Auto-posting runs every 2 hours in the background.")
92
 
93
  with gr.Row():
94
- topic_input = gr.Dropdown(choices=topics + ["Random"], label="Select Topic", value="Random")
95
  generate_btn = gr.Button("Generate Post")
96
 
97
- output_text = gr.Textbox(label="Generated Post (under 270 chars)", lines=5)
98
 
99
- post_btn = gr.Button("Post to X")
100
- post_result = gr.Textbox(label="Post Result")
101
 
102
  generate_btn.click(manual_generate, inputs=topic_input, outputs=output_text)
103
  post_btn.click(manual_post, inputs=output_text, outputs=post_result)
 
31
  def generate_post(topic=None):
32
  if topic is None:
33
  topic = random.choice(topics)
34
+ prompt = f"Generate a high-quality, educational, and informative X (Twitter) post under 280 characters showcasing expertise in {topic}. Make it engaging, insightful, and professional. Include relevant hashtags if natural."
35
  response = query({
36
  "messages": [
37
  {
 
41
  ],
42
  "model": "deepseek-ai/DeepSeek-V3.2:novita",
43
  "max_tokens": 200,
44
+ "temperature": 0.8
45
  })
46
  post_content = response["choices"][0]["message"]["content"].strip()
47
+ # Ensure under 280 chars
48
  if len(post_content) > 280:
49
  post_content = post_content[:277] + "..."
50
  return post_content
51
 
52
+ # Tweepy v2 Client setup (for posting tweets)
53
+ client = tweepy.Client(
54
+ consumer_key=CONSUMER_KEY,
55
+ consumer_secret=CONSUMER_SECRET,
56
+ access_token=ACCESS_TOKEN,
57
+ access_token_secret=ACCESS_SECRET
58
+ )
59
 
60
+ # Function to post to X (using v2 API)
61
  def post_to_x(content):
62
  try:
63
+ response = client.create_tweet(text=content)
64
+ tweet_id = response.data['id']
65
+ return f"Posted successfully! View: https://x.com/user/status/{tweet_id}\n\n{content}"
66
  except Exception as e:
67
  return f"Error posting: {str(e)}"
68
 
 
77
  schedule.every(2).hours.do(auto_post)
78
  while True:
79
  schedule.run_pending()
80
+ time.sleep(60)
81
 
82
  # Start scheduler in background
83
  threading.Thread(target=run_scheduler, daemon=True).start()
84
 
85
+ # Gradio interface
86
  def manual_generate(topic):
87
+ if topic == "Random":
88
+ topic = None
89
  content = generate_post(topic)
90
  return content
91
 
 
94
 
95
  with gr.Blocks() as demo:
96
  gr.Markdown("# AI/Tech/Startups/Entrepreneurship X Post Generator")
97
+ gr.Markdown("Generate and post high-quality content. Auto-posting every 2 hours in background.")
98
 
99
  with gr.Row():
100
+ topic_input = gr.Dropdown(choices=topics + ["Random"], label="Topic", value="Random")
101
  generate_btn = gr.Button("Generate Post")
102
 
103
+ output_text = gr.Textbox(label="Generated Post", lines=6)
104
 
105
+ post_btn = gr.Button("Post to X Now")
106
+ post_result = gr.Textbox(label="Result", lines=4)
107
 
108
  generate_btn.click(manual_generate, inputs=topic_input, outputs=output_text)
109
  post_btn.click(manual_post, inputs=output_text, outputs=post_result)