Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,14 @@ 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
|
| 17 |
print("Loading Translation Models...")
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
|
| 19 |
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
|
@@ -22,12 +23,22 @@ model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
|
|
| 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 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def save_to_sheet(original, translation, correction, direction):
|
| 32 |
try:
|
| 33 |
json_creds = os.getenv("GOOGLE_CREDENTIALS")
|
|
@@ -44,41 +55,32 @@ def save_to_sheet(original, translation, correction, direction):
|
|
| 44 |
except Exception as e:
|
| 45 |
return f"โ Error: {str(e)}"
|
| 46 |
|
| 47 |
-
# --- CORE FUNCTIONS ---
|
| 48 |
-
|
| 49 |
def speech_to_text(audio_path):
|
| 50 |
-
|
| 51 |
-
|
| 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 |
-
|
| 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 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
original_text = text
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
|
| 83 |
try:
|
| 84 |
results = target_pipeline(original_text, max_length=128)
|
|
@@ -86,43 +88,41 @@ def process_translation(text, audio_input, direction):
|
|
| 86 |
except Exception as e:
|
| 87 |
return str(e), "", None
|
| 88 |
|
| 89 |
-
#
|
| 90 |
audio_output = None
|
| 91 |
-
|
| 92 |
if direction == "Kurukh -> Hindi":
|
| 93 |
audio_output = text_to_speech(translated_text, "hi")
|
| 94 |
|
| 95 |
-
# Return: (Input
|
| 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
|
| 102 |
|
| 103 |
with gr.Tabs():
|
| 104 |
-
with gr.TabItem("๐ฃ๏ธ
|
| 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="
|
| 113 |
-
input_audio = gr.Audio(sources=["microphone"], type="filepath", label="
|
| 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
|
| 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]
|
| 126 |
)
|
| 127 |
|
| 128 |
with gr.TabItem("๐ Improve the AI"):
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
from gtts import gTTS
|
| 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("Loading Translation Models...")
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
|
| 20 |
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
|
|
|
| 23 |
pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
|
| 24 |
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 25 |
|
|
|
|
| 26 |
print("Loading Voice Model...")
|
|
|
|
| 27 |
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 28 |
|
| 29 |
+
# --- HELPER FUNCTIONS ---
|
| 30 |
+
|
| 31 |
+
def transliterate_to_hindi(text):
|
| 32 |
+
|
| 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 |
+
# The API returns a nested list; we grab the first suggestion
|
| 38 |
+
return result[1][0][1][0]
|
| 39 |
+
except:
|
| 40 |
+
return text # If it fails (no internet), return original text
|
| 41 |
+
|
| 42 |
def save_to_sheet(original, translation, correction, direction):
|
| 43 |
try:
|
| 44 |
json_creds = os.getenv("GOOGLE_CREDENTIALS")
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
return f"โ Error: {str(e)}"
|
| 57 |
|
|
|
|
|
|
|
| 58 |
def speech_to_text(audio_path):
|
| 59 |
+
if audio_path is None: return ""
|
| 60 |
+
return asr_pipeline(audio_path)["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def text_to_speech(text, language="hi"):
|
| 63 |
+
if not text: return None
|
|
|
|
|
|
|
| 64 |
try:
|
|
|
|
| 65 |
tts = gTTS(text=text, lang=language)
|
| 66 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 67 |
tts.save(temp_file.name)
|
| 68 |
return temp_file.name
|
| 69 |
+
except: return None
|
|
|
|
| 70 |
|
| 71 |
+
# --- MAIN TRANSLATION LOGIC ---
|
| 72 |
+
def process_translation(text, audio_input, direction, is_hinglish):
|
| 73 |
+
|
| 74 |
+
# 1. Get Text from Voice or Typing
|
| 75 |
+
original_text = speech_to_text(audio_input) if audio_input else text
|
| 76 |
+
if not original_text: return "", "", None
|
|
|
|
| 77 |
|
| 78 |
+
# 2. Handle Hinglish (NEW FEATURE)
|
| 79 |
+
# Only applies if translating TO Kurukh (User typing Hindi in English letters)
|
| 80 |
+
if direction == "Hindi -> Kurukh" and is_hinglish:
|
| 81 |
+
original_text = transliterate_to_hindi(original_text)
|
| 82 |
|
| 83 |
+
# 3. Translate
|
| 84 |
target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
|
| 85 |
try:
|
| 86 |
results = target_pipeline(original_text, max_length=128)
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
return str(e), "", None
|
| 90 |
|
| 91 |
+
# 4. Audio Output (For Hindi)
|
| 92 |
audio_output = None
|
|
|
|
| 93 |
if direction == "Kurukh -> Hindi":
|
| 94 |
audio_output = text_to_speech(translated_text, "hi")
|
| 95 |
|
| 96 |
+
# Return: (Updated Input Box), (Translation), (Audio)
|
| 97 |
return original_text, translated_text, audio_output
|
| 98 |
|
| 99 |
# --- THE UI ---
|
| 100 |
with gr.Blocks() as demo:
|
| 101 |
gr.Markdown("# ๐ฎ๐ณ AI Kurukh (Kurux) Translator")
|
| 102 |
+
gr.Markdown("### Voice & Hinglish Supported")
|
| 103 |
|
| 104 |
with gr.Tabs():
|
| 105 |
+
with gr.TabItem("๐ฃ๏ธ Translator"):
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Mode", value="Kurukh -> Hindi")
|
| 109 |
+
# NEW CHECKBOX
|
| 110 |
+
is_hinglish = gr.Checkbox(label="I am typing Hindi in English (e.g. 'Tumhara')", value=False)
|
| 111 |
|
|
|
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column():
|
| 114 |
+
input_text = gr.Textbox(label="Input", placeholder="Type here...", lines=3)
|
| 115 |
+
input_audio = gr.Audio(sources=["microphone"], type="filepath", label="Voice Input (Hindi)")
|
| 116 |
translate_btn = gr.Button("Translate ๐", variant="primary")
|
| 117 |
|
|
|
|
| 118 |
with gr.Column():
|
| 119 |
output_text = gr.Textbox(label="Translation", lines=3, interactive=False)
|
| 120 |
+
output_audio = gr.Audio(label="Listen (Hindi Only)", interactive=False)
|
| 121 |
|
|
|
|
| 122 |
translate_btn.click(
|
| 123 |
fn=process_translation,
|
| 124 |
+
inputs=[input_text, input_audio, direction, is_hinglish],
|
| 125 |
+
outputs=[input_text, output_text, output_audio]
|
| 126 |
)
|
| 127 |
|
| 128 |
with gr.TabItem("๐ Improve the AI"):
|