Ron Au commited on
Commit
42422ba
1 Parent(s): d047305

feat(endpoint): Change `create/task` from GET to POST

Browse files
Files changed (2) hide show
  1. app.py +9 -4
  2. static/js/network.js +7 -1
app.py CHANGED
@@ -4,6 +4,7 @@ from statistics import mean
4
  from fastapi import BackgroundTasks, FastAPI
5
  from fastapi.staticfiles import StaticFiles
6
  from fastapi.responses import FileResponse
 
7
 
8
  from modules.details import rand_details
9
  from modules.inference import generate_image
@@ -15,6 +16,10 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
15
  tasks = {}
16
 
17
 
 
 
 
 
18
  def get_place_in_queue(task_id):
19
  queued_tasks = list(task for task in tasks.values()
20
  if task["status"] == "queued" or task["status"] == "processing")
@@ -78,16 +83,16 @@ def generate_details():
78
  return rand_details()
79
 
80
 
81
- @app.get('/task/create')
82
- def create_task(background_tasks: BackgroundTasks, prompt: str = "покемон"):
83
  created_at = time()
84
 
85
- task_id = f"{str(created_at)}_{prompt}"
86
 
87
  tasks[task_id] = {
88
  "task_id": task_id,
89
  "created_at": created_at,
90
- "prompt": prompt,
91
  "status": "queued",
92
  "poll_count": 0,
93
  }
 
4
  from fastapi import BackgroundTasks, FastAPI
5
  from fastapi.staticfiles import StaticFiles
6
  from fastapi.responses import FileResponse
7
+ from pydantic import BaseModel
8
 
9
  from modules.details import rand_details
10
  from modules.inference import generate_image
 
16
  tasks = {}
17
 
18
 
19
+ class NewTask(BaseModel):
20
+ prompt = "покемон"
21
+
22
+
23
  def get_place_in_queue(task_id):
24
  queued_tasks = list(task for task in tasks.values()
25
  if task["status"] == "queued" or task["status"] == "processing")
 
83
  return rand_details()
84
 
85
 
86
+ @app.post('/task/create')
87
+ def create_task(background_tasks: BackgroundTasks, new_task: NewTask):
88
  created_at = time()
89
 
90
+ task_id = f"{str(created_at)}_{new_task.prompt}"
91
 
92
  tasks[task_id] = {
93
  "task_id": task_id,
94
  "created_at": created_at,
95
+ "prompt": new_task.prompt,
96
  "status": "queued",
97
  "poll_count": 0,
98
  }
static/js/network.js CHANGED
@@ -13,7 +13,13 @@ const generateDetails = async () => {
13
  };
14
 
15
  const createTask = async (prompt) => {
16
- const taskResponse = await fetch(pathFor(`task/create?prompt=${prompt}`));
 
 
 
 
 
 
17
  const task = await taskResponse.json();
18
 
19
  return task;
 
13
  };
14
 
15
  const createTask = async (prompt) => {
16
+ const taskResponse = await fetch(pathFor('task/create'), {
17
+ method: 'POST',
18
+ headers: {
19
+ 'Content-Type': 'application/json',
20
+ },
21
+ body: JSON.stringify({ prompt }),
22
+ });
23
  const task = await taskResponse.json();
24
 
25
  return task;