一、开发环境介绍

1、创建开发机

(1)、登录InternStudio平台创建环境

1、登录https://studio.intern-ai.org.cn/网站,打开首页,创建开发机

image-20240721233024132

2、填写相关配置信息创建开发机

image-20240721233101796

3、等待开发机创建完成,如图所示

image-20240721233140288

4、进入开发机,页面显示如图

image-20240721233322349

二、实现端口映射

1、SSH连接实现

1、找到开发机>SSH连接,点击进入

image-20240721233640917

2、复制连接的登录命令和密码

image-20240721233734525

3.Windows打开终端命令行,将登入命令复制到命令行,出现下面框框表示连接成功

这里我windows提前配置的SSH KEY所以没有登录密码验证,如果出现密码验证复制上面的密码即可

设置密码验证方法:

但是在我们开发学习的时候,每次远程都输入密码比较麻烦,我们可以设置SSH key来跳过输入密码这一步骤,在ssh命令中我们可以使用ssh-keygen命令来生成密钥

如果大家计算机的用户名是中文的,InternStudio会识别不了,这种情况就需要用密码来登录SSH,不过不影响使用,大家不用担心。

SSH密钥是一种安全便捷的登录认证方式,用于在SSH协议中进行身份验证和加密通信。

指定端口映射

ssh -CNg -L 6006:127.0.0.1:6006 root@ssh.intern-ai.org.cn -p 你的 ssh 端口号

ssh-keygen支持RSA和DSA两种认证密钥。

常用参数包括:

  • -t:指定密钥类型,如dsa、ecdsa、ed25519、rsa。
  • -b:指定密钥长度。
  • -C:添加注释。
  • -f:指定保存密钥的文件名。
  • -i:读取未加密的ssh-v2兼容的私钥/公钥文件。

这里我们使用RSA算法生成密钥,命令为:

ssh-keygen -t rsa

输入命令后一路回车就可以了,这里的密钥默认情况下是生成在~/.ssh/目录下的,~表示的是家目录,如果是windows就是C:\Users\{your_username}\。在powerShell中可以使用Get-Content命令查看生成的密钥,如果是linux操作系统可以使用cat命令。

image-20240721233817120

4、对应功能测试,可以在命令行输入命令测试

image-20240721234105097

2、vscode连接实现

(1)、安装远程连接插件Remote -ssh

image-20240721234321518

(2)、点击加号添加开发机SSH连接的登录命令

1、复制上面的SSH连接的登录命令

image-20240721234511521

2、出现提示

image-20240721234615319

表示已经连接成功

3、选择Linux

image-20240721234654983

如果出现

image-20240721234914435

可能需要终止原来的连接(windows命令行执行exit,exit两次)

4、出现开发机的根目录

image-20240721234752861

此时

image-20240721234828273

三、使用端口映射实现本地访问helloworld.py

1、新建一个py文件

在root新建一个helloworld.py文件,代码如下

import socket
import re
import gradio as gr

# 获取主机名
def get_hostname():
hostname = socket.gethostname()
match = re.search(r'-(\d+)$', hostname)
name = match.group(1)

return name

# 创建 Gradio 界面
with gr.Blocks(gr.themes.Soft()) as demo:
html_code = f"""
<p align="center">
<a href="https://intern-ai.org.cn/home">
<img src="https://intern-ai.org.cn/assets/headerLogo-4ea34f23.svg" alt="Logo" width="20%" style="border-radius: 5px;">
</a>
</p>
<h1 style="text-align: center;">☁️ Welcome {get_hostname()} user, welcome to the ShuSheng LLM Practical Camp Course!</h1>
<h2 style="text-align: center;">😀 Let’s go on a journey through ShuSheng Island together.</h2>
<p align="center">
<a href="https://github.com/InternLM/Tutorial/blob/camp3">
<img src="https://oss.lingkongstudy.com.cn/blog/202406301604074.jpg" alt="Logo" width="20%" style="border-radius: 5px;">
</a>
</p>

"""
gr.Markdown(html_code)

demo.launch()

2、使用端口映射

1、windows连接开发机

image-20240721235413175

2.运行代码,需要先安装gradio库,pip install gradio=4.29.0后运行python helloworld.py

image-20240721235553735

3、打开网页即可访问

image-20240721235447077