当我使用st.cache装饰器兑现拥抱面变压器模型时,我得到
无敌TypeError
这是密码
from transformers import pipeline
import streamlit as st
from io import StringIO
@st.cache(hash_funcs={StringIO: StringIO.getvalue})
def model() :
return pipeline("sentiment-analysis", model='akhooli/xlm-r-large-arabic-sent')发布于 2021-12-08 13:02:38
这对我起了作用:
from transformers import pipeline
import tokenizers
import streamlit as st
import copy
@st.cache(hash_funcs={tokenizers.Tokenizer: lambda _: None, tokenizers.AddedToken: lambda _: None})
def get_model() :
return pipeline("sentiment-analysis", model='akhooli/xlm-r-large-arabic-sent')
input = st.text_input('Text')
bt = st.button("Get Sentiment Analysis")
if bt and input:
model = copy.deepcopy(get_model())
st.write(model(input))备注1:使用输入model(input)调用管道会更改模型,我们不应该更改缓存的值,因此需要复制模型并在副本上运行它。
备注2:第一次运行将使用get_model函数加载模型,下一次运行将使用chace。
备注3:您可以在它们的documentation中阅读更多关于高级缓存的内容。
产出实例:


发布于 2021-12-08 13:39:07
在“流光回购”中的“问题”部分搜索后
我发现哈希参数不是必需的,只需要传递这个参数。
allow_output_mutation =真
https://stackoverflow.com/questions/70274841
复制相似问题