File size: 14,816 Bytes
668aead
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
7bfbdc3
668aead
 
 
 
7bfbdc3
 
668aead
 
 
7bfbdc3
668aead
7bfbdc3
668aead
7bfbdc3
668aead
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
 
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
e7c040d
668aead
e7c040d
668aead
e7c040d
668aead
 
 
 
 
 
 
 
e7c040d
668aead
e7c040d
 
668aead
 
 
 
 
e7c040d
668aead
e7c040d
668aead
 
 
e7c040d
 
668aead
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bfbdc3
668aead
7bfbdc3
668aead
7bfbdc3
 
668aead
 
 
7bfbdc3
668aead
 
 
 
 
 
7bfbdc3
 
668aead
 
 
7bfbdc3
 
e7c040d
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import os
import sys
from pathlib import Path
import spaces

# === Import project modules ===
PROJECT_ROOT = Path(__file__).resolve().parent
MMADA_ROOT = PROJECT_ROOT / "MMaDA"
if str(MMADA_ROOT) not in sys.path:
    sys.path.insert(0, str(MMADA_ROOT))

from inference.gradio_multimodal_demo_inst import OmadaDemo
import gradio as gr


# ----------------------------------------------------------------------
#                  1.  Asset Loading (Downloaded by entrypoint)
# ----------------------------------------------------------------------

ASSET_ROOT = PROJECT_ROOT / "_asset_cache" / "AIDAS-Omni-Modal-Diffusion-assets"
DEMO_ROOT = ASSET_ROOT  # asset repo already modality-split


# ----------------------------------------------------------------------
#                  2.  GPU Handler Wrapper
# ----------------------------------------------------------------------

def gpu_handler(fn):
    """
    Wrap an inference function using ZeroGPU.
    """
    @spaces.GPU
    def inner(*args, **kwargs):
        return fn(*args, **kwargs)
    return inner


# ----------------------------------------------------------------------
#                  3.  Build Demo UI With Examples
# ----------------------------------------------------------------------

