File size: 2,314 Bytes
30c6353
 
 
 
 
 
 
 
80b5945
30c6353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34fd186
30c6353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4456cd
30c6353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
from styleformer import Styleformer
import streamlit as st
import numpy as np
import json

class Demo:
    def __init__(self):
        st.set_page_config(
            page_title="DEMO",
            initial_sidebar_state="expanded"
            )
        self.style_map = {
            #key : (name , style_num)
            'ctf': ('Casual to Formal', 0),
            'ftc': ('Formal to Casual', 1),
            'atp': ('Active to Passive', 2),
            'pta': ('Passive to Active', 3)
            }
        with open("streamlit_examples.json") as f:
            self.examples = json.load(f)

    @st.cache(show_spinner=False, suppress_st_warning=True, allow_output_mutation=True)
    def load_sf(self, style=0):
        sf = Styleformer(style = style)
        return sf
        
    def main(self):
        st.title("Styleformer")
        st.write('A Neural Language Style Transfer framework to transfer natural language text smoothly between fine-grained language styles like formal/casual, active/passive, and many more')

        style_key = st.sidebar.selectbox(
            label='Choose Style',
            options=list(self.style_map.keys()),
            format_func=lambda x:self.style_map[x][0]
            )
        exp = st.sidebar.expander('Knobs', expanded=True)
        with exp:
            quality_filter = exp.slider(
                label='Quality filter',
                min_value=0.5,
                max_value=0.99,
                value=0.95
                )
        with st.spinner('Loading model..'):
            sf = self.load_sf(self.style_map[style_key][1])
        input_text = st.selectbox(
            label="Choose an example",
            options=self.examples[style_key]
            )
        input_text = st.text_input(
            label="Input text",
            value=input_text
        )

        if input_text.strip():
            result = sf.transfer(input_text, quality_filter=quality_filter, max_candidates=1)
            st.markdown(f'#### Output:')
            st.write('')
            if result:
                st.success(result)
            else:
                st.info('No good quality transfers available !')
        else:
            st.warning("Please select/enter text to proceed")
        


if __name__ == "__main__":
    obj = Demo()
    obj.main()