Fix examples and migrate UI to blocks
Browse files- .gitignore +1 -1
 - app.py +79 -42
 - requirements.txt +4 -2
 
    	
        .gitignore
    CHANGED
    
    | 
         @@ -1,2 +1,2 @@ 
     | 
|
| 1 | 
         
            -
            .venv
         
     | 
| 2 | 
         
             
            __pycache__
         
     | 
| 
         | 
| 
         | 
|
| 
         | 
|
| 1 | 
         
             
            __pycache__
         
     | 
| 2 | 
         
            +
            venv
         
     | 
    	
        app.py
    CHANGED
    
    | 
         @@ -1,32 +1,28 @@ 
     | 
|
| 1 | 
         
            -
            import tempfile
         
     | 
| 2 | 
         
             
            import gradio as gr
         
     | 
| 3 | 
         
             
            import os
         
     | 
| 
         | 
|
| 4 | 
         
             
            from espeak_phonemizer import Phonemizer
         
     | 
| 
         | 
|
| 5 | 
         | 
| 6 | 
         
            -
             
     | 
| 7 | 
         | 
| 8 | 
         
            -
             
     | 
| 9 | 
         | 
| 10 | 
         
            -
             
     | 
| 11 | 
         | 
| 12 | 
         
             
            def phonemiser(text, dialect):
         
     | 
| 13 | 
         
             
                dialects = {"Central": "ca", "Valencian": "ca-va", "North-West": "ca-nw"}
         
     | 
| 14 | 
         
             
                dialect = dialects[dialect] #Define dialect from espeak-ng-data/lang/roa/ca*
         
     | 
| 15 | 
         
             
                fonemitzador = Phonemizer(dialect) #Set correct dialect for the phonemiser
         
     | 
| 16 | 
         
            -
                if len(text) > MAX_TXT_LEN:
         
     | 
| 17 | 
         
            -
                    text = text[:MAX_TXT_LEN]
         
     | 
| 18 | 
         
            -
                    print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
         
     | 
| 19 | 
         
            -
                print(text)
         
     | 
| 20 | 
         | 
| 21 | 
         
             
                # synthesize
         
     | 
| 22 | 
         | 
| 23 | 
         
             
                fonemes = fonemitzador.phonemize(text, keep_clause_breakers=True)
         
     | 
| 24 | 
         | 
| 25 | 
         
            -
                global request_count
         
     | 
| 26 | 
         
            -
                request_count += 1
         
     | 
| 27 | 
         
            -
                print(f"Requests: {request_count}")
         
     | 
| 28 | 
         
             
                return fonemes
         
     | 
| 29 | 
         | 
| 
         | 
|
| 
         | 
|
| 30 | 
         
             
            description="""
         
     | 
| 31 | 
         
             
            Transcripció fonètica en català
         
     | 
| 32 | 
         
             
            Transcipció fonètica per a diferents dialectes del català mitjançant eSpeak.
         
     | 
| 
         @@ -34,34 +30,75 @@ Phonetic transcription for different dialects of Catalan 
     | 
|
| 34 | 
         
             
            using eSpeak.
         
     | 
| 35 | 
         
             
            repo: https://github.com/projecte-aina/espeak-ng/tree/dev-ca 
         
     | 
| 36 | 
         
             
            """
         
     | 
| 37 | 
         
            -
             
     | 
| 38 | 
         
            -
             
     | 
| 39 | 
         
            -
             
     | 
| 40 | 
         
            -
                 
     | 
| 41 | 
         
            -
             
     | 
| 42 | 
         
            -
             
     | 
| 43 | 
         
            -
             
     | 
| 44 | 
         
            -
             
     | 
| 45 | 
         
            -
             
     | 
| 46 | 
         
            -
             
     | 
| 47 | 
         
            -
             
     | 
| 48 | 
         
            -
             
     | 
| 49 | 
         
            -
             
     | 
| 50 | 
         
            -
                 
     | 
| 51 | 
         
            -
                    gr. 
     | 
| 52 | 
         
            -
             
     | 
| 53 | 
         
            -
             
     | 
| 54 | 
         
            -
             
     | 
| 55 | 
         
            -
             
     | 
| 56 | 
         
            -
                 
     | 
| 57 | 
         
            -
             
     | 
| 58 | 
         
            -
             
     | 
| 59 | 
         
            -
             
     | 
