ErikDaska commited on
Commit
cb232b1
verified
1 Parent(s): 94f5ee8

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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()