Cloud server builds Python project to achieve academic optimization chatgpt

1 Server preparation

A server that can access openAI. Please refer to my previous article for details on the mainland configuration:

Cloud Function builds the OpenAI agent available in the Mainland

Here we use Tencent cloud server, OpenCloud centos 8.6 system

2 Cloud server configuration

2.0 FTP configuration

The previous article wrote how to configure FTP for file transfer with linux cloud server

The second chapter in Cloud Function Building OpenAI Agents Available in the Mainland

2.1 python virtual environment

The cloud server seems to have its own python3.6.8. Since the environment required by the project is relatively new, it needs to be reinstalled. The newer the better, otherwise there will be many inexplicable problems, or you will be slaughtered by the virtual environment first. For example:
insert image description here
uninstall Python 3.6.8:

sudo yum remove python=3.6.8

Or uninstall Python (recommend this, uninstall all, to avoid some of the remaining python2 for you):

sudo yum remove python

2.1.1 python3.9 installation configuration

  1. Install Python 3.9:
sudo yum -y install python=3.9
  1. Check that Python 3.9 was successfully installed:
python --version
  1. If the installation is unsuccessful, you can try to install the EPEL warehouse first, and then repeat 1 and 2
sudo yum -y install epel-release

If you are not centos, the third-party software package that comes with it may not be yum, you can try it with apt-get

2.1.2 Download python project

  1. Find a folder to put the project in, and confirm that you can find this path.
 git clone https://github.com/binary-husky/chatgpt_academic.git

If you have not installed git, you still need to install git, but I think it is too troublesome to install git, and the download speed from github may be slow, so I downloaded it locally and transferred it via ftp.

And it is more convenient to modify it if you download it locally, I don't think you want to use that nasty vim, do you?

  1. Copy config.py to a config_private.py file, and modify the API_URL in it to refer to the Hong Kong proxy address obtained in the previous article plus the following section /v1/chat/completions

Because the Hong Kong proxy address only means https://api.openai.com

  1. Because it is directly mapped, no proxy is needed,USE_PROXY = False

  2. API_KEY Fill in your own key

  3. In addition, remember to add a parameter in the last paragraph of the main.py file share=True, otherwise no external link will be generated (not only accessible in the LAN).

demo.queue(concurrency_count=CONCURRENT_COUNT).launch(server_name="0.0.0.0", share=True, server_port=PORT, auth=AUTHENTICATION, favicon_path="docs/logo.png")

insert image description here
insert image description here

2.1.3 Create python virtual environment

If your linux has a graphical interface, you can consider using pycharm. It is more friendly to install python in it and get rid of the command line.

To create a virtual environment in Python,

Find a folder to put the virtual environment, I put it in the project directory,

You can follow these steps:

  1. Install virtualenvthe module:

    pip install virtualenv
    
  2. At the command line, navigate to the directory where you want to create the virtual environment, and create the virtual environment:

    virtualenv env_name
    

    where env_nameis the name you want to give the virtual environment. envNote that this will create a new folder in the current directory called and install the virtual environment into it.

  3. Activate the virtual environment. To activate a virtual environment from the command line, execute the following command:

    source env_name/bin/activate
    

    On Windows systems, to activate the virtual environment, run the following command:

    env_name\Scripts\activate
    
  4. Now in the virtual environment, you can install the corresponding installation package by yourself. In this project,

    python -m pip install -r requirements.txt
    

It should be noted that the directory at this time must be the directory of the project, otherwise the requirements.txt file cannot be found, you can pwdcheck the current directory path, and you can also lscheck which files are under the current path.

  1. With the environment and the project, you can run it directly.

    python main.py
    

If you made the changes in section 2.1.2 ok, it's done and two links will be generated. The output looks something like this:

insert image description here

It's ready now. It can indeed be opened through the public URL,

As for whether you can answer the question, it depends on whether your APIkey is correct and whether the proxy address is correct.

insert image description here

But, there is a very embarrassing problem. When you exit the python program, the program exits and the link becomes invalid.

And when your local host closes the connection with the cloud server, the python program also exits,

If you can't run off the local host without closing the connection, what's the point of going to the cloud?

So, how to stay forever?

3 Project "service" (python project running in the background)

In order to make it meaningful to go to the cloud,

The first reaction is to "service" the program

After thinking about the key words, it is running in the background.

in the previous project directory; in the previous virtual environment

nohup python -u main.py > logfile.log 2>&1 &

