Spaces:
Running
Running
PerryCheng614
commited on
Commit
•
de76a17
1
Parent(s):
cfb4e8c
audio+text in
Browse files
app.py
CHANGED
@@ -1,7 +1,57 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
|
5 |
+
# FastAPI endpoint
|
6 |
+
API_URL = "https://nexa-omni.nexa4ai.com/process-audio/"
|
7 |
|
8 |
+
def process_audio(audio_path, prompt=""):
|
9 |
+
"""
|
10 |
+
Send audio file to FastAPI backend for processing
|
11 |
+
"""
|
12 |
+
try:
|
13 |
+
# Prepare the file for upload
|
14 |
+
files = {
|
15 |
+
'file': ('audio.wav', open(audio_path, 'rb'), 'audio/wav')
|
16 |
+
}
|
17 |
+
# Send prompt as form data
|
18 |
+
data = {'prompt': prompt}
|
19 |
+
|
20 |
+
# Make the request to FastAPI
|
21 |
+
response = requests.post(API_URL, files=files, data=data)
|
22 |
+
response.raise_for_status()
|
23 |
+
|
24 |
+
return response.json()['response']
|
25 |
+
except Exception as e:
|
26 |
+
return f"Error processing audio: {str(e)}"
|
27 |
+
finally:
|
28 |
+
# Clean up the temporary file if it exists
|
29 |
+
if audio_path and os.path.exists(audio_path):
|
30 |
+
os.remove(audio_path)
|
31 |
+
|
32 |
+
# Create Gradio interface
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=process_audio,
|
35 |
+
inputs=[
|
36 |
+
gr.Audio(
|
37 |
+
type="filepath",
|
38 |
+
label="Upload or Record Audio",
|
39 |
+
sources=["upload", "microphone"]
|
40 |
+
),
|
41 |
+
gr.Textbox(
|
42 |
+
placeholder="Enter prompt (optional)",
|
43 |
+
label="Prompt",
|
44 |
+
value="transcribe this audio in English and return me the transcription:"
|
45 |
+
)
|
46 |
+
],
|
47 |
+
outputs=gr.Textbox(label="Response"),
|
48 |
+
title="Audio Processing Service",
|
49 |
+
description="Upload an audio file and optionally provide a prompt to analyze the audio content.",
|
50 |
+
examples=[
|
51 |
+
["path/to/example.wav", "transcribe this audio in English"],
|
52 |
+
["path/to/example2.wav", "describe what this audio is about"]
|
53 |
+
]
|
54 |
+
)
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|