ankitklakra commited on
Commit
a5ae00b
ยท
verified ยท
1 Parent(s): a2c9f05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -106
app.py CHANGED
@@ -9,13 +9,11 @@ from gtts import gTTS
9
  import tempfile
10
  import requests
11
 
12
-
13
  # --- CONFIGURATION ---
14
  MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
15
  MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
16
  SHEET_NAME = "Kurukh_Feedback_Log"
17
 
18
-
19
  # --- LOAD MODELS ---
20
  print(f"๐Ÿš€ Starting App with Gradio Version: {gr.__version__}")
21
 
@@ -30,20 +28,16 @@ pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer
30
  print("Loading Voice Model...")
31
  asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
32
 
33
-
34
  # --- HELPER FUNCTIONS ---
35
  def transliterate_to_hindi(text):
36
  try:
37
- url = (
38
- "https://inputtools.google.com/request?text={}&itc=hi-t-i0-und&num=1"
39
- )
40
  response = requests.get(url.format(text))
41
  result = response.json()
42
  return result[1][0][1][0]
43
  except:
44
  return text
45
 
46
-
47
  def save_to_sheet(original, translation, correction, direction):
48
  try:
49
  json_creds = os.getenv("GOOGLE_CREDENTIALS")
@@ -55,10 +49,9 @@ def save_to_sheet(original, translation, correction, direction):
55
  "https://spreadsheets.google.com/feeds",
56
  "https://www.googleapis.com/auth/drive",
57
  ]
58
- creds = ServiceAccountCredentials.from_json_keyfile_dict(
59
- creds_dict, scope
60
- )
61
  client = gspread.authorize(creds)
 
62
  sheet = client.open(SHEET_NAME).sheet1
63
 
64
  if not sheet.get_all_values():
@@ -73,56 +66,39 @@ def save_to_sheet(original, translation, correction, direction):
73
  )
74
 
75
  sheet.append_row(
76
- [
77
- str(datetime.now()),
78
- direction,
79
- original,
80
- translation,
81
- correction,
82
- ]
83
  )
 
84
  return "โœ… Saved to Google Sheets."
85
  except Exception as e:
86
  return f"โŒ Error: {str(e)}"
87
 
88
-
89
  def speech_to_text(audio_path):
90
  if audio_path is None:
91
  return ""
92
  return asr_pipeline(audio_path)["text"]
93
 
94
-
95
  def text_to_speech(text, language="hi"):
96
  if not text:
97
  return None
98
  try:
99
  tts = gTTS(text=text, lang=language)
100
- temp_file = tempfile.NamedTemporaryFile(
101
- delete=False, suffix=".mp3"
102
- )
103
  tts.save(temp_file.name)
104
  return temp_file.name
105
  except:
106
  return None
107
 
108
-
109
  # --- MAIN TRANSLATION LOGIC ---
110
  def process_translation(text, audio_input, direction, is_hinglish):
111
- original_text = (
112
- speech_to_text(audio_input) if audio_input else text
113
- )
114
-
115
  if not original_text:
116
  return "", "", None
117
 
118
  if direction == "Hindi -> Kurukh" and is_hinglish:
119
  original_text = transliterate_to_hindi(original_text)
120
 
121
- target_pipeline = (
122
- pipe_k2h
123
- if direction == "Kurukh -> Hindi"
124
- else pipe_h2k
125
- )
126
 
127
  try:
