Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,180 Bytes
8f64bcf a5c279e 321795b 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf 321795b 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf 321795b a5c279e 8f64bcf 4a1b813 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf a5c279e 8f64bcf 321795b a5c279e |
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 |
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 models.salmonn import SALMONN
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)
@spaces.GPU
def transcribe(audio_input, state, model_order):
if audio_input == None:
return (
"",
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 = ["β ", "β ", "β", "β"]
initial_responses = [("")]
resp_generators = [
gen_from_diva(),
]
order = -1
resp_generators = [resp_generators[model_order[0]]]
for generator in [initial_responses, *resp_generators]:
order += 1
for resps in generator:
s_resp, q_resp = resps
resp_1 = resps[model_order[0]]
spinner = spinners[spinner_id]
spinner_id = (spinner_id + 1) % 4
yield (
gr.Button(
value=spinner + " Generating Responses " + spinner,
interactive=False,
variant="primary",
),
resp_1,
state,
)
yield (
gr.Button(
value="Click to compare models!", interactive=True, variant="primary"
),
resp_1,
resp_2,
responses_complete(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."
)
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,
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) |