| 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) |
| |
| else: |
| output_sentance.append(target_sentence) |
| |
| output_sentance.append(target_sentence) |
| res = [i for i in output_sentance if i is not None] |
| |
| |
| final = "" |
| for value in res: |
| joint_value = "".join(value) |
| if final == "": |
| final += joint_value |
| else: |
| final = f"{final}.{joint_value}" |
| final = final.replace("..", ".") |
| |
|
|
| 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) |
| |
| else: |
| output_sentance.append(target_sentence) |
| |
| output_sentance.append(target_sentence) |
| res = [i for i in output_sentance if i is not None] |
| |
| |
| final = "" |
| for value in res: |
| joint_value = "".join(value) |
| if final == "": |
| final += joint_value |
| else: |
| final = f"{final}.{joint_value}" |
| final = final.replace("..", ".") |
| |
| 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) |
| |
| else: |
| output_sentance.append(target_sentence) |
| |
| output_sentance.append(target_sentence) |
| res = [i for i in output_sentance if i is not None] |
| |
| |
| 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:', "") |
| |
| 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) |
| |
| else: |
| output_sentance.append(target_sentence) |
| |
| output_sentance.append(target_sentence) |
| res = [i for i in output_sentance if i is not None] |
| |
| |
| 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('Passive to active:', "") |
| |
| 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) |