Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
from flask import Flask, request, jsonify, send_file
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
client = OpenAI()
|
| 10 |
+
|
| 11 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
pipe = pipeline(model="seeafricatz/kiaziboraasr")
|
| 13 |
+
|
| 14 |
+
def transcribe(audio_path):
|
| 15 |
+
text = pipe(audio_path)["text"]
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
def generate_response(transcribed_text):
|
| 19 |
+
response = client.chat_completions.create(
|
| 20 |
+
messages=[
|
| 21 |
+
{"role": "system", "content": "All your answers should be in Swahili only..."},
|
| 22 |
+
{"role": "user", "content": transcribed_text},
|
| 23 |
+
],
|
| 24 |
+
model="gpt-4"
|
| 25 |
+
)
|
| 26 |
+
return response.choices[0].message.content
|
| 27 |
+
|
| 28 |
+
def inference(text):
|
| 29 |
+
tts = gTTS(text, lang='sw')
|
| 30 |
+
output_file = "tts_output.mp3"
|
| 31 |
+
tts.save(output_file)
|
| 32 |
+
return output_file
|
| 33 |
+
|
| 34 |
+
@app.route('/process_audio', methods=['POST'])
|
| 35 |
+
def process_audio():
|
| 36 |
+
if 'audio' not in request.files:
|
| 37 |
+
return jsonify({'error': 'No audio file provided'}), 400
|
| 38 |
+
|
| 39 |
+
audio_file = request.files['audio']
|
| 40 |
+
audio_path = "temp_audio.wav"
|
| 41 |
+
audio_file.save(audio_path)
|
| 42 |
+
|
| 43 |
+
transcribed_text = transcribe(audio_path)
|
| 44 |
+
response_text = generate_response(transcribed_text)
|
| 45 |
+
output_file = inference(response_text)
|
| 46 |
+
|
| 47 |
+
return jsonify({
|
| 48 |
+
'response_text': response_text,
|
| 49 |
+
'response_audio_url': f'/audio/{output_file}'
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
@app.route('/audio/<path:filename>')
|
| 53 |
+
def audio(filename):
|
| 54 |
+
return send_file(filename, as_attachment=True)
|
| 55 |
+
|
| 56 |
+
if __name__ == '__main__':
|
| 57 |
+
app.run(host='0.0.0.0', port=8000)
|