File size: 2,444 Bytes
cfb4e8c
de76a17
 
cfb4e8c
de76a17
 
cfb4e8c
de76a17
 
 
 
ff9e518
 
 
de76a17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff9e518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de76a17
 
 
 
 
 
 
 
 
 
 
 
 
ff9e518
de76a17
 
 
7bd2b9b
de76a17
 
fd06c1e
de76a17
 
 
7bd2b9b
 
 
 
de76a17
 
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
import gradio as gr
import requests
import os

# FastAPI endpoint
API_URL = "https://nexa-omni.nexa4ai.com/process-audio/"

def process_audio(audio_path, prompt=""):
    """
    Send audio file to FastAPI backend for processing
    """
    # Clear any previous audio files before processing new one
    clear_previous_audio_files(audio_path)
    
    try:
        # Prepare the file for upload
        files = {
            'file': ('audio.wav', open(audio_path, 'rb'), 'audio/wav')
        }
        # Send prompt as form data
        data = {'prompt': prompt}
        
        # Make the request to FastAPI
        response = requests.post(API_URL, files=files, data=data)
        response.raise_for_status()
        
        return response.json()['response']
    except Exception as e:
        return f"Error processing audio: {str(e)}"

def clear_previous_audio_files(current_file):
    """
    Clear previous audio files in the same directory as the current file,
    except for the current file and example files
    """
    if not current_file:
        return
        
    directory = os.path.dirname(current_file)
    if not directory:
        directory = "."
        
    for file in os.listdir(directory):
        file_path = os.path.join(directory, file)
        # Skip if it's the current file, example files, or not a file
        if (file_path == current_file or 
            'example' in file_path or 
            not os.path.isfile(file_path) or 
            not file.endswith(('.wav', '.mp3'))):
            continue
        try:
            os.remove(file_path)
        except:
            pass  # Silently ignore deletion errors

# Create Gradio interface
demo = gr.Interface(
    fn=process_audio,
    inputs=[
        gr.Audio(
            type="filepath",
            label="Upload or Record Audio",
            sources=["upload", "microphone"]
        ),
        gr.Textbox(
            placeholder="Enter prompt (optional)",
            label="Prompt",
            value="Summarize this audio in English."
        )
    ],
    outputs=gr.Textbox(label="Response"),
    title="Nexa Omni",
    description="Upload an audio file and optionally provide a prompt to analyze the audio content.",
    examples=[
        ["example_audios/example_1.wav", "transcribe this audio in English"],
    ]
)

def clear_output(audio, prompt):
    return ""
demo.load_examples = clear_output

if __name__ == "__main__":
    demo.launch()