File size: 5,197 Bytes
61cf56e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
File: module_rewriting.py
Description: Rewrite some given text in a given style and language.
Author: Didier Guillevic
Date: 2025-03-16
"""

import gradio as gr
import vlm

tgt_language_codes = {
    'English': 'en',
    'French': 'fr'
}
code_to_languages = {v: k for k, v in tgt_language_codes.items()}

#
# Examples of bad writing: https://lafavephilosophy.x10host.com/writsamp0.htm
#

example_bad_writing_2 = (
    "Existing is being unique. Existence, reality, essence, cause, or truth is uniqueness. "
    "The geometric point in the center of the sphere is nature’s symbol of the immeasurable "
    "uniqueness within its measurable effect. "
    "A center is always unique; otherwise it would not be a center. "
    "Because uniqueness is reality, or that which makes a thing what it is, "
    "everything that is real is based on a centralization."
)
example_bad_writing_3 = (
    "The amount of grammer and usage error’s today is astounding. "
    "Not to mention spelling. If I was a teacher, I’d feel badly "
    "that less and less students seem to understand the basic principals "
    "of good writing. Neither the oldest high school students nor the "
    "youngest kindergartner know proper usage. "
    "A student often thinks they can depend on word processing programs "
    "to correct they’re errors. Know way!"
    "Watching TV all the time, its easy to see why their having trouble. "
    "TV interferes with them studying and it’s strong affect on children "
    "has alot to due with their grades. There’s other factors, too, "
    "including the indifference of parents like you and I. "
    "A Mom or Dad often doesn’t know grammer themselves. "
    "We should tell are children to study hard like we did at "
    "they’re age and to watch less TV then their classmates."
)
example_bad_writing_9 = (
    "Immanuel Kant was a great philosipher that came up with many "
    "philosophical thoughts. He represents philosophy at it’s best. "
    "One issue that went against his moral laws was that of people "
    "having a lack of honesty or lying. Kant was strongly in favor of "
    "the view that when the ethical and moral decision to lie is made "
    "by a person, they’re would always be negative consequences of "
    "they’re choice. "
    "Kant also held the firm belief that lying was wrong at all times. "
    "I disagree, my view is that sometimes all lying is not wrong."
)

rewrite_prompt = (
    "{} "
    "Respond exclusively using the {} language. "
    "Text:\n\n{}"
)

def rewrite_text(text, instruction, tgt_lang):
    """Rewrite the given text in the given target language.
    """
    # Build messages
    messages = [
        {
            'role': 'user',
            'content': [
                {
                    "type": "text",
                    "text": rewrite_prompt.format(
                        instruction, code_to_languages[tgt_lang], text)
                }
            ]
        }
    ]    
    yield from vlm.stream_response(messages)

#
# User interface
#
with gr.Blocks() as demo:
    with gr.Row():
        input_text = gr.Textbox(
            lines=5,
            placeholder="Enter text to rewrite",
            label="Text to rewrite",
            render=True
        )
        output_text = gr.Textbox(
            lines=5,
            label="Rewritten text",
            render=True
        )
    
    with gr.Row():
        tgt_lang = gr.Dropdown(
            choices=tgt_language_codes.items(),
            value="en",
            label="Target language",
            render=True,
            scale=1
        )
        instruction = gr.Textbox(
            lines=1,
            value="Rewrite the following text in a more professional style.",
            label="Instruction",
            render=True,
            scale=4
        )

    with gr.Row():
        rewrite_btn = gr.Button(value="Rewrite", variant="primary")
        clear_btn = gr.Button("Clear", variant="secondary")
    
    # Examples
    with gr.Accordion("Examples", open=False):
        examples = gr.Examples(
            [
                ["Howdy mate! Wanna grab a bite?", ],
                [example_bad_writing_3, ],
                [example_bad_writing_2, ],
                [ ("The work wa really not that great. "
                "They simply surfed the web to find the solution to their problem."), 
                ],
                ["Ils ont rien foutus. Ils sont restés assis sur leur postérieur toute la journée.", ],
            ],
            inputs=[input_text, instruction, tgt_lang],
            outputs=[output_text,],
            fn=rewrite_text,
            cache_examples=False,
            label="Examples"
        )

    # Documentation
    with gr.Accordion("Documentation", open=False):
        gr.Markdown(f"""
            - Model: {vlm.model_id}.
        """)
    
    # Click actions
    rewrite_btn.click(
        fn=rewrite_text,
        inputs=[input_text, instruction, tgt_lang],
        outputs=[output_text,]
    )
    clear_btn.click(
        fn=lambda : ('', ''), # input_text, output_text, output_text_google
        inputs=[],
        outputs=[input_text, output_text]
    )