Spaces:
Runtime error
Runtime error
save
Browse files- app.py +224 -183
- requirements.txt +4 -1
app.py
CHANGED
@@ -2,201 +2,242 @@ import os
|
|
2 |
os.system("pip install git+https://github.com/openai/whisper.git")
|
3 |
import gradio as gr
|
4 |
import whisper
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
from share_btn import community_icon_html, loading_icon_html, share_js
|
7 |
|
8 |
-
model = whisper.load_model("small")
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
def inference(audio):
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
css = """
|
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 |
-
block = gr.Blocks(css=css)
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
with block:
|
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 |
-
|
202 |
-
block.launch()
|
|
|
2 |
os.system("pip install git+https://github.com/openai/whisper.git")
|
3 |
import gradio as gr
|
4 |
import whisper
|
5 |
+
from flask import Flask, jsonify, request
|
6 |
+
import requests
|
7 |
+
import numpy as np
|
8 |
+
import streamlit as st
|
9 |
|
|
|
10 |
|
|
|
11 |
|
12 |
|
13 |
+
app = Flask(__name__)
|
14 |
+
|
15 |
+
@app.route("/")
|
16 |
+
def indexApi():
|
17 |
+
return jsonify({"output": "okay"})
|
18 |
+
|
19 |
+
@app.route("/run")
|
20 |
+
def runApi():
|
21 |
+
audio_url = request.form.get("audio_url")
|
22 |
+
# key = request.form.get("key")
|
23 |
+
# model = request.form.get("model")
|
24 |
+
|
25 |
+
if (modelSelection == None):
|
26 |
+
modelSelection = "small"
|
27 |
+
model = whisper.load_model(modelSelection)
|
28 |
+
|
29 |
+
# # reject if key not the same
|
30 |
+
# apiKey = st.secrets["Api-Key"]
|
31 |
+
# if apiKey != key:
|
32 |
+
# return jsonify({
|
33 |
+
# "image_url": image_url,
|
34 |
+
# "model": model,
|
35 |
+
# "result": "Invalid API Key",
|
36 |
+
# }), 400
|
37 |
+
|
38 |
+
|
39 |
+
# response = requests.get(audio_url)
|
40 |
+
|
41 |
+
# arr = np.asarray(bytearray(response.content), dtype=np.uint8)
|
42 |
+
result = model.transcribe(audio_url)
|
43 |
+
|
44 |
+
return jsonify({
|
45 |
+
"audio_url": audio_url,
|
46 |
+
"model": model,
|
47 |
+
"result": result["text"],
|
48 |
+
})
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
app.run(host="0.0.0.0", port=7860)
|
52 |
|
53 |
+
# def inference(audio):
|
54 |
+
# audio = whisper.load_audio(audio)
|
55 |
+
# audio = whisper.pad_or_trim(audio)
|
56 |
|
57 |
+
# mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
58 |
|
59 |
+
# _, probs = model.detect_language(mel)
|
60 |
|
61 |
+
# options = whisper.DecodingOptions(fp16 = False)
|
62 |
+
# result = whisper.decode(model, mel, options)
|
63 |
|
64 |
+
# # print(result.text)
|
65 |
+
# return result.text, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
# css = """
|
71 |
+
# .gradio-container {
|
72 |
+
# font-family: 'IBM Plex Sans', sans-serif;
|
73 |
+
# }
|
74 |
+
# .gr-button {
|
75 |
+
# color: white;
|
76 |
+
# border-color: black;
|
77 |
+
# background: black;
|
78 |
+
# }
|
79 |
+
# input[type='range'] {
|
80 |
+
# accent-color: black;
|
81 |
+
# }
|
82 |
+
# .dark input[type='range'] {
|
83 |
+
# accent-color: #dfdfdf;
|
84 |
+
# }
|
85 |
+
# .container {
|
86 |
+
# max-width: 730px;
|
87 |
+
# margin: auto;
|
88 |
+
# padding-top: 1.5rem;
|
89 |
+
# }
|
90 |
|
91 |
+
# .details:hover {
|
92 |
+
# text-decoration: underline;
|
93 |
+
# }
|
94 |
+
# .gr-button {
|
95 |
+
# white-space: nowrap;
|
96 |
+
# }
|
97 |
+
# .gr-button:focus {
|
98 |
+
# border-color: rgb(147 197 253 / var(--tw-border-opacity));
|
99 |
+
# outline: none;
|
100 |
+
# box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000);
|
101 |
+
# --tw-border-opacity: 1;
|
102 |
+
# --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);
|
103 |
+
# --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(3px var(--tw-ring-offset-width)) var(--tw-ring-color);
|
104 |
+
# --tw-ring-color: rgb(191 219 254 / var(--tw-ring-opacity));
|
105 |
+
# --tw-ring-opacity: .5;
|
106 |
+
# }
|
107 |
+
# .footer {
|
108 |
+
# margin-bottom: 45px;
|
109 |
+
# margin-top: 35px;
|
110 |
+
# text-align: center;
|
111 |
+
# border-bottom: 1px solid #e5e5e5;
|
112 |
+
# }
|
113 |
+
# .footer>p {
|
114 |
+
# font-size: .8rem;
|
115 |
+
# display: inline-block;
|
116 |
+
# padding: 0 10px;
|
117 |
+
# transform: translateY(10px);
|
118 |
+
# background: white;
|
119 |
+
# }
|
120 |
+
# .dark .footer {
|
121 |
+
# border-color: #303030;
|
122 |
+
# }
|
123 |
+
# .dark .footer>p {
|
124 |
+
# background: #0b0f19;
|
125 |
+
# }
|
126 |
+
# .prompt h4{
|
127 |
+
# margin: 1.25em 0 .25em 0;
|
128 |
+
# font-weight: bold;
|
129 |
+
# font-size: 115%;
|
130 |
+
# }
|
131 |
+
# .animate-spin {
|
132 |
+
# animation: spin 1s linear infinite;
|
133 |
+
# }
|
134 |
+
# @keyframes spin {
|
135 |
+
# from {
|
136 |
+
# transform: rotate(0deg);
|
137 |
+
# }
|
138 |
+
# to {
|
139 |
+
# transform: rotate(360deg);
|
140 |
+
# }
|
141 |
+
# }
|
142 |
+
# #share-btn-container {
|
143 |
+
# display: flex; margin-top: 1.5rem !important; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
|
144 |
+
# }
|
145 |
+
# #share-btn {
|
146 |
+
# all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
|
147 |
+
# }
|
148 |
+
# #share-btn * {
|
149 |
+
# all: unset;
|
150 |
+
# }
|
151 |
+
# """
|
152 |
+
|
153 |
+
# block = gr.Blocks(css=css)
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
# with block:
|
158 |
+
# gr.HTML(
|
159 |
+
# """
|
160 |
+
# <div style="text-align: center; max-width: 650px; margin: 0 auto;">
|
161 |
+
# <div
|
162 |
+
# style="
|
163 |
+
# display: inline-flex;
|
164 |
+
# align-items: center;
|
165 |
+
# gap: 0.8rem;
|
166 |
+
# font-size: 1.75rem;
|
167 |
+
# "
|
168 |
+
# >
|
169 |
+
# <svg
|
170 |
+
# width="0.65em"
|
171 |
+
# height="0.65em"
|
172 |
+
# viewBox="0 0 115 115"
|
173 |
+
# fill="none"
|
174 |
+
# xmlns="http://www.w3.org/2000/svg"
|
175 |
+
# >
|
176 |
+
# <rect width="23" height="23" fill="white"></rect>
|
177 |
+
# <rect y="69" width="23" height="23" fill="white"></rect>
|
178 |
+
# <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
|
179 |
+
# <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
180 |
+
# <rect x="46" width="23" height="23" fill="white"></rect>
|
181 |
+
# <rect x="46" y="69" width="23" height="23" fill="white"></rect>
|
182 |
+
# <rect x="69" width="23" height="23" fill="black"></rect>
|
183 |
+
# <rect x="69" y="69" width="23" height="23" fill="black"></rect>
|
184 |
+
# <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
|
185 |
+
# <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
|
186 |
+
# <rect x="115" y="46" width="23" height="23" fill="white"></rect>
|
187 |
+
# <rect x="115" y="115" width="23" height="23" fill="white"></rect>
|
188 |
+
# <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
189 |
+
# <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
|
190 |
+
# <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
191 |
+
# <rect x="92" y="69" width="23" height="23" fill="white"></rect>
|
192 |
+
# <rect x="69" y="46" width="23" height="23" fill="white"></rect>
|
193 |
+
# <rect x="69" y="115" width="23" height="23" fill="white"></rect>
|
194 |
+
# <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
|
195 |
+
# <rect x="46" y="46" width="23" height="23" fill="black"></rect>
|
196 |
+
# <rect x="46" y="115" width="23" height="23" fill="black"></rect>
|
197 |
+
# <rect x="46" y="69" width="23" height="23" fill="black"></rect>
|
198 |
+
# <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
|
199 |
+
# <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
|
200 |
+
# <rect x="23" y="69" width="23" height="23" fill="black"></rect>
|
201 |
+
# </svg>
|
202 |
+
# <h1 style="font-weight: 900; margin-bottom: 7px;">
|
203 |
+
# Whisper
|
204 |
+
# </h1>
|
205 |
+
# </div>
|
206 |
+
# <p style="margin-bottom: 10px; font-size: 94%">
|
207 |
+
# Whisper is a general-purpose speech recognition model. It is trained on a large dataset of diverse audio and is also a multi-task model that can perform multilingual speech recognition as well as speech translation and language identification. This demo cuts audio after around 30 secs.
|
208 |
+
# </p>
|
209 |
+
# <p>You can skip the queue by using google colab for the space: <a href="https://colab.research.google.com/drive/1WJ98KHgZxFGrHiMm4TyWZllSew_Af_ff?usp=sharing"><img data-canonical-src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" src="https://camo.githubusercontent.com/84f0493939e0c4de4e6dbe113251b4bfb5353e57134ffd9fcab6b8714514d4d1/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667"></a></p>
|
210 |
+
# </div>
|
211 |
+
# """
|
212 |
+
# )
|
213 |
+
# with gr.Group():
|
214 |
+
# with gr.Box():
|
215 |
+
# with gr.Row().style(mobile_collapse=False, equal_height=True):
|
216 |
+
# audio = gr.Audio(
|
217 |
+
# label="Input Audio",
|
218 |
+
# show_label=False,
|
219 |
+
# source="microphone",
|
220 |
+
# type="filepath"
|
221 |
+
# )
|
222 |
+
|
223 |
+
# btn = gr.Button("Transcribe")
|
224 |
+
# text = gr.Textbox(show_label=False, elem_id="result-textarea")
|
225 |
+
# with gr.Group(elem_id="share-btn-container"):
|
226 |
+
# community_icon = gr.HTML(community_icon_html, visible=False)
|
227 |
+
# loading_icon = gr.HTML(loading_icon_html, visible=False)
|
228 |
+
# share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
+
# btn.click(inference, inputs=[audio], outputs=[text, community_icon, loading_icon, share_button])
|
234 |
+
# share_button.click(None, [], [], _js=share_js)
|
235 |
|
236 |
+
# gr.HTML('''
|
237 |
+
# <div class="footer">
|
238 |
+
# <p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> - Gradio Demo by 🤗 Hugging Face
|
239 |
+
# </p>
|
240 |
+
# </div>
|
241 |
+
# ''')
|
242 |
+
|
243 |
+
# block.launch()
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
transformers
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
requests
|
3 |
+
streamlit
|
4 |
+
flask
|