NewSpace_1 / app.py
KimJunWon
requirements
0bac575
# import gradio as gr
# def greet(name):
# return "Hello " + name + "!!"
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
# iface.launch(share=True)
# -*- coding: utf-8 -*-
"""gradio.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1_GWRGRVb3znpFoAUO_7jj36vZG2FseJi
"""
# import os
# os.system('pip install wit')
# os.system('pip install scipy')
# !pip install gradio
# !pip install wit
# !pip install scipy
import gradio as gr
from wit import Wit
import numpy as np
import io
from scipy.io.wavfile import write
def is_enrolled_voice(name, wave_file):
return True
def STT(wave_file):
# Wit.ai API 연결을 μœ„ν•œ ν΄λΌμ΄μ–ΈνŠΈ 생성
wit_api_key = "HBAX6TJEDWRKUIUA62MFQZCXYG4BKQBQ"
wit_url = "https://api.wit.ai/speech"
wit_client = Wit(wit_api_key)
# μŒμ„± νŒŒμΌμ—μ„œ ν…μŠ€νŠΈ μΆ”μΆœ
print("STT μˆ˜ν–‰ μ „")
print("wave_file 의 νƒ€μž… :", type(wave_file))
print(wave_file)
sample_rate, data = wave_file
# μ •κ·œν™” 및 16λΉ„νŠΈλ‘œ λ³€ν™˜
scaled_data = (data / np.max(np.abs(data)) * 32767).astype(np.int16)
# λ³€μˆ˜μ— .wav 파일 데이터 μ €μž₯
wav_data = io.BytesIO()
write(wav_data, sample_rate, scaled_data)
response = wit_client.speech(wav_data, headers={'Content-Type': 'audio/wav'})
print("STT μˆ˜ν–‰ ν›„")
print(response)
print(type(response))
# μΆ”μΆœλœ ν…μŠ€νŠΈ 확인
if 'text' in response:
return response['text']
else:
return "μΆ”μΆœλœ ν…μŠ€νŠΈ μ—†μŒ!"
def function(name, wave_file):
is_enrolled = is_enrolled_voice(name, wave_file)
if is_enrolled: # λ“±λ‘λœ λͺ©μ†Œλ¦¬μΈ 경우 STT
print("λ“±λ‘λœ λͺ©μ†Œλ¦¬ μž…λ‹ˆλ‹€!")
output_text = STT(wave_file)
return output_text
else:
return "λ“±λ‘λ˜μ§€ μ•Šμ€ λͺ©μ†Œλ¦¬μž…λ‹ˆλ‹€!"
def main():
demo = gr.Interface(
function,
inputs=["text", "audio"],
outputs=["text"]
)
demo.launch(debug=True, share=True)
if __name__ == '__main__':
main()