当前位置: 首页 > news >正文

jupyter notebook用简易python代码跑本地模型

  1. python代码
# ========== 1. Rule-Based(原始版) ==========
def rule_based_sentiment(text):positive_words = ["good", "great", "happy", "love"]negative_words = ["bad", "sad", "terrible", "hate"]text = text.lower()if any(word in text for word in positive_words):return "Positive"elif any(word in text for word in negative_words):return "Negative"else:return "Neutral"print("Rule-Based (orig):", rule_based_sentiment("I love NLP but hate math!"))# ========== 2. Rule-Based(改进版:计数) ==========
def rule_based_sentiment_improved(text):positive_words = ["good", "great", "happy", "love"]negative_words = ["bad", "sad", "terrible", "hate"]text = text.lower()pos_count = sum(word in text for word in positive_words)neg_count = sum(word in text for word in negative_words)if pos_count > neg_count:return "Positive"elif neg_count > pos_count:return "Negative"else:return "Neutral"print("Rule-Based (improved):", rule_based_sentiment_improved("I love NLP but hate math!"))# ========== 3. Naive Bayes(不变) ==========
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNBtexts = ["I love NLP", "This is great", "I hate this", "This is terrible"]
labels = ["Positive", "Positive", "Negative", "Negative"]vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
model = MultinomialNB()
model.fit(X, labels)print("Naive Bayes:", model.predict(vectorizer.transform(["I love this project"]))[0])# ========== 4. Transformer(显式指定模型,消除警告) ==========
from transformers import pipelinesentiment_model = pipeline("sentiment-analysis",model="D:\hf\SmolLM3-3B"
)result = sentiment_model("I love NLP but hate math!")
print("Transformer:", result[0]['label'])

  1. 下载相关模块
python -m pip install scikit-learn  -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers
pip install Torch
  1. 拉取模型
pip install -U huggingface_hub

huggingface-cli download <模型名称> ^--local-dir <本地路径> ^--local-dir-use-symlinks False
  1. 模型名称在下面网址查询

hugging face 官网

hugging face 国内镜像站点如果官网不稳定无法进入

  1. 问题

原因:模型不匹配,笔者下载的SmolLM3-3B模型并不是专门做情感微调的模型,这里可以看到代码成功调用本地SmolLM3-3B模型但出现警告。

原因:无法成功链接模型,出现此处问题的python代码如下:

def rule_based_sentiment(text):positive_words = ["good", "great", "happy", "love"]negative_words = ["bad", "sad", "terrible", "hate"]text = text.lower()if any(word in text for word in positive_words):return "Positive"elif any(word in text for word in negative_words):return "Negative"else:return "Neutral"print("Rule-Based:", rule_based_sentiment("I love NLP but hate math!"))# Statistical (Naive Bayes)
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB# Training Data
texts = ["I love NLP", "This is great", "I hate this", "This is terrible"]
labels = ["Positive", "Positive", "Negative", "Negative"]vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)model = MultinomialNB()
model.fit(X, labels)print("Naive Bayes:", model.predict(vectorizer.transform(["I love this project"]))[0])# Transformer (Pre-trained with Hugging Face)
from transformers import pipeline
sentiment_model = pipeline("sentiment-analysis")
print("Transformer:", sentiment_model("I love NLP but hate math!")[0])

此处是下载本地时可能会出现的警告:huggingface-cli download 已经被废弃,请使用hf download下载

结语:朝乾夕惕,功不唐捐,玉汝于成

http://www.dtcms.com/a/397903.html

相关文章:

  • Android 安卓RIL介绍
  • 开源 java android app 开发(十五)绘图定义控件--仪表盘
  • Android如何自动弹出软键盘?
  • Linux Shell 脚本:从零到进阶的实战笔记
  • MR 一体机市场报告:2031全球规模突破 1.98亿美元,中国 40.8% 市占率成核心增长极
  • 网站管理员权限权重高的网站有哪些
  • 【Spark+Hive+hadoop】基于spark+hadoop基于大数据的全球用水量数据可视化分析系统大数据毕设
  • 07.【Linux系统编程】进程控制(进程创建fork、进程终止exit等、进程等待waitwaitpid、进程替换execl等)
  • 百度Qianfan-VL系列上线:推出3B/8B/70B三款视觉理解模型,覆盖不同算力需求
  • 基于 Python Keras 实现 猫狗图像的精准分类
  • 点云-标注-分类-航线规划软件 (一)点云自动分类
  • 挑战用R语言硬干一百万单细胞数据分析
  • 如何自己弄个免费网站wordpress前端登陆
  • npm install 时包库找不到报错解决
  • 【开题答辩实录分享】以《城市网约车服务预约与管理小程序的设计与实现》为例进行答辩实录分享
  • 网站建设软件哪个最好wordpress转发插件
  • C#异步协同常用例子
  • Flutter 中使用 Color 的最优方案
  • 一半都有哪些做影视外包的网站怎么做网站的签约编辑
  • Qt QEventLoop的使用的一个问题讨论
  • 保定网站seo技术wordpress主题左目录
  • net网站开发做手工简笔上海手机网站建设
  • 做地图的网站湖北专业网站建设市面价
  • 拜师做网站网站短链接生成
  • 用狐狸做logo的网站虾皮跨境电商注册多少钱
  • 东莞化工网站建设网站的思维导图怎么做
  • 公司网站制作银川微信投票网站怎么做
  • 英国零售电商网站开发好点的开发网站的公司
  • 韶关最新消息厦门关键词排名优化
  • 家居网站建设流程企业网站是怎么建站的