wsntxxn commited on
Commit
cd13388
Β·
1 Parent(s): 89a18fc
Files changed (2) hide show
  1. .gitignore +216 -0
  2. app.py +171 -59
.gitignore ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+
204
+ # Ruff stuff:
205
+ .ruff_cache/
206
+
207
+ # PyPI configuration file
208
+ .pypirc
209
+
210
+ # Marimo
211
+ marimo/_static/
212
+ marimo/_lsp/
213
+ __marimo__/
214
+
215
+ # Streamlit
216
+ .streamlit/secrets.toml
app.py CHANGED
@@ -22,31 +22,11 @@ def is_tdc_format_valid(tdc_str):
22
  except Exception:
23
  return False
24
 
25
- def a_to_b(a_str):
26
- items = a_str.split(';')
27
- result = []
28
- for item in items:
29
- item = item.strip()
30
- if not item:
31
- continue
32
- if '(' in item and ')' in item:
33
- name, times = item.split('(', 1)
34
- name = name.strip().replace(' ', '_')
35
- times = times.strip(')').replace(', ', '_').replace(',', '_')
36
- result.append(f"{name}__{times}")
37
- return '--'.join(result)
38
 
39
- def b_to_a(b_str):
40
- events = b_str.split('--')
41
- result = []
42
- for e in events:
43
- if '__' not in e:
44
- continue
45
- name, times = e.split('__', 1)
46
- name = name.replace('_', ' ')
47
- times = times.replace('_', ', ')
48
- result.append(f"{name}({times})")
49
- return '; '.join(result)
50
 
51
  def convert_tdc_to_tcc(b_str):
52
  events = b_str.split('--')
@@ -59,10 +39,46 @@ def convert_tdc_to_tcc(b_str):
59
  names.append(name)
60
  return ' and '.join(names)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  @spaces.GPU(duration=60)
63
  def infer(input_text, input_onset, input_length, time_control):
64
- # para
65
- input_onset = a_to_b(input_onset)
66
  if not input_text and input_onset and is_tdc_format_valid(input_onset):
67
  input_text = convert_tdc_to_tcc(input_onset)
68
  elif not input_text:
@@ -94,52 +110,148 @@ def infer(input_text, input_onset, input_length, time_control):
94
  waveform[0, 0].cpu().numpy(),
95
  samplerate=24000,
96
  )
97
- #input_onset = b_to_a(input_onset)
98
  return output_wav, str(input_onset)
99
 
