Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
|
| 4 |
# --- CONFIGURATION ---
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
# --- LOAD
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
print("Loading Kurukh -> Hindi Model...")
|
| 12 |
-
|
| 13 |
|
| 14 |
print("Loading Hindi -> Kurukh Model...")
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# --- TRANSLATION FUNCTION ---
|
| 18 |
def translate_text(text, direction):
|
| 19 |
if not text:
|
| 20 |
return ""
|
| 21 |
|
| 22 |
-
# Select the correct
|
| 23 |
if direction == "Kurukh -> Hindi":
|
| 24 |
target_pipeline = pipe_k2h
|
| 25 |
else:
|
| 26 |
target_pipeline = pipe_h2k
|
| 27 |
|
| 28 |
# Translate
|
| 29 |
-
# max_length=128 allow for longer sentences
|
| 30 |
results = target_pipeline(text, max_length=128)
|
| 31 |
return results[0]['generated_text']
|
| 32 |
|
| 33 |
# --- THE USER INTERFACE ---
|
| 34 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 35 |
|
| 36 |
-
# Header
|
| 37 |
gr.Markdown(
|
| 38 |
"""
|
| 39 |
# 🇮🇳 AI Kurukh (Kurux) Translator
|
|
@@ -42,7 +50,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 42 |
"""
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# Input Section
|
| 46 |
with gr.Row():
|
| 47 |
direction = gr.Radio(
|
| 48 |
["Kurukh -> Hindi", "Hindi -> Kurukh"],
|
|
@@ -63,10 +70,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 63 |
output_text = gr.Textbox(
|
| 64 |
label="Translation Result",
|
| 65 |
lines=5,
|
| 66 |
-
show_copy_button=True
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# Examples to help new users
|
| 70 |
gr.Examples(
|
| 71 |
examples=[
|
| 72 |
["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi"],
|
|
@@ -78,7 +84,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 78 |
label="Click on an example to test:"
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Logic Connection
|
| 82 |
translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
|
| 83 |
|
| 84 |
# Launch
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
# --- CONFIGURATION ---
|
| 5 |
|
| 6 |
+
MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
|
| 7 |
+
MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
|
| 8 |
|
| 9 |
+
# --- LOAD RESOURCES ---
|
| 10 |
+
# 1. Load the "Dictionary" (Tokenizer) from Google
|
| 11 |
|
| 12 |
+
print("Loading Tokenizer...")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
|
| 14 |
+
|
| 15 |
+
# 2. Load the "Brains" (Your Custom Models)
|
| 16 |
print("Loading Kurukh -> Hindi Model...")
|
| 17 |
+
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
| 18 |
|
| 19 |
print("Loading Hindi -> Kurukh Model...")
|
| 20 |
+
model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
|
| 21 |
+
|
| 22 |
+
# 3. Create the Pipelines
|
| 23 |
+
|
| 24 |
+
pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
|
| 25 |
+
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 26 |
|
| 27 |
# --- TRANSLATION FUNCTION ---
|
| 28 |
def translate_text(text, direction):
|
| 29 |
if not text:
|
| 30 |
return ""
|
| 31 |
|
| 32 |
+
# Select the correct pipeline
|
| 33 |
if direction == "Kurukh -> Hindi":
|
| 34 |
target_pipeline = pipe_k2h
|
| 35 |
else:
|
| 36 |
target_pipeline = pipe_h2k
|
| 37 |
|
| 38 |
# Translate
|
|
|
|
| 39 |
results = target_pipeline(text, max_length=128)
|
| 40 |
return results[0]['generated_text']
|
| 41 |
|
| 42 |
# --- THE USER INTERFACE ---
|
| 43 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 44 |
|
|
|
|
| 45 |
gr.Markdown(
|
| 46 |
"""
|
| 47 |
# 🇮🇳 AI Kurukh (Kurux) Translator
|
|
|
|
| 50 |
"""
|
| 51 |
)
|
| 52 |
|
|
|
|
| 53 |
with gr.Row():
|
| 54 |
direction = gr.Radio(
|
| 55 |
["Kurukh -> Hindi", "Hindi -> Kurukh"],
|
|
|
|
| 70 |
output_text = gr.Textbox(
|
| 71 |
label="Translation Result",
|
| 72 |
lines=5,
|
| 73 |
+
show_copy_button=True
|
| 74 |
)
|
| 75 |
|
|
|
|
| 76 |
gr.Examples(
|
| 77 |
examples=[
|
| 78 |
["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi"],
|
|
|
|
| 84 |
label="Click on an example to test:"
|
| 85 |
)
|
| 86 |
|
|
|
|
| 87 |
translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
|
| 88 |
|
| 89 |
# Launch
|