huedaya's picture
save
c0cca0a
raw
history blame
2.36 kB
import os
os.system("pip install git+https://github.com/openai/whisper.git")
import gradio as gr
import whisper
from flask import Flask, jsonify, request
import requests
import streamlit as st
model = whisper.load_model("small")
app = Flask(__name__)
@app.route("/")
def indexApi():
return jsonify({"output": "okay"})
@app.route("/run", methods=['POST'])
def runApi():
audio_url = request.form.get("audio_url")
# key = request.form.get("key")
# modelSelection = request.form.get("model")
# print(audio_url)
# if (modelSelection == None):
# modelSelection = "small"
# model = whisper.load_model(modelSelection)
# print(model)
# # reject if key not the same
# apiKey = st.secrets["Api-Key"]
# if apiKey != key:
# return jsonify({
# "image_url": image_url,
# "model": model,
# "result": "Invalid API Key",
# }), 400
response = requests.get(audio_url)
if response.status_code == requests.codes.ok:
with open("audio.mp3", "wb") as f:
f.write(response.content)
else:
return jsonify({
"result": "Unable to save file, status code: {response.status_code}" ,
}), 400
# arr = np.asarray(bytearray(response.content), dtype=np.uint8)
# result = model.transcribe("audio.mp3")
audio = "audio.mp3"
audio = whisper.load_audio(audio)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
options = whisper.DecodingOptions(fp16 = False)
result = whisper.decode(model, mel, options)
print(result.text)
return jsonify({
"audio_url": audio_url,
"model": model,
# "result": result.text,
})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
# def inference(audio):
# audio = whisper.load_audio(audio)
# audio = whisper.pad_or_trim(audio)
# mel = whisper.log_mel_spectrogram(audio).to(model.device)
# _, probs = model.detect_language(mel)
# options = whisper.DecodingOptions(fp16 = False)
# result = whisper.decode(model, mel, options)
# # print(result.text)
# return result.text, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)