Comments (3)
def get_input_box():
"""Get the child textarea of `PromptTextarea__TextareaWrapper`"""
return PAGE.query_selector("textarea")
def is_logged_in():
# See if we have a textarea with data-id="root"
return get_input_box() is not None
def is_loading_response() -> bool:
"""See if the send button is diabled, if it does, we're not loading"""
return not PAGE.query_selector("textarea ~ button").is_enabled()
def send_message(message):
# Send the message
box = get_input_box()
box.click()
box.fill(message)
box.press("Enter")
def get_last_message():
"""Get the latest message"""
while is_loading_response():
time.sleep(0.25)
page_elements = PAGE.query_selector_all("div[class*='request-:']")
last_element = page_elements.pop()
return last_element.inner_text()
def regenerate_response():
"""Clicks on the Try again button.
Returns None if there is no button"""
try_again_button = PAGE.query_selector("button:has-text('Try again')")
if try_again_button is not None:
try_again_button.click()
return try_again_button
def get_reset_button():
"""Returns the reset thread button (it is an a tag not a button)"""
return PAGE.query_selector("a:has-text('Reset thread')")
@APP.route("/chat", methods=["GET"]) #TODO: make this a POST
def chat():
message = flask.request.args.get("q")
print("Sending message: ", message)
send_message(message)
response = get_last_message()
print("Response: ", response)
return response
# create a route for regenerating the response
@APP.route("/regenerate", methods=["POST"])
def regenerate():
print("Regenerating response")
if regenerate_response() is None:
return "No response to regenerate"
response = get_last_message()
print("Response: ", response)
return response
@APP.route("/reset", methods=["POST"])
def reset():
print("Resetting chat")
get_reset_button().click()
return "Chat thread reset"
@APP.route("/restart", methods=["POST"])
def restart():
global PAGE,BROWSER,PLAY
PAGE.close()
BROWSER.close()
PLAY.stop()
time.sleep(0.25)
PLAY = sync_playwright().start()
BROWSER = PLAY.chromium.launch_persistent_context(
user_data_dir="/tmp/playwright",
headless=False,
)
PAGE = BROWSER.new_page()
PAGE.goto("https://chat.openai.com/")
return "API restart!"
def start_browser():
PAGE.goto("https://chat.openai.com/")
if not is_logged_in():
print("Please log in to OpenAI Chat")
print("Press enter when you're done")
input()
else:
print("Logged in")
APP.run(port=5001, threaded=False)
if __name__ == "__main__":
start_browser()
from unofficial-chatgpt-api.
from unofficial-chatgpt-api.
Fixed #17
Thanks @CoolLoong
from unofficial-chatgpt-api.
Related Issues (12)
- Didn't work for me, followed the steps, getting playwright._impl._api_types.Error: value: expected string, got undefined HOT 2
- too many errors
- 为GPT-chitchat模型写后端接口
- 如何使用chatgpt模型写一个本地应用
- 为本地chatgpt模型写API接口
- how to write a local application with chatgpt model
- Write API interface for native chatgpt model HOT 1
- I can't get passed the verify you are a human bottom. Any suggestions? HOT 8
- Getting this right after I login and hit enter HOT 5
- Incorrect usage of the term 'API' in project description HOT 6
- Improve response time
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unofficial-chatgpt-api.