---
title: Qwen
canonical_url: "https://www.modelscope.cn/models/Ceasus/Qwen"
md_url: "https://www.modelscope.cn/models/Ceasus/Qwen.md"
repository: Ceasus/Qwen
chinese_name: "Qwen/Qwen2.5-7B-Instruct微调CEFR评级模型"
last_updated: 2025-02-08
pipeline_tag: text-generation
tasks:
  - text-generation
parameters: 8.0M
tensor_type:
  - F32
  - I32
library_name:
  - pytorch
  - safetensors
frameworks:
  - pytorch
downloads: 12
stars: 2
---

# Qwen

> Qwen - Ceasus 在 ModelScope 开源的模型。basemodel: /mnt/workspace/.cache/modelscope/hub/Qwen/Qwen25-7B-Instruct

- **Repository**: Ceasus/Qwen
- **Tasks**: text-generation
- **Parameters**: 8.0M
- **Downloads**: 12
- **Stars**: 2
- **Last updated**: 2025-02-08

Source: https://www.modelscope.cn/models/Ceasus/Qwen

---

base_model: /mnt/workspace/.cache/modelscope/hub/Qwen/Qwen2___5-7B-Instruct

library_name: peft
---

# Model Card for CEFR English Rating Model

## Model Details

### Model Description

该模型是基于Qwen2.5-7B-Instruct微调的CEFR英语评级模型，旨在对用户提供的特定英语词句进行CEFR评级，以及根据用户给出的原句与评级生成对应的英语句子等功能。CEFR分级是一种英语难度分级形式，提供了A1, A2, B1, B2, C1, C2的分级方式，A1为最易、C2为最难。

- **Developed by:** [Your Organization/Team Name]
- **Model type:** Transformer-based (Fine-tuned from Qwen2.5-7B-Instruct)
- **Language(s) (NLP):** English
- **License:** [Specify License Type]
- **Finetuned from model:** Qwen2.5-7B-Instruct

## Uses

### Direct Use

该模型可以直接用于对英语句子进行CEFR评级，帮助用户了解句子的难度水平。此外，还可以根据用户提供的原句和目标评级生成符合相应难度的英语句子。

### Downstream Use

该模型可以作为自然语言处理任务中的一个模块，例如在语言学习应用中，为用户提供个性化的学习内容和练习。

### Out-of-Scope Use

该模型不适用于非英语语言的评级任务，也不适用于与CEFR评级无关的自然语言处理任务。此外，由于模型是基于特定数据集微调的，可能在处理与训练数据分布差异较大的句子时表现不佳。

## Bias, Risks, and Limitations

### Recommendations

- 用户在使用模型时应注意其可能存在的偏差，尤其是在处理不同地区、不同风格的英语句子时。
- 模型的输出仅供参考，不应完全依赖其结果进行重要决策。
- 模型的训练数据可能存在局限性，因此在实际应用中需要结合其他方法进行综合评估。

## How to Get Started with the Model

使用以下代码开始使用该模型：

```python
from enum import Enum
from typing import Tuple,Generator
from numpy.typing import NDArray
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from IPython.display import Audio, display
from datetime import datetime
from modelscope import AutoModelForCausalLM, AutoTokenizer
import torch
import os

# 指定模型路径
model_name = "/mnt/workspace/PicTalk/model_call/models/checkpoint-90-gptq-int2" #修改为模型所在checkpoint文件夹的路径
# 检查是否有可用的GPU，如果有的话，使用第一个GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 加载模型和分词器
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map={"": device})
tokenizer = AutoTokenizer.from_pretrained(model_name)
# system对模型的预设
system = "你是一个英语教学专家。你需要根据CERF英语等级，如“A1,A2”，或者中国中学英语等级，如'junior,senior'，根据给定词汇生成等级对应的英语片段。如：input:'A2,laptop bird dustbin cup coffee hit mobilephone' output:'A bird hit a laptop, spilling coffee from a cup. A mobilephone fell in the dustbin.'"
print("微调模型加载完成，模型名为： " + model_name)


def test_level(message_input: str) -> Level:
    """
    Desc:
        Return the level of the user, according to the composition wrote from the user.
        
    Usecase:
        >>> test_level("We have so many good things to eat for dinner.")
        Level.A1
    """
    torch.cuda.empty_cache()

    # 将输入文本封装为对话格式
    messages = [
        {"role": "system", "content": system},
        {"role": "user", "content": message_input}
    ]
    
    # 使用模型生成回复
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )
    model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

    generated_ids = model.generate(
        **model_inputs,
        max_new_tokens=512
    )
    
    # 提取生成的文本
    generated_ids = [
        output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
    ]
    response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    
    # 根据生成的文本判断等级（这里只是一个简单的示例逻辑，可以根据需要调整）
    if "A1" in response:
        return Level.A1
    elif "A2" in response:
        return Level.A2
    elif "B1" in response:
        return Level.B1
    elif "B2" in response:
        return Level.B2
    elif "C1" in response:
        return Level.C1
    elif "C2" in response:
        return Level.C2
    else:
        return Level.A1  # 默认返回 A1 级别

def get_new_context(words: list[str], level: Level) -> str:
    """
    Desc:
        Generate a new context description using the given words, based on the user's level.
        
    Usecase:
        >>> get_new_context(["laptop", "cup", "mobilephone"], Level.A1)
        "There is a laptop, a cup, and a mobile phone."
    """
    # 将输入词汇和等级封装为对话格式
    input_text = f"{level.value},{','.join(words)}"
    messages = [
        {"role": "system", "content": system},
        {"role": "user", "content": input_text}
    ]
    
    # 使用模型生成上下文
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )
    model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
    generated_ids = model.generate(
        **model_inputs,
        max_new_tokens=512
    )
    
    # 提取生成的文本
    generated_ids = [
        output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
    ]
    response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return response

# 测试 test_level 函数
user_input = "We have so many good things to eat for dinner."
level = test_level(user_input)

# 测试 get_new_context 函数
words = ["laptop", "cup", "mobilephone"]
context = get_new_context(words, Level.A1)
    
    
# 函数的输出
print(f"Detected Level: {level}") # 应该输出输入句子的难度等级
print(f"Generated Context: {context}") # 应该输出根据难度等级和输入词汇造出的句子
```

