jaimin commited on
Commit
761c54f
·
1 Parent(s): c696da0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from styleformer import Styleformer
2
+ import torch
3
+ import warnings
4
+
5
+ warnings.filterwarnings("ignore")
6
+ import gradio as gr
7
+
8
+ choices = ['Informal to Formal', 'Formal to Informal','Active to passive','Passive to active']
9
+
10
+
11
+ def para1(choices, source_sentences):
12
+ if choices == "Informal to Formal":
13
+ sf = Styleformer(style=0)
14
+ sentance1 = list(source_sentences.split("."))
15
+ output_sentance = []
16
+ for source_sentence in sentance1:
17
+ target_sentence = sf.transfer(source_sentence)
18
+ if target_sentence is not None:
19
+ output_sentance.append(target_sentence)
20
+ #print(target_sentence)
21
+ else:
22
+ output_sentance.append(target_sentence)
23
+ #print(target_sentence)
24
+ output_sentance.append(target_sentence)
25
+ res = [i for i in output_sentance if i is not None]
26
+ #print(output_sentance)
27
+ #print(res)
28
+ final = ""
29
+ for value in res:
30
+ joint_value = "".join(value)
31
+ if final == "":
32
+ final += joint_value
33
+ else:
34
+ final = f"{final}.{joint_value}"
35
+ final = final.replace("..", ".")
36
+ #print(final)
37
+
38
+ return final
39
+
40
+ if choices == "Formal to Informal":
41
+ sf = Styleformer(style=1)
42
+ sentance1 = list(source_sentences.split("."))
43
+ output_sentance = []
44
+ for source_sentence in sentance1:
45
+ target_sentence = sf.transfer(source_sentence)
46
+ if target_sentence is not None:
47
+ output_sentance.append(target_sentence)
48
+ #print(target_sentence)
49
+ else:
50
+ output_sentance.append(target_sentence)
51
+ #print(target_sentence)
52
+ output_sentance.append(target_sentence)
53
+ res = [i for i in output_sentance if i is not None]
54
+ #print(output_sentance)
55
+ #print(res)
56
+ final = ""
57
+ for value in res:
58
+ joint_value = "".join(value)
59
+ if final == "":
60
+ final += joint_value
61
+ else:
62
+ final = f"{final}.{joint_value}"
63
+ final = final.replace("..", ".")
64
+ #print(final)
65
+ return final
66
+
67
+ if choices == "Active to passive":
68
+ sf = Styleformer(style=2)
69
+ sentance1 = list(source_sentences.split("."))
70
+ output_sentance = []
71
+ for source_sentence in sentance1:
72
+ target_sentence = sf.transfer(source_sentence)
73
+ if target_sentence is not None:
74
+ output_sentance.append(target_sentence)
75
+ #print(target_sentence)
76
+ else:
77
+ output_sentance.append(target_sentence)
78
+ #print(target_sentence)
79
+ output_sentance.append(target_sentence)
80
+ res = [i for i in output_sentance if i is not None]
81
+ #print(output_sentance)
82
+ #print(res)
83
+ final = ""
84
+ for value in res:
85
+ joint_value = "".join(value)
86
+ if final == "":
87
+ final += joint_value
88
+ else:
89
+ final = f"{final}.{joint_value}"
90
+ final = final.replace("..", ".")
91
+ #print(final)
92
+ return final
93
+
94
+
95
+ if choices == "Passive to active":
96
+ sf = Styleformer(style=3)
97
+ sentance1 = list(source_sentences.split("."))
98
+ output_sentance = []
99
+ for source_sentence in sentance1:
100
+ target_sentence = sf.transfer(source_sentence)
101
+ if target_sentence is not None:
102
+ output_sentance.append(target_sentence)
103
+ #print(target_sentence)
104
+ else:
105
+ output_sentance.append(target_sentence)
106
+ #print(target_sentence)
107
+ output_sentance.append(target_sentence)
108
+ res = [i for i in output_sentance if i is not None]
109
+ #print(output_sentance)
110
+ #print(res)
111
+ final = ""
112
+ for value in res:
113
+ joint_value = "".join(value)
114
+ if final == "":
115
+ final += joint_value
116
+ else:
117
+ final = f"{final}.{joint_value}"
118
+ final = final.replace("..", ".")
119
+ #print(final)
120
+ return final
121
+
122
+ input_1 = gr.inputs.Radio(choices=choices, label='Choose a model.')
123
+ input_2 = gr.inputs.Textbox(placeholder='Enter your text here...', label='Input')
124
+
125
+ iface = gr.Interface(para1, [input_1, input_2], "text", theme='huggingface')
126
+ if __name__ == "__main__":
127
+ iface.launch(debug=True)