Spaces:
Runtime error
Runtime error
Initial version
Browse files
app.py
CHANGED
|
@@ -1,62 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 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 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
from datasets import load_dataset, Audio
|
| 5 |
+
from transformers import EncodecModel, AutoProcessor
|
| 6 |
+
|
| 7 |
"""
|
| 8 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 9 |
"""
|
| 10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 11 |
|
| 12 |
+
# load a demonstration datasets
|
| 13 |
+
librispeech_dummy = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
| 14 |
|
| 15 |
+
# load the model + processor (for pre-processing the audio)
|
| 16 |
+
model = EncodecModel.from_pretrained("facebook/encodec_24khz")
|
| 17 |
+
processor = AutoProcessor.from_pretrained("facebook/encodec_24khz")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# cast the audio data to the correct sampling rate for the model
|
| 20 |
+
librispeech_dummy = librispeech_dummy.cast_column("audio", Audio(sampling_rate=processor.sampling_rate))
|
| 21 |
+
audio_sample = librispeech_dummy[0]["audio"]["array"]
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# pre-process the inputs
|
| 24 |
+
inputs = processor(raw_audio=audio_sample, sampling_rate=processor.sampling_rate, return_tensors="pt")
|
| 25 |
|
| 26 |
+
# explicitly encode then decode the audio inputs
|
| 27 |
+
encoder_outputs = model.encode(inputs["input_values"], inputs["padding_mask"])
|
| 28 |
+
audio_values = model.decode(encoder_outputs.audio_codes, encoder_outputs.audio_scales, inputs["padding_mask"])[0]
|
| 29 |
|
| 30 |
+
# or the equivalent with a forward pass
|
| 31 |
+
audio_values = model(inputs["input_values"], inputs["padding_mask"]).audio_values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# def respond(
|
| 34 |
+
# message,
|
| 35 |
+
# history: list[tuple[str, str]],
|
| 36 |
+
# system_message,
|
| 37 |
+
# max_tokens,
|
| 38 |
+
# temperature,
|
| 39 |
+
# top_p,
|
| 40 |
+
# ):
|
| 41 |
+
# messages = [{"role": "system", "content": system_message}]
|
| 42 |
|
| 43 |
+
# for val in history:
|
| 44 |
+
# if val[0]:
|
| 45 |
+
# messages.append({"role": "user", "content": val[0]})
|
| 46 |
+
# if val[1]:
|
| 47 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
| 48 |
+
|
| 49 |
+
# messages.append({"role": "user", "content": message})
|
| 50 |
+
|
| 51 |
+
# response = ""
|
| 52 |
+
|
| 53 |
+
# for message in client.chat_completion(
|
| 54 |
+
# messages,
|
| 55 |
+
# max_tokens=max_tokens,
|
| 56 |
+
# stream=True,
|
| 57 |
+
# temperature=temperature,
|
| 58 |
+
# top_p=top_p,
|
| 59 |
+
# ):
|
| 60 |
+
# token = message.choices[0].delta.content
|
| 61 |
+
|
| 62 |
+
# response += token
|
| 63 |
+
# yield response
|
| 64 |
+
|
| 65 |
+
# """
|
| 66 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 67 |
+
# """
|
| 68 |
+
# demo = gr.ChatInterface(
|
| 69 |
+
# respond,
|
| 70 |
+
# additional_inputs=[
|
| 71 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 72 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 73 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 74 |
+
# gr.Slider(
|
| 75 |
+
# minimum=0.1,
|
| 76 |
+
# maximum=1.0,
|
| 77 |
+
# value=0.95,
|
| 78 |
+
# step=0.05,
|
| 79 |
+
# label="Top-p (nucleus sampling)",
|
| 80 |
+
# ),
|
| 81 |
+
# ],
|
| 82 |
+
# )
|
| 83 |
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|