File size: 1,370 Bytes
65f9ac9
aba8804
 
d6c20cc
7026386
aa62bf6
aba8804
65b1d66
 
 
5445cc3
65b1d66
 
 
 
 
 
 
 
 
 
 
 
 
 
ab8e629
65b1d66
 
 
7026386
363b770
7026386
 
f8d9058
7026386
 
65b1d66
5445cc3
65b1d66
2227fad
aba8804
 
7026386
65b1d66
aba8804
23e8b9e
7026386
2227fad
 
7026386
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
import gradio as gr
import requests
import os

def send_audio(audio):
    url = os.getenv("TRANSLATION_URL")
    
    if audio is None:
        return {"error": "No audio file provided"}

    try:
        if isinstance(audio, bytes):
            # If audio is a bytes object, save it to a temporary file
            with open("temp_audio.wav", "wb") as f:
                f.write(audio)
            audio_path = "temp_audio.wav"
        else:
            # If audio is already a file path
            audio_path = audio
        
        # Prepare form data for request
        files = {
            'file': open(audio_path, 'rb'),
        }
        data = {
            'lang': 'english-twi'
        }
        
        response = requests.post(url, files=files, data=data)
        response_json = response.json()
        #return response_json
        
        # Extract the translation part
        translation = response_json.get('data', 'No translation available')
        
        return translation
    
    except Exception as e:
        return {"error": str(e)}

# Gradio interface for recording speech
iface = gr.Interface(
    fn=send_audio,
    inputs=gr.Audio(type="filepath"),  # Expect a file path or file-like object
    outputs="text",
    title="English-Twi Speech Translation",
    description="Record speech and send for processing.",
)

iface.launch()