Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,131 @@ import tempfile
|
|
8 |
import os
|
9 |
import uuid
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
SAMPLE_RATE = 16000
|
12 |
model = nemo_asr.models.EncDecRNNTBPEModel.from_pretrained("nvidia/stt_en_conformer_transducer_xlarge")
|
13 |
model.change_decoding_strategy(None)
|
@@ -39,6 +164,7 @@ def transcribe(audio, state=""):
|
|
39 |
transcriptions = transcriptions[0]
|
40 |
transcriptions = transcriptions[0]
|
41 |
state = state + transcriptions + " "
|
|
|
42 |
return state, state
|
43 |
|
44 |
iface = gr.Interface(
|
@@ -53,9 +179,10 @@ iface = gr.Interface(
|
|
53 |
],
|
54 |
layout="horizontal",
|
55 |
theme="huggingface",
|
56 |
-
title="
|
57 |
description="Demo for English speech recognition using Conformer Transducers",
|
58 |
allow_flagging='never',
|
59 |
-
live=True,
|
|
|
60 |
)
|
61 |
iface.launch()
|
|
|
8 |
import os
|
9 |
import uuid
|
10 |
|
11 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
12 |
+
import torch
|
13 |
+
|
14 |
+
# PersistDataset -----
|
15 |
+
import os
|
16 |
+
import csv
|
17 |
+
import gradio as gr
|
18 |
+
from gradio import inputs, outputs
|
19 |
+
import huggingface_hub
|
20 |
+
from huggingface_hub import Repository, hf_hub_download, upload_file
|
21 |
+
from datetime import datetime
|
22 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/Carddata.csv"
|
23 |
+
DATASET_REPO_ID = "awacke1/Carddata.csv"
|
24 |
+
DATA_FILENAME = "Carddata.csv"
|
25 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
26 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
27 |
+
|
28 |
+
SCRIPT = """
|
29 |
+
<script>
|
30 |
+
if (!window.hasBeenRun) {
|
31 |
+
window.hasBeenRun = true;
|
32 |
+
console.log("should only happen once");
|
33 |
+
document.querySelector("button.submit").click();
|
34 |
+
}
|
35 |
+
</script>
|
36 |
+
"""
|
37 |
+
|
38 |
+
|
39 |
+
try:
|
40 |
+
hf_hub_download(
|
41 |
+
repo_id=DATASET_REPO_ID,
|
42 |
+
filename=DATA_FILENAME,
|
43 |
+
cache_dir=DATA_DIRNAME,
|
44 |
+
force_filename=DATA_FILENAME
|
45 |
+
)
|
46 |
+
except:
|
47 |
+
print("file not found")
|
48 |
+
repo = Repository(
|
49 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
50 |
+
)
|
51 |
+
|
52 |
+
def generate_html() -> str:
|
53 |
+
with open(DATA_FILE) as csvfile:
|
54 |
+
reader = csv.DictReader(csvfile)
|
55 |
+
rows = []
|
56 |
+
for row in reader:
|
57 |
+
rows.append(row)
|
58 |
+
rows.reverse()
|
59 |
+
if len(rows) == 0:
|
60 |
+
return "no messages yet"
|
61 |
+
else:
|
62 |
+
html = "<div class='chatbot'>"
|
63 |
+
for row in rows:
|
64 |
+
html += "<div>"
|
65 |
+
html += f"<span>{row['inputs']}</span>"
|
66 |
+
html += f"<span class='outputs'>{row['outputs']}</span>"
|
67 |
+
html += "</div>"
|
68 |
+
html += "</div>"
|
69 |
+
return html
|
70 |
+
|
71 |
+
|
72 |
+
def store_message(name: str, message: str):
|
73 |
+
if name and message:
|
74 |
+
with open(DATA_FILE, "a") as csvfile:
|
75 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
76 |
+
writer.writerow(
|
77 |
+
{"name": name.strip(), "message": message.strip(), "time": str(datetime.now())}
|
78 |
+
)
|
79 |
+
commit_url = repo.push_to_hub()
|
80 |
+
return ""
|
81 |
+
|
82 |
+
|
83 |
+
iface = gr.Interface(
|
84 |
+
store_message,
|
85 |
+
[
|
86 |
+
inputs.Textbox(placeholder="Your name"),
|
87 |
+
inputs.Textbox(placeholder="Your message", lines=2),
|
88 |
+
],
|
89 |
+
"html",
|
90 |
+
css="""
|
91 |
+
.message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
92 |
+
""",
|
93 |
+
title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
94 |
+
description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
95 |
+
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
|
96 |
+
)
|
97 |
+
|
98 |
+
|
99 |
+
mname = "facebook/blenderbot-400M-distill"
|
100 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
|
101 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(mname)
|
102 |
+
|
103 |
+
def take_last_tokens(inputs, note_history, history):
|
104 |
+
"""Filter the last 128 tokens"""
|
105 |
+
if inputs['input_ids'].shape[1] > 128:
|
106 |
+
inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-128:].tolist()])
|
107 |
+
inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-128:].tolist()])
|
108 |
+
note_history = ['</s> <s>'.join(note_history[0].split('</s> <s>')[2:])]
|
109 |
+
history = history[1:]
|
110 |
+
return inputs, note_history, history
|
111 |
+
|
112 |
+
def add_note_to_history(note, note_history):
|
113 |
+
"""Add a note to the historical information"""
|
114 |
+
note_history.append(note)
|
115 |
+
note_history = '</s> <s>'.join(note_history)
|
116 |
+
return [note_history]
|
117 |
+
|
118 |
+
|
119 |
+
def chat(message, history):
|
120 |
+
history = history or []
|
121 |
+
if history:
|
122 |
+
history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
|
123 |
+
else:
|
124 |
+
history_useful = []
|
125 |
+
history_useful = add_note_to_history(message, history_useful)
|
126 |
+
inputs = tokenizer(history_useful, return_tensors="pt")
|
127 |
+
inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
|
128 |
+
reply_ids = model.generate(**inputs)
|
129 |
+
response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
|
130 |
+
history_useful = add_note_to_history(response, history_useful)
|
131 |
+
list_history = history_useful[0].split('</s> <s>')
|
132 |
+
history.append((list_history[-2], list_history[-1]))
|
133 |
+
store_message(message, response) # Save to dataset
|
134 |
+
return history, history
|
135 |
+
|
136 |
SAMPLE_RATE = 16000
|
137 |
model = nemo_asr.models.EncDecRNNTBPEModel.from_pretrained("nvidia/stt_en_conformer_transducer_xlarge")
|
138 |
model.change_decoding_strategy(None)
|
|
|
164 |
transcriptions = transcriptions[0]
|
165 |
transcriptions = transcriptions[0]
|
166 |
state = state + transcriptions + " "
|
167 |
+
store_message(state, state) # Save to dataset
|
168 |
return state, state
|
169 |
|
170 |
iface = gr.Interface(
|
|
|
179 |
],
|
180 |
layout="horizontal",
|
181 |
theme="huggingface",
|
182 |
+
title="ASR Streaming Conformer Transducer Large - English",
|
183 |
description="Demo for English speech recognition using Conformer Transducers",
|
184 |
allow_flagging='never',
|
185 |
+
live=True,
|
186 |
+
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})"
|
187 |
)
|
188 |
iface.launch()
|