| import gradio as gr |
| from transformers import AutoTokenizer, AutoModel |
| from sklearn.metrics.pairwise import cosine_similarity |
| import torch |
| import numpy as np |
| from gradio_client import Client |
| from functools import lru_cache |
|
|
| |
| @lru_cache(maxsize=1) |
| def load_model_and_tokenizer(): |
| model_name = "./all-MiniLM-L6-v2" |
| tokenizer = AutoTokenizer.from_pretrained(model_name) |
| model = AutoModel.from_pretrained(model_name) |
| return tokenizer, model |
|
|
| |
| tokenizer, model = load_model_and_tokenizer() |
|
|
| |
| labels = [ |
| "aerospace", "anatomy", "anthropology", "art", |
| "automotive", "blockchain", "biology", "chemistry", |
| "cryptocurrency", "data science", "design", "e-commerce", |
| "education", "engineering", "entertainment", "environment", |
| "fashion", "finance", "food commerce", "gaming", |
| "healthcare", "history", "information technology", |
| "legal", "machine learning", "marketing", "medicine", |
| "music", "philosophy", "physics", "politics", "real estate", "retail", |
| "robotics", "social media", "sports", "technical", |
| "tourism", "travel" |
| ] |
|
|
| tones = [ |
| "formal", "positive", "negative", "poetic", "polite", "subtle", "casual", "neutral", |
| "informal", "pompous", "sustained", "rude", "sustained", |
| "sophisticated", "playful", "serious", "friendly" |
| ] |
|
|
| styles = [ |
| "poetry", "novel", "theater", "slang", "speech", "keywords", "html", "programming" |
| ] |
|
|
| gender_number = [ |
| "masculine singular", "masculine plural", "feminine singular", "feminine plural" |
| ] |
|
|
| @lru_cache(maxsize=1) |
| def precompute_label_embeddings(): |
| inputs = tokenizer(labels, padding=True, truncation=True, return_tensors="pt") |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| return outputs.last_hidden_state.mean(dim=1).numpy() |
|
|
| label_embeddings = precompute_label_embeddings() |
|
|
| |
| def softmax(x): |
| exp_x = np.exp(x - np.max(x)) |
| return exp_x / exp_x.sum() |
|
|
| |
| def detect_context(input_text, threshold=0.03): |
| |
| inputs = tokenizer([input_text], padding=True, truncation=True, return_tensors="pt") |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| input_embedding = outputs.last_hidden_state.mean(dim=1).numpy() |
|
|
| |
| similarities = cosine_similarity(input_embedding, label_embeddings)[0] |
|
|
| |
| probabilities = softmax(similarities) |
|
|
| |
| label_probabilities = list(zip(labels, probabilities)) |
|
|
| |
| high_confidence_contexts = [(label, score) for label, score in label_probabilities if score >= threshold] |
|
|
| |
| if not high_confidence_contexts: |
| high_confidence_contexts = [("general", 1.0)] |
|
|
| return high_confidence_contexts |
|
|
| |
| def get_translation_client(context): |
| """ |
| Returns the appropriate Hugging Face Space client for the given context. |
| For now, all contexts use the same mock space. |
| """ |
| return Client("Frenchizer/space_7") |
|
|
| def translate_text(input_text, context): |
| """ |
| Translates the input text using the appropriate model for the given context. |
| """ |
| client = get_translation_client(context) |
| return client.predict(input_text) |
|
|
| def process_request(input_text): |
| |
| context_results = detect_context(input_text) |
|
|
| |
| translations = {} |
| for context, score in context_results: |
| translations[context] = translate_text(input_text, context) |
|
|
| |
| print("High-confidence contexts (score >= 0.022):", context_results) |
| print("Translations:", translations) |
|
|
| |
| return translations, context_results |
|
|
| |
| def gradio_interface(input_text): |
| translation, contexts = process_request(input_text) |
| |
| output = f"{translation}\n" |
| return output.strip() |
|
|
| |
| interface = gr.Interface( |
| fn=gradio_interface, |
| inputs="text", |
| outputs="text", |
| title="Frenchizer", |
| description="Translate text from English to French with context detection and threshold." |
| ) |
|
|
| interface.launch() |