File size: 2,247 Bytes
ae4ea47
 
 
 
3e994a1
81136e1
 
 
 
 
 
 
 
 
 
3e994a1
 
81136e1
3e994a1
 
 
 
81136e1
 
 
 
ae4ea47
 
81136e1
 
 
 
3e994a1
 
 
 
 
 
 
 
 
 
81136e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e994a1
 
 
 
 
 
 
 
 
 
81136e1
 
 
 
 
 
 
 
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
import gradio as gr
import classla
classla.download('hr')
nlp = classla.Pipeline('hr')

# def greet(name):
#     doc = nlp(name)
#     #print(doc.to_conll())
#     return doc.to_conll()

# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
# iface.launch()

import gradio as gr
from gtts import gTTS
import wavio


from io import BytesIO
import base64

    

# Function to translate text
def parse_text(text):
    doc = nlp(text)
    return doc.to_conll()

# Function to play the original text as audio
def play_original_audio(text_to_play):
    tts = gTTS(text_to_play, lang='hr')  # Assuming the text is in English
    tts.save('original_audio.mp3')
    
    audio_bytes = BytesIO()
    tts.write_to_fp(audio_bytes)
    audio_bytes.seek(0)

    audio = base64.b64encode(audio_bytes.read()).decode("utf-8")
    audio_player = f'<audio src="data:audio/mpeg;base64,{audio}" controls autoplay></audio>'

    return audio_player
    

# Create Gradio interface components
text_input = gr.inputs.Textbox(lines=2, label="Enter text to parse")
parse_button = gr.inputs.Button(text="Parse")
play_audio_button = gr.inputs.Button(text="Play Original Audio")

# Create a Gradio interface
iface = gr.Interface(
    fn=None,  # No main function needed
    inputs=[text_input, target_language, parse_button, play_audio_button],
    outputs=[gr.outputs.Textbox(label="Parsing")],
)

# Define functions for button clicks
def parse_button_click():
    text = text_input.value
    target_lang = target_language.value
    parsed_text = translate_text(text, target_lang)
    iface.outputs[0].update(parsed_text)

def play_audio_button_click():
    text = text_input.value
    play_original_audio(text)

# Assign click functions to buttons
translate_button.command = translate_button_click
play_audio_button.command = play_audio_button_click

def audio_to_html(audio):
    audio_bytes = BytesIO()
    wavio.write(audio_bytes, audio[1].astype(np.float32), audio[0], sampwidth=4)
    audio_bytes.seek(0)

    audio_base64 = base64.b64encode(audio_bytes.read()).decode("utf-8")
    audio_player = f'<audio src="data:audio/mpeg;base64,{audio_base64}" controls autoplay></audio>'

    return audio_player
    
# Launch the Gradio interface
iface.launch()