ankitklakra commited on
Commit
28c5b18
ยท
verified ยท
1 Parent(s): 188594b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -59
app.py CHANGED
@@ -5,105 +5,136 @@ import json
5
  import gspread
6
  from oauth2client.service_account import ServiceAccountCredentials
7
  from datetime import datetime
 
 
8
 
9
  # --- CONFIGURATION ---
10
  MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
11
  MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
12
- SHEET_NAME = "Kurukh_Feedback_Log"
13
 
14
- # --- GOOGLE SHEETS SETUP ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def save_to_sheet(original, translation, correction, direction):
16
  try:
17
- # 1. Get credentials from Hugging Face Secrets
18
  json_creds = os.getenv("GOOGLE_CREDENTIALS")
19
- if not json_creds:
20
- return "โš ๏ธ Error: Google Credentials not found in Settings."
21
-
22
  creds_dict = json.loads(json_creds)
23
-
24
- # 2. Authenticate
25
  scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
26
  creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
27
  client = gspread.authorize(creds)
28
-
29
- # 3. Open Sheet
30
  sheet = client.open(SHEET_NAME).sheet1
31
-
32
- # 4. Add Headers if empty
33
  if not sheet.get_all_values():
34
  sheet.append_row(["Timestamp", "Direction", "Original Text", "AI Translation", "User Correction"])
35
-
36
- # 5. Append Row
37
  sheet.append_row([str(datetime.now()), direction, original, translation, correction])
38
- return "โœ… Success! Saved to Google Sheets."
39
-
40
  except Exception as e:
41
- return f"โŒ Error saving to Sheet: {str(e)}"
42
-
43
- # --- LOAD RESOURCES ---
44
- print("Loading Tokenizer...")
45
- tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
46
 
47
- print("Loading Models...")
48
- model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
49
- model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
50
 
51
- pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
52
- pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
 
 
 
 
 
53
 
54
- # --- TRANSLATION FUNCTION ---
55
- def translate_text(text, direction):
56
  if not text:
57
- return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
59
  try:
60
- results = target_pipeline(text, max_length=128)
61
- return results[0]['generated_text']
62
  except Exception as e:
63
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
64
 
65
  # --- THE UI ---
66
  with gr.Blocks() as demo:
67
  gr.Markdown("# ๐Ÿ‡ฎ๐Ÿ‡ณ AI Kurukh (Kurux) Translator")
68
- gr.Markdown("### Preserving Tribal Languages with Artificial Intelligence")
69
 
70
  with gr.Tabs():
71
- # TAB 1: TRANSLATOR
72
- with gr.TabItem("๐Ÿ—ฃ๏ธ Translator"):
73
- with gr.Row():
74
- direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Translation Mode", value="Kurukh -> Hindi")
75
  with gr.Row():
76
- input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4)
77
- output_text = gr.Textbox(label="AI Translation", lines=4, interactive=False)
78
 
79
- translate_btn = gr.Button("Translate ๐Ÿš€", variant="primary")
80
- translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
 
 
 
 
 
 
 
 
 
81
 
82
- gr.Examples(
83
- examples=[
84
- ["เคจเคฟเค˜เฅˆ เคจเคพเคฎเฅ‡ เค‡เคจเฅเคฆเฅเคฐเคพ เคนเคฟเค•เฅˆ?", "Kurukh -> Hindi"],
85
- ["เคคเฅเคฎเฅเคนเคพเคฐเคพ เคจเคพเคฎ เค•เฅเคฏเคพ เคนเฅˆ?", "Hindi -> Kurukh"]
86
- ],
87
- inputs=[input_text, direction]
88
  )
89
 
90
- # TAB 2: GOOGLE SHEETS FEEDBACK
91
  with gr.TabItem("๐Ÿ“ Improve the AI"):
92
  gr.Markdown("### Help us improve!")
93
  with gr.Row():
94
  fb_direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Direction", value="Kurukh -> Hindi")
95
  with gr.Row():
96
- fb_original = gr.Textbox(label="Original Text")
97
- fb_ai_output = gr.Textbox(label="What the AI said (Optional)")
98
- fb_user_correct = gr.Textbox(label="Your Correction (Required)", lines=2)
99
-
100
- submit_btn = gr.Button("Submit")
101
  status_lbl = gr.Label(label="Status")
102
-
103
- submit_btn.click(
104
- fn=save_to_sheet,
105
- inputs=[fb_original, fb_ai_output, fb_user_correct, fb_direction],
106
- outputs=status_lbl
107
- )
108
 
109
  demo.launch()
 
5
  import gspread
6
  from oauth2client.service_account import ServiceAccountCredentials
7
  from datetime import datetime
8
+ from gtts import gTTS
9
+ import tempfile
10
 
11
  # --- CONFIGURATION ---
12
  MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
13
  MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
14
+ SHEET_NAME = "Kurukh_Feedback_Log"
15
 
16
+ # --- LOAD TEXT MODELS ---
17
+ print("Loading Translation Models...")
18
+ tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
19
+ model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
20
+ model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
21
+
22
+ pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
23
+ pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
24
+
25
+ # --- LOAD AUDIO MODEL (WHISPER) ---
26
+ print("Loading Voice Model...")
27
+
28
+ asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
29
+
30
+ # --- GOOGLE SHEETS FUNCTION ---
31
  def save_to_sheet(original, translation, correction, direction):