100
- demo = gr.Interface(
101
- fn=infer,
102
- inputs=[
103
- gr.Textbox(label="TCC (necessary)", value="a dog barks"),
104
- gr.Textbox(label="TDC (optional, see format)", value="a dog barks(3.0-4.0, 6.0-7.0)"),
105
- gr.Textbox(label="Length (seconds, optional)", value="10.0"),
106
- gr.Checkbox(label="Enable Time Control", value=False),
107
- ],
108
- outputs=[
109
- gr.Audio(label="Generated Audio"),
110
- gr.Textbox(label="Final TDC Used (input_onset)")
111
- ],
112
- title="PicoAudio2 Online Inference",
113
- description="""
114
- ## Definition
115
 
116
- **TCC (Temporal Coarse Caption):**
 
 
117
  A brief text description for the overall audio scene.
118
- *Example*: `a dog barks`
 
 
 
 
 
119
 
120
- **TDC (Temporal Detailed Caption):**
121
- A **caption with timestamp information** for each event.
122
- It allows precise temporal control over when events happen in the generated audio.
123
- *Example*: `a dog barks(1.0-2.0, 3.0-4.0); a man speaks(5.0-6.0)`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  ---
126
 
127
- ## Input Requirements & Format
128
 
129
- - **TCC** is **required** for audio generation.
130
- - **TDC** is **optional**. If provided, it should follow the format: `event1(start1-end1, start2-end2); event2(start1-end1, ...)`
131
- - **Length** (in seconds) is optional, but recommended for temporal control. The length defaults to 10.0 seconds.
132
- - **Enable Time Control**: Tick to use TDC and length for precise event timing.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  ---
135
 
136
- ## Notes
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
- - If TDC format is incorrect or length is missing, the model will generate audio **without precise temporal control**.
139
- - For general audio generation, it is recommended to input `random` for TDC.
140
- - You may leave TDC blank to let the LLM generate timestamps automatically (subject to API quota).
141
  ---
142
- """
143
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  if __name__ == "__main__":
145
  demo.launch()
 
22
  except Exception:
23
  return False
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Event handlers
27
+ def clear_json():
28
+ return "{}"
29
+
 
 
 
 
 
 
 
30
 
31
  def convert_tdc_to_tcc(b_str):
32
  events = b_str.split('--')
 
39
  names.append(name)
40
  return ' and '.join(names)
41
 
42
+ def json_to_tdc(json_str):
43
+ """Convert JSON data to final internal format: event1__s1-e1_s2-e2--event2__s1-e1"""
44
+ if not json_str or not json_str.strip():
45
+ return ""
46
+
47
+ try:
48
+ events_dict = json.loads(json_str)
49
+ except json.JSONDecodeError as e:
50
+ return f"Error: Invalid JSON format - {str(e)}"
51
+
52
+ if not isinstance(events_dict, dict):
53
+ return "Error: JSON should be a dictionary/object"
54
+
55
+ # Build final internal format
56
+ result = []
57
+ for event_name, time_segments in events_dict.items():
58
+ if not isinstance(time_segments, list):
59
+ continue
60
+
61
+ valid_segments = []
62
+ for segment in time_segments:
63
+ if isinstance(segment, list) and len(segment) >= 2:
64
+ start, end = segment[0], segment[1]
65
+ if start is not None and end is not None:
66
+ valid_segments.append(f"{start}-{end}")
67
+
68
+ if valid_segments:
69
+ # Convert to internal format: event_name__time1_time2
70
+ event_name_clean = event_name.strip().replace(' ', '_')
71
+ times_str = '_'.join(valid_segments)
72
+ result.append(f"{event_name_clean}__{times_str}")
73
+
74
+ return '--'.join(result)
75
+
76
+ def generate_audio(tcc, json_data, length, time_ctrl):
77
+ tdc = json_to_tdc(json_data)
78
+ return infer(tcc, tdc, length, time_ctrl)
79
+
80
  @spaces.GPU(duration=60)
81
  def infer(input_text, input_onset, input_length, time_control):
 
 
82
  if not input_text and input_onset and is_tdc_format_valid(input_onset):
83
  input_text = convert_tdc_to_tcc(input_onset)
84
  elif not input_text:
 
110
  waveform[0, 0].cpu().numpy(),
111
  samplerate=24000,
112
  )
 
113
  return output_wav, str(input_onset)
114
 
