""" 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] )