|
import gradio as gr |
|
import os |
|
import sqlite3 |
|
|
|
|
|
conn = sqlite3.connect('mydata.db') |
|
cursor = conn.cursor() |
|
|
|
|
|
cursor.execute(''' |
|
CREATE TABLE IF NOT EXISTS data ( |
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
|
content TEXT NOT NULL |
|
) |
|
''') |
|
|
|
|
|
cursor.execute("SELECT content FROM data ORDER BY id DESC LIMIT 1") |
|
result = cursor.fetchone() |
|
data = result[0] if result else "" |
|
|
|
def get_data(password): |
|
"""Функция для получения данных из базы данных.""" |
|
if password == os.environ.get("password"): |
|
return data |
|
else: |
|
raise gr.Error("Неверный пароль!") |
|
|
|
def send_data(password, text): |
|
"""Функция для сохранения данных в базе данных.""" |
|
if password == os.environ.get("password"): |
|
global data |
|
data = text |
|
cursor.execute("INSERT INTO data (content) VALUES (?)", (data,)) |
|
conn.commit() |
|
raise gr.Info("Данные успешно сохранены!") |
|
return data |
|
else: |
|
raise gr.Error("Неверный пароль!") |
|
|
|
css = """ |
|
footer {visibility: hidden !important;} |
|
""" |
|
|
|
with gr.Blocks(css=css) as demo: |
|
with gr.Row(): |
|
password_input = gr.Textbox(type="password", label="Введите пароль:") |
|
with gr.Row(): |
|
text_input = gr.Textbox(label="Введите текст:") |
|
with gr.Row(): |
|
get_button = gr.Button("Получить") |
|
send_button = gr.Button("Отправить") |
|
with gr.Row(): |
|
output_text = gr.Textbox(label="Результат:") |
|
|
|
get_button.click(get_data, inputs=password_input, outputs=output_text) |
|
send_button.click(send_data, inputs=[password_input, text_input], outputs=output_text) |
|
|
|
demo.launch() |
|
|
|
|
|
conn.close() |