Streamlit Unhashable TypeError 当我使用 st.cache
Streamlit Unhashable TypeError when i use st.cache
当我使用 st.cache 装饰器兑现抱脸变换器模型时,我得到
Unhashable 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')
这对我有用:
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.
中阅读更多关于 stremlit 中的高级缓存的信息
输出示例:
在 streamlit 存储库的问题部分中搜索后
我发现散列参数不是必需的,只需要传递这个参数
allow_output_mutation = True
当我使用 st.cache 装饰器兑现抱脸变换器模型时,我得到
Unhashable 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')
这对我有用:
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.
中阅读更多关于 stremlit 中的高级缓存的信息输出示例:
在 streamlit 存储库的问题部分中搜索后
我发现散列参数不是必需的,只需要传递这个参数
allow_output_mutation = True