Spaces:
Running
Running
Commit
Β·
902cd01
1
Parent(s):
89d62bb
Done
Browse files- app.py +194 -0
- postBuild +1 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
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"
|
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 both models ahead of time
|
17 |
+
model_en_to_indic = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True).to(DEVICE)
|
18 |
+
tokenizer_en_to_indic = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True)
|
19 |
+
model_indic_to_en = AutoModelForSeq2SeqLM.from_pretrained("ai4bharat/indictrans2-indic-en-1B", trust_remote_code=True).to(DEVICE)
|
20 |
+
tokenizer_indic_to_en = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-indic-en-1B", trust_remote_code=True)
|
21 |
+
ip = IndicProcessor(inference=True)
|
22 |
+
|
23 |
+
# Separate save function (only called if user clicks Save button)
|
24 |
+
def save_to_supabase(input_text, output_text, direction):
|
25 |
+
if not input_text.strip() or not output_text.strip():
|
26 |
+
return "Nothing to save."
|
27 |
+
|
28 |
+
# Choose table name based on direction
|
29 |
+
table_name = "translations" if direction == "en_to_ks" else "ks_to_en_translations"
|
30 |
+
|
31 |
+
payload = {
|
32 |
+
"timestamp": datetime.utcnow().isoformat(),
|
33 |
+
"input_text": input_text,
|
34 |
+
"output_text": output_text
|
35 |
+
}
|
36 |
+
|
37 |
+
headers = {
|
38 |
+
"apikey": SUPABASE_API_KEY,
|
39 |
+
"Authorization": f"Bearer {SUPABASE_API_KEY}",
|
40 |
+
"Content-Type": "application/json"
|
41 |
+
}
|
42 |
+
|
43 |
+
try:
|
44 |
+
response = requests.post(
|
45 |
+
f"{SUPABASE_URL}/rest/v1/{table_name}",
|
46 |
+
headers=headers,
|
47 |
+
json=payload,
|
48 |
+
timeout=10
|
49 |
+
)
|
50 |
+
|
51 |
+
if response.status_code == 201:
|
52 |
+
return "β
Saved successfully!"
|
53 |
+
else:
|
54 |
+
print("SAVE ERROR:", response.status_code, response.text)
|
55 |
+
return "β Failed to save."
|
56 |
+
except Exception as e:
|
57 |
+
print("SAVE EXCEPTION:", e)
|
58 |
+
return "β Save request error."
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
# Function to retrieve recent translation history from Supabase
|
63 |
+
def get_translation_history(direction="en_to_ks"):
|
64 |
+
table_name = "translations" if direction == "en_to_ks" else "ks_to_en_translations"
|
65 |
+
|
66 |
+
headers = {
|
67 |
+
"apikey": SUPABASE_API_KEY,
|
68 |
+
"Authorization": f"Bearer {SUPABASE_API_KEY}"
|
69 |
+
}
|
70 |
+
|
71 |
+
try:
|
72 |
+
response = requests.get(
|
73 |
+
f"{SUPABASE_URL}/rest/v1/{table_name}?order=timestamp.desc&limit=10",
|
74 |
+
headers=headers,
|
75 |
+
timeout=10
|
76 |
+
)
|
77 |
+
|
78 |
+
if response.status_code == 200:
|
79 |
+
records = response.json()
|
80 |
+
return "\n\n".join(
|
81 |
+
[f"Input: {r['input_text']} β Output: {r['output_text']}" for r in records]
|
82 |
+
)
|
83 |
+
else:
|
84 |
+
return "Failed to load history."
|
85 |
+
except Exception as e:
|
86 |
+
print("HISTORY FETCH ERROR:", e)
|
87 |
+
return "Error loading history."
|
88 |
+
|
89 |
+
|
90 |
+
# Translation function
|
91 |
+
def translate(text, direction):
|
92 |
+
if not text.strip():
|
93 |
+
return "Please enter some text.", gr.update(), gr.update()
|
94 |
+
|
95 |
+
if direction == "en_to_ks":
|
96 |
+
src_lang = "eng_Latn"
|
97 |
+
tgt_lang = "kas_Arab"
|
98 |
+
model = model_en_to_indic
|
99 |
+
tokenizer = tokenizer_en_to_indic
|
100 |
+
else:
|
101 |
+
src_lang = "kas_Arab"
|
102 |
+
tgt_lang = "eng_Latn"
|
103 |
+
model = model_indic_to_en
|
104 |
+
tokenizer = tokenizer_indic_to_en
|
105 |
+
|
106 |
+
try:
|
107 |
+
processed = ip.preprocess_batch([text], src_lang=src_lang, tgt_lang=tgt_lang)
|
108 |
+
batch = tokenizer(processed, return_tensors="pt", padding=True).to(DEVICE)
|
109 |
+
|
110 |
+
with torch.no_grad():
|
111 |
+
outputs = model.generate(
|
112 |
+
**batch,
|
113 |
+
max_length=256,
|
114 |
+
num_beams=5,
|
115 |
+
num_return_sequences=1
|
116 |
+
)
|
117 |
+
|
118 |
+
translated = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
119 |
+
result = ip.postprocess_batch(translated, lang=tgt_lang)[0]
|
120 |
+
|
121 |
+
return result, gr.update(), gr.update()
|
122 |
+
except Exception as e:
|
123 |
+
print("Translation Error:", e)
|
124 |
+
return "β οΈ Translation failed.", gr.update(), gr.update()
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
# Toggle function to switch direction and update labels
|
129 |
+
def switch_direction(direction, input_text_val, output_text_val):
|
130 |
+
new_direction = "ks_to_en" if direction == "en_to_ks" else "en_to_ks"
|
131 |
+
input_label = "Kashmiri Text" if new_direction == "ks_to_en" else "English Text"
|
132 |
+
output_label = "English Translation" if new_direction == "ks_to_en" else "Kashmiri Translation"
|
133 |
+
|
134 |
+
# Swap input/output text too
|
135 |
+
return (
|
136 |
+
new_direction,
|
137 |
+
gr.update(value=output_text_val, label=input_label),
|
138 |
+
gr.update(value=input_text_val, label=output_label)
|
139 |
+
)
|
140 |
+
|
141 |
+
|
142 |
+
# Update your Gradio interface block
|
143 |
+
with gr.Blocks() as interface:
|
144 |
+
gr.HTML("""
|
145 |
+
<div style="display: flex; justify-content: space-between; align-items: center; padding: 10px;">
|
146 |
+
<img src="https://raw.githubusercontent.com/BurhaanRasheedZargar/Images/211321a234613a9c3dd944fe9367cf13d1386239/assets/left_logo.png" style="height:150px; width:auto;">
|
147 |
+
<h2 style="margin: 0; text-align: center;">English β Kashmiri Translator</h2>
|
148 |
+
<img src="https://raw.githubusercontent.com/BurhaanRasheedZargar/Images/77797f7f7cbee328fa0f9d31cf3e290441e04cd3/assets/right_logo.png">
|
149 |
+
</div>
|
150 |
+
""")
|
151 |
+
|
152 |
+
|
153 |
+
translation_direction = gr.State(value="en_to_ks")
|
154 |
+
|
155 |
+
with gr.Row():
|
156 |
+
input_text = gr.Textbox(lines=2, label="English Text", placeholder="Enter text....")
|
157 |
+
output_text = gr.Textbox(lines=2, label="Kashmiri Translation", placeholder="Translated text....")
|
158 |
+
|
159 |
+
with gr.Row():
|
160 |
+
translate_button = gr.Button("Translate")
|
161 |
+
save_button = gr.Button("Save Translation")
|
162 |
+
switch_button = gr.Button("Switch") # β New button
|
163 |
+
|
164 |
+
save_status = gr.Textbox(label="Save Status", interactive=False)
|
165 |
+
history_box = gr.Textbox(lines=10, label="Translation History", interactive=False)
|
166 |
+
|
167 |
+
# Actions
|
168 |
+
translate_button.click(
|
169 |
+
fn=translate,
|
170 |
+
inputs=[input_text, translation_direction],
|
171 |
+
outputs=[output_text, input_text, output_text]
|
172 |
+
)
|
173 |
+
|
174 |
+
save_button.click(
|
175 |
+
fn=save_to_supabase,
|
176 |
+
inputs=[input_text, output_text, translation_direction],
|
177 |
+
outputs=save_status
|
178 |
+
).then(
|
179 |
+
fn=get_translation_history,
|
180 |
+
inputs=translation_direction,
|
181 |
+
outputs=history_box
|
182 |
+
)
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
+
switch_button.click(
|
187 |
+
fn=switch_direction,
|
188 |
+
inputs=[translation_direction, input_text, output_text],
|
189 |
+
outputs=[translation_direction, input_text, output_text]
|
190 |
+
)
|
191 |
+
|
192 |
+
|
193 |
+
if __name__ == "__main__":
|
194 |
+
interface.launch(share=True, inbrowser=True)
|
postBuild
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
python -m nltk.downloader punkt
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=1.12
|
2 |
+
transformers>=4.30.0
|
3 |
+
sentencepiece
|
4 |
+
nltk
|
5 |
+
mosestokenizer
|
6 |
+
gradio
|
7 |
+
requests
|
8 |
+
git+https://github.com/VarunGumma/IndicTransToolkit.git
|