Spaces:
Sleeping
Sleeping
Commit
·
fe1b089
1
Parent(s):
bcd92c2
Initial commit
Browse files- .gitignore +1 -0
- gradio_app.py +69 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
venv/
|
gradio_app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import HfFolder
|
4 |
+
import requests
|
5 |
+
import asyncio
|
6 |
+
from gtts import gTTS
|
7 |
+
|
8 |
+
model_id = "sanchit-gandhi/whisper-small-dv" # update with your model id
|
9 |
+
pipe = pipeline("automatic-speech-recognition", model=model_id)
|
10 |
+
|
11 |
+
async def query(text, model_id="tiiuae/falcon-7b-instruct"):
|
12 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
13 |
+
headers = {"Authorization": "Bearer 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"}
|
14 |
+
payload = {"inputs": text}
|
15 |
+
|
16 |
+
print(f"Querying...: {text}")
|
17 |
+
loop = asyncio.get_event_loop()
|
18 |
+
response = await loop.run_in_executor(None, lambda: requests.post(api_url, headers=headers, json=payload))
|
19 |
+
print("\n")
|
20 |
+
print("\n")
|
21 |
+
print(response.json())
|
22 |
+
print("\n")
|
23 |
+
return response.json()[0]["generated_text"].split("\n")[1]
|
24 |
+
|
25 |
+
async def transcribe_speech(filepath):
|
26 |
+
output = pipe(
|
27 |
+
filepath,
|
28 |
+
max_new_tokens=256,
|
29 |
+
generate_kwargs={
|
30 |
+
"task": "transcribe",
|
31 |
+
"language": "english",
|
32 |
+
}, # update with the language you've fine-tuned on
|
33 |
+
chunk_length_s=30,
|
34 |
+
batch_size=8,
|
35 |
+
)
|
36 |
+
return await query(output["text"])
|
37 |
+
|
38 |
+
|
39 |
+
def final(filepath):
|
40 |
+
answer=asyncio.run(transcribe_speech(filepath))
|
41 |
+
return answer
|
42 |
+
|
43 |
+
def main(filepath):
|
44 |
+
response=final(filepath)
|
45 |
+
# print(response)
|
46 |
+
# myobj = gTTS(text=response, lang='en', slow=False)
|
47 |
+
# myobj.save(filepath)
|
48 |
+
# return filepath
|
49 |
+
return response
|
50 |
+
|
51 |
+
mic_transcribe = gr.Interface(
|
52 |
+
fn=main,
|
53 |
+
inputs=gr.Audio(sources="microphone", type="filepath"),
|
54 |
+
outputs="text",
|
55 |
+
)
|
56 |
+
|
57 |
+
file_transcribe = gr.Interface(
|
58 |
+
fn=main,
|
59 |
+
inputs=gr.Audio(sources="upload", type="filepath"),
|
60 |
+
outputs="audio",
|
61 |
+
)
|
62 |
+
|
63 |
+
|
64 |
+
demo=gr.TabbedInterface(
|
65 |
+
[mic_transcribe, file_transcribe],
|
66 |
+
["Transcribe Microphone", "Transcribe Audio File"],
|
67 |
+
)
|
68 |
+
|
69 |
+
demo.launch(debug=True)
|