Building ChatGPT Localhost Server

Valentin Ahrend
2 min readApr 15, 2023

In this short story I will state and explain how to create a localhost server that enters search inputs in ChatGPT and returns the output via a local API Endpoint. Please note that this method should only be used experimentally. For a commercial and professional usage of OpenAI products use their AI API Endpoints.

Overview

  1. Start by creating a new python project (via your favourite editor).
  2. Create the main.py file that acts as a Flask Server and uses Selenium to operate with a browser.
  3. I am currently using Chrome 112. It won’t be a problem to use other browsers but you might change your code slightly. Download the chromedriver and place it in the same folder as your main.py file.
  4. Now it’s code time. In your python file create your Flask Application. Open an Endpoint and retrieve the incoming data. Then enter the data into ChatGPT (via selenium). After doing so, wait for the answer of the machine and after detecting no change of the output the output will be sent back to the client.
  5. Before starting the Flask Server and opening the Endpoint start the selenium web driver and open the ChatGPT website.

Final Code

I highly recommend to not use this code in a professional way. It is a quick fix to try out OpenAIs ChatGPT locally and for free.

from flask import Flask, request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

import time
import flask

app = Flask(__name__) # Flask Application

browser = None


def start_web_driver():
global browser
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
browser = webdriver.Chrome(options=chrome_options)
cookie_str = open("cookies").read()
browser.get("https://chat.openai.com/")
for element in cookie_str.split("; "):
cookie = element.split("=")
print(cookie)
browser.add_cookie({
"name": cookie[0].replace(" ", ""),
"value": cookie[1],
"domain": ""
})

browser.get("https://chat.openai.com/")
# the user has to manually close dialogs or other problematic operations


def add_to_local_storage(browser0, var, val):
browser0.execute_script("localStorage.setItem(arguments[0],arguments[1])", var, val)


def get_element_by_multiple_classes(browser0, class_str):
classes = class_str.split(" ")
if len(classes) == 0:
return None
elements = browser0.find_elements(By.CLASS_NAME, classes[0])
solution = []
for element in elements:
print(str(element.get_attribute("class")))
if str(element.get_attribute("class")) == class_str:
solution.append(element)
return solution


# minimum return speed 5.0 seconds
@app.route("/call")
def call():
q = request.args.get("q")
idx = request.args.get("id")
print("Entering " + q + " in ChatGPT")
search_input = browser.find_elements(By.TAG_NAME, "textarea")[0]
search_input.send_keys(q)
search_input.send_keys(Keys.ENTER)
time.sleep(2.5)
current_var = ""
while True:
time.sleep(1.0)
els = browser.find_elements(By.CLASS_NAME, "markdown")
if len(els) >= int(idx):
return "nah, can't be the right id", 202
element = els[int(idx)]
text_elements = element.find_elements(By.TAG_NAME, "p")
if len(text_elements) > 0:
local_var = ""
for element in text_elements:
local_var += element.text
local_var += "\n"
if len(local_var) == len(current_var):
current_var = local_var
break
current_var = local_var
# retrieve answer
return current_var, 200


# Start Test Module
if __name__ == '__main__':
start_web_driver()
app.run(port=3003)

--

--

Valentin Ahrend

Self taught software developer. Sharing some ideas, experiences and projects.