Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,31 @@
|
|
1 |
import streamlit as st
|
2 |
import firebase_admin
|
3 |
-
from firebase_admin import credentials
|
4 |
-
from firebase_admin import firestore
|
5 |
import datetime
|
6 |
-
from transformers import pipeline
|
7 |
import gradio as gr
|
8 |
-
|
9 |
import tempfile
|
|
|
|
|
|
|
|
|
10 |
from typing import Optional
|
11 |
-
import numpy as np
|
12 |
from TTS.utils.manage import ModelManager
|
13 |
from TTS.utils.synthesizer import Synthesizer
|
|
|
|
|
|
|
14 |
|
|
|
15 |
@st.experimental_singleton
|
16 |
def get_db_firestore():
|
17 |
cred = credentials.Certificate('test.json')
|
18 |
firebase_admin.initialize_app(cred, {'projectId': u'clinical-nlp-b9117',})
|
19 |
db = firestore.client()
|
20 |
return db
|
21 |
-
|
22 |
db = get_db_firestore()
|
23 |
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
24 |
|
|
|
25 |
MODEL_NAMES = [
|
26 |
"en/ljspeech/tacotron2-DDC",
|
27 |
"en/ljspeech/glow-tts",
|
@@ -47,20 +51,37 @@ for MODEL_NAME in MODEL_NAMES:
|
|
47 |
)
|
48 |
MODELS[MODEL_NAME] = synthesizer
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def transcribe(audio):
|
51 |
text = asr(audio)["text"]
|
52 |
return text
|
53 |
|
|
|
54 |
classifier = pipeline("text-classification")
|
55 |
|
|
|
56 |
def speech_to_text(speech):
|
57 |
text = asr(speech)["text"]
|
58 |
return text
|
59 |
-
|
|
|
60 |
def text_to_sentiment(text):
|
61 |
sentiment = classifier(text)[0]["label"]
|
62 |
return sentiment
|
63 |
|
|
|
64 |
def upsert(text):
|
65 |
date_time =str(datetime.datetime.today())
|
66 |
doc_ref = db.collection('Text2SpeechSentimentSave').document(date_time)
|
@@ -69,13 +90,15 @@ def upsert(text):
|
|
69 |
# check it here: https://console.firebase.google.com/u/0/project/clinical-nlp-b9117/firestore/data/~2FStreamlitSpaces
|
70 |
return saved
|
71 |
|
|
|
72 |
def select(collection, document):
|
73 |
doc_ref = db.collection(collection).document(document)
|
74 |
doc = doc_ref.get()
|
75 |
docid = ("The id is: ", doc.id)
|
76 |
contents = ("The contents are: ", doc.to_dict())
|
77 |
return contents
|
78 |
-
|
|
|
79 |
def selectall(text):
|
80 |
docs = db.collection('Text2SpeechSentimentSave').stream()
|
81 |
doclist=''
|
@@ -83,7 +106,8 @@ def selectall(text):
|
|
83 |
r=(f'{doc.id} => {doc.to_dict()}')
|
84 |
doclist += r
|
85 |
return doclist
|
86 |
-
|
|
|
87 |
def tts(text: str, model_name: str):
|
88 |
print(text, model_name)
|
89 |
synthesizer = MODELS.get(model_name, None)
|
@@ -94,26 +118,57 @@ def tts(text: str, model_name: str):
|
|
94 |
synthesizer.save_wav(wavs, fp)
|
95 |
return fp.name
|
96 |
|
|
|
|
|
|
|
|
|
|
|
97 |
demo = gr.Blocks()
|
98 |
with demo:
|
|
|
99 |
audio_file = gr.inputs.Audio(source="microphone", type="filepath")
|
100 |
text = gr.Textbox()
|
101 |
label = gr.Label()
|
102 |
saved = gr.Textbox()
|
103 |
savedAll = gr.Textbox()
|
104 |
TTSchoice = gr.inputs.Radio( label="Pick a TTS Model", choices=MODEL_NAMES, )
|
|
|
105 |
audio = gr.Audio(label="Output", interactive=False)
|
106 |
|
|
|
|
|
107 |
b1 = gr.Button("Recognize Speech")
|
108 |
b2 = gr.Button("Classify Sentiment")
|
109 |
b3 = gr.Button("Save Speech to Text")
|
110 |
b4 = gr.Button("Retrieve All")
|
111 |
b5 = gr.Button("Read It Back Aloud")
|
|
|
112 |
|
|
|
113 |
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
114 |
b2.click(text_to_sentiment, inputs=text, outputs=label)
|
115 |
b3.click(upsert, inputs=text, outputs=saved)
|
116 |
b4.click(selectall, inputs=text, outputs=savedAll)
|
117 |
b5.click(tts, inputs=[text,TTSchoice], outputs=audio)
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import firebase_admin
|
|
|
|
|
3 |
import datetime
|
|
|
4 |
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
import tempfile
|
7 |
+
|
8 |
+
from firebase_admin import credentials
|
9 |
+
from firebase_admin import firestore
|
10 |
+
from transformers import pipeline
|
11 |
from typing import Optional
|
|
|
12 |
from TTS.utils.manage import ModelManager
|
13 |
from TTS.utils.synthesizer import Synthesizer
|
14 |
+
from gradio import inputs
|
15 |
+
from gradio.inputs import Textbox
|
16 |
+
from gradio import outputs
|
17 |
|
18 |
+
#Persistence via Cloud Store
|
19 |
@st.experimental_singleton
|
20 |
def get_db_firestore():
|
21 |
cred = credentials.Certificate('test.json')
|
22 |
firebase_admin.initialize_app(cred, {'projectId': u'clinical-nlp-b9117',})
|
23 |
db = firestore.client()
|
24 |
return db
|
|
|
25 |
db = get_db_firestore()
|
26 |
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
27 |
|
28 |
+
#STT Models
|
29 |
MODEL_NAMES = [
|
30 |
"en/ljspeech/tacotron2-DDC",
|
31 |
"en/ljspeech/glow-tts",
|
|
|
51 |
)
|
52 |
MODELS[MODEL_NAME] = synthesizer
|
53 |
|
54 |
+
# Generators
|
55 |
+
#generator2 = gr.Interface.load("huggingface/EleutherAI/gpt-neo-2.7B")
|
56 |
+
#generator3 = gr.Interface.load("huggingface/EleutherAI/gpt-j-6B")
|
57 |
+
#generator1 = gr.Interface.load("huggingface/gpt2-large")
|
58 |
+
|
59 |
+
|
60 |
+
GEN_NAMES = [
|
61 |
+
"huggingface/EleutherAI/gpt-neo-2.7B",
|
62 |
+
"huggingface/EleutherAI/gpt-j-6B",
|
63 |
+
"huggingface/gpt2-large",
|
64 |
+
]
|
65 |
+
|
66 |
+
#ASR
|
67 |
def transcribe(audio):
|
68 |
text = asr(audio)["text"]
|
69 |
return text
|
70 |
|
71 |
+
#Sentiment Classifier
|
72 |
classifier = pipeline("text-classification")
|
73 |
|
74 |
+
#STT
|
75 |
def speech_to_text(speech):
|
76 |
text = asr(speech)["text"]
|
77 |
return text
|
78 |
+
|
79 |
+
#TTSentiment
|
80 |
def text_to_sentiment(text):
|
81 |
sentiment = classifier(text)[0]["label"]
|
82 |
return sentiment
|
83 |
|
84 |
+
#Save
|
85 |
def upsert(text):
|
86 |
date_time =str(datetime.datetime.today())
|
87 |
doc_ref = db.collection('Text2SpeechSentimentSave').document(date_time)
|
|
|
90 |
# check it here: https://console.firebase.google.com/u/0/project/clinical-nlp-b9117/firestore/data/~2FStreamlitSpaces
|
91 |
return saved
|
92 |
|
93 |
+
#OpenLast
|
94 |
def select(collection, document):
|
95 |
doc_ref = db.collection(collection).document(document)
|
96 |
doc = doc_ref.get()
|
97 |
docid = ("The id is: ", doc.id)
|
98 |
contents = ("The contents are: ", doc.to_dict())
|
99 |
return contents
|
100 |
+
|
101 |
+
#OpenAll
|
102 |
def selectall(text):
|
103 |
docs = db.collection('Text2SpeechSentimentSave').stream()
|
104 |
doclist=''
|
|
|
106 |
r=(f'{doc.id} => {doc.to_dict()}')
|
107 |
doclist += r
|
108 |
return doclist
|
109 |
+
|
110 |
+
#TTS
|
111 |
def tts(text: str, model_name: str):
|
112 |
print(text, model_name)
|
113 |
synthesizer = MODELS.get(model_name, None)
|
|
|
118 |
synthesizer.save_wav(wavs, fp)
|
119 |
return fp.name
|
120 |
|
121 |
+
#Stories
|
122 |
+
def Story(text: input):
|
123 |
+
return
|
124 |
+
|
125 |
+
#Blocks Rock It
|
126 |
demo = gr.Blocks()
|
127 |
with demo:
|
128 |
+
#UI
|
129 |
audio_file = gr.inputs.Audio(source="microphone", type="filepath")
|
130 |
text = gr.Textbox()
|
131 |
label = gr.Label()
|
132 |
saved = gr.Textbox()
|
133 |
savedAll = gr.Textbox()
|
134 |
TTSchoice = gr.inputs.Radio( label="Pick a TTS Model", choices=MODEL_NAMES, )
|
135 |
+
Storychoice = gr.inputs.Radio( label="Pick a Story Generator", choices=GEN_NAMES, )
|
136 |
audio = gr.Audio(label="Output", interactive=False)
|
137 |
|
138 |
+
|
139 |
+
#Buttons
|
140 |
b1 = gr.Button("Recognize Speech")
|
141 |
b2 = gr.Button("Classify Sentiment")
|
142 |
b3 = gr.Button("Save Speech to Text")
|
143 |
b4 = gr.Button("Retrieve All")
|
144 |
b5 = gr.Button("Read It Back Aloud")
|
145 |
+
b6 = gr.Button("Generate a Story")
|
146 |
|
147 |
+
#Event Model Chains
|
148 |
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
149 |
b2.click(text_to_sentiment, inputs=text, outputs=label)
|
150 |
b3.click(upsert, inputs=text, outputs=saved)
|
151 |
b4.click(selectall, inputs=text, outputs=savedAll)
|
152 |
b5.click(tts, inputs=[text,TTSchoice], outputs=audio)
|
153 |
+
b6.click(tts, inputs=[text,Storychoice], outputs=text)
|
154 |
+
|
155 |
+
# Lets Do It
|
156 |
+
demo.launch(share=True)
|
157 |
+
|
158 |
+
title = "Story Generators"
|
159 |
+
|
160 |
+
examples = [
|
161 |
+
["At which point do we invent Love?"],
|
162 |
+
["Love is a capacity more than consciousness is universal."],
|
163 |
+
["See the grace of god in eachother."],
|
164 |
+
["Love is a capacity more than consciousness is universal."],
|
165 |
+
["Love is generativity when there is more energy than what they need for equilibrium."],
|
166 |
+
["Collections of people have agency and mass having agency at the mesoscopic level"],
|
167 |
+
["Having a deep human connection is an interface problem to solve."],
|
168 |
+
["Having a collective creates agency since we build trust in eachother."]
|
169 |
+
]
|
170 |
+
|
171 |
+
#gr.Parallel(generator1, generator2, generator3, inputs=gr.inputs.Textbox(lines=5, label="Enter a sentence to get another sentence."),
|
172 |
+
# title=title, examples=examples).launch(share=False)
|
173 |
+
|
174 |
+
|