-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
97 lines (76 loc) · 3.08 KB
/
app.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from flask import Flask, render_template, request, jsonify , send_file
from agents import get_content
import os
from groq import Groq
from helper import parser
import time
from elevenlabs import ElevenLabs
from request import convert_text_to_speech, upload_to_cloudinary
from dotenv import load_dotenv
from mergefiles import merge_audio_files
from flask_cors import CORS
import shutil
load_dotenv()
voice_id1=os.getenv("voicemodel_id_phil")
voice_id2=os.getenv("voicemodel_id_walter")
app = Flask(__name__)
CORS(app)
client=Groq()
@app.route('/getscript', methods=['POST'])
def get_script():
if request.method == 'POST':
data = request.get_json() # Parse JSON input
user_input = data.get('user_input')
if not user_input:
return jsonify({"error": "No user_input provided"}), 400
script = get_content(user_input) # Fetch script content
formatted_script = parser(script) # Parse the script
print("Formatted Script:", formatted_script)
count = 0 # Initialize count for unique filenames
for element in formatted_script:
speaker, dialogue = element[0].strip("*"), element[1]
if speaker and dialogue:
print(f"Processing: {speaker}: {dialogue}")
# Determine the voice ID based on the speaker
voice_id = voice_id1 if speaker == "Phil Dunphy" else voice_id2 if speaker == "Walter White" else None
if voice_id:
convert_text_to_speech(dialogue, voice_id, count)
count += 1 # Increment count for unique filenames
else:
print(f"Skipped invalid element: {element}")
merge_audio_files("./audio","./finalaudio.mp3")
audio_url = upload_to_cloudinary("./finalaudio.mp3")
# Move the file
if audio_url:
return jsonify({"message": "Script processed successfully", "audio_url": audio_url}), 200
else:
return jsonify({"error": "Failed to upload audio to cloud"}), 500
@app.route("/transcribe", methods=["POST"])
def transcribe_audio():
if 'file' not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
# Save the uploaded file temporarily
temp_path = f"./{file.filename}"
file.save(temp_path)
try:
# Transcribe the audio
with open(temp_path, "rb") as file:
transcription = client.audio.transcriptions.create(
file=(temp_path, file.read()),
model="whisper-large-v3",
response_format="verbose_json",
)
print(transcription.text)
finally:
# Clean up the temporary file
os.remove(temp_path)
##Post request to get the audio from 11labs
@app.route("/getaudio", methods=["POST"])
def get_audio():
if request.method == 'POST':
pass
if __name__ == '__main__':
app.run()