File size: 4,629 Bytes
761c54f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84054bd
761c54f
84054bd
761c54f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84054bd
 
 
761c54f
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from styleformer import Styleformer
import torch
import warnings

warnings.filterwarnings("ignore")
import gradio as gr

choices = ['Informal to Formal', 'Formal to Informal','Active to passive','Passive to active']


def para1(choices, source_sentences):
    if choices == "Informal to Formal":
        sf = Styleformer(style=0)
        sentance1 = list(source_sentences.split("."))
        output_sentance = []
        for source_sentence in sentance1:
            target_sentence = sf.transfer(source_sentence)
            if target_sentence is not None:
                output_sentance.append(target_sentence)
                #print(target_sentence)
            else:
                output_sentance.append(target_sentence)
                #print(target_sentence)
                output_sentance.append(target_sentence)
        res = [i for i in output_sentance if i is not None]
        #print(output_sentance)
        #print(res)
        final = ""
        for value in res:
            joint_value = "".join(value)
            if final == "":
                final += joint_value
            else:
                final = f"{final}.{joint_value}"
        final = final.replace("..", ".")
        #print(final)

        return final

    if choices == "Formal to Informal":
        sf = Styleformer(style=1)
        sentance1 = list(source_sentences.split("."))
        output_sentance = []
        for source_sentence in sentance1:
            target_sentence = sf.transfer(source_sentence)
            if target_sentence is not None:
                output_sentance.append(target_sentence)
                #print(target_sentence)
            else:
                output_sentance.append(target_sentence)
                #print(target_sentence)
                output_sentance.append(target_sentence)
        res = [i for i in output_sentance if i is not None]
        #print(output_sentance)
        #print(res)
        final = ""
        for value in res:
            joint_value = "".join(value)
            if final == "":
                final += joint_value
            else:
                final = f"{final}.{joint_value}"
        final = final.replace("..", ".")
        #print(final)
        return final
    
    if choices == "Active to passive":
        sf = Styleformer(style=2)
        sentance1 = list(source_sentences.split("."))
        output_sentance = []
        for source_sentence in sentance1:
            target_sentence = sf.transfer(source_sentence)
            if target_sentence is not None:
                output_sentance.append(target_sentence)
                #print(target_sentence)
            else:
                output_sentance.append(target_sentence)
                #print(target_sentence)
                output_sentance.append(target_sentence)
        res = [i for i in output_sentance if i is not None]
        #print(output_sentance)
        #print(res)
        final = ""
        for value in res:
            joint_value = "".join(value)
            if final == "":
                final += joint_value
            else:
                final = f"{final}.{joint_value}"
        final = final.replace("..", ".")
        new_output = final.replace('Active to passive:', "")
        #print(final)
        return new_output


    if choices == "Passive to active":
        sf = Styleformer(style=3)
        sentance1 = list(source_sentences.split("."))
        output_sentance = []
        for source_sentence in sentance1:
            target_sentence = sf.transfer(source_sentence)
            if target_sentence is not None:
                output_sentance.append(target_sentence)
                #print(target_sentence)
            else:
                output_sentance.append(target_sentence)
                #print(target_sentence)
                output_sentance.append(target_sentence)
        res = [i for i in output_sentance if i is not None]
        #print(output_sentance)
        #print(res)
        final = ""
        for value in res:
            joint_value = "".join(value)
            if final == "":
                final += joint_value
            else:
                final = f"{final}.{joint_value}"
        final = final.replace("..", ".")
        #print(final)
        new_output = final.replace('Passive to active:', "")
        #print(final)
        return new_output

input_1 = gr.inputs.Radio(choices=choices, label='Choose a model.')
input_2 = gr.inputs.Textbox(placeholder='Enter your text here...', label='Input')

iface = gr.Interface(para1, [input_1, input_2], "text", theme='huggingface')
if __name__ == "__main__":
    iface.launch(debug=True)