Spaces:
Sleeping
Sleeping
Commit
·
778769f
1
Parent(s):
9601b01
trial
Browse files- app.py +70 -8
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -2,14 +2,66 @@ import torch
|
|
| 2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
from IndicTransToolkit.processor import IndicProcessor
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
|
|
|
|
| 8 |
model_name = "ai4bharat/indictrans2-en-indic-1B"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, trust_remote_code=True).to(DEVICE)
|
| 11 |
ip = IndicProcessor(inference=True)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def translate_english_to_kashmiri(text):
|
| 14 |
if not text.strip():
|
| 15 |
return "Please enter some English text."
|
|
@@ -30,15 +82,25 @@ def translate_english_to_kashmiri(text):
|
|
| 30 |
|
| 31 |
translated = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 32 |
result = ip.postprocess_batch(translated, lang=tgt_lang)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return result
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
interface.launch(
|
|
|
|
| 2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
from IndicTransToolkit.processor import IndicProcessor
|
| 4 |
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
+
# Supabase configuration
|
| 9 |
+
SUPABASE_URL = "https://gptmdbhzblfybdnohqnh.supabase.co" # ← Replace with your project URL
|
| 10 |
+
SUPABASE_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImdwdG1kYmh6YmxmeWJkbm9ocW5oIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDc0NjY1NDgsImV4cCI6MjA2MzA0MjU0OH0.CfWArts6Kd_x7Wj0a_nAyGJfrFt8F7Wdy_MdYDj9e7U" # ← Replace with your anon/public API key
|
| 11 |
+
SUPABASE_TABLE = "translations"
|
| 12 |
+
|
| 13 |
+
# Device configuration
|
| 14 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
+
# Load model and tokenizer
|
| 17 |
model_name = "ai4bharat/indictrans2-en-indic-1B"
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 19 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, trust_remote_code=True).to(DEVICE)
|
| 20 |
ip = IndicProcessor(inference=True)
|
| 21 |
|
| 22 |
+
# Function to save translation to Supabase
|
| 23 |
+
def save_to_supabase(input_text, output_text):
|
| 24 |
+
payload = {
|
| 25 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 26 |
+
"input_text": input_text,
|
| 27 |
+
"output_text": output_text
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
headers = {
|
| 31 |
+
"apikey": SUPABASE_API_KEY,
|
| 32 |
+
"Authorization": f"Bearer {SUPABASE_API_KEY}",
|
| 33 |
+
"Content-Type": "application/json"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
response = requests.post(
|
| 37 |
+
f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}",
|
| 38 |
+
headers=headers,
|
| 39 |
+
json=payload
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return response.status_code == 201
|
| 43 |
+
|
| 44 |
+
# Function to retrieve recent translation history from Supabase
|
| 45 |
+
def get_translation_history():
|
| 46 |
+
headers = {
|
| 47 |
+
"apikey": SUPABASE_API_KEY,
|
| 48 |
+
"Authorization": f"Bearer {SUPABASE_API_KEY}"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
response = requests.get(
|
| 52 |
+
f"{SUPABASE_URL}/rest/v1/{SUPABASE_TABLE}?order=timestamp.desc&limit=10",
|
| 53 |
+
headers=headers
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if response.status_code == 200:
|
| 57 |
+
records = response.json()
|
| 58 |
+
return "\n\n".join(
|
| 59 |
+
[f"[{r['timestamp']}]\nEN: {r['input_text']}\nKS: {r['output_text']}" for r in records]
|
| 60 |
+
)
|
| 61 |
+
else:
|
| 62 |
+
return "Failed to load history."
|
| 63 |
+
|
| 64 |
+
# Translation function
|
| 65 |
def translate_english_to_kashmiri(text):
|
| 66 |
if not text.strip():
|
| 67 |
return "Please enter some English text."
|
|
|
|
| 82 |
|
| 83 |
translated = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 84 |
result = ip.postprocess_batch(translated, lang=tgt_lang)[0]
|
| 85 |
+
|
| 86 |
+
# Save to Supabase
|
| 87 |
+
save_to_supabase(text, result)
|
| 88 |
+
|
| 89 |
return result
|
| 90 |
|
| 91 |
+
# Gradio Interface
|
| 92 |
+
with gr.Blocks() as interface:
|
| 93 |
+
gr.Markdown("## English to Kashmiri Translator\nIndicTrans2 model with Supabase history logging.")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
input_text = gr.Textbox(lines=2, label="English Text", placeholder="Enter English text...")
|
| 97 |
+
output_text = gr.Textbox(lines=2, label="Kashmiri Translation", placeholder="Translated Kashmiri text...")
|
| 98 |
+
|
| 99 |
+
translate_button = gr.Button("Translate")
|
| 100 |
+
history_box = gr.Textbox(lines=10, label="Translation History", interactive=False)
|
| 101 |
+
|
| 102 |
+
translate_button.click(fn=translate_english_to_kashmiri, inputs=input_text, outputs=output_text)\
|
| 103 |
+
.then(fn=get_translation_history, outputs=history_box)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
interface.launch()
|
requirements.txt
CHANGED
|
@@ -4,4 +4,5 @@ sentencepiece
|
|
| 4 |
nltk
|
| 5 |
mosestokenizer
|
| 6 |
gradio
|
|
|
|
| 7 |
git+https://github.com/VarunGumma/IndicTransToolkit.git
|
|
|
|
| 4 |
nltk
|
| 5 |
mosestokenizer
|
| 6 |
gradio
|
| 7 |
+
requests
|
| 8 |
git+https://github.com/VarunGumma/IndicTransToolkit.git
|