Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
|
| 4 |
from gtts import gTTS
|
| 5 |
-
import tempfile
|
| 6 |
-
import os
|
| 7 |
-
import requests
|
| 8 |
-
|
| 9 |
from langdetect import detect
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
-
|
| 13 |
CORS(app)
|
| 14 |
|
| 15 |
-
|
| 16 |
language_names = {
|
| 17 |
'ru': 'Русский',
|
| 18 |
'en': 'Английский',
|
|
@@ -142,39 +139,36 @@ language_names = {
|
|
| 142 |
@app.route('/', methods=['GET', 'POST'])
|
| 143 |
def index():
|
| 144 |
if request.method == 'POST':
|
| 145 |
-
# Проверка пароля
|
| 146 |
password = request.form.get('password')
|
| 147 |
if password != 'gg':
|
| 148 |
return jsonify({"status": "error", "message": "Неверный пароль"})
|
| 149 |
|
| 150 |
-
# Получение текста из формы
|
| 151 |
input_text = request.form.get('text')
|
| 152 |
language_code = detect(input_text)
|
| 153 |
-
|
| 154 |
-
# Генерируем аудио
|
| 155 |
-
tts = gTTS(text=input_text, lang=language_code, slow=False)
|
| 156 |
|
| 157 |
-
|
| 158 |
temp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 159 |
-
|
| 160 |
-
tts.save(temp_file_name)
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
return render_template('index.html')
|
| 165 |
|
| 166 |
@app.route('/language', methods=['GET', 'POST'])
|
| 167 |
-
def
|
| 168 |
if request.method == 'POST':
|
| 169 |
-
# Проверка пароля
|
| 170 |
password = request.form.get('password')
|
| 171 |
-
#if password != 'gg':
|
| 172 |
-
# return jsonify({"status": "error", "message": "Неверный пароль"})
|
| 173 |
-
|
| 174 |
-
# Получение текста из формы
|
| 175 |
input_text = request.form.get('text')
|
| 176 |
language_code = detect(input_text)
|
| 177 |
-
|
| 178 |
if language_code in language_names:
|
| 179 |
language_name = language_names[language_code]
|
| 180 |
return jsonify({"status": "success", "lang_code": language_code, "lang_name": language_name})
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
from flask_cors import CORS
|
|
|
|
| 3 |
from gtts import gTTS
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langdetect import detect
|
| 5 |
+
from gofile import Gofile
|
| 6 |
+
import os
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
|
|
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
+
# Словарь с названиями языков
|
| 13 |
language_names = {
|
| 14 |
'ru': 'Русский',
|
| 15 |
'en': 'Английский',
|
|
|
|
| 139 |
@app.route('/', methods=['GET', 'POST'])
|
| 140 |
def index():
|
| 141 |
if request.method == 'POST':
|
|
|
|
| 142 |
password = request.form.get('password')
|
| 143 |
if password != 'gg':
|
| 144 |
return jsonify({"status": "error", "message": "Неверный пароль"})
|
| 145 |
|
|
|
|
| 146 |
input_text = request.form.get('text')
|
| 147 |
language_code = detect(input_text)
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
tts = gTTS(text=input_text, lang=language_code, slow=False)
|
| 150 |
temp_file = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
| 151 |
+
tts.save(temp_file.name)
|
|
|
|
| 152 |
|
| 153 |
+
# Загрузка файла на Gofile
|
| 154 |
+
gofile_client = Gofile()
|
| 155 |
+
upload_result = gofile_client.upload(temp_file.name)
|
| 156 |
+
|
| 157 |
+
if upload_result['status'] == "ok":
|
| 158 |
+
file_url = upload_result['data']['downloadPage']
|
| 159 |
+
return jsonify({"status": "success", "url": file_url})
|
| 160 |
+
else:
|
| 161 |
+
return jsonify({"status": "error", "message": "Ошибка загрузки файла"})
|
| 162 |
|
| 163 |
return render_template('index.html')
|
| 164 |
|
| 165 |
@app.route('/language', methods=['GET', 'POST'])
|
| 166 |
+
def language():
|
| 167 |
if request.method == 'POST':
|
|
|
|
| 168 |
password = request.form.get('password')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
input_text = request.form.get('text')
|
| 170 |
language_code = detect(input_text)
|
| 171 |
+
|
| 172 |
if language_code in language_names:
|
| 173 |
language_name = language_names[language_code]
|
| 174 |
return jsonify({"status": "success", "lang_code": language_code, "lang_name": language_name})
|