Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,57 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"
|
| 12 |
-
"
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Translate text
|
| 60 |
-
translation = translate_text(input_text)
|
| 61 |
-
return translation
|
| 62 |
-
|
| 63 |
-
# Create a Gradio interface
|
| 64 |
-
interface = gr.Interface(
|
| 65 |
-
fn=process_request,
|
| 66 |
-
inputs="text",
|
| 67 |
-
outputs="text",
|
| 68 |
-
title="Frenchizer",
|
| 69 |
-
description="Translate text from English to French with context detection."
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
# Launch the Gradio app
|
| 73 |
-
interface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
+
|
| 5 |
+
# Load the model and precompute label embeddings
|
| 6 |
+
context_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 7 |
+
labels = [
|
| 8 |
+
"aerospace", "anatomy", "anthropology", "art",
|
| 9 |
+
"automotive", "blockchain", "biology", "chemistry",
|
| 10 |
+
"cryptocurrency", "data science", "design", "e-commerce",
|
| 11 |
+
"education", "engineering", "entertainment", "environment",
|
| 12 |
+
"fashion", "finance", "food commerce", "general",
|
| 13 |
+
"gaming", "healthcare", "history", "html",
|
| 14 |
+
"information technology", "IT", "keywords", "legal",
|
| 15 |
+
"literature", "machine learning", "marketing", "medicine",
|
| 16 |
+
"music", "personal development", "philosophy", "physics",
|
| 17 |
+
"politics", "poetry", "programming", "real estate", "retail",
|
| 18 |
+
"robotics", "slang", "social media", "speech", "sports",
|
| 19 |
+
"sustained", "technical", "theater", "tourism", "travel"
|
| 20 |
+
]
|
| 21 |
+
label_embeddings = context_model.encode(labels)
|
| 22 |
+
|
| 23 |
+
def detect_context(input_text, high_confidence_threshold=0.9, fallback_threshold=0.8, max_results=3):
|
| 24 |
+
input_embedding = context_model.encode([input_text])
|
| 25 |
+
similarities = cosine_similarity(input_embedding, label_embeddings)[0]
|
| 26 |
+
|
| 27 |
+
for label, score in zip(labels, similarities):
|
| 28 |
+
if score >= high_confidence_threshold:
|
| 29 |
+
return [label]
|
| 30 |
+
|
| 31 |
+
label_scores = [(label, score) for label, score in zip(labels, similarities) if score >= fallback_threshold]
|
| 32 |
+
sorted_labels = sorted(label_scores, key=lambda x: x[1], reverse=True)[:max_results]
|
| 33 |
+
return [label for label, score in sorted_labels] if sorted_labels else ["general"]
|
| 34 |
+
|
| 35 |
+
# Translation client
|
| 36 |
+
from gradio_client import Client
|
| 37 |
+
translation_client = Client("Frenchizer/space_3")
|
| 38 |
+
|
| 39 |
+
def translate_text(input_text):
|
| 40 |
+
return translation_client.predict(input_text)
|
| 41 |
+
|
| 42 |
+
def process_request(input_text):
|
| 43 |
+
context = detect_context(input_text)
|
| 44 |
+
print(f"Detected context: {context}")
|
| 45 |
+
translation = translate_text(input_text)
|
| 46 |
+
return translation
|
| 47 |
+
|
| 48 |
+
# Gradio interface
|
| 49 |
+
interface = gr.Interface(
|
| 50 |
+
fn=process_request,
|
| 51 |
+
inputs="text",
|
| 52 |
+
outputs="text",
|
| 53 |
+
title="Frenchizer",
|
| 54 |
+
description="Translate text from English to French with context detection."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|