diff --git a/examples/sentiments/README.md b/examples/sentiments/README.md new file mode 100644 index 00000000..8db7cb29 --- /dev/null +++ b/examples/sentiments/README.md @@ -0,0 +1,22 @@ +# Tweet sentiments analysis + +This example takes a tweet's URL and analyse the sentiments of the text + +## Run the Example + +```bash +# Create a Python venv +python3 -m venv venv + +# Source it +source venv/bin/activate + +# Install the packages +pip install -r requirements.txt + +# Set your OpenAI key +export OPENAI_API_KEY=your-api-key + +# Run the example +gptscript tweet.gpt --url https://x.com/ibuildthecloud/status/1765748793998467179 +``` diff --git a/examples/sentiments/main.py b/examples/sentiments/main.py new file mode 100644 index 00000000..5b01dbda --- /dev/null +++ b/examples/sentiments/main.py @@ -0,0 +1,24 @@ +import sys +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + +# Get url +url = sys.argv[1] + +# Setup WebDriver options +options = webdriver.ChromeOptions() +options.add_argument("--headless") +options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36") +driver = webdriver.Chrome(options=options) + +# Navigate to the page and get field +driver.get(url) +try: + element = WebDriverWait(driver, 50).until( + EC.presence_of_element_located((By.CSS_SELECTOR, "div[data-testid='tweetText'] > span")) + ) + print(element.text) +finally: + driver.quit() diff --git a/examples/sentiments/requirements.txt b/examples/sentiments/requirements.txt new file mode 100644 index 00000000..80c2c892 --- /dev/null +++ b/examples/sentiments/requirements.txt @@ -0,0 +1 @@ +selenium==4.18.1 \ No newline at end of file diff --git a/examples/sentiments/tweet.gpt b/examples/sentiments/tweet.gpt new file mode 100644 index 00000000..5a25acf2 --- /dev/null +++ b/examples/sentiments/tweet.gpt @@ -0,0 +1,24 @@ +tools: sys.write, get-tweet-text, sentiments +description: get the sentiments expressed in a tweet +args: url: URL of the tweet to analyze + +Perform the actions in the following order: + +Get the text of the tweet referenced by "${url} and save it in the file content.txt +Next analyze the sentiments expressed in this file + +--- +name: get-tweet-text +description: get text content of a tweet +args: url: url towards a tweet + +#!python3 main.py "$url" + +--- +name: sentiments +description: Get the sentiment of the writer +args: content: any string + +You are a program designed to assess the sentiments expressed by a writer in a text. + +Do not give too many details, just the overall sentiments that is expressed in "${content}" \ No newline at end of file