Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import datetime
|
3 |
+
from transformers import pipeline
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
import tempfile
|
7 |
+
from typing import Optional
|
8 |
+
import numpy as np
|
9 |
+
from TTS.utils.manage import ModelManager
|
10 |
+
from TTS.utils.synthesizer import Synthesizer
|
11 |
+
|
12 |
+
# PersistDataset -----
|
13 |
+
import os
|
14 |
+
import csv
|
15 |
+
import gradio as gr
|
16 |
+
from gradio import inputs, outputs
|
17 |
+
import huggingface_hub
|
18 |
+
from huggingface_hub import Repository, hf_hub_download, upload_file
|
19 |
+
from datetime import datetime
|
20 |
+
|
21 |
+
# created new dataset as awacke1/MindfulStory.csv
|
22 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/MindfulStory.csv"
|
23 |
+
DATASET_REPO_ID = "awacke1/MindfulStory.csv"
|
24 |
+
DATA_FILENAME = "MindfulStory.csv"
|
25 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
26 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
27 |
+
|
28 |
+
# Download dataset repo using hub download
|
29 |
+
try:
|
30 |
+
hf_hub_download(
|
31 |
+
repo_id=DATASET_REPO_ID,
|
32 |
+
filename=DATA_FILENAME,
|
33 |
+
cache_dir=DATA_DIRNAME,
|
34 |
+
force_filename=DATA_FILENAME
|
35 |
+
)
|
36 |
+
except:
|
37 |
+
print("file not found")
|
38 |
+
|
39 |
+
def AIMemory(name: str, message: str):
|
40 |
+
if name and message:
|
41 |
+
with open(DATA_FILE, "a") as csvfile:
|
42 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
43 |
+
writer.writerow({"name": name, "message": message, "time": str(datetime.now())})
|
44 |
+
commit_url = repo.push_to_hub()
|
45 |
+
return {"name": name, "message": message, "time": str(datetime.now())}
|
46 |
+
|
47 |
+
with open('Mindfulness.txt', 'r') as file:
|
48 |
+
context = file.read()
|
49 |
+
|
50 |
+
# Set up cloned dataset from repo for operations
|
51 |
+
repo = Repository( local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN)
|
52 |
+
|
53 |
+
# set up ASR
|
54 |
+
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
55 |
+
|
56 |
+
# set up TTS
|
57 |
+
MODEL_NAMES = [
|
58 |
+
"en/ljspeech/tacotron2-DDC",
|
59 |
+
"en/ljspeech/glow-tts",
|
60 |
+
"en/ljspeech/speedy-speech-wn",
|
61 |
+
"en/ljspeech/vits",
|
62 |
+
"en/sam/tacotron-DDC",
|
63 |
+
"fr/mai/tacotron2-DDC",
|
64 |
+
"de/thorsten/tacotron2-DCA",
|
65 |
+
]
|
66 |
+
|
67 |
+
# Use Model Manager to load vocoders
|
68 |
+
MODELS = {}
|
69 |
+
manager = ModelManager()
|
70 |
+
for MODEL_NAME in MODEL_NAMES:
|
71 |
+
print(f"downloading {MODEL_NAME}")
|
72 |
+
model_path, config_path, model_item = manager.download_model(f"tts_models/{MODEL_NAME}")
|
73 |
+
vocoder_name: Optional[str] = model_item["default_vocoder"]
|
74 |
+
vocoder_path = None
|
75 |
+
vocoder_config_path = None
|
76 |
+
if vocoder_name is not None:
|
77 |
+
vocoder_path, vocoder_config_path, _ = manager.download_model(vocoder_name)
|
78 |
+
|
79 |
+
synthesizer = Synthesizer(
|
80 |
+
model_path, config_path, None, vocoder_path, vocoder_config_path,
|
81 |
+
)
|
82 |
+
MODELS[MODEL_NAME] = synthesizer
|
83 |
+
|
84 |
+
# transcribe
|
85 |
+
def transcribe(audio):
|
86 |
+
text = asr(audio)["text"]
|
87 |
+
return text
|
88 |
+
|
89 |
+
#text classifier
|
90 |
+
classifier = pipeline("text-classification")
|
91 |
+
|
92 |
+
|
93 |
+
def speech_to_text(speech):
|
94 |
+
text = asr(speech)["text"]
|
95 |
+
#rMem = AIMemory("STT", text)
|
96 |
+
return text
|
97 |
+
|
98 |
+
def text_to_sentiment(text):
|
99 |
+
sentiment = classifier(text)[0]["label"]
|
100 |
+
#rMem = AIMemory(text, sentiment)
|
101 |
+
return sentiment
|
102 |
+
|
103 |
+
def upsert(text):
|
104 |
+
date_time =str(datetime.datetime.today())
|
105 |
+
doc_ref = db.collection('Text2SpeechSentimentSave').document(date_time)
|
106 |
+
doc_ref.set({u'firefield': 'Recognize Speech', u'first': 'https://huggingface.co/spaces/awacke1/TTS-STT-Blocks/', u'last': text, u'born': date_time,})
|
107 |
+
saved = select('TTS-STT', date_time)
|
108 |
+
return saved
|
109 |
+
|
110 |
+
def select(collection, document):
|
111 |
+
doc_ref = db.collection(collection).document(document)
|
112 |
+
doc = doc_ref.get()
|
113 |
+
docid = ("The id is: ", doc.id)
|
114 |
+
contents = ("The contents are: ", doc.to_dict())
|
115 |
+
return contents
|
116 |
+
|
117 |
+
def selectall(text):
|
118 |
+
docs = db.collection('Text2SpeechSentimentSave').stream()
|
119 |
+
doclist=''
|
120 |
+
for doc in docs:
|
121 |
+
r=(f'{doc.id} => {doc.to_dict()}')
|
122 |
+
doclist += r
|
123 |
+
return doclist
|
124 |
+
|
125 |
+
def tts(text: str, model_name: str):
|
126 |
+
print(text, model_name)
|
127 |
+
synthesizer = MODELS.get(model_name, None)
|
128 |
+
if synthesizer is None:
|
129 |
+
raise NameError("model not found")
|
130 |
+
wavs = synthesizer.tts(text)
|
131 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
|
132 |
+
synthesizer.save_wav(wavs, fp)
|
133 |
+
|
134 |
+
#rMem = AIMemory("TTS", text + model_name)
|
135 |
+
|
136 |
+
return fp.name
|
137 |
+
|
138 |
+
demo = gr.Blocks()
|
139 |
+
with demo:
|
140 |
+
audio_file = gr.inputs.Audio(source="microphone", type="filepath")
|
141 |
+
text = gr.Textbox(label="Speech to Text")
|
142 |
+
#label = gr.Label()
|
143 |
+
#saved = gr.Textbox(label="Saved")
|
144 |
+
#savedAll = gr.Textbox(label="SavedAll")
|
145 |
+
TTSchoice = gr.inputs.Radio( label="Pick a Text to Speech Model", choices=MODEL_NAMES, )
|
146 |
+
audio = gr.Audio(label="Output", interactive=False)
|
147 |
+
|
148 |
+
b1 = gr.Button("Recognize Speech")
|
149 |
+
#b2 = gr.Button("Classify Sentiment")
|
150 |
+
#b3 = gr.Button("Save Speech to Text")
|
151 |
+
#b4 = gr.Button("Retrieve All")
|
152 |
+
b5 = gr.Button("Read It Back Aloud")
|
153 |
+
|
154 |
+
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
155 |
+
#b2.click(text_to_sentiment, inputs=text, outputs=label)
|
156 |
+
#b3.click(upsert, inputs=text, outputs=saved)
|
157 |
+
#b4.click(selectall, inputs=text, outputs=savedAll)
|
158 |
+
b5.click(tts, inputs=[text,TTSchoice], outputs=audio)
|
159 |
+
|
160 |
+
demo.launch(share=True)
|