## Training Details

### Training Data

该模型基于Qwen2.5-7B-Instruct进行微调，训练数据包括标注了CEFR等级的英语句子对。数据来源和预处理方法如下：

- **数据来源：**

  -  https://github.com/yukiar/CEFR-SP

  -  https://github.com/Maximax67/Words-CEFR-Dataset

  -  

- **预处理方法：** 对句子进行分词、编码等操作，处理为模型微调可接受的jsonl格式文件，并将其与对应的CEFR等级标签一起输入到模型中进行训练。具体格式如下：

  ```json
  {"messages": [{"role": "user", "content": "Do you need something to eat ?"}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "I have two fish in a bowl ."}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "She wanted the grey coat ."}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "I sent my girlfriend red roses ."}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "She needs to use your phone ."}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "Do you have a car ?"}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "Sometimes I sleep until noon ."}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "A monkey climbed on my shoulders ."}, {"role": "assistant", "content": "A1"}]}
  {"messages": [{"role": "user", "content": "It 's 10 p. m."}, {"role": "assistant", "content": "A1"}]}
  ```

- **微调脚本：**
```bach
CUDA_VISIBLE_DEVICES=0  nohup swift sft 
--torch_dtype 'bfloat16' 
--model 'Qwen/Qwen2.5-7B-Instruct' 
--model_type 'qwen2_5' 
--template 'qwen2_5' 
--system '你是一个英语教学专家。你需要根据CERF英语等级，如“A1,A2”，或者中国中学英语等级，如"junior,senior"，根据给定词汇生成等级对应的英语片段。如：input:"A2,laptop bird dustbin cup coffee hit mobilephone" output:"A bird hit a laptop, spilling coffee from a cup. A mobilephone fell in the dustbin."' 
--dataset 'data/modified_dataset.jsonl' 
--max_length '1024' 
--lora_rank '32' 
--lora_alpha '8' 
--init_weights 'True' 
--learning_rate '1e-4' 
--attn_impl 'flash_attn' 
--gradient_accumulation_steps '16' 
--eval_steps '500'  
--add_version False 
--output_dir /mnt/workspace/output/Qwen2.5-7B-Instruct/v2-20250202-112423 
--logging_dir /mnt/workspace/output/Qwen2.5-7B-Instruct/v2-20250202-112423/runs 
--ignore_args_error True
```

### Training Procedure

#### Preprocessing 

- 对输入句子进行分词处理，使用特定的分词器将句子转换为模型可接受的格式。
- 将句子及其对应的CEFR等级标签进行编码，形成训练所需的输入数据。

#### Factors

- **评估维度：** CEFR等级预测的准确性、生成句子的流畅性和相关性等。

### Results

![05fb1aecb2e5008aa508561575ec99e](images/05fb1aecb2e5008aa508561575ec99e.png)

#### Summary

该模型在CEFR等级预测和句子生成任务上表现出较好的性能，但在某些情况下可能存在偏差。建议用户在实际应用中结合其他方法进行综合评估。

### Compute Infrastructure

#### Hardware

- **训练硬件：** 3060 laptop

#### Software

- **框架版本：** PEFT 0.14.0

## Model Card Contact

- **联系人：** Cerasus
- **邮箱：** 1716696964@qq.com
