Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import sqlite3
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
CREATE TABLE IF NOT EXISTS data (
|
| 12 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 13 |
-
content TEXT NOT NULL
|
| 14 |
-
)
|
| 15 |
-
''')
|
| 16 |
-
|
| 17 |
-
# Загрузка данных из базы данных
|
| 18 |
-
cursor.execute("SELECT content FROM data ORDER BY id DESC LIMIT 1")
|
| 19 |
-
result = cursor.fetchone()
|
| 20 |
-
data = result[0] if result else ""
|
| 21 |
|
| 22 |
def get_data(password):
|
| 23 |
-
"""Функция для получения данных
|
| 24 |
if password == os.environ.get("password"):
|
| 25 |
return data
|
| 26 |
else:
|
| 27 |
raise gr.Error("Неверный пароль!")
|
| 28 |
|
| 29 |
def send_data(password, text):
|
| 30 |
-
"""Функция для сохранения данных
|
| 31 |
if password == os.environ.get("password"):
|
| 32 |
global data
|
| 33 |
data = text
|
| 34 |
-
|
| 35 |
-
|
| 36 |
raise gr.Info("Данные успешно сохранены!")
|
| 37 |
-
|
|
|
|
| 38 |
else:
|
| 39 |
raise gr.Error("Неверный пароль!")
|
| 40 |
|
|
@@ -56,7 +46,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 56 |
get_button.click(get_data, inputs=password_input, outputs=output_text)
|
| 57 |
send_button.click(send_data, inputs=[password_input, text_input], outputs=output_text)
|
| 58 |
|
| 59 |
-
demo.launch()
|
| 60 |
-
|
| 61 |
-
# Закрытие соединения с базой данных при завершении работы
|
| 62 |
-
conn.close()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
+
# Загрузка данных с диска, если они есть
|
| 5 |
+
try:
|
| 6 |
+
with open("data.txt", "r") as f:
|
| 7 |
+
data = f.read()
|
| 8 |
+
except FileNotFoundError:
|
| 9 |
+
data = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def get_data(password):
|
| 12 |
+
"""Функция для получения данных с диска."""
|
| 13 |
if password == os.environ.get("password"):
|
| 14 |
return data
|
| 15 |
else:
|
| 16 |
raise gr.Error("Неверный пароль!")
|
| 17 |
|
| 18 |
def send_data(password, text):
|
| 19 |
+
"""Функция для сохранения данных на диск."""
|
| 20 |
if password == os.environ.get("password"):
|
| 21 |
global data
|
| 22 |
data = text
|
| 23 |
+
with open("data.txt", "w") as f:
|
| 24 |
+
f.write(data)
|
| 25 |
raise gr.Info("Данные успешно сохранены!")
|
| 26 |
+
with open("data.txt", "r") as l:
|
| 27 |
+
return l.read()
|
| 28 |
else:
|
| 29 |
raise gr.Error("Неверный пароль!")
|
| 30 |
|
|
|
|
| 46 |
get_button.click(get_data, inputs=password_input, outputs=output_text)
|
| 47 |
send_button.click(send_data, inputs=[password_input, text_input], outputs=output_text)
|
| 48 |
|
| 49 |
+
demo.launch()
|
|
|
|
|
|
|
|
|