Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,16 +9,14 @@ from gtts import gTTS
|
|
| 9 |
import tempfile
|
| 10 |
import requests
|
| 11 |
|
| 12 |
-
import sys
|
| 13 |
-
print("Python:", sys.version)
|
| 14 |
-
print("Gradio version:", gr.__version__)
|
| 15 |
-
|
| 16 |
# --- CONFIGURATION ---
|
| 17 |
MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
|
| 18 |
MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
|
| 19 |
SHEET_NAME = "Kurukh_Feedback_Log"
|
| 20 |
|
| 21 |
# --- LOAD MODELS ---
|
|
|
|
|
|
|
| 22 |
print("Loading Translation Models...")
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
|
| 24 |
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
|
@@ -28,7 +26,6 @@ pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer
|
|
| 28 |
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 29 |
|
| 30 |
print("Loading Voice Model...")
|
| 31 |
-
|
| 32 |
asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 33 |
|
| 34 |
# --- HELPER FUNCTIONS ---
|
|
@@ -59,7 +56,6 @@ def save_to_sheet(original, translation, correction, direction):
|
|
| 59 |
|
| 60 |
def speech_to_text(audio_path):
|
| 61 |
if audio_path is None: return ""
|
| 62 |
-
# Using the Transformers pipeline
|
| 63 |
return asr_pipeline(audio_path)["text"]
|
| 64 |
|
| 65 |
def text_to_speech(text, language="hi"):
|
|
@@ -88,8 +84,8 @@ def process_translation(text, audio_input, direction, is_hinglish):
|
|
| 88 |
original_text,
|
| 89 |
max_length=128,
|
| 90 |
num_beams=5,
|
| 91 |
-
no_repeat_ngram_size=2,
|
| 92 |
-
repetition_penalty=2.0,
|
| 93 |
early_stopping=True
|
| 94 |
)
|
| 95 |
translated_text = results[0]['generated_text']
|
|
@@ -101,27 +97,49 @@ def process_translation(text, audio_input, direction, is_hinglish):
|
|
| 101 |
if direction == "Kurukh -> Hindi":
|
| 102 |
audio_output = text_to_speech(translated_text, "hi")
|
| 103 |
|
| 104 |
-
|
| 105 |
return original_text, translated_text, audio_output
|
| 106 |
|
| 107 |
-
# ---
|
| 108 |
-
|
|
|
|
| 109 |
<style>
|
| 110 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
</style>
|
| 116 |
"""
|
| 117 |
|
| 118 |
# --- THE UI ---
|
| 119 |
-
|
|
|
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
gr.HTML(
|
| 123 |
|
| 124 |
-
#
|
| 125 |
gr.HTML("""
|
| 126 |
<div class="header-div">
|
| 127 |
<h1 class="header-title">๐ฎ๐ณ AI Kurukh (Oraon) Translator</h1>
|
|
@@ -153,7 +171,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Kurukh AI Translator") as demo:
|
|
| 153 |
|
| 154 |
input_text = gr.Textbox(label="Enter Text", placeholder="Type sentences here...", lines=4)
|
| 155 |
|
| 156 |
-
|
| 157 |
input_audio = gr.Audio(sources=["microphone"], type="filepath", label="๐๏ธ Voice Input (Hindi Only)")
|
| 158 |
|
| 159 |
translate_btn = gr.Button("Translate ๐", variant="primary")
|
|
@@ -161,7 +179,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Kurukh AI Translator") as demo:
|
|
| 161 |
# RIGHT COLUMN
|
| 162 |
with gr.Column():
|
| 163 |
gr.Markdown("### ๐ค Translation Result")
|
| 164 |
-
# show_copy_button works in Gradio 4.0+
|
| 165 |
output_text = gr.Textbox(label="Translation", lines=4, interactive=False, show_copy_button=True)
|
| 166 |
output_audio = gr.Audio(label="๐ Listen (Hindi Only)", interactive=False, autoplay=False)
|
| 167 |
|
|
|
|
| 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 |
+
|
| 20 |
print("Loading Translation Models...")
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
|
| 22 |
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
|
|
|
| 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 ---
|
|
|
|
| 56 |
|
| 57 |
def speech_to_text(audio_path):
|
| 58 |
if audio_path is None: return ""
|
|
|
|
| 59 |
return asr_pipeline(audio_path)["text"]
|
| 60 |
|
| 61 |
def text_to_speech(text, language="hi"):
|
|
|
|
| 84 |
original_text,
|
| 85 |
max_length=128,
|
| 86 |
num_beams=5,
|
| 87 |
+
no_repeat_ngram_size=2,
|
| 88 |
+
repetition_penalty=2.0,
|
| 89 |
early_stopping=True
|
| 90 |
)
|
| 91 |
translated_text = results[0]['generated_text']
|
|
|
|
| 97 |
if direction == "Kurukh -> Hindi":
|
| 98 |
audio_output = text_to_speech(translated_text, "hi")
|
| 99 |
|
|
|
|
| 100 |
return original_text, translated_text, audio_output
|
| 101 |
|
| 102 |
+
# --- CSS INJECTION ---
|
| 103 |
+
|
| 104 |
+
universal_css = """
|
| 105 |
<style>
|
| 106 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
|
| 107 |
+
|
| 108 |
+
/* Force Font Family */
|
| 109 |
+
body, button, input, select, textarea, .gradio-container {
|
| 110 |
+
font-family: 'Poppins', sans-serif !important;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/* Header Styling */
|
| 114 |
+
.header-div {
|
| 115 |
+
text-align: center;
|
| 116 |
+
margin-bottom: 25px;
|
| 117 |
+
padding: 20px;
|
| 118 |
+
background: linear-gradient(to right, #f8f9fa, #e9ecef);
|
| 119 |
+
border-radius: 15px;
|
| 120 |
+
}
|
| 121 |
+
.header-title {
|
| 122 |
+
font-size: 2.2em;
|
| 123 |
+
font-weight: 700;
|
| 124 |
+
color: #2c3e50;
|
| 125 |
+
margin: 0;
|
| 126 |
+
}
|
| 127 |
+
.header-subtitle {
|
| 128 |
+
font-size: 1.1em;
|
| 129 |
+
color: #576574;
|
| 130 |
+
margin-top: 8px;
|
| 131 |
+
}
|
| 132 |
</style>
|
| 133 |
"""
|
| 134 |
|
| 135 |
# --- THE UI ---
|
| 136 |
+
|
| 137 |
+
with gr.Blocks(title="Kurukh AI Translator") as demo:
|
| 138 |
|
| 139 |
+
# 1. Inject the CSS
|
| 140 |
+
gr.HTML(universal_css)
|
| 141 |
|
| 142 |
+
# 2. Header
|
| 143 |
gr.HTML("""
|
| 144 |
<div class="header-div">
|
| 145 |
<h1 class="header-title">๐ฎ๐ณ AI Kurukh (Oraon) Translator</h1>
|
|
|
|
| 171 |
|
| 172 |
input_text = gr.Textbox(label="Enter Text", placeholder="Type sentences here...", lines=4)
|
| 173 |
|
| 174 |
+
|
| 175 |
input_audio = gr.Audio(sources=["microphone"], type="filepath", label="๐๏ธ Voice Input (Hindi Only)")
|
| 176 |
|
| 177 |
translate_btn = gr.Button("Translate ๐", variant="primary")
|
|
|
|
| 179 |
# RIGHT COLUMN
|
| 180 |
with gr.Column():
|
| 181 |
gr.Markdown("### ๐ค Translation Result")
|
|
|
|
| 182 |
output_text = gr.Textbox(label="Translation", lines=4, interactive=False, show_copy_button=True)
|
| 183 |
output_audio = gr.Audio(label="๐ Listen (Hindi Only)", interactive=False, autoplay=False)
|
| 184 |
|