128
  results = target_pipeline(
@@ -144,71 +120,62 @@ def process_translation(text, audio_input, direction, is_hinglish):
144
  return original_text, translated_text, audio_output
145
 
146
 
147
- # --- CUSTOM CSS ---
148
  universal_css = """
149
  <style>
150
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
151
-
152
- body, .gradio-container {
153
- font-family: 'Poppins', sans-serif !important;
154
  }
155
-
156
- .header-div {
157
- text-align: center;
158
- margin-bottom: 25px;
159
  padding: 20px;
160
  background: linear-gradient(to right, #f8f9fa, #e9ecef);
161
  border-radius: 15px;
162
  }
163
- .header-title {
164
- font-size: 2.2em;
165
- font-weight: 700;
166
- color: #2c3e50;
167
  }
168
- .header-subtitle {
169
- font-size: 1.1em;
170
- color: #576574;
171
  }
172
  </style>
173
  """
174
 
175
 
176
  # --- UI ---
177
- with gr.Blocks(
178
- title="Kurukh AI Translator", theme=gr.themes.Soft()
179
- ) as demo:
180
 
181
  gr.HTML(universal_css)
182
 
183
- gr.HTML(
184
- """
185
  <div class="header-div">
186
  <h1 class="header-title">๐Ÿ‡ฎ๐Ÿ‡ณ AI Kurukh (Oraon) Translator</h1>
187
  <p class="header-subtitle">
188
  Bridging Communities with Artificial Intelligence | Voice & Hinglish Supported
189
  </p>
190
  </div>
191
- """
192
- )
193
 
194
  with gr.Tabs():
195
 
196
- # ---------------- TRANSLATOR TAB ----------------
197
- with gr.Tab("๐Ÿ—ฃ๏ธ Translator"):
198
 
199
- with gr.Accordion(
200
- "โ„น๏ธ How to use (Click to expand)", open=False
201
- ):
202
- gr.Markdown(
203
- """
204
- 1. **Select Mode:** Kurukh โ†’ Hindi OR Hindi โ†’ Kurukh
205
- 2. **Hinglish:** Enable if you type Hindi in English
206
- 3. **Voice:** Speak using the microphone (Hindi only)
207
- """
208
- )
209
 
210
  with gr.Row():
211
 
 
212
  with gr.Column():
213
  direction = gr.Radio(
214
  ["Kurukh -> Hindi", "Hindi -> Kurukh"],
@@ -217,55 +184,42 @@ with gr.Blocks(
217
  )
218
 
219
  is_hinglish = gr.Checkbox(
220
- label="๐Ÿ”ค Hinglish Typing", value=False
221
  )
222
 
223
  input_text = gr.Textbox(
224
- label="Enter Text",
225
- placeholder="Type sentences here...",
226
- lines=4,
227
  )
228
 
229
  input_audio = gr.Audio(
230
- label="๐ŸŽ™๏ธ Voice Input (Hindi Only)",
231
  sources=["microphone"],
232
  type="filepath",
 
233
  )
234
 
235
- translate_btn = gr.Button(
236
- "Translate ๐Ÿš€", variant="primary"
237
- )
238
 
 
239
  with gr.Column():
240
  output_text = gr.Textbox(
241
  label="Translation",
242
  lines=4,
243
  interactive=False,
 
244
  )
245
-
246
  output_audio = gr.Audio(
247
- label="๐Ÿ”Š Listen (Hindi Only)",
248
- interactive=False,
249
- autoplay=False,
250
  )
251
 
252
  translate_btn.click(
253
  fn=process_translation,
254
- inputs=[
255
- input_text,
256
- input_audio,
257
- direction,
258
- is_hinglish,
259
- ],
260
  outputs=[input_text, output_text, output_audio],
261
  )
262
 
263
- # ---------------- FEEDBACK TAB ----------------
264
- with gr.Tab("๐Ÿ“ Improve the AI"):
265
-
266
- gr.Markdown(
267
- "### ๐Ÿ› ๏ธ Help us improve accuracy by submitting corrections."
268
- )
269
 
270
  fb_direction = gr.Radio(
271
  ["Kurukh -> Hindi", "Hindi -> Kurukh"],
@@ -274,34 +228,23 @@ with gr.Blocks(
274
  )
275
 
276
  fb_original = gr.Textbox(label="Original Text")
277
- fb_ai_output = gr.Textbox(
278
- label="AI's Wrong Translation"
279
- )
280
  fb_user_correct = gr.Textbox(
281
- label="โœ… Your Correct Translation",
282
- lines=2,
283
- )
284
-
285
- submit_btn = gr.Button(
286
- "Submit Correction to Database", variant="secondary"
287
  )
288
 
 
289
  status_lbl = gr.Label(label="Status")
290
 
291
  submit_btn.click(
292
  fn=save_to_sheet,
293
- inputs=[
294
- fb_original,
295
- fb_ai_output,
296
- fb_user_correct,
297
- fb_direction,
298
- ],
299
  outputs=status_lbl,
300
  )
301
 
302
  gr.Markdown("---")
303
  gr.HTML(
304
- "<center style='color:#888'>Built for the Kurukh Community โ€ข Powered by Google mT5 & Whisper</center>"
305
  )
306
 
307
  demo.launch()
 
9
  import tempfile
10
  import requests
11
 
 
12
  # --- CONFIGURATION ---
13
  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
 
 
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):
33
  try:
34
+ url = "https://inputtools.google.com/request?text={}&itc=hi-t-i0-und&num=1"
 
 
35
  response = requests.get(url.format(text))
36
  result = response.json()
37
  return result[1][0][1][0]
38
  except:
39
  return text
40
 
 
41
  def save_to_sheet(original, translation, correction, direction):
42
  try:
43
  json_creds = os.getenv("GOOGLE_CREDENTIALS")
 
49
  "https://spreadsheets.google.com/feeds",
50
  "https://www.googleapis.com/auth/drive",
51
  ]
52
+ creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
 
 
53
  client = gspread.authorize(creds)
54
+
55
  sheet = client.open(SHEET_NAME).sheet1
56
 
57
  if not sheet.get_all_values():
 
66
  )
