Download app.py from Tonic/noorbot: direct link, hf CLI and curl.
- Browser
- Download file 2 kB
-
https://huggingface.co/spaces/Tonic/noorbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Tonic/noorbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Tonic/noorbot/resolve/main/app.py
2 kB
| 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() | |