Spaces:
Running
on
Zero
Running
on
Zero
Upload module_rewriting.py
Browse files- module_rewriting.py +155 -0
module_rewriting.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
File: module_rewriting.py
|
3 |
+
Description: Rewrite some given text in a given style and language.
|
4 |
+
Author: Didier Guillevic
|
5 |
+
Date: 2025-03-16
|
6 |
+
"""
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import vlm
|
10 |
+
|
11 |
+
tgt_language_codes = {
|
12 |
+
'English': 'en',
|
13 |
+
'French': 'fr'
|
14 |
+
}
|
15 |
+
code_to_languages = {v: k for k, v in tgt_language_codes.items()}
|
16 |
+
|
17 |
+
#
|
18 |
+
# Examples of bad writing: https://lafavephilosophy.x10host.com/writsamp0.htm
|
19 |
+
#
|
20 |
+
|
21 |
+
example_bad_writing_2 = (
|
22 |
+
"Existing is being unique. Existence, reality, essence, cause, or truth is uniqueness. "
|
23 |
+
"The geometric point in the center of the sphere is nature’s symbol of the immeasurable "
|
24 |
+
"uniqueness within its measurable effect. "
|
25 |
+
"A center is always unique; otherwise it would not be a center. "
|
26 |
+
"Because uniqueness is reality, or that which makes a thing what it is, "
|
27 |
+
"everything that is real is based on a centralization."
|
28 |
+
)
|
29 |
+
example_bad_writing_3 = (
|
30 |
+
"The amount of grammer and usage error’s today is astounding. "
|
31 |
+
"Not to mention spelling. If I was a teacher, I’d feel badly "
|
32 |
+
"that less and less students seem to understand the basic principals "
|
33 |
+
"of good writing. Neither the oldest high school students nor the "
|
34 |
+
"youngest kindergartner know proper usage. "
|
35 |
+
"A student often thinks they can depend on word processing programs "
|
36 |
+
"to correct they’re errors. Know way!"
|
37 |
+
"Watching TV all the time, its easy to see why their having trouble. "
|
38 |
+
"TV interferes with them studying and it’s strong affect on children "
|
39 |
+
"has alot to due with their grades. There’s other factors, too, "
|
40 |
+
"including the indifference of parents like you and I. "
|
41 |
+
"A Mom or Dad often doesn’t know grammer themselves. "
|
42 |
+
"We should tell are children to study hard like we did at "
|
43 |
+
"they’re age and to watch less TV then their classmates."
|
44 |
+
)
|
45 |
+
example_bad_writing_9 = (
|
46 |
+
"Immanuel Kant was a great philosipher that came up with many "
|
47 |
+
"philosophical thoughts. He represents philosophy at it’s best. "
|
48 |
+
"One issue that went against his moral laws was that of people "
|
49 |
+
"having a lack of honesty or lying. Kant was strongly in favor of "
|
50 |
+
"the view that when the ethical and moral decision to lie is made "
|
51 |
+
"by a person, they’re would always be negative consequences of "
|
52 |
+
"they’re choice. "
|
53 |
+
"Kant also held the firm belief that lying was wrong at all times. "
|
54 |
+
"I disagree, my view is that sometimes all lying is not wrong."
|
55 |
+
)
|
56 |
+
|
57 |
+
rewrite_prompt = (
|
58 |
+
"{} "
|
59 |
+
"Respond exclusively using the {} language. "
|
60 |
+
"Text:\n\n{}"
|
61 |
+
)
|
62 |
+
|
63 |
+
def rewrite_text(text, instruction, tgt_lang):
|
64 |
+
"""Rewrite the given text in the given target language.
|
65 |
+
"""
|
66 |
+
# Build messages
|
67 |
+
messages = [
|
68 |
+
{
|
69 |
+
'role': 'user',
|
70 |
+
'content': [
|
71 |
+
{
|
72 |
+
"type": "text",
|
73 |
+
"text": rewrite_prompt.format(
|
74 |
+
instruction, code_to_languages[tgt_lang], text)
|
75 |
+
}
|
76 |
+
]
|
77 |
+
}
|
78 |
+
]
|
79 |
+
yield from vlm.stream_response(messages)
|
80 |
+
|
81 |
+
#
|
82 |
+
# User interface
|
83 |
+
#
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
with gr.Row():
|
86 |
+
input_text = gr.Textbox(
|
87 |
+
lines=5,
|
88 |
+
placeholder="Enter text to rewrite",
|
89 |
+
label="Text to rewrite",
|
90 |
+
render=True
|
91 |
+
)
|
92 |
+
output_text = gr.Textbox(
|
93 |
+
lines=5,
|
94 |
+
label="Rewritten text",
|
95 |
+
render=True
|
96 |
+
)
|
97 |
+
|
98 |
+
with gr.Row():
|
99 |
+
tgt_lang = gr.Dropdown(
|
100 |
+
choices=tgt_language_codes.items(),
|
101 |
+
value="en",
|
102 |
+
label="Target language",
|
103 |
+
render=True,
|
104 |
+
scale=1
|
105 |
+
)
|
106 |
+
instruction = gr.Textbox(
|
107 |
+
lines=1,
|
108 |
+
value="Rewrite the following text in a more professional style.",
|
109 |
+
label="Instruction",
|
110 |
+
render=True,
|
111 |
+
scale=4
|
112 |
+
)
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
rewrite_btn = gr.Button(value="Rewrite", variant="primary")
|
116 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
117 |
+
|
118 |
+
# Examples
|
119 |
+
with gr.Accordion("Examples", open=False):
|
120 |
+
examples = gr.Examples(
|
121 |
+
[
|
122 |
+
["Howdy mate! Wanna grab a bite?", ],
|
123 |
+
[example_bad_writing_3, ],
|
124 |
+
[example_bad_writing_2, ],
|
125 |
+
[ ("The work wa really not that great. "
|
126 |
+
"They simply surfed the web to find the solution to their problem."),
|
127 |
+
],
|
128 |
+
["Ils ont rien foutus. Ils sont restés assis sur leur postérieur toute la journée.", ],
|
129 |
+
],
|
130 |
+
inputs=[input_text, instruction, tgt_lang],
|
131 |
+
outputs=[output_text,],
|
132 |
+
fn=rewrite_text,
|
133 |
+
cache_examples=False,
|
134 |
+
label="Examples"
|
135 |
+
)
|
136 |
+
|
137 |
+
# Documentation
|
138 |
+
with gr.Accordion("Documentation", open=False):
|
139 |
+
gr.Markdown(f"""
|
140 |
+
- Model: {vlm.model_id}.
|
141 |
+
""")
|
142 |
+
|
143 |
+
# Click actions
|
144 |
+
rewrite_btn.click(
|
145 |
+
fn=rewrite_text,
|
146 |
+
inputs=[input_text, instruction, tgt_lang],
|
147 |
+
outputs=[output_text,]
|
148 |
+
)
|
149 |
+
clear_btn.click(
|
150 |
+
fn=lambda : ('', ''), # input_text, output_text, output_text_google
|
151 |
+
inputs=[],
|
152 |
+
outputs=[input_text, output_text]
|
153 |
+
)
|
154 |
+
|
155 |
+
|