67
 
68
  sheet.append_row(
69
+ [str(datetime.now()), direction, original, translation, correction]
 
 
 
 
 
 
70
  )
71
+
72
  return "โœ… Saved to Google Sheets."
73
  except Exception as e:
74
  return f"โŒ Error: {str(e)}"
75
 
 
76
  def speech_to_text(audio_path):
77
  if audio_path is None:
78
  return ""
79
  return asr_pipeline(audio_path)["text"]
80
 
 
81
  def text_to_speech(text, language="hi"):
82
  if not text:
83
  return None
84
  try:
85
  tts = gTTS(text=text, lang=language)
86
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
 
 
87
  tts.save(temp_file.name)
88
  return temp_file.name
89
  except:
90
  return None
91
 
 
92
  # --- MAIN TRANSLATION LOGIC ---
93
  def process_translation(text, audio_input, direction, is_hinglish):
94
+ original_text = speech_to_text(audio_input) if audio_input else text
 
 
 
95
  if not original_text:
96
  return "", "", None
97
 
98
  if direction == "Hindi -> Kurukh" and is_hinglish:
99
  original_text = transliterate_to_hindi(original_text)
100
 
101
+ target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
 
 
 
 
102
 
103
  try:
104
  results = target_pipeline(
 
120
  return original_text, translated_text, audio_output
121
 
122
 
123
+ # --- CSS ---
124
  universal_css = """
125
  <style>
126
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
127
+ body, button, input, select, textarea, .gradio-container {
128
+ font-family: 'Poppins', sans-serif !important;
 
129
  }
130
+ .header-div {
131
+ text-align: center;
132
+ margin-bottom: 25px;
 
133
  padding: 20px;
134
  background: linear-gradient(to right, #f8f9fa, #e9ecef);
135
  border-radius: 15px;
136
  }
137
+ .header-title {
138
+ font-size: 2.2em;
139
+ font-weight: 700;
140
+ color: #2c3e50;
141
  }
142
+ .header-subtitle {
143
+ font-size: 1.1em;
144
+ color: #576574;
145
  }
146
  </style>
147
  """
148
 
149
 
150
  # --- UI ---
151
+ with gr.Blocks(title="Kurukh AI Translator") as demo:
 
 
152
 
153
  gr.HTML(universal_css)
154
 
155
+ gr.HTML("""
 
156
  <div class="header-div">
157
  <h1 class="header-title">๐Ÿ‡ฎ๐Ÿ‡ณ AI Kurukh (Oraon) Translator</h1>
158
  <p class="header-subtitle">
159
  Bridging Communities with Artificial Intelligence | Voice & Hinglish Supported
160
  </p>
161
  </div>
162
+ """)
 
163
 
164
  with gr.Tabs():
165
 
166
+ # --- Translator Tab ---
167
+ with gr.TabItem("๐Ÿ—ฃ๏ธ Translator"):
168
 
169
+ with gr.Accordion("โ„น๏ธ How to use (Click to expand)", open=False):
170
+ gr.Markdown("""
171
+ 1. Select translation mode.
172
+ 2. Enable Hinglish if typing Hindi in English letters.
173
+ 3. Use Voice input if needed.
174
+ """)
 
 
 
 
175
 
176
  with gr.Row():
177
 
178
+ # LEFT
179
  with gr.Column():
180
  direction = gr.Radio(
181
  ["Kurukh -> Hindi", "Hindi -> Kurukh"],
 
184
  )
185
 
186
  is_hinglish = gr.Checkbox(
187
+ label="๐Ÿ”ค Hinglish Typing (e.g., 'Tumhara')", value=False
188
  )
189
 
190
  input_text = gr.Textbox(
191
+ label="Enter Text", placeholder="Type sentences here...", lines=4
 
 
192
  )
193
 
194
  input_audio = gr.Audio(
 
195
  sources=["microphone"],
196
  type="filepath",
197
+ label="๐ŸŽ™๏ธ Voice Input (Hindi Only)",
198
  )
199
 
200
+ translate_btn = gr.Button("Translate ๐Ÿš€")
 
 
201
 
202
+ # RIGHT
203
  with gr.Column():
204
  output_text = gr.Textbox(
205
  label="Translation",
206
  lines=4,
207
  interactive=False,
208
+ show_copy_button=True,
209
  )
 
210
  output_audio = gr.Audio(
211
+ label="๐Ÿ”Š Listen (Hindi Only)", interactive=False
 
 
212
  )
213
 
214
  translate_btn.click(
215
  fn=process_translation,
216
+ inputs=[input_text, input_audio, direction, is_hinglish],
 
 
 
 
 
217
  outputs=[input_text, output_text, output_audio],
218
  )
219
 
220
+ # --- Feedback Tab ---
221
+ with gr.TabItem("๐Ÿ“ Improve the AI"):
222
+ gr.Markdown("### ๐Ÿ› ๏ธ Help us improve accuracy")
 
 
 
223
 
224
  fb_direction = gr.Radio(
225
  ["Kurukh -> Hindi", "Hindi -> Kurukh"],
 
228
  )
229
 
230
  fb_original = gr.Textbox(label="Original Text")
231
+ fb_ai_output = gr.Textbox(label="AI's Translation")
 
 
232
  fb_user_correct = gr.Textbox(
233
+ label="Correct Translation", lines=2
 
 
 
 
 
234
  )
235
 
236
+ submit_btn = gr.Button("Submit Correction")
237
  status_lbl = gr.Label(label="Status")
238
 
239
  submit_btn.click(
240
  fn=save_to_sheet,
241
+ inputs=[fb_original, fb_ai_output, fb_user_correct, fb_direction],
 
 
 
 
 
242
  outputs=status_lbl,
243
  )
244
 
245
  gr.Markdown("---")
246
  gr.HTML(
247
+ "<center style='color:#777;'>Built with โค๏ธ for the Kurukh Community</center>"
248
  )
249
 
250
  demo.launch()