|
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_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_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 process_result.get("topic", "No topic detected"), process_result.get("transcript", "No transcript available") |
|
|
|
|
|
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.", |
|
) |
|
|
|
|
|
iface.launch() |