Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import azure.cognitiveservices.speech as speechsdk
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
# Azure credentials
|
| 6 |
+
SPEECH_KEY = "your_speech_key"
|
| 7 |
+
SERVICE_REGION = "your_service_region"
|
| 8 |
+
|
| 9 |
+
# Define the language and dialect mapping
|
| 10 |
+
language_dialects = {
|
| 11 |
+
"Arabic": {
|
| 12 |
+
"Egypt": "ar-EG",
|
| 13 |
+
"Saudi Arabia": "ar-SA",
|
| 14 |
+
"United Arab Emirates": "ar-AE",
|
| 15 |
+
"Bahrain": "ar-BH",
|
| 16 |
+
"Algeria": "ar-DZ",
|
| 17 |
+
"Iraq": "ar-IQ",
|
| 18 |
+
"Jordan": "ar-JO",
|
| 19 |
+
"Kuwait": "ar-KW",
|
| 20 |
+
"Lebanon": "ar-LB",
|
| 21 |
+
"Libya": "ar-LY",
|
| 22 |
+
"Morocco": "ar-MA",
|
| 23 |
+
"Oman": "ar-OM",
|
| 24 |
+
"Palestinian Authority": "ar-PS",
|
| 25 |
+
"Qatar": "ar-QA",
|
| 26 |
+
"Syria": "ar-SY",
|
| 27 |
+
"Tunisia": "ar-TN",
|
| 28 |
+
"Yemen": "ar-YE"
|
| 29 |
+
},
|
| 30 |
+
"English": {
|
| 31 |
+
"United States": "en-US",
|
| 32 |
+
"United Kingdom": "en-GB",
|
| 33 |
+
"Australia": "en-AU",
|
| 34 |
+
"Canada": "en-CA",
|
| 35 |
+
"India": "en-IN",
|
| 36 |
+
"Ireland": "en-IE",
|
| 37 |
+
"New Zealand": "en-NZ",
|
| 38 |
+
"South Africa": "en-ZA",
|
| 39 |
+
"Singapore": "en-SG",
|
| 40 |
+
"Philippines": "en-PH"
|
| 41 |
+
},
|
| 42 |
+
"French": {
|
| 43 |
+
"France": "fr-FR",
|
| 44 |
+
"Canada": "fr-CA",
|
| 45 |
+
"Switzerland": "fr-CH"
|
| 46 |
+
},
|
| 47 |
+
"Spanish": {
|
| 48 |
+
"Spain": "es-ES",
|
| 49 |
+
"Mexico": "es-MX",
|
| 50 |
+
"Argentina": "es-AR",
|
| 51 |
+
"Colombia": "es-CO",
|
| 52 |
+
"Chile": "es-CL",
|
| 53 |
+
"Peru": "es-PE",
|
| 54 |
+
"Venezuela": "es-VE"
|
| 55 |
+
},
|
| 56 |
+
"German": {
|
| 57 |
+
"Germany": "de-DE",
|
| 58 |
+
"Austria": "de-AT",
|
| 59 |
+
"Switzerland": "de-CH"
|
| 60 |
+
},
|
| 61 |
+
"Portuguese": {
|
| 62 |
+
"Portugal": "pt-PT",
|
| 63 |
+
"Brazil": "pt-BR"
|
| 64 |
+
},
|
| 65 |
+
"Chinese": {
|
| 66 |
+
"Mainland China": "zh-CN",
|
| 67 |
+
"Hong Kong": "zh-HK",
|
| 68 |
+
"Taiwan": "zh-TW"
|
| 69 |
+
},
|
| 70 |
+
"Italian": {
|
| 71 |
+
"Italy": "it-IT"
|
| 72 |
+
},
|
| 73 |
+
"Japanese": {
|
| 74 |
+
"Japan": "ja-JP"
|
| 75 |
+
},
|
| 76 |
+
"Korean": {
|
| 77 |
+
"Korea": "ko-KR"
|
| 78 |
+
}
|
| 79 |
+
# Add more languages and dialects as needed
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# Function to get dialects based on selected language
|
| 84 |
+
def get_dialects(language):
|
| 85 |
+
dialects = list(language_dialects.get(language, {}).keys())
|
| 86 |
+
return gr.update(choices=dialects, value=dialects[0] if dialects else None)
|
| 87 |
+
|
| 88 |
+
# Function to transcribe audio
|
| 89 |
+
def transcribe_audio(audio_file, duration, language, dialect):
|
| 90 |
+
# Simulate recording duration
|
| 91 |
+
print(f"Recording for {duration} seconds...")
|
| 92 |
+
time.sleep(duration)
|
| 93 |
+
|
| 94 |
+
# Get the locale code
|
| 95 |
+
locale_code = language_dialects.get(language, {}).get(dialect, "en-US")
|
| 96 |
+
print(f"Selected Locale Code: {locale_code}")
|
| 97 |
+
|
| 98 |
+
# Set up speech recognition
|
| 99 |
+
speech_config = speechsdk.SpeechConfig(subscription=SPEECH_KEY, region=SERVICE_REGION)
|
| 100 |
+
speech_config.speech_recognition_language = locale_code
|
| 101 |
+
print(locale_code)
|
| 102 |
+
audio_input = speechsdk.audio.AudioConfig(filename=audio_file)
|
| 103 |
+
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input)
|
| 104 |
+
|
| 105 |
+
result = speech_recognizer.recognize_once()
|
| 106 |
+
|
| 107 |
+
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
|
| 108 |
+
return result.text
|
| 109 |
+
elif result.reason == speechsdk.ResultReason.NoMatch:
|
| 110 |
+
return "No speech could be recognized"
|
| 111 |
+
elif result.reason == speechsdk.ResultReason.Canceled:
|
| 112 |
+
cancellation_details = result.cancellation_details
|
| 113 |
+
return f"Speech recognition canceled: {cancellation_details.error_details}"
|
| 114 |
+
else:
|
| 115 |
+
return "Unknown error occurred during speech recognition"
|
| 116 |
+
|
| 117 |
+
# Create the Gradio interface
|
| 118 |
+
with gr.Blocks() as demo:
|
| 119 |
+
gr.Markdown("## Azure Speech to Text with Language and Dialect Selection")
|
| 120 |
+
|
| 121 |
+
with gr.Row():
|
| 122 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
| 123 |
+
duration_input = gr.Dropdown(choices=[5, 10], label="Recording Duration", value=5)
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
language_input = gr.Dropdown(choices=list(language_dialects.keys()), label="Select Language")
|
| 127 |
+
dialect_input = gr.Dropdown(choices=[], label="Select Dialect")
|
| 128 |
+
|
| 129 |
+
transcribe_button = gr.Button("Transcribe")
|
| 130 |
+
output_text = gr.Textbox(label="Transcription Result")
|
| 131 |
+
|
| 132 |
+
# Update dialect options based on selected language
|
| 133 |
+
language_input.change(fn=get_dialects, inputs=language_input, outputs=dialect_input)
|
| 134 |
+
|
| 135 |
+
# Transcribe audio on button click
|
| 136 |
+
transcribe_button.click(fn=transcribe_audio, inputs=[audio_input, duration_input, language_input, dialect_input], outputs=output_text)
|
| 137 |
+
|
| 138 |
+
# Launch the app
|
| 139 |
+
demo.launch()
|