File size: 2,118 Bytes
1ba0699
 
bf52a5d
1ba0699
bf52a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ba0699
 
bf52a5d
1ba0699
 
 
9c876a9
1ba0699
113590b
bf52a5d
113590b
 
 
bf52a5d
 
113590b
bf52a5d
113590b
 
1ba0699
94b7a9f
 
 
 
 
1ba0699
 
 
 
 
 
 
 
 
 
 
113590b
1ba0699
bf52a5d
 
 
 
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
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()