File size: 5,568 Bytes
8f64bcf
 
 
 
 
a5c279e
321795b
8f64bcf
 
 
 
 
 
 
 
a5c279e
8f64bcf
 
 
 
 
 
 
 
a5c279e
053edf5
8f64bcf
 
 
 
 
053edf5
 
 
8f64bcf
a5c279e
8f64bcf
053edf5
8f64bcf
 
a5c279e
8f64bcf
a5c279e
8f64bcf
 
 
 
 
053edf5
 
 
 
8f64bcf
49aaa8f
fe1e574
6b7a155
a9ed129
6b7a155
 
 
fe1e574
 
f2b30cf
fe1e574
 
 
6b7a155
fe1e574
 
49aaa8f
 
 
fe1e574
8f64bcf
a5c279e
8f64bcf
 
053edf5
8f64bcf
 
 
 
a5c279e
 
053edf5
a5c279e
8f64bcf
 
 
 
053edf5
8f64bcf
 
 
053edf5
 
 
 
 
 
 
 
 
 
 
 
 
8f64bcf
053edf5
3054c91
28ebe68
8f64bcf
 
 
 
 
 
a5c279e
8f64bcf
 
 
 
 
 
 
 
 
 
6b7a155
8f64bcf
 
 
053edf5
8f64bcf
 
 
 
 
a5c279e
8f64bcf
 
 
 
 
 
 
053edf5
8f64bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321795b
 
a5c279e
 
8f64bcf
 
4a1b813
8f64bcf
 
 
 
 
 
 
 
 
a5c279e
8f64bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
fe1e574
a5c279e
 
8f64bcf
 
 
a5c279e
 
8f64bcf
 
 
 
321795b
053edf5
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
import copy
import os
import random
import sys

import xxhash
import gradio as gr
import librosa
import numpy as np
import soundfile as sf
import torch
import torch.nn.functional as F
from accelerate import infer_auto_device_map
from datasets import Audio
from safetensors.torch import load, load_model
import spaces
from torch import nn
from transformers import (
    AutoModelForCausalLM,
    AutoProcessor,
    AutoTokenizer,
    LlamaForCausalLM,
    TextIteratorStreamer,
    WhisperForConditionalGeneration,
    AutoProcessor,
    AutoModel,
)
from transformers.generation import GenerationConfig

anonymous = False

diva_model = AutoModel.from_pretrained(
    "WillHeld/DiVA-llama-3-v0-8b", trust_remote_code=True
)

resampler = Audio(sampling_rate=16_000)


@spaces.GPU
@torch.no_grad
def diva_audio(audio_input, do_sample=False, temperature=0.001):
    sr, y = audio_input
    x = xxhash.xxh32(bytes(y)).hexdigest()
    y = y.astype(np.float32)
    y /= np.max(np.abs(y))
    a = resampler.decode_example(
        resampler.encode_example({"array": y, "sampling_rate": sr})
    )
    yield from diva_model.generate_stream(
        a["array"], None, do_sample=do_sample, max_new_tokens=256
    )


def transcribe_wrapper(audio_input, state, model_order):
    spinner = "◒"
    d_resp = gr.Textbox(
        value="♫♪.ılılıll|̲̅̅●̲̅̅|̲̅̅=̲̅̅|̲̅̅●̲̅̅|llılılı.♫♪loading♫♪.ılılıll|̲̅̅●̲̅̅|̲̅̅=̲̅̅|̲̅̅●̲̅̅|llılılı.♫♪loading♫♪.ılılıll|̲̅̅●̲̅̅|̲̅̅=̲̅̅|̲̅̅●̲̅̅|llılılı.♫♪♫♪",
        visible=True,
        label=model_names[0] if not anonymous else f"Model {order}",
    )
    yield (
        gr.Button(
            value="Loading Weights onto ZeroGPU..."
            interactive=False,
            variant="primary",
        ),
        d_resp,
        state,
    )

    yield from transcribe(audio_input, state, model_order)


@spaces.GPU
def transcribe(audio_input, state, model_order):
    if audio_input == None:
        return (
            "Click to run inference!",
            "",
            state,
        )

    def gen_from_diva():
        diva_resp = diva_audio(audio_input)
        for resp in diva_resp:
            d_resp = gr.Textbox(
                value=resp,
                visible=True,
                label=model_names[0] if not anonymous else f"Model {order}",
            )
            yield d_resp

    spinner_id = 0
    spinners = ["◐ ", "◓ ", "◑", "◒"]

    for response in gen_from_diva():
        spinner = spinners[spinner_id]
        spinner_id = (spinner_id + 1) % 4
        yield (
            gr.Button(
                value=spinner + " Generating Responses " + spinner,
                interactive=False,
                variant="primary",
            ),
            response,
            state,
        )
    yield (
        gr.Button(value="Click to run inference!", interactive=True, variant="primary"),
        response,
        state,
    )


def on_page_load(state, model_order):
    if state == 0:
        gr.Info(
            "Record something you'd say to an AI Assistant! Think about what you usually use Siri, Google Assistant, or ChatGPT for."
        )
        state = 1
        if anonymous:
            random.shuffle(model_order)
    return state, model_order


def recording_complete(state):
    if state == 1:
        gr.Info(
            "Once you submit your recording, DiVA will stream back a response! This might take a second as ZeroGPU needs to load model weights into vRAM!."
        )
        state = 2
    return (
        gr.Button(value="Click to run inference!", interactive=True, variant="primary"),
        state,
    )


def clear_factory(button_id):
    def clear(audio_input, model_order):
        return (
            model_order,
            gr.Button(
                value="Record Audio to Submit!",
                interactive=False,
            ),
            None,
            None,
        )

    return clear


theme = gr.themes.Soft(
    primary_hue=gr.themes.Color(
        c100="#82000019",
        c200="#82000033",
        c300="#8200004c",
        c400="#82000066",
        c50="#8200007f",
        c500="#8200007f",
        c600="#82000099",
        c700="#820000b2",
        c800="#820000cc",
        c900="#820000e5",
        c950="#820000f2",
    ),
    secondary_hue="rose",
    neutral_hue="stone",
)

model_names = ["DiVA Llama 3 8B"]
model_shorthand = ["diva"]
with gr.Blocks(theme=theme) as demo:
    state = gr.State(0)
    model_order = gr.State([0, 1])
    with gr.Row():
        audio_input = gr.Audio(
            sources=["microphone"], streaming=False, label="Audio Input"
        )

    with gr.Row():
        btn = gr.Button(value="Record Audio to Submit!", interactive=False)

    with gr.Row():
        out1 = gr.Textbox(visible=False)

    audio_input.stop_recording(
        recording_complete,
        [state],
        [btn, state],
    )
    audio_input.start_recording(
        lambda: gr.Button(
            value="Uploading Audio to Cloud", interactive=False, variant="primary"
        ),
        None,
        btn,
    )
    btn.click(
        fn=transcribe_wrapper,
        inputs=[audio_input, state, model_order],
        outputs=[btn, out1, state],
    )
    audio_input.clear(
        clear_factory(None),
        [audio_input, model_order],
        [model_order, btn, audio_input, out1],
    )
    demo.load(
        fn=on_page_load, inputs=[state, model_order], outputs=[state, model_order]
    )

demo.launch(share=True)