Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import sqlite3
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def init_db():
|
| 14 |
-
conn =
|
| 15 |
-
|
| 16 |
-
|
| 17 |
"""
|
| 18 |
CREATE TABLE IF NOT EXISTS tasks (
|
| 19 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -25,104 +29,68 @@ def init_db():
|
|
| 25 |
conn.commit()
|
| 26 |
conn.close()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
tasks =
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def
|
|
|
|
|
|
|
| 46 |
if not title:
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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(
|
| 63 |
-
"UPDATE tasks SET completed = ? WHERE id = ?",
|
| 64 |
-
(1 if completed else 0, int(task_id)),
|
| 65 |
)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
return
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
with gr.Blocks() as demo:
|
| 85 |
-
gr.Markdown("# Docker Task Manager")
|
| 86 |
-
with gr.Row():
|
| 87 |
-
task_table = gr.Dataframe(
|
| 88 |
-
label="Tasks",
|
| 89 |
-
headers=["id", "title", "completed"],
|
| 90 |
-
datatype=["number", "str", "bool"],
|
| 91 |
-
value=[],
|
| 92 |
-
interactive=False,
|
| 93 |
-
)
|
| 94 |
-
refresh_btn = gr.Button("Refresh")
|
| 95 |
-
with gr.Row():
|
| 96 |
-
add_title = gr.Textbox(label="New Task Title", placeholder="Enter task title")
|
| 97 |
-
add_btn = gr.Button("Add Task")
|
| 98 |
-
with gr.Row():
|
| 99 |
-
upd_id = gr.Dropdown(
|
| 100 |
-
label="Task ID to Update",
|
| 101 |
-
choices=get_task_options(),
|
| 102 |
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
upd_btn = gr.Button("Update Task")
|
| 106 |
-
with gr.Row():
|
| 107 |
-
del_id = gr.Dropdown(
|
| 108 |
-
label="Task ID to Delete",
|
| 109 |
-
choices=get_task_options(),
|
| 110 |
-
)
|
| 111 |
-
del_btn = gr.Button("Delete Task")
|
| 112 |
-
|
| 113 |
-
# Bind actions
|
| 114 |
-
refresh_btn.click(fn=refresh_tasks, inputs=None, outputs=task_table)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import sqlite3
|
| 4 |
+
from flask import Flask, request, jsonify, g
|
| 5 |
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
DATABASE = os.path.join(os.path.dirname(__file__), "tasks.db")
|
| 9 |
+
|
| 10 |
+
def get_db():
|
| 11 |
+
db = getattr(g, "_database", None)
|
| 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 = sqlite3.connect(DATABASE)
|
| 19 |
+
cursor = conn.cursor()
|
| 20 |
+
cursor.execute(
|
| 21 |
"""
|
| 22 |
CREATE TABLE IF NOT EXISTS tasks (
|
| 23 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 29 |
conn.commit()
|
| 30 |
conn.close()
|
| 31 |
|
| 32 |
+
@app.teardown_appcontext
|
| 33 |
+
def close_connection(exception):
|
| 34 |
+
db = getattr(g, "_database", None)
|
| 35 |
+
if db is not None:
|
| 36 |
+
db.close()
|
| 37 |
+
|
| 38 |
+
@app.route("/tasks", methods=["GET"])
|
| 39 |
+
def list_tasks():
|
| 40 |
+
db = get_db()
|
| 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 |
+
@app.route("/tasks", methods=["POST"])
|
| 49 |
+
def create_task():
|
| 50 |
+
data = request.get_json()
|
| 51 |
+
title = data.get("title")
|
| 52 |
if not title:
|
| 53 |
+
return jsonify({"error": "title is required"}), 400
|
| 54 |
+
db = get_db()
|
| 55 |
+
cursor = db.execute(
|
| 56 |
+
"INSERT INTO tasks (title, completed) VALUES (?, ?)",
|
| 57 |
+
(title, 0),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
+
db.commit()
|
| 60 |
+
task_id = cursor.lastrowid
|
| 61 |
+
return jsonify({"id": task_id, "title": title, "completed": False}), 201
|
| 62 |
+
|
| 63 |
+
@app.route("/tasks/<int:task_id>", methods=["PUT"])
|
| 64 |
+
def update_task(task_id):
|
| 65 |
+
data = request.get_json()
|
| 66 |
+
title = data.get("title")
|
| 67 |
+
completed = data.get("completed")
|
| 68 |
+
if title is None and completed is None:
|
| 69 |
+
return jsonify({"error": "nothing to update"}), 400
|
| 70 |
+
db = get_db()
|
| 71 |
+
if title is not None:
|
| 72 |
+
db.execute("UPDATE tasks SET title = ? WHERE id = ?", (title, task_id))
|
| 73 |
+
if completed is not None:
|
| 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 |
+
db = get_db()
|
| 84 |
+
db.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
|
| 85 |
+
db.commit()
|
| 86 |
+
return "", 204
|
| 87 |
+
|
| 88 |
+
@app.route("/", methods=["GET"])
|
| 89 |
+
def index():
|
| 90 |
+
return "Task API is running."
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
# Ensure the database exists before the first request
|
| 94 |
+
if not os.path.isfile(DATABASE):
|
| 95 |
+
init_db()
|
| 96 |
+
app.run(host="0.0.0.0", port=7860)
|