explain:

  • nohup is not specified to run in the background, but this nohup is to let the program run forever, it has nothing to do with the front and back (this does not need to be changed)

  • "-u" means that the cache is not enabled, and the print information is output to the log file in real time (if you do not add -u, the log file will not refresh the information of the print function in the code in real time), but the program still runs, but overall it is still not
    good Comfortable, out of control. That is, you feel that the program seems to have collapsed.

  • main.py This is the main program of the project, which is the program you want to run

  • "> logfile.log" This closing bracket is to put your output log into the following logfile.log log file

  • 2>&1 This 2 represents the error message output when an error occurs >& This represents redirection; 1 represents standard output, so the combination is to redirect the error message to standard output

  • & This means running in the background

Then the specific output information can be seen in the logfile.log file. The command to view is:

cat logfile.log

localURLandpublicURLit's all in there, including yourweb port, proxy information, and so on. Although publicURL says it is valid for 72 hours, but after my actual measurement, it is indefinite, so be happy.

If it is not done well, the nohup command will also give you a process number. If you find something wrong, remember to kill the process and start again (after all, the nohup command is to run in the background forever!)

kill -9 PID

-9 should be an attribute such as killing recklessly (don't think it's the process number, I just played myself to death like this); PID refers to the process number, which is the value returned by the above command.

If you feel that everything is fine after running in the background, then close it, and then find something is wrong, but you can’t find the process, how to find the process?

ps aux |grep main

That’s all.

But this link forwarded by huggingface will lead to slower loading. And if you want to access through your public network ip or through your domain name,

Consider localURL

4 localURL access (public network ip access, domain name access)

If you want to access directly through localURL, you need to add firewall rules to the cloud server and open ports in the cloud server system.

4.1 Add firewall rules to the cloud server

insert image description here

4.2 The firewall of the cloud server system releases the port

Instead of iptables, use firewalld

  1. Open the firewall service firewalld
# 开启防火墙服务
sudo service firewalld start
或者
sudo systemctl start firewalld
# 重启防火墙服务
sudo service firewalld restart
# 关闭防火墙服务
sudo service firewalld stop
  1. View firewall service status
# 查看防火墙服务状态
sudo systemctl status firewalld
# 查看防火墙状态
sudo firewall-cmd --state
  1. In the running state, add the required ports to the firewall
# 查看防火墙服务状态
sudo firewall-cmd --permanent --zone=public --add-port=666/tcp
或者
sudo firewall-cmd --zone=public --add-port=5151/tcp --permanent

# 重新加载配置
sudo firewall-cmd --reload

The function of the command is to permanently open ports 666 and 5151 of the tcp protocol to the public domain as an administrator, that is, to allow the tcp traffic of these two ports to pass through.

Add those ports you want.

If you make a mistake and want to delete it, just change add-port to remove-port. For example, Google has disabled some unsafe ports:
Google restricts ports
. 6666 is in it.

sudo firewall-cmd --permanent --zone=public --remove-port=6666/tcp
  1. View open ports
# 查看对公开域永久放开的端口
sudo firewall-cmd --permanent --zone=public --list-ports
或者
# 列出所有的firewalld规则
sudo firewall-cmd --list-all

If you run the python project in the background first, find a random web port, and then add firewall rules, you can already access the web application through the public network ip+port.

If you want to fix this released port, so as not to re-release the port every time you run it, then modify the python configuration file to set the fixed port.

4.3 Modify the port in the python project

Go to the python project path to modify the port in the config_private.py configuration file, WEB_PORT = -1which means that the port is random every time it is turned on, which is troublesome, so I changed it to the port I want to let go, corresponding to the previous firewall rules.

insert image description here

insert image description here

Then rerun it as in Chapter 3.

5 domain name access

5.1 Filing

After working on it for a few days, it was extremely troublesome, the requirements were still strict, and my mentality collapsed.

5.2 Modify DNS resolution (if you don't want to file, use Cloudflare)

Just register for an account with Cloudflare dashboard
and start using it for free.

  1. Sign up for a Cloudflare dashboard account to start using it for free.
  2. Add site (Add site), enter your domain name.
  3. Then just follow the instructions, if not, just follow the default, save all the way to the end, and save the DNS server he gave (to be modified to the DNS server of the cloud server later). After submitting, you will receive an email that Cloudflare has parsed within 24 hours:
    insert image description here
  4. Find the domain name registration in the cloud product of the domain name you bought, then choose to manage the domain name, modify the DNS server in the DNS resolution, and replace it with the address you just saved.

insert image description here
insert image description here
5. Wait for it to take effect,

https analysis,,, to be written.

Guess you like

Origin blog.csdn.net/weixin_43938876/article/details/130652335