HuggingFace镜像/Gemma-2B-Finetuined-pythonCode
模型介绍文件和版本分析
下载使用量0

开放思维

from openmind import AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
import torch.nn.functional as F


# 均值池化 - 考虑注意力掩码以进行正确的平均
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0] # model_output的第一个元素包含所有token嵌入
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path",
        type=str,
        help="Path to model",
        default="Rose/Gemma-2B-Finetuined-pythonCode",
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    model_path = args.model_name_or_path

    if is_torch_npu_available():
        device = "npu:0"
    else:
        device = "cpu"

    # 我们想要获取句子嵌入的句子
    sentences = ['This is an example sentence', 'Each sentence is converted']

    # 从openmind_hub加载模型
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModel.from_pretrained(model_path)

    # 对句子进行分词
    encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')

    # 计算token嵌入
    with torch.no_grad():
        model_output = model(**encoded_input)

    # 执行池化
    sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])

    # 归一化嵌入
    sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)

    print("Sentence embeddings:")
    print(sentence_embeddings)


if __name__ == "__main__":
    main()

Gemma-2B 微调 Python 模型

概述

Gemma-2B 微调 Python 模型是一款基于 Gemma-2B 架构的深度学习模型,专门针对 Python 编程任务进行了微调。该模型旨在理解 Python 代码,并通过提供建议、补全代码片段或提供修改意见来协助开发人员,从而提高代码质量和效率。

模型详情

  • 模型名称:Gemma-2B 微调 Python 模型
  • 模型类型:深度学习模型
  • 基础模型:Gemma-2B
  • 语言:Python
  • 任务:Python 代码理解与辅助

示例用例

  • 代码补全:根据部分输入自动补全代码片段。
  • 语法纠错:识别 Python 代码中的语法错误并提出修改建议。
  • 代码质量提升:提供增强代码可读性、效率和可维护性的建议。
  • 调试辅助:通过识别潜在错误或低效之处,为调试 Python 代码提供见解和建议。

如何使用

  1. 安装 Gemma Python 包:
     pip install -q -U transformers==4.38.0
     pip install torch

推理

  1. 如何在我们的笔记本中使用该模型:
# Load model directly
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("Mr-Vicky-01/Gemma-2B-Finetuined-pythonCode")
model = AutoModelForCausalLM.from_pretrained("Mr-Vicky-01/Gemma-2B-Finetuined-pythonCode")

query = input('enter a query:')
prompt_template = f"""
<start_of_turn>user based on given instruction create a solution\n\nhere are the instruction {query}
<end_of_turn>\n<start_of_turn>model
"""
prompt = prompt_template
encodeds = tokenizer(prompt, return_tensors="pt", add_special_tokens=True).input_ids

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
inputs = encodeds.to(device)


# Increase max_new_tokens if needed
generated_ids = model.generate(inputs, max_new_tokens=1000, do_sample=False, pad_token_id=tokenizer.eos_token_id)
ans = ''
for i in tokenizer.decode(generated_ids[0], skip_special_tokens=True).split('<end_of_turn>')[:2]:
    ans += i

# Extract only the model's answer
model_answer = ans.split("model")[1].strip()
print(model_answer)