Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import sqlite3
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
if db is None:
|
| 13 |
-
db = g._database = sqlite3.connect(DATABASE)
|
| 14 |
-
db.row_factory = sqlite3.Row
|
| 15 |
-
return db
|
| 16 |
|
| 17 |
def init_db():
|
| 18 |
-
conn =
|
| 19 |
-
|
| 20 |
-
|
| 21 |
"""
|
| 22 |
CREATE TABLE IF NOT EXISTS tasks (
|
| 23 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -29,64 +25,89 @@ def init_db():
|
|
| 29 |
conn.commit()
|
| 30 |
conn.close()
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
cursor = db.execute("SELECT id, title, completed FROM tasks")
|
| 42 |
-
tasks = [
|
| 43 |
-
{"id": row["id"], "title": row["title"], "completed": bool(row["completed"])}
|
| 44 |
-
for row in cursor.fetchall()
|
| 45 |
-
]
|
| 46 |
-
return jsonify(tasks)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
def create_task():
|
| 50 |
-
data = request.get_json()
|
| 51 |
-
title = data.get("title")
|
| 52 |
if not title:
|
| 53 |
-
return
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
task_id = cursor.lastrowid
|
| 61 |
-
return jsonify({"id": task_id, "title": title, "completed": False}), 201
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
db.execute(
|
| 75 |
-
"UPDATE tasks SET completed = ? WHERE id = ?",
|
| 76 |
-
(1 if completed else 0, task_id),
|
| 77 |
-
)
|
| 78 |
-
db.commit()
|
| 79 |
-
return jsonify({"id": task_id, "title": title, "completed": completed})
|
| 80 |
|
| 81 |
-
@app.route("/tasks/<int:task_id>", methods=["DELETE"])
|
| 82 |
def delete_task(task_id):
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
# Ensure the database exists before the first request
|
| 90 |
-
if not os.path.isfile(DATABASE):
|
| 91 |
-
init_db()
|
| 92 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import sqlite3
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
DB_PATH = os.path.join(os.path.dirname(__file__), "tasks.db")
|
| 7 |
|
| 8 |
+
def get_connection():
|
| 9 |
+
conn = sqlite3.connect(DB_PATH)
|
| 10 |
+
conn.row_factory = sqlite3.Row
|
| 11 |
+
return conn
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def init_db():
|
| 14 |
+
conn = get_connection()
|
| 15 |
+
cur = conn.cursor()
|
| 16 |
+
cur.execute(
|
| 17 |
"""
|
| 18 |
CREATE TABLE IF NOT EXISTS tasks (
|
| 19 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 25 |
conn.commit()
|
| 26 |
conn.close()
|
| 27 |
|
| 28 |
+
def fetch_all():
|
| 29 |
+
conn = get_connection()
|
| 30 |
+
cur = conn.cursor()
|
| 31 |
+
cur.execute("SELECT id, title, completed FROM tasks")
|
| 32 |
+
rows = cur.fetchall()
|
| 33 |
+
conn.close()
|
| 34 |
+
data = [[r["id"], r["title"], bool(r["completed"])] for r in rows]
|
| 35 |
+
return data
|
| 36 |
+
|
| 37 |
+
def get_task_options():
|
| 38 |
+
data = fetch_all()
|
| 39 |
+
return [str(row[0]) for row in data]
|
| 40 |
|
| 41 |
+
def refresh_tasks():
|
| 42 |
+
data = fetch_all()
|
| 43 |
+
return data, ["id", "title", "completed"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
def add_task(title):
|
|
|
|
|
|
|
|
|
|
| 46 |
if not title:
|
| 47 |
+
return fetch_all(), ["id", "title", "completed"]
|
| 48 |
+
conn = get_connection()
|
| 49 |
+
cur = conn.cursor()
|
| 50 |
+
cur.execute("INSERT INTO tasks (title, completed) VALUES (?, ?)", (title, 0))
|
| 51 |
+
conn.commit()
|
| 52 |
+
conn.close()
|
| 53 |
+
return refresh_tasks()
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
def update_task(task_id, new_title, completed):
|
| 56 |
+
if not task_id:
|
| 57 |
+
return refresh_tasks()
|
| 58 |
+
conn = get_connection()
|
| 59 |
+
cur = conn.cursor()
|
| 60 |
+
if new_title:
|
| 61 |
+
cur.execute("UPDATE tasks SET title = ? WHERE id = ?", (new_title, int(task_id)))
|
| 62 |
+
cur.execute("UPDATE tasks SET completed = ? WHERE id = ?", (1 if completed else 0, int(task_id)))
|
| 63 |
+
conn.commit()
|
| 64 |
+
conn.close()
|
| 65 |
+
return refresh_tasks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
| 67 |
def delete_task(task_id):
|
| 68 |
+
if not task_id:
|
| 69 |
+
return refresh_tasks()
|
| 70 |
+
conn = get_connection()
|
| 71 |
+
cur = conn.cursor()
|
| 72 |
+
cur.execute("DELETE FROM tasks WHERE id = ?", (int(task_id),))
|
| 73 |
+
conn.commit()
|
| 74 |
+
conn.close()
|
| 75 |
+
return refresh_tasks()
|
| 76 |
+
|
| 77 |
+
# Initialize database on first run
|
| 78 |
+
if not os.path.isfile(DB_PATH):
|
| 79 |
+
init_db()
|
| 80 |
+
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("# Docker Task Manager")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
task_table = gr.Dataframe(
|
| 85 |
+
label="Tasks",
|
| 86 |
+
headers=["id", "title", "completed"],
|
| 87 |
+
datatype=["number", "str", "bool"],
|
| 88 |
+
interactive=False,
|
| 89 |
+
)
|
| 90 |
+
refresh_btn = gr.Button("Refresh")
|
| 91 |
+
with gr.Row():
|
| 92 |
+
add_title = gr.Textbox(label="New Task Title", placeholder="Enter task title")
|
| 93 |
+
add_btn = gr.Button("Add Task")
|
| 94 |
+
with gr.Row():
|
| 95 |
+
upd_id = gr.Dropdown(label="Task ID to Update", choices=get_task_options())
|
| 96 |
+
upd_title = gr.Textbox(label="New Title (optional)", placeholder="Leave empty to keep current")
|
| 97 |
+
upd_completed = gr.Checkbox(label="Completed")
|
| 98 |
+
upd_btn = gr.Button("Update Task")
|
| 99 |
+
with gr.Row():
|
| 100 |
+
del_id = gr.Dropdown(label="Task ID to Delete", choices=get_task_options())
|
| 101 |
+
del_btn = gr.Button("Delete Task")
|
| 102 |
+
|
| 103 |
+
# Bind actions
|
| 104 |
+
refresh_btn.click(fn=refresh_tasks, inputs=None, outputs=[task_table])
|
| 105 |
+
add_btn.click(fn=add_task, inputs=add_title, outputs=[task_table])
|
| 106 |
+
upd_btn.click(fn=update_task, inputs=[upd_id, upd_title, upd_completed], outputs=[task_table])
|
| 107 |
+
del_btn.click(fn=delete_task, inputs=del_id, outputs=[task_table])
|
| 108 |
+
|
| 109 |
+
# Update dropdown options after any change
|
| 110 |
+
for btn in [add_btn, upd_btn, del_btn]:
|
| 111 |
+
btn.click(fn=get_task_options, inputs=None, outputs=[upd_id, del_id])
|
| 112 |
|
| 113 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|