Spaces:
Runtime error
Runtime error
File size: 1,964 Bytes
1e434e8 8ddfa6e 1e434e8 961346c 8ddfa6e 1f6bd67 7a5750a 1f6bd67 1e434e8 1f6bd67 961346c 1e434e8 15ccfd9 8fa0f4a 8b09f28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
from distutils.command.upload import upload
import pandas as pd
import streamlit as st
from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
from transformers import pipeline
@st.cache
def load_data(file):
df = pd.read_csv(file, encoding='utf-8', nrows=50)
return df
def load_pipeline(model_cp, tokenizer_cp):
return pipeline("question-answering", model=model_cp, tokenizer=tokenizer_cp)
def choose_model():
with st.sidebar: # streamlit doesn't know how to has this by default?
st.write("# Model Selection")
model_cp = st.selectbox('Select model for inference',
('deepset/roberta-base-squad2',
'aidan-o-brien/recipe-improver'))
# If not my model > model_cp = tokenizer_cp, else > albert tokenizer
if model_cp == "aidan-o-brien/recipe-improver":
return model_cp, "albert-base-v2"
else:
return model_cp, model_cp
# Page config
title = "Recipe Improver"
icon = "🍣"
st.set_page_config(page_title=title, page_icon=icon)
st.title(title)
# Load model and tokenzier
model_cp, tokenizer_cp = choose_model()
question_answer = load_pipeline(model_cp, tokenizer_cp)
st.write("Model and tokenizer successfully loaded.")
# Upload csv - format with expander for aesthetics
with st.expander("Upload csv file"):
uploaded_file = st.file_uploader("Choose a csv file", type="csv", key='file_uploader')
# If file is uploaded, run inference
if uploaded_file is not None:
df = load_data(uploaded_file)
# Run inference on first example
first_example = df['review'][0]
question = "how to improve this recipe?"
answer = question_answer(question=question, context=first_example)
# Present results
st.markdown(f"""
# Results
The review provided was:
{first_example}
The answer given to the question 'how to improve this recipe?' was:
{answer['answer']}
""") |