| 60 | 
         
            -
             
     | 
| 61 | 
         
            -
                     
     | 
| 62 | 
         
            -
                     
     | 
| 63 | 
         
            -
             
     | 
| 64 | 
         
            -
                     
     | 
| 65 | 
         
            -
             
     | 
| 66 | 
         
            -
            ) 
     | 
| 67 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 
         | 
|
| 1 | 
         
             
            import gradio as gr
         
     | 
| 2 | 
         
             
            import os
         
     | 
| 3 | 
         
            +
            from AinaTheme import AinaGradioTheme
         
     | 
| 4 | 
         
             
            from espeak_phonemizer import Phonemizer
         
     | 
| 5 | 
         
            +
            from dotenv import load_dotenv
         
     | 
| 6 | 
         | 
| 7 | 
         
            +
            load_dotenv()
         
     | 
| 8 | 
         | 
| 9 | 
         
            +
            MAX_INPUT_TEXT_LEN = int(os.environ.get("MAX_INPUT_TEXT_LEN", default=325))
         
     | 
| 10 | 
         | 
| 11 | 
         
            +
            #fonemitzador = Phonemizer("ca")
         
     | 
| 12 | 
         | 
| 13 | 
         
             
            def phonemiser(text, dialect):
         
     | 
| 14 | 
         
             
                dialects = {"Central": "ca", "Valencian": "ca-va", "North-West": "ca-nw"}
         
     | 
| 15 | 
         
             
                dialect = dialects[dialect] #Define dialect from espeak-ng-data/lang/roa/ca*
         
     | 
| 16 | 
         
             
                fonemitzador = Phonemizer(dialect) #Set correct dialect for the phonemiser
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 17 | 
         | 
| 18 | 
         
             
                # synthesize
         
     | 
| 19 | 
         | 
| 20 | 
         
             
                fonemes = fonemitzador.phonemize(text, keep_clause_breakers=True)
         
     | 
| 21 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 22 | 
         
             
                return fonemes
         
     | 
| 23 | 
         | 
| 24 | 
         
            +
             
     | 
| 25 | 
         
            +
            title = "Comparativa de síntesi lliure en català️"
         
     | 
| 26 | 
         
             
            description="""
         
     | 
| 27 | 
         
             
            Transcripció fonètica en català
         
     | 
| 28 | 
         
             
            Transcipció fonètica per a diferents dialectes del català mitjançant eSpeak.
         
     | 
| 
         | 
|
| 30 | 
         
             
            using eSpeak.
         
     | 
| 31 | 
         
             
            repo: https://github.com/projecte-aina/espeak-ng/tree/dev-ca 
         
     | 
| 32 | 
         
             
            """
         
     | 
| 33 | 
         
            +
             
     | 
| 34 | 
         
            +
            def submit_input(input_, dialect):
         
     | 
| 35 | 
         
            +
                output = None
         
     | 
| 36 | 
         
            +
                if input_ is not None and len(input_) < MAX_INPUT_TEXT_LEN:
         
     | 
| 37 | 
         
            +
                    output = phonemiser(input_, dialect)
         
     | 
| 38 | 
         
            +
                else:
         
     | 
| 39 | 
         
            +
                    gr.Warning(f"Your text exceeds the {MAX_INPUT_TEXT_LEN}-character limit.")
         
     | 
| 40 | 
         
            +
                return output
         
     | 
| 41 | 
         
            +
             
     | 
| 42 | 
         
            +
            def change_interactive(text):
         
     | 
| 43 | 
         
            +
                input_state = text
         
     | 
| 44 | 
         
            +
                if input_state.strip() != "":
         
     | 
| 45 | 
         
            +
                    return gr.update(interactive = True)
         
     | 
| 46 | 
         
            +
                else:
         
     | 
| 47 | 
         
            +
                    return gr.update(interactive = False)
         
     | 
| 48 | 
         
            +
            def clean(): 
         
     | 
| 49 | 
         
            +
                return (
         
     | 
| 50 | 
         
            +
                    None, 
         
     | 
| 51 | 
         
            +
                    None,
         
     | 
| 52 | 
         
            +
                )
         
     | 
| 53 | 
         
            +
             
     | 
| 54 | 
         
            +
             
     | 
| 55 | 
         
            +
             
     | 
| 56 | 
         
            +
            with gr.Blocks(**AinaGradioTheme().get_kwargs()) as app:
         
     | 
