Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +73 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,75 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 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 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import transformers
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
def infer(sent, max_length, num_return_sequences):
|
| 6 |
+
generator = pipeline('text-generation', model='gpt2')
|
| 7 |
+
|
| 8 |
+
return generator(sent, max_length=max_length, num_return_sequences=num_return_sequences)
|
| 9 |
+
|
| 10 |
+
def instantiate_gpt2(max_length_ : int, num_return_sequences : int, text : str) -> dict:
|
| 11 |
+
pipe = pipeline(task='text-generation', model='Iscte-Sintra/GPT2-Kriolu')
|
| 12 |
+
results = pipe(text, max_length=max_length_, num_return_sequences=num_return_sequences)
|
| 13 |
+
return results
|
| 14 |
+
|
| 15 |
+
def instantiate_roberta(top_k : int, text : str) -> dict:
|
| 16 |
+
pipe = pipeline("fill-mask", model="Iscte-Sintra/RoBERTa-Kriolu")
|
| 17 |
+
return pipe(text, top_k=top_k)
|
| 18 |
+
|
| 19 |
+
def build_gpt2_page():
|
| 20 |
+
try:
|
| 21 |
+
st.title("GPT-2 : Decoder")
|
| 22 |
+
max_length : int = st.sidebar.slider("Max Length", 10, 200)
|
| 23 |
+
num_return_sequences : int = st.sidebar.number_input('Number of Sequences to be Generated', min_value=1, max_value=10, value=1, step=1)
|
| 24 |
+
text : str = st.text_area("Text", "Katxor sta tr谩s di p贸rta.", height=75)
|
| 25 |
+
|
| 26 |
+
if st.button("Submit"):
|
| 27 |
+
results = instantiate_gpt2(max_length, num_return_sequences, text)
|
| 28 |
+
if results:
|
| 29 |
+
for result in results:
|
| 30 |
+
st.write(f"**Generated Text**: {result['generated_text']}")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.warning('Max length must be greater than default sentence number of tokens!', icon="鈿狅笍")
|
| 33 |
+
|
| 34 |
+
def build_roberta_page():
|
| 35 |
+
|
| 36 |
+
st.title("RoBERTa : Encoder")
|
| 37 |
+
|
| 38 |
+
top_k = st.sidebar.number_input('Number of predictions to return', min_value=1, max_value=5, value=1, step=1)
|
| 39 |
+
|
| 40 |
+
st.write("Enter a sentence with a **<mask>** token, and the model will predict the missing word.")
|
| 41 |
+
|
| 42 |
+
results = None
|
| 43 |
+
|
| 44 |
+
col1, col2 = st.columns(2)
|
| 45 |
+
|
| 46 |
+
with col1:
|
| 47 |
+
st.subheader("Input")
|
| 48 |
+
input_text = st.text_input("Input Sentence", "Nha ca<mask>, nha regra!")
|
| 49 |
+
|
| 50 |
+
submit = st.button("Submit")
|
| 51 |
+
try:
|
| 52 |
+
if submit and input_text:
|
| 53 |
+
results = instantiate_roberta(top_k, input_text)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.warning('There must be a special token "<mask>" in sentence!', icon="鈿狅笍")
|
| 56 |
+
|
| 57 |
+
with col2:
|
| 58 |
+
st.subheader("Prediction")
|
| 59 |
+
if results:
|
| 60 |
+
predicted_text = st.text_input("Predicted Token", value=results[0]['sequence'], disabled=True)
|
| 61 |
+
for result in results:
|
| 62 |
+
st.write(f"**Prediction**: {result['token_str']} | **Confidence**: {round(result['score'], 4)}")
|
| 63 |
+
else:
|
| 64 |
+
predicted_text = st.text_input("Predicted Token", disabled=True)
|
| 65 |
+
|
| 66 |
+
# Your dictionary of models
|
| 67 |
+
model_dict = {'RoBERTa': 1, 'GPT-2': 2}
|
| 68 |
+
|
| 69 |
+
# Always appears at the top of the sidebar
|
| 70 |
+
selected_model = st.sidebar.selectbox("Architecture", list(model_dict.keys()))
|
| 71 |
+
|
| 72 |
+
if model_dict[selected_model] == 1:
|
| 73 |
+
build_roberta_page()
|
| 74 |
+
else:
|
| 75 |
+
build_gpt2_page()
|