InternLM Tutorial Notes (1)
InternLM Tutorial Beginner Notes。

Linux

SSH Connection

Create a personal development machine and SSH remote connection using VS Code on InternStudio platform.
Display summary information about GPU status, nvidia-smi

GPU status

Port Mapping

Use the ssh command for port mapping.
Run a web demo, helloworld.py

import socket
import re
import gradio as gr
 
# get hostname
def get_hostname():
    hostname = socket.gethostname()
    match = re.search(r'-(\d+)$', hostname)
    name = match.group(1)
    
    return name
 
# create Gradio interface
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()

When running in the terminal of the Web IDE without port mapping, you can’t access it with local IP, but after port mapping, you can see the web ui interface when you open the link in the web page.
VS Code provides automatic port mapping without the need to configure it manually.

web-demo

Python

Wordcount

Implements a wordcount function that counts the number of times each word occurs in an English string. Returns a dictionary with key as the word and value as the number of times the word occurs.
Example:
Input:

"""Hello world!  
This is an example.  
Word count is fun.  
Is it fun to count words?  
Yes, it is fun!"""

Output:

{'hello': 1,'world!': 1,'this': 1,'is': 3,'an': 1,'example': 1,'word': 1, 
'count': 2,'fun': 1,'Is': 1,'it': 2,'to': 1,'words': 1,'Yes': 1,'fun': 1  }

Function implementation, where it is assumed that ' is part of it's and will not be removed and split into it is. For simplicity, consider only the punctuation present in the example inputs:

def wordcount(text: str):
    punctuation = [",", ".", "!", "?"]
    for p in punctuation:
        text = text.replace(p, "")

    text = text.lower()

    words = text.split()

    word_count = {}
    for word in words:
        if word not in word_count:
            word_count[word] = 1
        else:
            word_count[word] += 1

    return word_count


if __name__ == "__main__":
    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.
    """

    result = wordcount(text)
    print(result)

Debug

Click on the left side of the line number to set a breakpoint in the code where you want to check. For example, set breakpoints at lines words = text.split() and word_count = {}.
Check for changes in the state of variable values during debugging.
Use the dictionary word_counts to count the number of times each word occurs. If the word is already in the dictionary, add 1 to the count; if the word is not in the dictionary, initialize the count to 1.

debug

Debugging results, the program executes normally and outputs the correct word count results:

wordcount-result

Git

Github Notes Repository

Reference

https://github.com/InternLM/Tutorial


Last modified on 2024-07-10