Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,22 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
from gtts import gTTS
|
4 |
-
import tempfile
|
5 |
-
import os
|
6 |
-
import requests
|
7 |
from langdetect import detect
|
|
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
CORS(app)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
language_names = {
|
13 |
'ru': 'Русский',
|
14 |
'en': 'Английский',
|
@@ -135,8 +143,6 @@ language_names = {
|
|
135 |
'ko': 'Корейский',
|
136 |
}
|
137 |
|
138 |
-
CATBOX_API_URL = "https://catbox.moe/user/api.php"
|
139 |
-
|
140 |
@app.route('/', methods=['GET', 'POST'])
|
141 |
def index():
|
142 |
if request.method == 'POST':
|
@@ -146,48 +152,33 @@ def index():
|
|
146 |
|
147 |
input_text = request.form.get('text')
|
148 |
language_code = detect(input_text)
|
149 |
-
|
150 |
-
tts = gTTS(text=input_text, lang=language_code, slow=False)
|
151 |
-
temp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
152 |
-
temp_file_name = temp_file.name
|
153 |
-
tts.save(temp_file_name)
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
os.
|
|
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
return jsonify({"status": "success", "url": uploaded_url})
|
162 |
-
else:
|
163 |
-
return jsonify({"status": "error", "message": "Ошибка при загрузке файла"})
|
164 |
|
165 |
return render_template('index.html')
|
166 |
|
|
|
|
|
|
|
|
|
167 |
@app.route('/language', methods=['GET', 'POST'])
|
168 |
def language_detection():
|
169 |
if request.method == 'POST':
|
170 |
-
password = request.form.get('password')
|
171 |
-
if password != 'gg':
|
172 |
-
return jsonify({"status": "error", "message": "Неверный пароль"})
|
173 |
-
|
174 |
input_text = request.form.get('text')
|
175 |
language_code = detect(input_text)
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
179 |
|
180 |
return render_template('index.html')
|
181 |
|
182 |
-
def catbox_upload(file_path, reqtype, userhash=None, additional_data=None):
|
183 |
-
with open(file_path, 'rb') as file:
|
184 |
-
data = {'reqtype': reqtype}
|
185 |
-
files = {'fileToUpload': file}
|
186 |
-
if userhash:
|
187 |
-
data['userhash'] = userhash
|
188 |
-
if additional_data:
|
189 |
-
data.update(additional_data)
|
190 |
-
return requests.post(CATBOX_API_URL, files=files, data=data)
|
191 |
-
|
192 |
if __name__ == '__main__':
|
193 |
app.run(host="0.0.0.0", port=7860, debug=False)
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory
|
2 |
from flask_cors import CORS
|
3 |
from gtts import gTTS
|
|
|
|
|
|
|
4 |
from langdetect import detect
|
5 |
+
import os
|
6 |
+
import uuid
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
CORS(app)
|
10 |
|
11 |
+
# Путь для сохранения аудиофайлов
|
12 |
+
AUDIO_FOLDER = os.path.join('static', 'audio')
|
13 |
+
app.config['AUDIO_FOLDER'] = AUDIO_FOLDER
|
14 |
+
|
15 |
+
# Создаем директорию для аудио, если она не существует
|
16 |
+
if not os.path.isdir(AUDIO_FOLDER):
|
17 |
+
os.makedirs(AUDIO_FOLDER)
|
18 |
+
|
19 |
+
# Словарь языков
|
20 |
language_names = {
|
21 |
'ru': 'Русский',
|
22 |
'en': 'Английский',
|
|
|
143 |
'ko': 'Корейский',
|
144 |
}
|
145 |
|
|
|
|
|
146 |
@app.route('/', methods=['GET', 'POST'])
|
147 |
def index():
|
148 |
if request.method == 'POST':
|
|
|
152 |
|
153 |
input_text = request.form.get('text')
|
154 |
language_code = detect(input_text)
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
+
tts = gTTS(text=input_text, lang=language_code, slow=False)
|
157 |
+
filename = str(uuid.uuid4()) + ".mp3"
|
158 |
+
file_path = os.path.join(app.config['AUDIO_FOLDER'], filename)
|
159 |
+
tts.save(file_path)
|
160 |
|
161 |
+
file_url = request.host_url + 'audio/' + filename
|
162 |
+
return jsonify({"status": "success", "url": file_url})
|
|
|
|
|
|
|
163 |
|
164 |
return render_template('index.html')
|
165 |
|
166 |
+
@app.route('/audio/<filename>')
|
167 |
+
def uploaded_file(filename):
|
168 |
+
return send_from_directory(app.config['AUDIO_FOLDER'], filename)
|
169 |
+
|
170 |
@app.route('/language', methods=['GET', 'POST'])
|
171 |
def language_detection():
|
172 |
if request.method == 'POST':
|
|
|
|
|
|
|
|
|
173 |
input_text = request.form.get('text')
|
174 |
language_code = detect(input_text)
|
175 |
+
if language_code in language_names:
|
176 |
+
language_name = language_names[language_code]
|
177 |
+
return jsonify({"status": "success", "lang_code": language_code, "lang_name": language_name})
|
178 |
+
else:
|
179 |
+
return jsonify({"status": "error", "message": "Неизвестный язык"})
|
180 |
|
181 |
return render_template('index.html')
|
182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
if __name__ == '__main__':
|
184 |
app.run(host="0.0.0.0", port=7860, debug=False)
|