File size: 10,977 Bytes
9ebf5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f24ece6
9ebf5e3
f24ece6
 
9ebf5e3
f24ece6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ebf5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
019296d
9ebf5e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import gradio as gr
from google import genai
import nemo.collections.asr as nemo_asr
from pydub import AudioSegment
import os
from huggingface_hub import login
from hazm import Normalizer
import numpy as np
import re

# Fetch API Keys from environment variables
HF_TOKEN = os.getenv("HF_TOKEN")
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")

if not HF_TOKEN:
    raise ValueError("HF_TOKEN environment variable not set. Please provide a valid Hugging Face token.")

if not GEMINI_API_KEY:
    raise ValueError("GEMINI_API_KEY environment variable not set. Please provide a valid GEMINI_API_KEY.")

# Authenticate with Hugging Face
login(HF_TOKEN)

# Load the NeMo ASR Model
try:
    asr_model = nemo_asr.models.EncDecHybridRNNTCTCBPEModel.from_pretrained(
        model_name="faimlab/stt_fa_fastconformer_hybrid_large_dataset_v30"
    )
except Exception as e:
    raise RuntimeError(f"Failed to load model: {str(e)}")

# Persian text normalizer
normalizer = Normalizer()

# Function to load audio
def load_audio(audio_path):
    audio = AudioSegment.from_file(audio_path)
    audio = audio.set_channels(1).set_frame_rate(16000)
    audio_samples = np.array(audio.get_array_of_samples(), dtype=np.float32)
    audio_samples /= np.max(np.abs(audio_samples))
    return audio_samples, audio.frame_rate

# Transcribe audio chunk
def transcribe_chunk(audio_chunk, model):
    transcription = model.transcribe([audio_chunk], batch_size=1, verbose=False)
    return transcription[0].text

# Transcribe the full audio file
def transcribe_audio(file_path, model, chunk_size=30*16000):
    waveform, _ = load_audio(file_path)
    transcriptions = []
    for start in range(0, len(waveform), chunk_size):
        end = min(len(waveform), start + chunk_size)
        transcription = transcribe_chunk(waveform[start:end], model)
        transcriptions.append(transcription)

    transcriptions = ' '.join(transcriptions)
    transcriptions = re.sub(' +', ' ', transcriptions)
    transcriptions = normalizer.normalize(transcriptions)
    
    return transcriptions

# Main transcription function
def transcribe(audio):
    if audio is None:
        return "Please upload an audio file.", gr.update(interactive=False), gr.update(interactive=False)

    transcription = transcribe_audio(audio, asr_model)

    # Enable summarize & translate buttons once transcription is done
    return transcription

# List of 100 languages for summarization
languages = [
    "English", "Persian", "French", "Spanish", "German", "Italian", "Portuguese", "Dutch", "Swedish", "Danish",
    "Finnish", "Norwegian", "Russian", "Polish", "Turkish", "Arabic", "Hindi", "Chinese", "Japanese", "Korean",
    "Thai", "Vietnamese", "Indonesian", "Hebrew", "Greek", "Czech", "Hungarian", "Romanian", "Bulgarian", "Serbian",
    "Croatian", "Slovak", "Slovenian", "Ukrainian", "Lithuanian", "Latvian", "Estonian", "Macedonian", "Albanian",
    "Basque", "Catalan", "Maltese", "Icelandic", "Georgian", "Armenian", "Belarusian", "Yiddish", "Pashto", "Urdu",
    "Bengali", "Punjabi", "Tamil", "Telugu", "Malayalam", "Sinhala", "Burmese", "Lao", "Khmer", "Mongolian",
    "Nepali", "Marathi", "Gujarati", "Kannada", "Odia", "Assamese", "Maithili", "Kurdish", "Azerbaijani", "Kazakh",
    "Uzbek", "Turkmen", "Tajik", "Kyrgyz", "Uighur", "Tatar", "Haitian Creole", "Swahili", "Hausa", "Yoruba",
    "Zulu", "Xhosa", "Amharic", "Somali", "Tigrinya", "Shona", "Igbo", "Malagasy", "Quechua", "Aymara", "Guarani",
    "Sundanese", "Javanese", "Filipino", "Hmong", "Fijian", "Tongan", "Samoan", "Chamorro", "Hawaiian"
]
languages = sorted(languages)

# List of available AI models
model_selections = [
    'gemini-2.0-flash',
    'gemini-2.0-pro-exp-02-05',
    'gemini-2.0-flash-lite'
]

def translate(text, target_language, model_sel):
    client = genai.Client(api_key=GEMINI_API_KEY)
    prompt = f"Translate the following text into {target_language}. Only reply with the translation.\n'{text}'."
    
    response = client.models.generate_content(
        model=model_sel,
        contents=[prompt]
    )
    
    return response.text

# Function to summarize transcribed text
def summarize(transcript_text, word_count, model_sel, lang_sel):
    client = genai.Client(api_key=GEMINI_API_KEY)
    prompt = f"Summarize the following text in {word_count} words in {lang_sel}: '{transcript_text}'."
    
    response = client.models.generate_content(
        model=model_sel,
        contents=[prompt]
    )
    
    return response.text

