Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,17 @@
|
|
| 1 |
import json
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
-
import os
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
-
from
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
os.environ["HF_TOKEN"] = os.getenv("token", "Not set")
|
| 10 |
-
|
| 11 |
-
# Debug: Confirm HF_TOKEN is set
|
| 12 |
-
print("HF_TOKEN:", os.getenv("HF_TOKEN", "Not set"))
|
| 13 |
-
|
| 14 |
-
# 1) Load the embedding model
|
| 15 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 16 |
|
| 17 |
-
# 2) Load
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def load_docs(path, fmt):
|
| 19 |
with open(path, 'r') as f:
|
| 20 |
data = json.load(f)
|
|
@@ -30,16 +27,13 @@ truck_docs = load_docs(
|
|
| 30 |
)
|
| 31 |
docs = call_docs + truck_docs
|
| 32 |
|
| 33 |
-
#
|
| 34 |
doc_embeddings = embedder.encode(
|
| 35 |
docs,
|
| 36 |
convert_to_numpy=True,
|
| 37 |
normalize_embeddings=True
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# 4) Initialize the Hugging Face Inference client with t5-small
|
| 41 |
-
client = InferenceClient(model="t5-small")
|
| 42 |
-
|
| 43 |
def rag_translate(query, top_k):
|
| 44 |
# 4.1 Retrieval step
|
| 45 |
q_emb = embedder.encode(
|
|
@@ -52,23 +46,31 @@ def rag_translate(query, top_k):
|
|
| 52 |
retrieved = [docs[i] for i in idxs]
|
| 53 |
retrieved_text = "\n".join(retrieved)
|
| 54 |
|
| 55 |
-
# 4.2 Translation step
|
| 56 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Translate to Spanish
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
-
spanish_text =
|
| 64 |
|
| 65 |
# Translate to French
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
-
french_text =
|
| 72 |
|
| 73 |
# Combine translations
|
| 74 |
translations = f"Spanish:\n{spanish_text}\n\nFrench:\n{french_text}"
|
|
@@ -107,7 +109,7 @@ with gr.Blocks() as demo:
|
|
| 107 |
"""
|
| 108 |
**How it works**
|
| 109 |
1) Retrieval: fetch top-k snippets from JSON KBs.
|
| 110 |
-
2) Translation: translate retrieved content to Spanish and French using t5-small.
|
| 111 |
"""
|
| 112 |
)
|
| 113 |
|
|
|
|
| 1 |
import json
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 6 |
|
| 7 |
+
# 1) Load the embedding model for retrieval
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
|
| 10 |
+
# 2) Load the T5 model and tokenizer for translation
|
| 11 |
+
tokenizer = T5Tokenizer.from_pretrained('t5-small')
|
| 12 |
+
model = T5ForConditionalGeneration.from_pretrained('t5-small')
|
| 13 |
+
|
| 14 |
+
# 3) Load JSON knowledge bases
|
| 15 |
def load_docs(path, fmt):
|
| 16 |
with open(path, 'r') as f:
|
| 17 |
data = json.load(f)
|
|
|
|
| 27 |
)
|
| 28 |
docs = call_docs + truck_docs
|
| 29 |
|
| 30 |
+
# 4) Pre-compute embeddings for all documents
|
| 31 |
doc_embeddings = embedder.encode(
|
| 32 |
docs,
|
| 33 |
convert_to_numpy=True,
|
| 34 |
normalize_embeddings=True
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
def rag_translate(query, top_k):
|
| 38 |
# 4.1 Retrieval step
|
| 39 |
q_emb = embedder.encode(
|
|
|
|
| 46 |
retrieved = [docs[i] for i in idxs]
|
| 47 |
retrieved_text = "\n".join(retrieved)
|
| 48 |
|
| 49 |
+
# 4.2 Translation step using local t5-small
|
| 50 |
try:
|
| 51 |
+
# Prepare input for translation
|
| 52 |
+
spanish_input = f"translate English to Spanish: {retrieved_text}"
|
| 53 |
+
french_input = f"translate English to French: {retrieved_text}"
|
| 54 |
+
|
| 55 |
# Translate to Spanish
|
| 56 |
+
spanish_inputs = tokenizer(spanish_input, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 57 |
+
spanish_outputs = model.generate(
|
| 58 |
+
spanish_inputs["input_ids"],
|
| 59 |
+
max_length=512,
|
| 60 |
+
num_beams=4,
|
| 61 |
+
early_stopping=True
|
| 62 |
)
|
| 63 |
+
spanish_text = tokenizer.decode(spanish_outputs[0], skip_special_tokens=True)
|
| 64 |
|
| 65 |
# Translate to French
|
| 66 |
+
french_inputs = tokenizer(french_input, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 67 |
+
french_outputs = model.generate(
|
| 68 |
+
french_inputs["input_ids"],
|
| 69 |
+
max_length=512,
|
| 70 |
+
num_beams=4,
|
| 71 |
+
early_stopping=True
|
| 72 |
)
|
| 73 |
+
french_text = tokenizer.decode(french_outputs[0], skip_special_tokens=True)
|
| 74 |
|
| 75 |
# Combine translations
|
| 76 |
translations = f"Spanish:\n{spanish_text}\n\nFrench:\n{french_text}"
|
|
|
|
| 109 |
"""
|
| 110 |
**How it works**
|
| 111 |
1) Retrieval: fetch top-k snippets from JSON KBs.
|
| 112 |
+
2) Translation: translate retrieved content to Spanish and French using t5-small locally.
|
| 113 |
"""
|
| 114 |
)
|
| 115 |
|