32
  try:
 
33
  json_creds = os.getenv("GOOGLE_CREDENTIALS")
34
+ if not json_creds: return "โš ๏ธ Error: Credentials missing."
 
 
35
  creds_dict = json.loads(json_creds)
 
 
36
  scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
37
  creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
38
  client = gspread.authorize(creds)
 
 
39
  sheet = client.open(SHEET_NAME).sheet1
 
 
40
  if not sheet.get_all_values():
41
  sheet.append_row(["Timestamp", "Direction", "Original Text", "AI Translation", "User Correction"])
 
 
42
  sheet.append_row([str(datetime.now()), direction, original, translation, correction])
43
+ return "โœ… Saved to Google Sheets."
 
44
  except Exception as e:
45
+ return f"โŒ Error: {str(e)}"
 
 
 
 
46
 
47
+ # --- CORE FUNCTIONS ---
 
 
48
 
49
+ def speech_to_text(audio_path):
50
+ """Converts Microphone Audio -> Hindi/English Text"""
51
+ if audio_path is None:
52
+ return ""
53
+ print("Transcribing audio...")
54
+ text = asr_pipeline(audio_path)["text"]
55
+ return text
56
 
57
+ def text_to_speech(text, language="hi"):
58
+ """Converts Text -> Audio File"""
59
  if not text:
60
+ return None
61
+ try:
62
+ # Save audio to a temporary file
63
+ tts = gTTS(text=text, lang=language)
64
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
65
+ tts.save(temp_file.name)
66
+ return temp_file.name
67
+ except:
68
+ return None
69
+
70
+ def process_translation(text, audio_input, direction):
71
+ # 1. Handle Input Source (Audio vs Text)
72
+ if audio_input is not None:
73
+ # If user spoke, transcribe it first
74
+ original_text = speech_to_text(audio_input)
75
+ else:
76
+ original_text = text
77
+
78
+ if not original_text:
79
+ return "", "", None
80
+
81
+ # 2. Translate
82
  target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
83
  try:
84
+ results = target_pipeline(original_text, max_length=128)
85
+ translated_text = results[0]['generated_text']
86
  except Exception as e:
87
+ return str(e), "", None
88
+
89
+ # 3. Handle Audio Output (Read aloud if Hindi)
90
+ audio_output = None
91
+
92
+ if direction == "Kurukh -> Hindi":
93
+ audio_output = text_to_speech(translated_text, "hi")
94
+
95
+ # Return: (Input Text shown to user), (Translation), (Audio File)
96
+ return original_text, translated_text, audio_output
97
 
98
  # --- THE UI ---
99
  with gr.Blocks() as demo:
100
  gr.Markdown("# ๐Ÿ‡ฎ๐Ÿ‡ณ AI Kurukh (Kurux) Translator")
101
+ gr.Markdown("### Voice-Enabled Tribal Language Preserver")
102
 
103
  with gr.Tabs():
104
+ with gr.TabItem("๐Ÿ—ฃ๏ธ Voice & Text Translator"):
105
+
 
 
106
  with gr.Row():
107
+ direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Mode", value="Kurukh -> Hindi")
 
108
 
109
+ # INPUT SECTION
110
+ with gr.Row():
111
+ with gr.Column():
112
+ input_text = gr.Textbox(label="Type Here", placeholder="Or use the microphone below...", lines=3)
113
+ input_audio = gr.Audio(sources=["microphone"], type="filepath", label="Or Speak (Hindi Only)")
114
+ translate_btn = gr.Button("Translate ๐Ÿš€", variant="primary")
115
+
116
+ # OUTPUT SECTION
117
+ with gr.Column():
118
+ output_text = gr.Textbox(label="Translation", lines=3, interactive=False)
119
+ output_audio = gr.Audio(label="Listen to Translation (Hindi)", interactive=False)
120
 
121
+ # LOGIC
122
+ translate_btn.click(
123
+ fn=process_translation,
124
+ inputs=[input_text, input_audio, direction],
125
+ outputs=[input_text, output_text, output_audio] # Updates input box with transcribed text too!
 
126
  )
127
 
 
128
  with gr.TabItem("๐Ÿ“ Improve the AI"):
129
  gr.Markdown("### Help us improve!")
130
  with gr.Row():
131
  fb_direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Direction", value="Kurukh -> Hindi")
132
  with gr.Row():
133
+ fb_original = gr.Textbox(label="Original")
134
+ fb_ai_output = gr.Textbox(label="AI Said")
135
+ fb_user_correct = gr.Textbox(label="Correction", lines=2)
136
+ submit_btn = gr.Button("Submit to Google Sheets โœ…")
 
137
  status_lbl = gr.Label(label="Status")
138
+ submit_btn.click(fn=save_to_sheet, inputs=[fb_original, fb_ai_output, fb_user_correct, fb_direction], outputs=status_lbl)
 
 
 
 
 
139
 
140
  demo.launch()