def punctuate(transcript_text, model_sel):
    client = genai.Client(api_key=GEMINI_API_KEY)
    prompt = f"""
    Task: Punctuate the given Persian transcript text from an ASR model accurately according to Persian punctuation rules. Do not change any characters, correct mistakes, or modify the text in any way. You are ONLY allowed to add appropriate punctuations.

    Guidelines:
    1. Period (نقطه .)
    Ends a sentence.

    Used after abbreviations (e.g., آی.ب.ام.).

    2. Comma (ویرگول ,)
    Separates words/phrases in a list.

    Used in compound or conditional sentences.

    Prevents ambiguity (e.g., شلنگ مخصوص، آتش‌نشانی).

    Surrounds parenthetical phrases (e.g., استیو جابز، بنیان‌گذار اپل،…).

    Distinguishes adverbs (e.g., بعد از چندین ماه، ورزش کردم).

    Separates repeated words (e.g., آن کشور، کشور خوبی است).

    3. Semicolon (نقطه‌ویرگول ؛)
    Connects related sentences when a full stop is too strong.

    Separates clauses in complex lists (e.g., آلمان، ایتالیا و ژاپن؛ انگلیس، شوروی و آمریکا).

    Used before explanatory phrases (e.g., فتوسنتز مهم است؛ یعنی…).

    4. Colon (دونقطه :)
    Introduces explanations, lists, or direct quotes (e.g., او گفت: «من آماده‌ام.»).

    5. Ellipsis (سه‌نقطه …)
    Indicates omitted words (e.g., فرهنگی، سیاسی، اجتماعی و …).

    6. Parentheses (پرانتز ())
    Encloses extra information, dates, or clarifications (e.g., جنگ جهانی دوم (۱۹۴۵)).

    7. Quotation Marks (گیومه «»)
    Encloses direct speech (e.g., او گفت: «سلام!»).

    Highlights specific words (e.g., او را «نابغه» خواندند).

    8. Question Mark (علامت سؤال ؟)
    Ends direct questions (e.g., آیا آمدی؟).

    9. Exclamation Mark (علامت تعجب !)
    Expresses surprise, emphasis, or commands (e.g., چه پرنده زیبایی!).


    Instructions:
    Apply these punctuation rules without modifying the original text.

    Do not correct typos, spelling, or grammar mistakes.

    Only return the punctuated text as output—no explanations or additional comments.
    Input:
    {transcript_text}  
    """   
    response = client.models.generate_content(
        model=model_sel,
        contents=[prompt]
    )
    
    return response.text

# Gradio Interface
with gr.Blocks(theme="huggingface") as demo:
    gr.Markdown(
        """
        # 📝 Persian ASR, Translation & Summarization  
        
        Welcome to the **Persian Speech-to-Text & NLP** platform! This app allows you to upload an audio file,  
        get an accurate **transcription**, and enhance the output with **translation**, **summarization**,  
        and **punctuation restoration**.

        ## 🎯 How It Works  
        1️⃣ Upload an **audio file** containing Persian speech. To transcribe YouTube videos, open this [Colab Notebook](https://colab.research.google.com/github/saeedzou/NeMo-Gradio/blob/main/yt_transcribe_gradio.ipynb) (If your request was detected as bot, disconnect and run all cells again).  
        2️⃣ Click **"Transcribe"** to generate the text output.  
        3️⃣ Use additional features: **Translate**, **Summarize**, or **Restore Punctuation**.  
        4️⃣ Customize settings: Select a **language**, **AI model**, and **summary length**.  
        5️⃣ View and copy the processed text for your use!  

        """
    )

    with gr.Row():
        audio_input = gr.Audio(type="filepath", label="🎵 Upload Audio File")
        transcript_output = gr.Textbox(label="📝 Transcription", interactive=True)
        translation_output = gr.Textbox(label="🌍 Translation", interactive=False)
        summarized_output = gr.Textbox(label="📖 Summarized Text", interactive=False)

    transcribe_button = gr.Button("🎙️ Transcribe")
    translate_button = gr.Button("🌐 Translate", interactive=True)
    summarize_button = gr.Button("✂️ Summarize", interactive=True)
    punctuate_button = gr.Button("🔤 Restore Punctuation", interactive=True)

    with gr.Row():
        word_count_input = gr.Number(value=50, label="📏 Word Count for Summary")
        lang_selection = gr.Dropdown(choices=languages, value="English", label="🌎 Select Language")
        model_selection = gr.Dropdown(choices=model_selections, value="gemini-2.0-flash", label="🤖 Select AI Model")

    # Button Click Actions
    transcribe_button.click(
        transcribe, 
        inputs=audio_input, 
        outputs=transcript_output
    )

    translate_button.click(
        translate, 
        inputs=[transcript_output, lang_selection, model_selection], 
        outputs=translation_output
    )

    summarize_button.click(
        summarize, 
        inputs=[transcript_output, word_count_input, model_selection, lang_selection], 
        outputs=summarized_output
    )

    punctuate_button.click(  
        punctuate,  
        inputs=[transcript_output, model_selection],  
        outputs=transcript_output  
    )

    gr.Markdown(
        """
        \n\n
        ---

        Powered by NVIDIA’s **NeMo Fast Conformer**, this tool is optimized for high-quality **Persian ASR (Automatic Speech Recognition)**. 

        **📚 Trained on 800+ Hours of Speech Data:**  
        - Common Voice 17 (~300 hours)  
        - YouTube (~400 hours)  
        - NasleMana (~90 hours)  
        - In-house dataset (~70 hours) 

        ---

        ## 📜 License & Business Inquiries  

        This application is licensed under **Creative Commons Attribution-NonCommercial 4.0 (CC BY-NC 4.0)**.
        - **🛑 Non-Commercial Use Only** – Commercial use is not permitted without prior approval.  
        - **🔗 Attribution Required** – Credit must be given to FAIM Group, Sharif University of Technology.  
        - **❌ No Derivatives** – Modifications or adaptations of this work are not allowed.  

        📜 Full License Details: [CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/)  

        📩 **Business Inquiries:**  
        If you're interested in commercial applications, please contact us at:  
        ✉️ **Email:** [[email protected]](mailto:[email protected])  

        ---
        """
    )
# Launch Gradio app
demo.launch()