115
+ with gr.Blocks(title="PicoAudio2 Online Inference", theme=gr.themes.Soft()) as demo:
116
+ gr.Markdown("# 🎡 PicoAudio2 Online Inference")
117
+
118
+ gr.Markdown("""
119
+ <div style="text-align: left; padding: 10px;">
120
+
121
+ ## πŸ“– Definition
 
 
 
 
 
 
 
 
122
 
123
+ <div style="background-color: #f0f8ff; padding: 15px; border-radius: 8px; margin: 10px 0;">
124
+
125
+ **TCC (Temporal Coarse Caption)**
126
  A brief text description for the overall audio scene.
127
+ > *Example*: `a dog barks`
128
+
129
+ **TDC (Temporal Detailed Caption)**
130
+ **Event descriptions with timestamps**. It allows precise temporal control over when events happen in the generated audio.
131
+
132
+ > *Example*: See the format below.
133
 
134
+ </div>
135
+
136
+ ## πŸ“Š TDC Event Timestamp JSON (Optional)
137
+
138
+ <div style="background-color: #fffef0; padding: 12px; border-radius: 6px; border-left: 4px solid #ffa500;">
139
+
140
+ **πŸ’‘ JSON Format:**
141
+ - Each **event description** is a key in the JSON object
142
+ - The value is an **array of [start, end] timestamp pairs** (in seconds)
143
+
144
+ **Example:**
145
+ <pre style="background-color: #f5f5f5; padding: 8px; border-radius: 4px; margin: 5px 0; font-size: 13px;">
146
+ {
147
+ "a dog barks": [
148
+ [3.0, 4.0],
149
+ [6.0, 7.0]
150
+ ],
151
+ "a man speaks": [
152
+ [5.0, 6.0]
153
+ ]
154
+ }</pre>
155
+
156
+ It means the events of `a dog barks` happen from 3.0 to 4.0 seconds, and the events of `a man speaks` happen from 5.0 to 6.0 seconds.
157
+
158
+ </div>
159
 
160
  ---
161
 
162
+ </div>
163
 
164
+ """)
165
+
166
+ with gr.Row():
167
+ with gr.Column():
168
+ tcc_input = gr.Textbox(
169
+ label="🎯 TCC (Temporal Coarse Caption) - Required",
170
+ value="a dog barks",
171
+ placeholder="e.g., a dog barks and a man speaks",
172
+ lines=2
173
+ )
174
+
175
+ event_json = gr.Code(
176
+ label="Event Timestamp JSON",
177
+ value="""{
178
+ "a dog barks": [
179
+ [3.0, 4.0],
180
+ [6.0, 7.0]
181
+ ]
182
+ }""",
183
+ language="json",
184
+ lines=10,
185
+ interactive=True
186
+ )
187
+
188
+ clear_btn = gr.Button("πŸ—‘οΈ Clear JSON", size="sm")
189
+
190
+ gr.Markdown("---")
191
+
192
+ with gr.Row():
193
+ length_input = gr.Textbox(label="⏱️ Length (seconds)", value="10.0", scale=2)
194
+ time_control = gr.Checkbox(
195
+ label="βš™οΈ Enable Time Control",
196
+ value=True,
197
+ scale=1,
198
+ )
199
+
200
+ generate_btn = gr.Button("🎡 Generate Audio", variant="primary", size="lg")
201
+
202
+ with gr.Column():
203
+ audio_output = gr.Audio(label="πŸ”Š Generated Audio")
204
+ tdc_used = gr.Textbox(label="πŸ“‹ Final TDC Used (Internal Format)", lines=3)
205
+
206
+ gr.Markdown("""
207
+ <div style="text-align: left; padding: 10px;">
208
 
209
  ---
210
 
211
+ ## πŸ“ Input Requirements
212
+
213
+ <div style="background-color: #fff5e6; padding: 15px; border-radius: 8px; margin: 10px 0;">
214
+
215
+ **TCC** is **required** for audio generation.
216
+
217
+ **TDC (JSON)** is **optional** for precise temporal control of events.
218
+
219
+ **Length** (in seconds) is optional, but recommended for temporal control. Defaults to 10.0 seconds.
220
+
221
+ **Enable Time Control**: Tick to use TDC and length for precise event timing.
222
+
223
+ </div>
224
 
 
 
 
225
  ---
226
+
227
+ ## πŸ’‘ Notes
228
+
229
+ <div style="background-color: #f0fff0; padding: 15px; border-radius: 8px; margin: 10px 0;">
230
+
231
+ If TDC format is incorrect or length is missing, the model will generate audio **without precise temporal control**.
232
+
233
+ For general audio generation without precise timing, you can leave the JSON empty.
234
+
235
+ You may leave TDC blank to let the LLM generate timestamps automatically (subject to API quota).
236
+
237
+ </div>
238
+
239
+ </div>
240
+ """)
241
+
242
+
243
+
244
+ clear_btn.click(
245
+ fn=clear_json,
246
+ inputs=[],
247
+ outputs=[event_json]
248
+ )
249
+
250
+ generate_btn.click(
251
+ fn=generate_audio,
252
+ inputs=[tcc_input, event_json, length_input, time_control],
253
+ outputs=[audio_output, tdc_used]
254
+ )
255
+
256
  if __name__ == "__main__":
257
  demo.launch()