PerryCheng614 commited on
Commit
1bccd9f
1 Parent(s): ff9e518

update gradio UI for slider and file system

Browse files
Files changed (1) hide show
  1. app.py +37 -46
app.py CHANGED
@@ -5,54 +5,43 @@ import os
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
- # Clear any previous audio files before processing new one
13
- clear_previous_audio_files(audio_path)
 
 
 
 
 
 
 
 
 
 
14
 
15
  try:
16
- # Prepare the file for upload
17
- files = {
18
- 'file': ('audio.wav', open(audio_path, 'rb'), 'audio/wav')
19
- }
20
- # Send prompt as form data
21
- data = {'prompt': prompt}
22
-
23
- # Make the request to FastAPI
24
- response = requests.post(API_URL, files=files, data=data)
25
- response.raise_for_status()
26
-
27
- return response.json()['response']
 
28
  except Exception as e:
29
  return f"Error processing audio: {str(e)}"
30
 
31
- def clear_previous_audio_files(current_file):
32
- """
33
- Clear previous audio files in the same directory as the current file,
34
- except for the current file and example files
35
- """
36
- if not current_file:
37
- return
38
-
39
- directory = os.path.dirname(current_file)
40
- if not directory:
41
- directory = "."
42
-
43
- for file in os.listdir(directory):
44
- file_path = os.path.join(directory, file)
45
- # Skip if it's the current file, example files, or not a file
46
- if (file_path == current_file or
47
- 'example' in file_path or
48
- not os.path.isfile(file_path) or
49
- not file.endswith(('.wav', '.mp3'))):
50
- continue
51
- try:
52
- os.remove(file_path)
53
- except:
54
- pass # Silently ignore deletion errors
55
-
56
  # Create Gradio interface
57
  demo = gr.Interface(
58
  fn=process_audio,
@@ -62,21 +51,23 @@ demo = gr.Interface(
62
  label="Upload or Record Audio",
63
  sources=["upload", "microphone"]
64
  ),
65
- gr.Textbox(
66
- placeholder="Enter prompt (optional)",
67
- label="Prompt",
68
- value="Summarize this audio in English."
 
 
69
  )
70
  ],
71
  outputs=gr.Textbox(label="Response"),
72
  title="Nexa Omni",
73
  description="Upload an audio file and optionally provide a prompt to analyze the audio content.",
74
  examples=[
75
- ["example_audios/example_1.wav", "transcribe this audio in English"],
76
  ]
77
  )
78
 
79
- def clear_output(audio, prompt):
80
  return ""
81
  demo.load_examples = clear_output
82
 
 
5
  # FastAPI endpoint
6
  API_URL = "https://nexa-omni.nexa4ai.com/process-audio/"
7
 
8
+ # Add this global variable to track the last valid audio file
9
+ last_valid_audio = None
10
+
11
+ def process_audio(audio_path, max_tokens):
12
  """
13
  Send audio file to FastAPI backend for processing
14
  """
15
+ global last_valid_audio
16
+
17
+ # Check audio file availability
18
+ if audio_path and os.path.exists(audio_path):
19
+ # New audio uploaded/recorded
20
+ last_valid_audio = audio_path
21
+ elif not audio_path and not last_valid_audio:
22
+ # No audio provided and no previous valid audio
23
+ return "Please upload or record an audio file first."
24
+
25
+ # Use the appropriate audio path
26
+ current_audio = last_valid_audio if last_valid_audio else audio_path
27
 
28
  try:
29
+ # Only proceed if we have a valid audio file
30
+ if current_audio and os.path.exists(current_audio):
31
+ files = {
32
+ 'file': ('audio.wav', open(current_audio, 'rb'), 'audio/wav')
33
+ }
34
+ data = {'max_tokens': max_tokens}
35
+
36
+ response = requests.post(API_URL, files=files, data=data)
37
+ response.raise_for_status()
38
+
39
+ return response.json()['response']
40
+ else:
41
+ return "No valid audio file available."
42
  except Exception as e:
43
  return f"Error processing audio: {str(e)}"
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Create Gradio interface
46
  demo = gr.Interface(
47
  fn=process_audio,
 
51
  label="Upload or Record Audio",
52
  sources=["upload", "microphone"]
53
  ),
54
+ gr.Slider(
55
+ minimum=50,
56
+ maximum=200,
57
+ value=50,
58
+ step=1,
59
+ label="Max Tokens"
60
  )
61
  ],
62
  outputs=gr.Textbox(label="Response"),
63
  title="Nexa Omni",
64
  description="Upload an audio file and optionally provide a prompt to analyze the audio content.",
65
  examples=[
66
+ ["example_audios/example_1.wav", 50],
67
  ]
68
  )
69
 
70
+ def clear_output(audio, max_tokens):
71
  return ""
72
  demo.load_examples = clear_output
73