Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,14 @@ from transformers import AutoTokenizer, AutoModel
|
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
-
|
|
|
|
| 7 |
from functools import lru_cache
|
| 8 |
|
| 9 |
# Cache the model and tokenizer using lru_cache
|
| 10 |
@lru_cache(maxsize=1)
|
| 11 |
def load_model_and_tokenizer():
|
| 12 |
-
model_name = "./all-MiniLM-L6-v2" #
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
model = AutoModel.from_pretrained(model_name)
|
| 15 |
return tokenizer, model
|
|
@@ -19,117 +20,77 @@ tokenizer, model = load_model_and_tokenizer()
|
|
| 19 |
|
| 20 |
# Precompute label embeddings
|
| 21 |
labels = [
|
| 22 |
-
"aerospace", "anatomy", "anthropology", "art",
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
"music", "philosophy", "physics", "politics", "real estate", "retail",
|
| 30 |
-
"robotics", "social media", "sports", "technical",
|
| 31 |
-
"tourism", "travel"
|
| 32 |
]
|
| 33 |
|
| 34 |
tones = [
|
| 35 |
"formal", "positive", "negative", "poetic", "polite", "subtle", "casual", "neutral",
|
| 36 |
-
"informal", "pompous", "sustained", "rude"
|
| 37 |
-
"sophisticated", "playful", "serious", "friendly"
|
| 38 |
]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
]
|
| 43 |
-
|
| 44 |
-
gender_number = [
|
| 45 |
-
"masculine singular", "masculine plural", "feminine singular", "feminine plural"
|
| 46 |
-
]
|
| 47 |
-
|
| 48 |
-
@lru_cache(maxsize=1)
|
| 49 |
-
def precompute_label_embeddings():
|
| 50 |
-
inputs = tokenizer(labels, padding=True, truncation=True, return_tensors="pt")
|
| 51 |
with torch.no_grad():
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
label_embeddings = precompute_label_embeddings()
|
| 56 |
|
| 57 |
-
|
| 58 |
-
def softmax(x):
|
| 59 |
-
exp_x = np.exp(x - np.max(x)) # Subtract max for numerical stability
|
| 60 |
-
return exp_x / exp_x.sum()
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
inputs = tokenizer([input_text], padding=True, truncation=True, return_tensors="pt")
|
| 66 |
with torch.no_grad():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
print("Translations:", translations)
|
| 115 |
-
|
| 116 |
-
# Return the translations and contexts
|
| 117 |
-
return translations, context_results
|
| 118 |
-
|
| 119 |
-
# Gradio interface
|
| 120 |
-
def gradio_interface(input_text):
|
| 121 |
-
translation, contexts = process_request(input_text)
|
| 122 |
-
# Format the output
|
| 123 |
-
output = f"{translation}\n"
|
| 124 |
-
return output.strip()
|
| 125 |
-
|
| 126 |
-
# Create the Gradio interface
|
| 127 |
-
interface = gr.Interface(
|
| 128 |
-
fn=gradio_interface,
|
| 129 |
-
inputs="text",
|
| 130 |
-
outputs="text",
|
| 131 |
-
title="Frenchizer",
|
| 132 |
-
description="Translate text from English to French with context detection and threshold."
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
interface.launch()
|
|
|
|
| 3 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
+
import json
|
| 7 |
+
import requests
|
| 8 |
from functools import lru_cache
|
| 9 |
|
| 10 |
# Cache the model and tokenizer using lru_cache
|
| 11 |
@lru_cache(maxsize=1)
|
| 12 |
def load_model_and_tokenizer():
|
| 13 |
+
model_name = "./all-MiniLM-L6-v2" # Adjust if needed
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
model = AutoModel.from_pretrained(model_name)
|
| 16 |
return tokenizer, model
|
|
|
|
| 20 |
|
| 21 |
# Precompute label embeddings
|
| 22 |
labels = [
|
| 23 |
+
"aerospace", "anatomy", "anthropology", "art", "automotive", "blockchain",
|
| 24 |
+
"biology", "chemistry", "cryptocurrency", "data science", "design", "e-commerce",
|
| 25 |
+
"education", "engineering", "entertainment", "environment", "fashion", "finance",
|
| 26 |
+
"food commerce", "gaming", "healthcare", "history", "information technology",
|
| 27 |
+
"legal", "machine learning", "marketing", "medicine", "music", "philosophy",
|
| 28 |
+
"physics", "politics", "real estate", "retail", "robotics", "social media",
|
| 29 |
+
"sports", "technical", "tourism", "travel"
|
|
|
|
|
|
|
|
|
|
| 30 |
]
|
| 31 |
|
| 32 |
tones = [
|
| 33 |
"formal", "positive", "negative", "poetic", "polite", "subtle", "casual", "neutral",
|
| 34 |
+
"informal", "pompous", "sustained", "rude"
|
|
|
|
| 35 |
]
|
| 36 |
|
| 37 |
+
# Compute label embeddings
|
| 38 |
+
def get_label_embeddings():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
with torch.no_grad():
|
| 40 |
+
tokenized = tokenizer(labels, padding=True, truncation=True, return_tensors="pt")
|
| 41 |
+
label_embeddings = model(**tokenized).last_hidden_state[:, 0, :].numpy()
|
| 42 |
+
return label_embeddings
|
|
|
|
| 43 |
|
| 44 |
+
label_embeddings = get_label_embeddings()
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
def detect_context(text: str):
|
| 47 |
+
# Encode input text
|
| 48 |
+
tokenized = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
|
|
|
|
| 49 |
with torch.no_grad():
|
| 50 |
+
text_embedding = model(**tokenized).last_hidden_state[:, 0, :].numpy()
|
| 51 |
+
|
| 52 |
+
# Compute similarity scores
|
| 53 |
+
similarities = cosine_similarity(text_embedding, label_embeddings)[0]
|
| 54 |
+
|
| 55 |
+
# Get best matching context
|
| 56 |
+
best_index = np.argmax(similarities)
|
| 57 |
+
detected_context = labels[best_index]
|
| 58 |
+
|
| 59 |
+
return detected_context
|
| 60 |
+
|
| 61 |
+
def process_and_translate(text: str):
|
| 62 |
+
detected_context = detect_context(text)
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
# Forward text and detected context to space_7 for translation
|
| 66 |
+
translation_response = requests.post(
|
| 67 |
+
"https://api.gradio.app/v2/Frenchizer/space_7/predict",
|
| 68 |
+
json={"data": [text, detected_context]}
|
| 69 |
+
).json()
|
| 70 |
+
|
| 71 |
+
if "error" in translation_response:
|
| 72 |
+
return json.dumps({
|
| 73 |
+
"error": "Translation failed",
|
| 74 |
+
"context": detected_context
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
return json.dumps({
|
| 78 |
+
"context": detected_context,
|
| 79 |
+
"translation": translation_response["data"][0]
|
| 80 |
+
})
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return json.dumps({
|
| 84 |
+
"error": str(e),
|
| 85 |
+
"context": detected_context
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
# Define Gradio interface
|
| 89 |
+
with gr.Blocks() as interface:
|
| 90 |
+
input_text = gr.Textbox(label="Input Text")
|
| 91 |
+
output_json = gr.JSON(label="Context & Translation")
|
| 92 |
+
process_button = gr.Button("Process & Translate")
|
| 93 |
+
process_button.click(fn=process_and_translate, inputs=[input_text], outputs=[output_json])
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|