├── README.md └── streamlit.py /README.md: -------------------------------------------------------------------------------- 1 | # GPT-3 Browser Automation Demo 2 | 3 | # DEMO 4 | 5 | https://twitter.com/dory111111/status/1609734301658275841 6 | 7 | # Requirements 8 | 9 | ```bash 10 | pip install openai streamlit selenium 11 | ``` 12 | 13 | # Usage 14 | 15 | ```bash 16 | streamlit run streamlit.py 17 | ``` 18 | -------------------------------------------------------------------------------- /streamlit.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | import streamlit as st 4 | from langchain import PromptTemplate, OpenAI 5 | from langchain.chains import PALChain 6 | from selenium import webdriver 7 | from selenium.webdriver.common.by import By 8 | from contextlib import contextmanager, redirect_stdout 9 | 10 | from io import StringIO 11 | 12 | @contextmanager 13 | def st_capture(output_function): 14 | with StringIO() as stdout, redirect_stdout(stdout): 15 | old_write = stdout.write 16 | 17 | def new_write(string): 18 | ret = old_write(string) 19 | output_function(escape_ansi(stdout.getvalue())) 20 | return ret 21 | 22 | stdout.write = new_write 23 | yield 24 | 25 | def escape_ansi(line): 26 | return re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]').sub('', line) 27 | 28 | 29 | template = """If someone asks you to perform a task, your job is to come up with a series of Selenium Python commands that will perform the task. 30 | There is no need to need to include the descriptive text about the program in your answer, only the commands. 31 | Note that the version of selenium is 4.7.2. 32 | find_element_by_class_name is deprecated. 33 | Please use find_element(by=By.CLASS_NAME, value=name) instead. 34 | You must use detach option when webdriver 35 | You must starting webdriver with --lang=en-US 36 | 37 | Begin! 38 | Your job: {question} 39 | """ 40 | 41 | st.set_page_config(layout="wide") 42 | st.title('🐧 Demo: Using GPT-3 for Browser Automation') 43 | col1, col2 = st.columns(2) 44 | 45 | with col1: 46 | openai_api_key = st.text_input(label="OpenAI API key", placeholder="Input your OpenAI API key here:",type="password") 47 | question = st.text_area( 48 | label = "Input" , 49 | placeholder = "e.g. Go to https://www.google.com/ and search for GPT-3" 50 | ) 51 | start_button = st.button('Run') 52 | 53 | with col2: 54 | if start_button: 55 | with st.spinner("Running..."): 56 | llm=OpenAI(temperature=0,openai_api_key=openai_api_key) 57 | chain = PALChain.from_colored_object_prompt(llm, verbose=True) 58 | output = st.empty() 59 | with st_capture(output.code): 60 | prompt = PromptTemplate( 61 | template=template, 62 | input_variables=["question"] 63 | ) 64 | prompt = prompt.format(question=question) 65 | chain.run(prompt) --------------------------------------------------------------------------------