Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 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 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Compute cosine similarities
|
| 4 |
+
similarities = cosine_similarity(input_embedding, label_embeddings)[0]
|
| 5 |
+
|
| 6 |
+
# Apply softmax to convert similarities to probabilities
|
| 7 |
+
probabilities = softmax(similarities)
|
| 8 |
+
|
| 9 |
+
# Pair each label with its probability
|
| 10 |
+
label_probabilities = list(zip(labels, probabilities))
|
| 11 |
+
|
| 12 |
+
# Filter contexts with confidence >= threshold
|
| 13 |
+
high_confidence_contexts = [(label, score) for label, score in label_probabilities if score >= threshold]
|
| 14 |
+
|
| 15 |
+
# If no contexts meet the threshold, default to "general"
|
| 16 |
+
if not high_confidence_contexts:
|
| 17 |
+
high_confidence_contexts = [("general", 1.0)] # Assign a default score of 1.0 for "general"
|
| 18 |
+
|
| 19 |
+
return high_confidence_contexts
|
| 20 |
+
|
| 21 |
+
# Mock translation clients for different contexts
|
| 22 |
+
def get_translation_client(context):
|
| 23 |
+
"""
|
| 24 |
+
Returns the appropriate Hugging Face Space client for the given context.
|
| 25 |
+
For now, all contexts use the same mock space.
|
| 26 |
+
"""
|
| 27 |
+
return Client("Frenchizer/space_18") # Replace with actual Space paths for each context
|
| 28 |
+
|
| 29 |
+
def translate_text(input_text, context):
|
| 30 |
+
"""
|
| 31 |
+
Translates the input text using the appropriate model for the given context.
|
| 32 |
+
"""
|
| 33 |
+
client = get_translation_client(context)
|
| 34 |
+
return client.predict(input_text)
|
| 35 |
+
|
| 36 |
+
def process_request(input_text):
|
| 37 |
+
# Step 1: Detect context
|
| 38 |
+
context_results = detect_context(input_text)
|
| 39 |
+
|
| 40 |
+
# Step 2: Translate the text for each context
|
| 41 |
+
translations = {}
|
| 42 |
+
for context, score in context_results:
|
| 43 |
+
translations[context] = translate_text(input_text, context)
|
| 44 |
+
|
| 45 |
+
# Step 3: Print the list of high-confidence contexts and translations
|
| 46 |
+
print("High-confidence contexts (score >= 0.022):", context_results)
|
| 47 |
+
print("Translations:", translations)
|
| 48 |
+
|
| 49 |
+
# Return the translations and contexts
|
| 50 |
+
return translations, context_results
|
| 51 |
+
|
| 52 |
+
# Gradio interface
|
| 53 |
+
def gradio_interface(input_text):
|
| 54 |
+
translation, contexts = process_request(input_text)
|
| 55 |
+
# Format the output
|
| 56 |
+
output = f"{translation}\n"
|
| 57 |
+
return output.strip()
|
| 58 |
+
|
| 59 |
+
# Create the Gradio interface
|
| 60 |
+
interface = gr.Interface(
|
| 61 |
+
fn=gradio_interface,
|
| 62 |
+
inputs="text",
|
| 63 |
+
outputs="text",
|
| 64 |
+
title="Frenchizer",
|
| 65 |
+
description="Translate text from English to French with context detection and threshold."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
interface.launch()
|