File size: 2,584 Bytes
43c2663
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fe4028
43c2663
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa8113
 
43c2663
 
 
 
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
import gradio as gr
import requests
import os
import time

def upload_audio(file_path, progress=gr.Progress()):
    url = f'{os.getenv("VOCHAI_URL")}/upload'
    headers = {
        'accept': 'application/json',
        'Authorization': f'Bearer {os.getenv("VOCHAI_TOKEN")}',
    }
    with open(file_path, 'rb') as file:
        files = {
            'file': (os.path.basename(file_path), file, 'audio/*')
        }
        progress(0, desc="Uploading audio...")
        response = requests.post(url, headers=headers, files=files)
    if response.status_code == 200:
        progress(0.5, desc="Upload complete")
        os.remove(file_path)
        return response.json()
    else:
        print("Error:", response.status_code, response.text)
        return None

def process_audio(audio_uri, progress=gr.Progress()):
    url = f'{os.getenv("VOCHAI_URL")}/process?audio_uri={audio_uri}'
    headers = {
        'accept': 'application/json',
        'Authorization': f'Bearer {os.getenv("VOCHAI_TOKEN")}',
    }
    progress(0.5, desc="Processing audio...")
    response = requests.post(url, headers=headers)
    if response.status_code == 200:
        progress(1.0, desc="Processing complete")
        return response.json()
    else:
        print("Error:", response.status_code, response.text)
        return None

def upload_and_process_audio(audio_path, progress=gr.Progress()):
    gr.Info("Uploading and processing audio. This may take a moment...")
    
    # Upload the audio file
    upload_result = upload_audio(audio_path, progress)
    if not upload_result:
        gr.Error("Upload failed")
        return "Upload failed", "Upload failed"
    
    audio_uri = upload_result.get('uri')
    
    # Process the audio
    process_result = process_audio(audio_uri, progress)
    if not process_result:
        gr.Error("Processing failed")
        return "Processing failed", "Processing failed"
    
    gr.Info("Audio processing completed successfully!")
    # Return the transcript and topic
    return process_result.get("topic", "No topic detected"), process_result.get("transcript", "No transcript available")

# Create the Gradio interface
iface = gr.Interface(
    fn=upload_and_process_audio,
    inputs=gr.Audio(type="filepath", label="Upload Audio File"),
    outputs=[
        gr.Textbox(label="Topic", rtl=True),
        gr.Textbox(label="Transcript", rtl=True)
    ],
    title="Arabic Audio Transcription and Topic Detection",
    description="Upload an audio file in arabic to get its transcript and detected topic.",
)

# Launch the interface
iface.launch()