【问题标题】:Streamlit Unhashable TypeError when i use st.cache当我使用 st.cache 时,Streamlit Unhashable TypeError
【发布时间】:2022-01-13 09:28:17
【问题描述】:

当我使用 st.cache 装饰器兑现拥抱脸变压器模型时,我得到了

不可散列的类型错误

这是代码

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')

【问题讨论】:

    标签: huggingface-transformers streamlit


    【解决方案1】:

    这对我有用:

    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 中高级缓存的更多信息。

    输出示例:

    【讨论】:

    • 这也有效,我试过 allow_output_mutation = True 也有效
    【解决方案2】:

    在 streamlit repo 的问题部分中搜索后

    我发现不需要散列参数,只需要传递这个参数

    allow_output_mutation = 真

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 1970-01-01
      • 2017-11-05
      • 2021-12-03
      • 2013-07-14
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多