ankitklakra commited on
Commit
799bba5
·
verified ·
1 Parent(s): 07c7bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -14,19 +14,24 @@ MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
14
  MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
15
  SHEET_NAME = "Kurukh_Feedback_Log"
16
 
17
- # --- LOAD MODELS ---
18
- print(f"🚀 Starting App with Gradio Version: {gr.__version__}")
19
 
20
  print("Loading Translation Models...")
21
- tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
22
- model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
23
- model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
 
24
 
25
- pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
26
- pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
 
 
27
 
28
  print("Loading Voice Model...")
29
- asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
 
 
 
 
30
 
31
  # --- HELPER FUNCTIONS ---
32
  def transliterate_to_hindi(text):
@@ -40,7 +45,6 @@ def transliterate_to_hindi(text):
40
 
41
  def save_to_sheet(original, translation, correction, direction):
42
  # --- VALIDATION CHECK ---
43
-
44
  if not original or not original.strip():
45
  return "⚠️ Error: Original text is missing."
46
 
@@ -62,7 +66,6 @@ def save_to_sheet(original, translation, correction, direction):
62
 
63
  sheet = client.open(SHEET_NAME).sheet1
64
 
65
- # Check if header exists, if not add it
66
  if not sheet.get_all_values():
67
  sheet.append_row(
68
  [
@@ -83,7 +86,7 @@ def save_to_sheet(original, translation, correction, direction):
83
  return f"❌ Error: {str(e)}"
84
 
85
  def speech_to_text(audio_path):
86
- if audio_path is None:
87
  return ""
88
  return asr_pipeline(audio_path)["text"]
89
 
@@ -219,16 +222,16 @@ with gr.Blocks(title="Kurukh AI Translator") as demo:
219
  output_audio = gr.Audio(
220
  label="🔊 Listen (Hindi Only)", interactive=False
221
  )
222
-
 
223
  gr.Markdown("### 💡 Try these examples:")
224
- gr.Examples(
225
  examples=[
226
-
227
  # 1. Kurukh (Devanagari Script)
228
-
229
  ["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi", False],
230
 
231
- # 2. Hindi (Devanagari Script - Sentence)
232
  ["तुम कहाँ जा रहे हो?", "Hindi -> Kurukh", False],
233
 
234
  # 3. Hinglish (Roman Script -> needs Transliteration)
@@ -273,11 +276,5 @@ with gr.Blocks(title="Kurukh AI Translator") as demo:
273
  gr.HTML(
274
  "<center style='color:#777;'>Built with ❤️ for the Kurukh Community</center>"
275
  )
276
-
277
- translate_btn.click(
278
- fn=lambda orig, trans, direc: (orig, trans, direc),
279
- inputs=[input_text, output_text, direction],
280
- outputs=[fb_original, fb_ai_output, fb_direction]
281
- )
282
 
283
- demo.launch()
 
14
  MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
15
  SHEET_NAME = "Kurukh_Feedback_Log"
16
 
 
 
17
 
18
  print("Loading Translation Models...")
19
+ try:
20
+ tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
21
+ model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
22
+ model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
23
 
24
+ pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
25
+ pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
26
+ except Exception as e:
27
+ print(f"Error loading translation models: {e}")
28
 
29
  print("Loading Voice Model...")
30
+ try:
31
+ asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
32
+ except Exception as e:
33
+ print(f"Error loading whisper model: {e}")
34
+ asr_pipeline = None
35
 
36
  # --- HELPER FUNCTIONS ---
37
  def transliterate_to_hindi(text):
 
45
 
46
  def save_to_sheet(original, translation, correction, direction):
47
  # --- VALIDATION CHECK ---
 
48
  if not original or not original.strip():
49
  return "⚠️ Error: Original text is missing."
50
 
 
66
 
67
  sheet = client.open(SHEET_NAME).sheet1
68
 
 
69
  if not sheet.get_all_values():
70
  sheet.append_row(
71
  [
 
86
  return f"❌ Error: {str(e)}"
87
 
88
  def speech_to_text(audio_path):
89
+ if audio_path is None or asr_pipeline is None:
90
  return ""
91
  return asr_pipeline(audio_path)["text"]
92
 
 
222
  output_audio = gr.Audio(
223
  label="🔊 Listen (Hindi Only)", interactive=False
224
  )
225
+
226
+ # --- EXAMPLES SECTION ---
227
  gr.Markdown("### 💡 Try these examples:")
228
+ gr.Examples(
229
  examples=[
230
+
231
  # 1. Kurukh (Devanagari Script)
 
232
  ["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi", False],
233
 
234
+ # 2. Hindi (Devanagari Script)
235
  ["तुम कहाँ जा रहे हो?", "Hindi -> Kurukh", False],
236
 
237
  # 3. Hinglish (Roman Script -> needs Transliteration)
 
276
  gr.HTML(
277
  "<center style='color:#777;'>Built with ❤️ for the Kurukh Community</center>"
278
  )
 
 
 
 
 
 
279
 
280
+ demo.launch()