【书生大模型实战营】书生入门岛通关
Linux 基础知识
教程链接:链接
主要任务
完成SSH连接与端口映射并运行hello_world.py
流程
进入InternStudio创建好开发机后,点击ssh连接,如下:
出现如下界面:
将登录命令复制到自己电脑的命令行中,然后输入密码,如下:
然后进行端口映射部分。
首先使用命令vim hello_world.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()
生成的hello_world.py
的过程如下:
然后进行端口映射,具体的语法为:
ssh -p 34511 root@ssh.intern-ai.org.cn -CNg -L {本地机器_PORT}:127.0.0.1:{开发机_PORT} -o StrictHostKeyChecking=no
其中34511和ssh连接部分的端口一致。
然后,使用命令python hello_world.py
运行python文件,如下:
开发机上是7860端口,因此进行端口映射的命令为:
ssh -p 34511 root@ssh.intern-ai.org.cn -CNg -L 7861:127.0.0.1:7860 -o StrictHostKeyChecking=no
本地的端口可以随便选择一个能够访问的。另外在本地开一个命令行,执行上面的命令,有:
最后在本地访问127.0.0.1:7861,出现如下界面说明成功了:
Python 基础知识
任务说明
- Python实现wordcount:统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
- Vscode连接InternStudio debug笔记
流程
为了方便操作,首先先使用VSCode连接InternStudio。
先参考这部分教程下载好相关插件,然后进行ssh连接。
如下所示创建ssh连接:
输入之前的ssh连接命令,如下:
然后选择你想将配置文件保存的位置:
然后在右下角弹出的窗口中点击连接,选择Linux,然后输入密码,即可完成连接。
打开配置文件,可以发现信息已经添加:
完成连接后,按照下面的顺序,选择要操作的工作目录,我们选择root目录:
然后我们在VSCode中创建wordcount.py
文件,编写代码如下:
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
def wordcount(text):
# 分割字符串成单词列表
words = text.split()
# 手动计数每个单词的出现次数
word_count_dict = {}
for word in words:
if word in word_count_dict:
word_count_dict[word] += 1
else:
word_count_dict[word] = 1
print(word_count_dict)
text = text.replace("'s", "")
# 将字符串转换为小写并去除标点符号
cleaned_text = ""
for char in text:
if char.isalnum() or char.isspace():
cleaned_text += char.lower()
wordcount(cleaned_text)
然后对代码进行debug。首先设置如下4个断点:
然后找到debug的按钮,如下:
然后选择Python Debugger,再选择Python文件,如下:
运行到第一个断点时,可以发现结果是符合预期的,如下:
然后点击第一个,继续运行到下一个断点:
第二个断点时,该变小写的也变成了小写,如下:
运行到第三个断点,text根据空格被切分成了列表:
运行到第四个断点,得到了每个单词出现的次数的字典:
Git 基础知识
任务说明
- 破冰活动:自我介绍
- 实践项目:构建个人项目