File size: 2,004 Bytes
c475dd2
 
b2af2fe
 
450b759
 
c475dd2
450b759
c475dd2
450b759
c475dd2
b64f5fb
c475dd2
 
b64f5fb
 
5697929
 
 
b64f5fb
1984678
5697929
1623793
5697929
1623793
 
 
 
 
 
 
 
5697929
1623793
 
5697929
1623793
 
5697929
 
 
c475dd2
5697929
 
 
 
 
 
 
 
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
import tweepy
import os
import gradio as gr

# Directly load the secrets from environment variables
api_key = os.getenv("API_key")
api_secret = os.getenv("API_key_secret")
bearer_token = os.getenv("Bearer_Token")
access_token = os.getenv("Access_Token")
access_token_secret = os.getenv("Access_Token_Secret")

clientw = tweepy.Client(bearer_token, api_key, api_secret, access_token, access_token_secret)
auth = tweepy.OAuth1UserHandler(api_key, api_secret, access_token, access_token_secret)
api = tweepy.API(auth)
from gradio_client import Client

# Assuming you have a Gradio client setup for your model
from gradio_client import Client
model_client = Client("https://tonic-surerag.hf.space/--replicas/m666j/")

def generate_and_tweet(query):
    # Generate text based on the user's query
    response = model_client.predict(query, api_name="/predict")
    
    # Assuming the response is a dictionary and the generated text is under a key, e.g., 'generated_text'
    # Adjust the key as per your model's response structure
    if isinstance(response, dict) and 'generated_text' in response:
        tweet_text = response['generated_text']
    elif isinstance(response, str):  # Direct string response
        tweet_text = response
    else:
        return "Error: The model's response was not in the expected format."
    
    # Ensure the tweet text is a string and trim it to Twitter's character limit
    tweet_text = str(tweet_text)[:280]  # Twitter's character limit is 280
    
    tweet_text = result[:180]  # Twitter's character limit is 280
    response = clientw.create_tweet(text=tweet_text)
    tweet_id = response.data['id']
    tweet_url = f"https://twitter.com/user/status/{tweet_id}"
    return f"Tweeted successfully! Here's the URL: {tweet_url}"

iface = gr.Interface(
    fn=generate_and_tweet,
    inputs="text",
    outputs="text",
    title="Generate and Tweet",
    description="Enter your query to generate a Twitter post about it and automatically tweet it."
)
iface.launch()