ankitklakra commited on
Commit
b4eae74
·
verified ·
1 Parent(s): 6246d58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -45
app.py CHANGED
@@ -2,25 +2,18 @@ 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
 
@@ -29,26 +22,20 @@ 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
48
- ### Preserving Tribal Languages with Artificial Intelligence
49
- *Powered by Custom Fine-Tuned Google mT5 Models*
50
- """
51
- )
52
 
53
  with gr.Row():
54
  direction = gr.Radio(
@@ -58,21 +45,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
  )
59
 
60
  with gr.Row():
61
- with gr.Column():
62
- input_text = gr.Textbox(
63
- label="Input Text",
64
- placeholder="Type your sentence here...",
65
- lines=5
66
- )
67
- translate_btn = gr.Button("Translate 🚀", variant="primary")
68
-
69
- with gr.Column():
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"],
@@ -80,11 +57,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
80
  ["तुम्हारा नाम क्या है?", "Hindi -> Kurukh"],
81
  ["मुझे पानी दो।", "Hindi -> Kurukh"]
82
  ],
83
- inputs=[input_text, direction],
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
90
  demo.launch()
 
2
  from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
  # --- CONFIGURATION ---
5
+ MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
6
+ MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
 
7
 
8
  # --- LOAD RESOURCES ---
 
 
9
  print("Loading Tokenizer...")
10
  tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
11
 
12
+ print("Loading Models...")
 
13
  model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
 
 
14
  model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
15
 
16
+ # Create Pipelines
 
17
  pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
18
  pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
19
 
 
22
  if not text:
23
  return ""
24
 
25
+ target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
 
 
 
 
26
 
27
+ try:
28
+ results = target_pipeline(text, max_length=128)
29
+ return results[0]['generated_text']
30
+ except Exception as e:
31
+ return f"Error: {str(e)}"
32
 
33
+ # --- THE UI ---
34
+
35
+ with gr.Blocks() as demo:
36
 
37
+ gr.Markdown("# 🇮🇳 AI Kurukh (Kurux) Translator")
38
+ gr.Markdown("### Preserving Tribal Languages with Artificial Intelligence")
 
 
 
 
 
39
 
40
  with gr.Row():
41
  direction = gr.Radio(
 
45
  )
46
 
47
  with gr.Row():
48
+ input_text = gr.Textbox(label="Input Text", lines=5, placeholder="Type here...")
49
+ output_text = gr.Textbox(label="Translation Result", lines=5)
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ translate_btn = gr.Button("Translate 🚀", variant="primary")
52
+
53
  gr.Examples(
54
  examples=[
55
  ["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi"],
 
57
  ["तुम्हारा नाम क्या है?", "Hindi -> Kurukh"],
58
  ["मुझे पानी दो।", "Hindi -> Kurukh"]
59
  ],
60
+ inputs=[input_text, direction]
 
61
  )
62
 
63
  translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
64
 
 
65
  demo.launch()