CoolFace
Apppublic

skoneru/contextual_refinement_ende

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
1likes
app.py88 linesDownload Raw Back to root
1import os2import sys3import gradio as gr4from huggingface_hub import InferenceClient5from transformers import AutoTokenizer6 7client = InferenceClient(model="https://083b-141-3-25-29.ngrok-free.app")8tokenizer = AutoTokenizer.from_pretrained("enoch/llama-65b-hf", padding_side='left',return_token_type_ids=False)9 10 11def inference(message):12    output = client.text_generation(message, max_new_tokens=512)13    return output.replace("<SS> ","\n")14 15def tokenize_len(sents):16    return len(tokenizer.encode("\n".join(sents)))17 18def validate_inputs(source, sent_hyp, pe_hyp):19    src_sents = source.split("\n")20    sent_hyp_sents = sent_hyp.split("\n")21    pe_hyp_sents = pe_hyp.split("\n")22    max_len = 51223 24    if len(src_sents) < 1:25        return False26 27    if len(sent_hyp_sents) != len(src_sents) or len(pe_hyp_sents) > len(src_sents):28        return False29 30    if tokenize_len(src_sents) > max_len or tokenize_len(sent_hyp_sents) > max_len or tokenize_len(pe_hyp_sents) > max_len:31        return Flase32 33    return True34 35 36def translate(source, sent_hyp, pe_hyp):37 38    if validate_inputs(source, sent_hyp, pe_hyp):39        prefix="English:\n"40        suffix="\nGerman Translation:\n"41        pe_suffix="\nPost-Edited Translation:\n"42 43        source = " <SS> ".join(source.split("\n"))44        sent_hyp = " <SS> ".join(sent_hyp.split("\n"))45        pe_hyp = " <SS> ".join(pe_hyp.split("\n"))46 47        prompt = prefix + source + "\n" + suffix + sent_hyp + "\n" +  pe_suffix + "\n" + pe_hyp 48    else:49        raise gr.Error("Please make sure that you meet the following conditions: Source and sentence level hypothesis lines are equal and the initial post-edited translation lines are less than source, The number of tokens in each box is less than 256.")50 51    52    pe_hyp = "\n".join(pe_hyp.split(" <SS> "))53    return pe_hyp + inference(prompt)54 55 56example_pronoun_false = ["- Yeah, but Rico's garland beat them all.\nIt was big.", "- Ja, aber Ricos Kränz war der schönste.\nEs war groß.",""]57example_pronoun_correct = ["- Yeah, but Rico's garland beat them all.\nIt was big.", "- Ja, aber Ricos Kränz war der schönste.\nEs war groß.","- Ja, aber Ricos Kranz"]58 59 60example_term_false = ["Lets talk about large language models.\nThese days, large language models can be used everywhere.", "Lassen Sie uns über große Sprachmodelle sprechen.\nHeutzutage können große Sprachmodelle überall eingesetzt werden.",""]61example_term_correct = ["Lets talk about large language models.\nThese days, large language models can be used everywhere.", "Lassen Sie uns über große Sprachmodelle sprechen.\nHeutzutage können große Sprachmodelle überall eingesetzt werden.","Lassen Sie uns über Large Language Models sprechen."]62 63example_formal_false = ["You should be excited\nbut, calm down!\nyou must be careful","Sie sollten aufgeregt sein\naber beruhigen Sie sich!\ndu musst vorsichtig sein",""]64example_formal_correct = ["You should be excited\nbut, calm down!\nyou must be careful","Sie sollten aufgeregt sein\naber beruhigen Sie sich!\ndu musst vorsichtig sein","Du solltest aufgeregt sein"]65 66with open('description.md',mode='r',encoding='utf-8') as f:67    description = f.readlines()68    description = "\n".join(description)69 70css_code = ".gradio-container {background: url('file=background.png');}"71iface = gr.Interface(72        fn=translate,73        inputs=[gr.Textbox(lines=2, placeholder="Enter your English Sentences that you want to translate", label="English Sentences"), gr.Textbox(lines=2, placeholder="Enter your sentence-level German Tranlations that you want to post-edit using Llama2",label="Sentence-Level German Translations"),gr.Textbox(lines=2, placeholder="Enter your partially corrected translation and the model will continue from there - Can be left empty or generate the output once and correct it later :)", label="Manual Post-Edited German Translation")],74        outputs=gr.Textbox(lines=2,placeholder="Enter your inputs and click submit!",label="Automatic Post-Edited German Translation"),75        examples=[76            example_term_false,77            example_term_correct,78            example_formal_false,79            example_formal_correct,80            example_pronoun_false,81            example_pronoun_correct,82        ],83        title="Contextual Refinement of Translations: Integrating Manual Feedback",84        description=description,85        )86    87iface.launch(share=True)   88