| import gradio as gr |
| from openai import OpenAI |
| import re |
| from konlpy.tag import Kkma |
| from TextUtil.digit2text import digit2txt, NNGdigit2txt, CSign2txt |
|
|
| def process_txt(text): |
| kkma = Kkma() |
|
|
| result = "" |
| pattern = re.compile(r'([κ°-ν£]+)|([a-zA-Z.]+)|(\d[\d,.]*)|(\$|β¬|Β£|Β₯|οΏ¦)|(\s+)') |
|
|
| matches = pattern.finditer(text) |
| for match in matches: |
| if match.group(1): |
| result += match.group(1) |
| elif match.group(2): |
| result += match.group(2) |
| elif match.group(3): |
| end_index = match.end(3) |
| |
| next_word = kkma.pos(text[end_index:])[0] |
| if next_word[1] == "NNG" and next_word[0] not in ['λ¬λ¬', 'μ λ£', 'νμ΄λ', 'μ', 'μ']: |
| result += NNGdigit2txt(match.group(3).replace(',', '')) |
| else: |
| result += digit2txt(match.group(3).replace(',', '')) |
| elif match.group(4): |
| result += CSign2txt(match.group(4)) |
| elif match.group(5): |
| result += match.group(5) |
| return result |
| |
|
|
| def generate_audio(api_key, file, model, voice): |
| |
| client = OpenAI(api_key=api_key) |
|
|
| |
| text = file.decode("utf-8") |
| |
| |
| text = process_txt(text) |
| print(text) |
|
|
| |
| response = client.audio.speech.create( |
| model=model, |
| voice=voice, |
| input=text |
| ) |
| |
| |
| f_name = "generated_audio" |
| speech_file_path = f"{f_name}.mp3" |
| response.stream_to_file(speech_file_path) |
| |
| return speech_file_path |
|
|
| |
| iface = gr.Interface( |
| fn=generate_audio, |
| inputs=[ |
| gr.Text(label="Enter OpenAI API Key"), |
| gr.File(label="Upload Text File", type="binary"), |
| gr.Radio(choices=["tts-1", "tts-1-hd"], label="Model"), |
| gr.Radio(choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Voice") |
| ], |
| outputs=gr.File(label="Download MP3 File"), |
| title="Text-to-Speech Converter (Korean Digit2Text)", |
| description="Upload a text file and enter your OpenAI API key to convert it into speech using OpenAI's Text-to-Speech models.<br>*ν΄λΉ μλΉμ€λ νκ΅μ΄μ λ§μΆ€νλμ΄ μμ΅λλ€. <br>*νκ΅μ΄ μ«μ λ°μ λ³νμ ν΅ν΄ λ μ νν μ«μ TTSλ₯Ό κ°λ₯νκ² ν©λλ€.<br>*μμ: 50,000$ -> μ€λ§λ¬λ¬, 5κ°μ§ -> λ€μ―κ°μ§, 99κΆ -> μνμνκΆ" |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch() |