def build_zero_gpu_demo(app: OmadaDemo):

    with gr.Blocks(title="AIDAS Omni-Modal Diffusion (ZeroGPU)") as demo:

        # ---------------- Header ----------------
        gr.Markdown(
            "<h1 style='text-align:center'>AIDAS Omni-Modal Diffusion Model</h1>"
        )

        try:
            logo_path = "/mnt/data/A2E36E9F-F389-487D-9984-FFF21C9228E3.png"
            gr.Image(logo_path, elem_id="logo", show_label=False, height=120)
        except:
            pass

        gr.Markdown("### Multimodal Inference Demo (ZeroGPU Optimized)")
        gr.Markdown("---")

        # ---------------- Tabs ----------------

        with gr.Tabs():

            # ============================================================
            # 1) TEXT β†’ SPEECH (T2S)
            # ============================================================
            with gr.Tab("Text β†’ Speech (T2S)"):

                t2s_in = gr.Textbox(label="Input Text")
                t2s_btn = gr.Button("Generate")
                t2s_audio = gr.Audio(label="Speech Output")
                t2s_status = gr.Textbox(label="Status", interactive=False)

                t2s_examples = []
                t2s_dir = DEMO_ROOT / "t2s"
                if t2s_dir.exists():
                    for f in t2s_dir.glob("*.txt"):
                        txt = f.read_text().strip()
                        t2s_examples.append([txt])

                if len(t2s_examples) > 0:
                    gr.Examples(
                        examples=t2s_examples,
                        inputs=[t2s_in],
                        outputs=[t2s_audio, t2s_status],
                        fn=gpu_handler(app.run_t2s),
                    )

                t2s_btn.click(
                    gpu_handler(app.run_t2s),
                    inputs=[t2s_in],
                    outputs=[t2s_audio, t2s_status],
                )

            # ============================================================
            # 2) SPEECH β†’ SPEECH (S2S)
            # ============================================================
            with gr.Tab("Speech β†’ Speech (S2S)"):

                s2s_in = gr.Audio(type="filepath", label="Input Speech")
                s2s_btn = gr.Button("Generate")
                s2s_audio = gr.Audio(label="Output Speech")
                s2s_status = gr.Textbox(label="Status", interactive=False)

                s2s_examples = []
                s2s_dir = DEMO_ROOT / "s2s"
                if s2s_dir.exists():
                    for f in s2s_dir.glob("*.wav"):
                        s2s_examples.append([str(f)])

                if len(s2s_examples) > 0:
                    gr.Examples(
                        examples=s2s_examples,
                        inputs=[s2s_in],
                        outputs=[s2s_audio, s2s_status],
                        fn=gpu_handler(app.run_s2s),
                    )

                s2s_btn.click(
                    gpu_handler(app.run_s2s),
                    inputs=[s2s_in],
                    outputs=[s2s_audio, s2s_status]
                )

            # ============================================================
            # 3) SPEECH β†’ TEXT (S2T)
            # ============================================================
            with gr.Tab("Speech β†’ Text (S2T)"):

                s2t_in = gr.Audio(type="filepath", label="Input Speech")
                s2t_btn = gr.Button("Transcribe")
                s2t_text = gr.Textbox(label="Transcribed Text")
                s2t_status = gr.Textbox(label="Status", interactive=False)

                s2t_examples = []
                s2t_dir = DEMO_ROOT / "s2t"
                if s2t_dir.exists():
                    for f in s2t_dir.glob("*.wav"):
                        s2t_examples.append([str(f)])

                if len(s2t_examples) > 0:
                    gr.Examples(
                        examples=s2t_examples,
                        inputs=[s2t_in],
                        outputs=[s2t_text, s2t_status],
                        fn=gpu_handler(app.run_s2t),
                    )

                s2t_btn.click(
                    gpu_handler(app.run_s2t),
                    inputs=[s2t_in],
                    outputs=[s2t_text, s2t_status],
                )

            # ============================================================
            # 4) VIDEO β†’ TEXT (V2T)
            # ============================================================
            with gr.Tab("Video β†’ Text (V2T)"):

                v2t_in = gr.Video(type="filepath", label="Input Video")
                v2t_btn = gr.Button("Generate Caption")
                v2t_text = gr.Textbox(label="Caption")
                v2t_status = gr.Textbox(label="Status")

                v2t_examples = []
                v2t_dir = DEMO_ROOT / "v2t"
                if v2t_dir.exists():
                    for f in v2t_dir.glob("*.mp4"):
                        v2t_examples.append([str(f)])

                if len(v2t_examples) > 0:
                    gr.Examples(
                        examples=v2t_examples,
                        inputs=[v2t_in],
                        outputs=[v2t_text, v2t_status],
                        fn=gpu_handler(app.run_v2t),
                    )

                v2t_btn.click(
                    gpu_handler(app.run_v2t),
                    inputs=[v2t_in],
                    outputs=[v2t_text, v2t_status],
                )

            # ============================================================
            # 5) VIDEO β†’ SPEECH (V2S)
            # ============================================================
            with gr.Tab("Video β†’ Speech (V2S)"):

                v2s_in = gr.Video(type="filepath", label="Input Video")
                v2s_btn = gr.Button("Generate Speech")
                v2s_audio = gr.Audio(label="Speech Output")
                v2s_status = gr.Textbox(label="Status")

                v2s_examples = []
                v2s_dir = DEMO_ROOT / "v2s"
                if v2s_dir.exists():
                    for f in v2s_dir.glob("*.mp4"):
                        v2s_examples.append([str(f)])

                if len(v2s_examples) > 0:
                    gr.Examples(
                        examples=v2s_examples,
                        inputs=[v2s_in],
                        outputs=[v2s_audio, v2s_status],
                        fn=gpu_handler(app.run_v2s),
                    )

                v2s_btn.click(
                    gpu_handler(app.run_v2s),
                    inputs=[v2s_in],
                    outputs=[v2s_audio, v2s_status],
                )

            # ============================================================
            # 6) IMAGE β†’ SPEECH (I2S)
            # ============================================================
            with gr.Tab("Image β†’ Speech (I2S)"):

                i2s_in = gr.Image(type="filepath", label="Input Image")
                i2s_btn = gr.Button("Generate Speech")
                i2s_audio = gr.Audio(label="Speech")
                i2s_status = gr.Textbox(label="Status")

                # Only if folder exists
                i2s_examples = []
                i2s_dir = DEMO_ROOT / "i2s"
                if i2s_dir.exists():
                    for f in i2s_dir.glob("*.*"):
                        i2s_examples.append([str(f)])

                if len(i2s_examples) > 0:
                    gr.Examples(
                        examples=i2s_examples,
                        inputs=[i2s_in],
                        outputs=[i2s_audio, i2s_status],
                        fn=gpu_handler(app.run_i2s),
                    )

                i2s_btn.click(
                    gpu_handler(app.run_i2s),
                    inputs=[i2s_in],
                    outputs=[i2s_audio, i2s_status],
                )

            # ============================================================
            # 7) CHAT
            # ============================================================
            with gr.Tab("Chat (Text)"):

                chat_in = gr.Textbox(label="Message")
                chat_btn = gr.Button("Send")
                chat_out = gr.Textbox(label="Response")
                chat_status = gr.Textbox(label="Status")

                chat_examples = []
                chat_dir = DEMO_ROOT / "chat"
                if chat_dir.exists():
                    for f in chat_dir.glob("*.txt"):
                        txt = f.read_text().strip()
                        chat_examples.append([txt])

                if len(chat_examples) > 0:
                    gr.Examples(
                        examples=chat_examples,
                        inputs=[chat_in],
                        outputs=[chat_out, chat_status],
                        fn=gpu_handler(app.run_chat),
                    )

                chat_btn.click(
                    gpu_handler(app.run_chat),
                    inputs=[chat_in],
                    outputs=[chat_out, chat_status],
                )

            # ============================================================
            # 8) MMU (single image β†’ text)
            # ============================================================
            with gr.Tab("MMU (Image β†’ Text)"):

                mmu_img = gr.Image(type="filepath", label="Input Image")
                mmu_prompt = gr.Textbox(label="Prompt")
                mmu_btn = gr.Button("Run MMU")
                mmu_out = gr.Textbox(label="Output")
                mmu_status = gr.Textbox(label="Status")

                mmu_examples = []
                mmu_dir = DEMO_ROOT / "mmu"
                if mmu_dir.exists():
                    for f in mmu_dir.glob("*.png"):
                        mmu_examples.append([
                            str(f),
                            "Describe the main subject of this image."
                        ])

                if len(mmu_examples) > 0:
                    gr.Examples(
                        examples=mmu_examples,
                        inputs=[mmu_img, mmu_prompt],
                        outputs=[mmu_out, mmu_status],
                        fn=gpu_handler(app.run_mmu),
                    )

                mmu_btn.click(
                    gpu_handler(app.run_mmu),
                    inputs=[mmu_img, mmu_prompt],
                    outputs=[mmu_out, mmu_status]
                )

            # ============================================================
            # 9) TEXT β†’ IMAGE (T2I)
            # ============================================================
            with gr.Tab("Text β†’ Image (T2I)"):

                t2i_in = gr.Textbox(label="Prompt")
                t2i_btn = gr.Button("Generate Image")
                t2i_img = gr.Image(label="Generated Image")
                t2i_status = gr.Textbox(label="Status")

                t2i_examples = []
                t2i_dir = DEMO_ROOT / "t2i"
                if t2i_dir.exists():
                    for f in t2i_dir.glob("*.txt"):
                        txt = f.read_text().strip()
                        t2i_examples.append([txt])

                if len(t2i_examples) > 0:
                    gr.Examples(
                        examples=t2i_examples,
                        inputs=[t2i_in],
                        outputs=[t2i_img, t2i_status],
                        fn=gpu_handler(app.run_t2i),
                    )

                t2i_btn.click(
                    gpu_handler(app.run_t2i),
                    inputs=[t2i_in],
                    outputs=[t2i_img, t2i_status],
                )

            # ============================================================
            # 10) IMAGE EDITING (I2I)
            # ============================================================
            with gr.Tab("Image Editing (I2I)"):

                i2i_in = gr.Image(type="filepath", label="Input Image")
                i2i_prompt = gr.Textbox(label="Edit Instruction")
                i2i_btn = gr.Button("Apply Edit")
                i2i_img = gr.Image(label="Edited Image")
                i2i_status = gr.Textbox(label="Status")

                i2i_examples = []
                i2i_dir = DEMO_ROOT / "i2i"
                if i2i_dir.exists():
                    for f in i2i_dir.glob("*.*"):
                        i2i_examples.append([str(f), "Make it more vibrant."])

                if len(i2i_examples) > 0:
                    gr.Examples(
                        examples=i2i_examples,
                        inputs=[i2i_in, i2i_prompt],
                        outputs=[i2i_img, i2i_status],
                        fn=gpu_handler(app.run_i2i),
                    )

                i2i_btn.click(
                    gpu_handler(app.run_i2i),
                    inputs=[i2i_in, i2i_prompt],
                    outputs=[i2i_img, i2i_status]
                )

        # End Tabs

    return demo


# ----------------------------------------------------------------------
#                  4.  Entry Point for Space
# ----------------------------------------------------------------------

@spaces.GPU
def main():
    app = OmadaDemo(
        train_config=str(MMADA_ROOT / "inference/demo/demo.yaml"),
        checkpoint=os.getenv("MODEL_CHECKPOINT_DIR", "_ckpt_cache/omada"),
        device="cpu"
    )

    demo = build_zero_gpu_demo(app)
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)


if __name__ == "__main__":
    main()