CoolFace
Apppublic

Bhavibond/ProfessionalTranslator

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
app.py208 linesDownload Raw Back to root
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForSeq2SeqLM3import torch4import re5from collections import defaultdict6import numpy as np7 8# Load tokenizer and model (optimized)9tokenizer = AutoTokenizer.from_pretrained("facebook/m2m100_418M")10model = AutoModelForSeq2SeqLM.from_pretrained("facebook/m2m100_418M", torch_dtype=torch.float16)11 12device = "cuda" if torch.cuda.is_available() else "cpu"13model = model.to(device)14 15# Utility functions16def clean_text(text):17    return re.sub(r'\s+', ' ', text.strip())18 19def detect_sentiment(text):20    positive_words = ["good", "happy", "love", "excellent", "fantastic", "great"]21    negative_words = ["bad", "sad", "hate", "terrible", "horrible", "awful"]22 23    score = sum(1 for word in text.split() if word.lower() in positive_words) - sum(24        1 for word in text.split() if word.lower() in negative_words25    )26    return score / len(text.split()) if len(text.split()) > 0 else 027 28def similar(a, b):29    a_tokens = tokenizer(a, return_tensors="pt", padding=True).to(device)30    b_tokens = tokenizer(b, return_tensors="pt", padding=True).to(device)31    32    a_emb = model.get_input_embeddings()(a_tokens["input_ids"]).mean(dim=1)33    b_emb = model.get_input_embeddings()(b_tokens["input_ids"]).mean(dim=1)34    35    similarity = torch.cosine_similarity(a_emb, b_emb).item()36    return similarity37 38# Translation Memory (efficient caching)39translation_memory = {}40 41# Named Entity Preservation (optimized)42ner_entities = {43    "COMPANY": ["Google", "Microsoft", "Tesla"],44    "DATE": ["2023", "2024"],45    "PRODUCT": ["iPhone", "MacBook"],46    "LOCATION": ["Paris", "New York", "Berlin"]47}48 49# Idiom Map (optimized)50idiom_map = {51    "C'est la vie": "That's life",52    "J'ai le cafard": "I'm feeling down"53}54 55# Professional Tone Mapping (optimized)56professional_tone_map = {57    "Hi": "Dear Sir/Madam",58    "Thanks": "Thank you very much",59    "Bye": "Kind regards"60}61 62# Market Trend Adaptation (optimized)63market_trends = {64    "AI": "artificial intelligence",65    "Crypto": "cryptocurrency"66}67 68# Reinforcement Learning Adjustment69learning_rate = 0.00170def adjust_weights(score):71    global learning_rate72    learning_rate += (score - 0.5) * 0.0173    learning_rate = max(0.0001, min(0.1, learning_rate))74 75# Translation Function (optimized)76def translate(text):77    inputs = tokenizer(text, return_tensors="pt", padding=True).to(device)78    outputs = model.generate(**inputs, forced_bos_token_id=tokenizer.get_lang_id("fr"), num_return_sequences=1, max_length=200)79    translation = tokenizer.decode(outputs[0], skip_special_tokens=True)80    return translation81 82# Best Translation Selection83def best_of_best(text):84    if text in translation_memory:85        return translation_memory[text]86    87    translation = translate(text)88    original_sentiment = detect_sentiment(text)89    translated_sentiment = detect_sentiment(translation)90 91    similarity_score = similar(text, translation)92    sentiment_score = abs(original_sentiment - translated_sentiment)93    total_score = similarity_score - sentiment_score94 95    translation_memory[text] = translation96    return translation97 98# Next Best Translation99def next_best_translation(text):100    return translate(text)101 102# Professional Tone Adjustment103def adjust_professional_tone(text):104    for casual, professional in professional_tone_map.items():105        text = text.replace(casual, professional)106    return text107 108# Politeness Handling109def adjust_politeness(text):110    sentiment = detect_sentiment(text)111    if sentiment < -0.2:112        text = f"Kindly consider the following: {text}"113    elif sentiment > 0.2:114        text = f"I would like to express my gratitude: {text}"115    return text116 117# Idiom Handling118def handle_idioms(text):119    for idiom, translation in idiom_map.items():120        text = text.replace(idiom, translation)121    return text122 123# Market Trend Adaptation124def adapt_to_trends(text):125    for term, replacement in market_trends.items():126        text = text.replace(term, replacement)127    return text128 129# Named Entity Preservation130def preserve_ner(text):131    for entity, values in ner_entities.items():132        for value in values:133            if value in text:134                text = text.replace(value, f"[{entity}: {value}]")135    return text136 137# Semantic Error Correction138def semantic_error_correction(text):139    text = re.sub(r'[^a-zA-Z0-9.,!?;:\'\"()\s]', '', text)140    return text141 142# Core Translation Pipeline143def handle_translation(text):144    text = clean_text(text)145    text = handle_idioms(text)146    text = adapt_to_trends(text)147    text = preserve_ner(text)148    text = adjust_professional_tone(text)149    text = adjust_politeness(text)150    text = semantic_error_correction(text)151    152    return best_of_best(text)153 154# A/B Testing (Next Best vs Best of the Best)155feedback_data = defaultdict(list)156def ab_test(text):157    translation_1 = best_of_best(text)158    translation_2 = next_best_translation(text)159    feedback_data[text].append((translation_1, translation_2))160    return translation_1, translation_2161 162# Feedback Handling (efficient)163def feedback(text, preferred_version):164    if text not in feedback_data:165        return "No feedback data available."166    167    version_1, version_2 = feedback_data[text][0]168    if preferred_version == "Version 1":169        score = similar(text, version_1)170    else:171        score = similar(text, version_2)172 173    adjust_weights(score)174    return f"Feedback applied. New learning rate: {learning_rate:.5f}"175 176# Gradio App Setup (optimized)177with gr.Blocks() as app:178    gr.Markdown("## AI-Driven Professional Localization Tool")179 180    input_text = gr.Textbox(label="Enter Text")181    output_text = gr.Textbox(label="Translated Text")182 183    translate_button = gr.Button("Translate")184    translate_button.click(fn=handle_translation, inputs=input_text, outputs=output_text)185 186    with gr.Row():187        ab_output1 = gr.Textbox(label="Best of the Best")188        ab_output2 = gr.Textbox(label="Next Best")189        ab_test_button = gr.Button("A/B Test")190        ab_test_button.click(fn=ab_test, inputs=input_text, outputs=[ab_output1, ab_output2])191 192        feedback_input = gr.Dropdown(["Version 1", "Version 2"], label="Preferred Translation")193        feedback_output = gr.Textbox(label="Feedback Status")194        feedback_button = gr.Button("Submit Feedback")195        feedback_button.click(fn=feedback, inputs=[input_text, feedback_input], outputs=feedback_output)196 197    with gr.Tab("Advanced Settings"):198        learning_rate_display = gr.Textbox(label="Learning Rate", value=str(learning_rate))199 200    gr.Markdown("### Key Features")201    gr.Markdown("- Best of the Best Translation")202    gr.Markdown("- Next Best Translation")203    gr.Markdown("- Professional Tone Handling")204    gr.Markdown("- Market Adaptation and Idiom Handling")205    gr.Markdown("- Reinforcement Learning")206 207# Faster loading and lower memory footprint on HuggingFace Free Tier..208app.launch(share=True)