Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import matplotlib.pyplot as plt
|
|
| 10 |
import gradio as gr
|
| 11 |
from sacrebleu import corpus_bleu
|
| 12 |
import os
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Load Models
|
| 15 |
lang_detect_model = AutoModelForSequenceClassification.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
|
@@ -75,6 +77,18 @@ def search_semantic(query, top_k=3):
|
|
| 75 |
query_embedding = embed_model.encode([query])
|
| 76 |
distances, indices = index.search(query_embedding, top_k)
|
| 77 |
return [(corpus[i], float(distances[0][idx])) for idx, i in enumerate(indices[0])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Main Pipeline
|
| 80 |
def full_pipeline(user_input_text, target_lang_code, human_ref=""):
|
|
@@ -111,7 +125,10 @@ def full_pipeline(user_input_text, target_lang_code, human_ref=""):
|
|
| 111 |
bleu = corpus_bleu([translated], [[human_ref]])
|
| 112 |
bleu_score = f"{bleu.score:.2f}"
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# Gradio Interface
|
| 117 |
gr.Interface(
|
|
@@ -126,8 +143,9 @@ gr.Interface(
|
|
| 126 |
gr.Textbox(label="Translated Text"),
|
| 127 |
gr.Textbox(label="Top Semantic Matches"),
|
| 128 |
gr.Image(label="Semantic Similarity Plot"),
|
| 129 |
-
gr.Textbox(label="BLEU Score")
|
|
|
|
| 130 |
],
|
| 131 |
-
title="Multilingual Translator + Semantic Search",
|
| 132 |
-
description="Detects language β Translates β Finds related Sanskrit concepts β BLEU optional."
|
| 133 |
).launch()
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
from sacrebleu import corpus_bleu
|
| 12 |
import os
|
| 13 |
+
import tempfile
|
| 14 |
+
|
| 15 |
|
| 16 |
# Load Models
|
| 17 |
lang_detect_model = AutoModelForSequenceClassification.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
|
|
|
| 77 |
query_embedding = embed_model.encode([query])
|
| 78 |
distances, indices = index.search(query_embedding, top_k)
|
| 79 |
return [(corpus[i], float(distances[0][idx])) for idx, i in enumerate(indices[0])]
|
| 80 |
+
|
| 81 |
+
# Create downloadable output file
|
| 82 |
+
def save_output_to_file(detected_lang, translated, sem_results, bleu_score):
|
| 83 |
+
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".txt") as f:
|
| 84 |
+
f.write(f"Detected Language: {detected_lang}\n")
|
| 85 |
+
f.write(f"Translated Text: {translated}\n\n")
|
| 86 |
+
f.write("Top Semantic Matches:\n")
|
| 87 |
+
for i, (text, score) in enumerate(sem_results):
|
| 88 |
+
f.write(f"{i+1}. {text} (Score: {score:.2f})\n")
|
| 89 |
+
if bleu_score:
|
| 90 |
+
f.write(f"\nBLEU Score: {bleu_score}")
|
| 91 |
+
return f.name
|
| 92 |
|
| 93 |
# Main Pipeline
|
| 94 |
def full_pipeline(user_input_text, target_lang_code, human_ref=""):
|
|
|
|
| 125 |
bleu = corpus_bleu([translated], [[human_ref]])
|
| 126 |
bleu_score = f"{bleu.score:.2f}"
|
| 127 |
|
| 128 |
+
# Create downloadable result file
|
| 129 |
+
download_file_path = save_output_to_file(detected_lang, translated, sem_results, bleu_score)
|
| 130 |
+
return detected_lang, translated, "\n".join(result_list), plot_path, bleu_score, download_file_path
|
| 131 |
+
|
| 132 |
|
| 133 |
# Gradio Interface
|
| 134 |
gr.Interface(
|
|
|
|
| 143 |
gr.Textbox(label="Translated Text"),
|
| 144 |
gr.Textbox(label="Top Semantic Matches"),
|
| 145 |
gr.Image(label="Semantic Similarity Plot"),
|
| 146 |
+
gr.Textbox(label="BLEU Score"),
|
| 147 |
+
gr.File(label="Download Translation Report") # NEW OUTPUT
|
| 148 |
],
|
| 149 |
+
title=" Multilingual Translator + Semantic Search",
|
| 150 |
+
description="Detects language β Translates β Finds related Sanskrit concepts β BLEU optional β Downloadable report."
|
| 151 |
).launch()
|