Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,17 +25,12 @@ labels = [
|
|
| 25 |
"education", "engineering", "entertainment", "environment",
|
| 26 |
"fashion", "finance", "food commerce", "general",
|
| 27 |
"gaming", "healthcare", "history", "html",
|
| 28 |
-
"information technology", "keywords", "legal",
|
| 29 |
"literature", "machine learning", "marketing", "medicine",
|
| 30 |
-
"music", "
|
|
|
|
| 31 |
"robotics", "slang", "social media", "speech", "sports",
|
| 32 |
-
"technical", "theater", "tourism", "travel"
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
-
styles = [
|
| 36 |
-
"formal", "positive", "negative", "poetic", "polite", "subtle", "casual", "neutral",
|
| 37 |
-
"informal", "pompous", "sustained", "rude", "sustained",
|
| 38 |
-
"sophisticated", "playful", "serious", "friendly"
|
| 39 |
]
|
| 40 |
|
| 41 |
@lru_cache(maxsize=1)
|
|
@@ -63,11 +58,6 @@ def detect_context(input_text, top_n=3):
|
|
| 63 |
# Compute cosine similarities
|
| 64 |
similarities = cosine_similarity(input_embedding, label_embeddings)[0]
|
| 65 |
|
| 66 |
-
# Debugging: Print all labels and their similarity scores
|
| 67 |
-
print("Debug: Similarity scores for all labels:")
|
| 68 |
-
for label, score in zip(labels, similarities):
|
| 69 |
-
print(f"{label}: {score:.4f}")
|
| 70 |
-
|
| 71 |
# Apply softmax to convert similarities to probabilities
|
| 72 |
probabilities = softmax(similarities)
|
| 73 |
|
|
@@ -80,7 +70,8 @@ def detect_context(input_text, top_n=3):
|
|
| 80 |
# Select the top N contexts
|
| 81 |
top_contexts = label_probabilities[:top_n]
|
| 82 |
|
| 83 |
-
|
|
|
|
| 84 |
|
| 85 |
# Translation client
|
| 86 |
translation_client = Client("Frenchizer/space_7")
|
|
@@ -94,21 +85,27 @@ def process_request(input_text):
|
|
| 94 |
translation = translate_text(input_text)
|
| 95 |
|
| 96 |
# Step 2: Detect context
|
| 97 |
-
|
| 98 |
|
| 99 |
-
# Step 3: Print the list of high-confidence contexts
|
| 100 |
-
print("Detected Contexts (Top 3):",
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Return the translation and contexts
|
| 103 |
-
return translation,
|
| 104 |
|
| 105 |
# Gradio interface
|
| 106 |
def gradio_interface(input_text):
|
| 107 |
-
translation,
|
| 108 |
# Format the output
|
| 109 |
output = f"Translation: {translation}\n\nDetected Contexts (Top 3):\n"
|
| 110 |
-
for context, score in
|
| 111 |
output += f"- {context} (confidence: {score:.4f})\n"
|
|
|
|
|
|
|
|
|
|
| 112 |
return output.strip()
|
| 113 |
|
| 114 |
# Create the Gradio interface
|
|
|
|
| 25 |
"education", "engineering", "entertainment", "environment",
|
| 26 |
"fashion", "finance", "food commerce", "general",
|
| 27 |
"gaming", "healthcare", "history", "html",
|
| 28 |
+
"information technology", "IT", "keywords", "legal",
|
| 29 |
"literature", "machine learning", "marketing", "medicine",
|
| 30 |
+
"music", "personal development", "philosophy", "physics",
|
| 31 |
+
"politics", "poetry", "programming", "real estate", "retail",
|
| 32 |
"robotics", "slang", "social media", "speech", "sports",
|
| 33 |
+
"sustained", "technical", "theater", "tourism", "travel"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
]
|
| 35 |
|
| 36 |
@lru_cache(maxsize=1)
|
|
|
|
| 58 |
# Compute cosine similarities
|
| 59 |
similarities = cosine_similarity(input_embedding, label_embeddings)[0]
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# Apply softmax to convert similarities to probabilities
|
| 62 |
probabilities = softmax(similarities)
|
| 63 |
|
|
|
|
| 70 |
# Select the top N contexts
|
| 71 |
top_contexts = label_probabilities[:top_n]
|
| 72 |
|
| 73 |
+
# Return both the top N contexts and all context scores
|
| 74 |
+
return top_contexts, label_probabilities
|
| 75 |
|
| 76 |
# Translation client
|
| 77 |
translation_client = Client("Frenchizer/space_7")
|
|
|
|
| 85 |
translation = translate_text(input_text)
|
| 86 |
|
| 87 |
# Step 2: Detect context
|
| 88 |
+
top_contexts, all_contexts = detect_context(input_text)
|
| 89 |
|
| 90 |
+
# Step 3: Print the list of high-confidence contexts and all context scores
|
| 91 |
+
print("Detected Contexts (Top 3):", top_contexts)
|
| 92 |
+
print("All Context Scores:")
|
| 93 |
+
for context, score in all_contexts:
|
| 94 |
+
print(f"- {context}: {score:.4f}")
|
| 95 |
|
| 96 |
# Return the translation and contexts
|
| 97 |
+
return translation, top_contexts, all_contexts
|
| 98 |
|
| 99 |
# Gradio interface
|
| 100 |
def gradio_interface(input_text):
|
| 101 |
+
translation, top_contexts, all_contexts = process_request(input_text)
|
| 102 |
# Format the output
|
| 103 |
output = f"Translation: {translation}\n\nDetected Contexts (Top 3):\n"
|
| 104 |
+
for context, score in top_contexts:
|
| 105 |
output += f"- {context} (confidence: {score:.4f})\n"
|
| 106 |
+
output += "\nAll Context Scores:\n"
|
| 107 |
+
for context, score in all_contexts:
|
| 108 |
+
output += f"- {context}: {score:.4f}\n"
|
| 109 |
return output.strip()
|
| 110 |
|
| 111 |
# Create the Gradio interface
|