| 57 | 
         
            +
                    
         
     | 
| 58 | 
         
            +
                    gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>")
         
     | 
| 59 | 
         
            +
             
     | 
| 60 | 
         
            +
                    gr.Markdown(description)
         
     | 
| 61 | 
         
            +
             
     | 
| 62 | 
         
            +
                    with gr.Row(equal_height=False):
         
     | 
| 63 | 
         
            +
                
         
     | 
| 64 | 
         
            +
                        with gr.Column(variant='panel'):
         
     | 
| 65 | 
         
            +
                            input_ = gr.Textbox(
         
     | 
| 66 | 
         
            +
                                label="Text",
         
     | 
| 67 | 
         
            +
                                value="Les coses importants són les que no ho semblen.",
         
     | 
| 68 | 
         
            +
                                lines=4
         
     | 
| 69 | 
         
            +
                            )
         
     | 
| 70 | 
         
            +
             
     | 
| 71 | 
         
            +
                            dialect = gr.Dropdown(label="Dialect", choices=["Central", "Valencian", "North-West"], value="Central")
         
     | 
| 72 | 
         
            +
                            with gr.Row():
         
     | 
| 73 | 
         
            +
                                clear_btn = gr.Button(
         
     | 
| 74 | 
         
            +
                                    "Clean", 
         
     | 
| 75 | 
         
            +
                                )
         
     | 
| 76 | 
         
            +
                                submit_btn = gr.Button(
         
     | 
| 77 | 
         
            +
                                    "Submit", 
         
     | 
| 78 | 
         
            +
                                    variant="primary", 
         
     | 
| 79 | 
         
            +
                                )
         
     | 
| 80 | 
         
            +
                        with gr.Column(variant='panel'):
         
     | 
| 81 | 
         
            +
                            output = gr.Textbox( 
         
     | 
| 82 | 
         
            +
                                label="Output", 
         
     | 
| 83 | 
         
            +
                                interactive=False, 
         
     | 
| 84 | 
         
            +
                                show_copy_button=True
         
     | 
| 85 | 
         
            +
                            )
         
     | 
| 86 | 
         
            +
                    gr.Examples(
         
     | 
| 87 | 
         
            +
                        label="Examples",
         
     | 
| 88 | 
         
            +
                        examples=[
         
     | 
| 89 | 
         
            +
                            ["Les coses importants són les que no ho semblen.", "Central"],
         
     | 
| 90 | 
         
            +
                            ["Les coses importants són les que no ho semblen.", "Valencian",],
         
     | 
| 91 | 
         
            +
                            ["Les coses importants són les que no ho semblen.", "North-West",],
         
     | 
| 92 | 
         
            +
                        ],
         
     | 
| 93 | 
         
            +
                        inputs=[input_, dialect],
         
     | 
| 94 | 
         
            +
                        outputs=output,
         
     | 
| 95 | 
         
            +
                        fn=submit_input)
         
     | 
| 96 | 
         
            +
             
     | 
| 97 | 
         
            +
                    for button in [submit_btn, clear_btn]:
         
     | 
| 98 | 
         
            +
                        input_.change(fn=change_interactive, inputs=[input_], outputs=button)
         
     | 
| 99 | 
         
            +
             
     | 
| 100 | 
         
            +
                    clear_btn.click(fn=clean, inputs=[], outputs=[input_, output] , queue=False)
         
     | 
| 101 | 
         
            +
                    submit_btn.click(fn=submit_input, inputs=[input_, dialect], outputs=output)
         
     | 
| 102 | 
         
            +
                    
         
     | 
| 103 | 
         
            +
            app.queue(concurrency_count=2, api_open=False)
         
     | 
| 104 | 
         
            +
            app.launch(show_api=False)
         
     | 
    	
        requirements.txt
    CHANGED
    
    | 
         @@ -1,2 +1,4 @@ 
     | 
|
| 1 | 
         
            -
            gradio
         
     | 
| 2 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            git+https://gitlab.bsc.es/projecte-aina/aina-gradio[email protected]
         
     | 
| 2 | 
         
            +
            gradio==3.44.2
         
     | 
| 3 | 
         
            +
            espeak-phonemizer>=1.1.0,<2 
         
     | 
| 4 | 
         
            +
            python-dotenv==1.0.0
         
     |