Spaces:
Sleeping
Sleeping
File size: 1,511 Bytes
8b8f607 7f898b9 8b8f607 7f898b9 8b8f607 7f898b9 8b8f607 7f898b9 8b8f607 7dc18b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 |
import os
from dotenv import load_dotenv
import openai
from flask import Flask, request, jsonify, send_file
from transformers import pipeline
from gtts import gTTS
app = Flask(__name__)
# Load environment variables from .env file
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
os.environ["HF_HOME"] = os.getenv("HF_HOME")
pipe = pipeline(model="seeafricatz/kiaziboraasr")
def transcribe(audio_path):
text = pipe(audio_path)["text"]
return text
def generate_response(transcribed_text):
response = openai.Completion.create(
engine="davinci",
prompt=transcribed_text,
max_tokens=50
)
return response.choices[0].text
def inference(text):
tts = gTTS(text, lang='sw')
output_file = "tts_output.mp3"
tts.save(output_file)
return output_file
@app.route('/process_audio', methods=['POST'])
def process_audio():
if 'audio' not in request.files:
return jsonify({'error': 'No audio file provided'}), 400
audio_file = request.files['audio']
audio_path = "temp_audio.wav"
audio_file.save(audio_path)
transcribed_text = transcribe(audio_path)
response_text = generate_response(transcribed_text)
output_file = inference(response_text)
return jsonify({
'response_text': response_text,
'response_audio_url': f'/audio/{output_file}'
})
@app.route('/audio/<path:filename>')
def audio(filename):
return send_file(filename, as_attachment=True)
if __name__ == '__main__':
app.run()
|