App works on new .py file but error after closing and reopen

Summary

I have a problem with running the application. The problem occurs with the following code snippet. When I create a new py. everything works fine. I saved a py. file and closed the editor and when I try to execute the same code again I get an error.

Requires any csv file with a column containing string (min. 50 rows to run Top2Vec)

Code snippet:

import pandas as pd
import io
import pickle
from top2vec import Top2Vec
import streamlit as st

@st.cache_resource()
def topic_modeling(docs):
    model = Top2Vec(docs, embedding_model='universal-sentence-encoder-multilingual')
    model.save("model_save")
    model = Top2Vec.load("model_save")
    return (model)

def pickle_model(model):
    f = io.BytesIO()
    pickle.dump(model, f)
    return f

uploaded_file = st.file_uploader("Choose a file", type=['csv'], help='Accept only CSV extenction')

if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
    df=df.head(1000)
    docs = list(df.loc[:,"NPSCombined"].values)
    model = topic_modeling(docs)
    file = pickle_model(model)
    st.write(model, file)
else:
    st.write("upload file")

Expected behavior:

Actual behavior:

Additional information

Please help me find a solution. Iโ€™m going to be all gray soon :slight_smile:

Hey @Keszzz,

Can you share an example CSV file that we can use to test your app? I get an error when I run your snippet locally due to the fact that my CSV file doesnโ€™t contain โ€œNPSCombined.โ€

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.