Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline, MBartForConditionalGeneration, MBart50TokenizerFast
|
| 3 |
+
|
| 4 |
+
# Load ASR model
|
| 5 |
+
asr = pipeline("automatic-speech-recognition", model="Subu19/whisper-small-nepali")
|
| 6 |
+
|
| 7 |
+
# Load translation model
|
| 8 |
+
model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
|
| 9 |
+
tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-many-to-many-mmt")
|
| 10 |
+
|
| 11 |
+
def translate_nepali_to_english(text):
|
| 12 |
+
tokenizer.src_lang = "ne_NP"
|
| 13 |
+
encoded = tokenizer(text, return_tensors="pt")
|
| 14 |
+
generated = model.generate(**encoded, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
|
| 15 |
+
return tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
|
| 16 |
+
|
| 17 |
+
def translate_english_to_nepali(text):
|
| 18 |
+
tokenizer.src_lang = "en_XX"
|
| 19 |
+
encoded = tokenizer(text, return_tensors="pt")
|
| 20 |
+
generated = model.generate(**encoded, forced_bos_token_id=tokenizer.lang_code_to_id["ne_NP"])
|
| 21 |
+
return tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
|
| 22 |
+
|
| 23 |
+
# Load summarizer
|
| 24 |
+
summarizer = pipeline("summarization")
|
| 25 |
+
|
| 26 |
+
def summarize_text(text):
|
| 27 |
+
word_count = len(text.split())
|
| 28 |
+
if word_count < 25:
|
| 29 |
+
return text
|
| 30 |
+
summary = summarizer(text, max_length=word_count, min_length=int(word_count * 0.4), do_sample=False)
|
| 31 |
+
return summary[0]['summary_text']
|
| 32 |
+
|
| 33 |
+
def pipeline_fn(audio):
|
| 34 |
+
result = asr(audio)["text"]
|
| 35 |
+
english = translate_nepali_to_english(result)
|
| 36 |
+
summary = summarize_text(english)
|
| 37 |
+
nepali_summary = translate_english_to_nepali(summary)
|
| 38 |
+
return result, english, summary, nepali_summary
|
| 39 |
+
|
| 40 |
+
gr.Interface(
|
| 41 |
+
fn=pipeline_fn,
|
| 42 |
+
inputs=gr.Audio(source="microphone", type="filepath", label="π€ Speak Nepali"),
|
| 43 |
+
outputs=[
|
| 44 |
+
gr.Textbox(label="π£οΈ Transcribed Nepali"),
|
| 45 |
+
gr.Textbox(label="π Translated English"),
|
| 46 |
+
gr.Textbox(label="π English Summary"),
|
| 47 |
+
gr.Textbox(label="π Summarized Nepali"),
|
| 48 |
+
],
|
| 49 |
+
title="Nepali Voice Summarizer",
|
| 50 |
+
description="Speak Nepali β Get English & Nepali Summary"
|
| 51 |
+
).launch()
|