CoolFace
Apppublic

Didier/Vision_Language_Mistral_Small

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
3likes
module_rewriting.py159 linesDownload Raw Back to root
1"""2File: module_rewriting.py3Description: Rewrite some given text in a given style and language.4Author: Didier Guillevic5Date: 2025-03-166"""7 8import gradio as gr9import vlm10 11tgt_language_codes = {12    'English': 'en',13    'French': 'fr'14}15code_to_languages = {v: k for k, v in tgt_language_codes.items()}16 17#18# Examples of bad writing: https://lafavephilosophy.x10host.com/writsamp0.htm19#20 21example_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)29example_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)45example_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 57rewrite_prompt = (58    "{} "59    "Respond exclusively using the {} language. "60    "Text:\n\n{}"61)62 63def rewrite_text(text, instruction, tgt_lang):64    """Rewrite the given text in the given target language.65    """66    # Build messages67    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#83# User interface84#85with gr.Blocks() as demo:86    with gr.Row():87        input_text = gr.Textbox(88            lines=5,89            placeholder="Enter text to rewrite",90            label="Text to rewrite",91            render=True92        )93        output_text = gr.Textbox(94            lines=5,95            label="Rewritten text",96            render=True97        )98    99    with gr.Row():100        tgt_lang = gr.Dropdown(101            choices=tgt_language_codes.items(),102            value="en",103            label="Target language",104            render=True,105            scale=1106        )107        instruction = gr.Textbox(108            lines=1,109            value="Rewrite the following text in a more professional style.",110            label="Instruction",111            render=True,112            scale=4113        )114 115    with gr.Row():116        rewrite_btn = gr.Button(value="Rewrite", variant="primary")117        clear_btn = gr.Button("Clear", variant="secondary")118    119    # Examples120    with gr.Accordion("Examples", open=False):121        examples = gr.Examples(122            [123                ["Howdy mate! Wanna grab a bite?", ],124                [example_bad_writing_3, ],125                [example_bad_writing_2, ],126                [ ("The work wa really not that great. "127                "They simply surfed the web to find the solution to their problem."), 128                ],129                ["Ils ont rien foutus. Ils sont restés assis sur leur postérieur toute la journée.", ],130            ],131            inputs=[input_text, instruction, tgt_lang],132            outputs=[output_text,],133            fn=rewrite_text,134            cache_examples=False,135            label="Examples"136        )137 138    # Documentation139    with gr.Accordion("Documentation", open=False):140        gr.Markdown(f"""141            - Model: {vlm.model_id}.142        """)143    144    # Click actions145    rewrite_btn.click(146        fn=rewrite_text,147        inputs=[input_text, instruction, tgt_lang],148        outputs=[output_text,]149    )150    clear_btn.click(151        fn=lambda : ('', ''), # input_text, output_text, output_text_google152        inputs=[],153        outputs=[input_text, output_text]154    )155 156    157if __name__ == "__main__":158    demo.launch()159