Spaces:
Running
Running
jianghuyihei
commited on
Commit
•
8202bd6
1
Parent(s):
5819f9e
fix
Browse files
app.py
CHANGED
@@ -288,6 +288,8 @@ html_template = """
|
|
288 |
<div class="form-group">
|
289 |
<label for="topic">Topic:</label>
|
290 |
<input type="text" id="topic" name="topic">
|
|
|
|
|
291 |
<button type="submit" id="submit-btn">{{ button_text }}</button>
|
292 |
</div>
|
293 |
</form>
|
@@ -310,23 +312,15 @@ html_template = """
|
|
310 |
{% endif %}
|
311 |
</div>
|
312 |
<script>
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
}
|
323 |
-
if (!state) {
|
324 |
-
state = "generate";
|
325 |
-
sessionStorage.setItem("state", state);
|
326 |
-
}
|
327 |
-
console.log("set User ID:", userId, "state:", state);
|
328 |
-
}
|
329 |
-
window.onload = getUserId;
|
330 |
{{ script}}
|
331 |
</script>
|
332 |
</body>
|
@@ -364,27 +358,19 @@ def fix_markdown(text):
|
|
364 |
return '<br>'.join(result)
|
365 |
|
366 |
|
367 |
-
@app.get("/user_id")
|
368 |
-
async def get_user_id(request: Request):
|
369 |
-
user_id = request.cookies.get("user_id")
|
370 |
-
if not user_id:
|
371 |
-
user_id = str(uuid.uuid4())
|
372 |
-
print("successfully set user_id = ", user_id)
|
373 |
-
return {"user_id": user_id}
|
374 |
-
|
375 |
-
|
376 |
@app.get("/", response_class=HTMLResponse)
|
377 |
def form_get():
|
378 |
return Template(html_template).render(idea= "This is a example of the idea geneartion", error=None, reply_count=reply_count,button_text="Generate",loading_text="Generating content, Usually takes 3-4 minutes, please wait...")
|
379 |
|
380 |
@app.post("/", response_class=HTMLResponse)
|
381 |
-
def form_post(request: Request,response: Response,topic: str = Form(...)):
|
382 |
global reply_count
|
383 |
start_time = time.time()
|
384 |
|
385 |
-
|
386 |
-
|
387 |
-
state
|
|
|
388 |
|
389 |
response.set_cookie(key="user_id", value=user_id)
|
390 |
response.set_cookie(key="state", value=state)
|
@@ -397,7 +383,7 @@ def form_post(request: Request,response: Response,topic: str = Form(...)):
|
|
397 |
sessionStorage.setItem("state", "{state}");
|
398 |
}};
|
399 |
</script>
|
400 |
-
"""
|
401 |
|
402 |
print(f"current user_id={user_id}, state={state}")
|
403 |
loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
|
|
|
288 |
<div class="form-group">
|
289 |
<label for="topic">Topic:</label>
|
290 |
<input type="text" id="topic" name="topic">
|
291 |
+
<input type="hidden" id="user_id" name="user_id">
|
292 |
+
<input type="hidden" id="state" name="state">
|
293 |
<button type="submit" id="submit-btn">{{ button_text }}</button>
|
294 |
</div>
|
295 |
</form>
|
|
|
312 |
{% endif %}
|
313 |
</div>
|
314 |
<script>
|
315 |
+
function populateFormData() {
|
316 |
+
const userId = sessionStorage.getItem("user_id") || "";
|
317 |
+
const state = sessionStorage.getItem("state") || "";
|
318 |
+
|
319 |
+
document.getElementById("user_id").value = userId;
|
320 |
+
document.getElementById("state").value = state;
|
321 |
+
}
|
322 |
+
|
323 |
+
window.onload = populateFormData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
{{ script}}
|
325 |
</script>
|
326 |
</body>
|
|
|
358 |
return '<br>'.join(result)
|
359 |
|
360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
@app.get("/", response_class=HTMLResponse)
|
362 |
def form_get():
|
363 |
return Template(html_template).render(idea= "This is a example of the idea geneartion", error=None, reply_count=reply_count,button_text="Generate",loading_text="Generating content, Usually takes 3-4 minutes, please wait...")
|
364 |
|
365 |
@app.post("/", response_class=HTMLResponse)
|
366 |
+
def form_post(request: Request,response: Response,topic: str = Form(...), user_id: str = Form(...), state: str = Form(...)):
|
367 |
global reply_count
|
368 |
start_time = time.time()
|
369 |
|
370 |
+
if user_id == "":
|
371 |
+
user_id = str(uuid.uuid4())
|
372 |
+
if state == "":
|
373 |
+
state = "generate"
|
374 |
|
375 |
response.set_cookie(key="user_id", value=user_id)
|
376 |
response.set_cookie(key="state", value=state)
|
|
|
383 |
sessionStorage.setItem("state", "{state}");
|
384 |
}};
|
385 |
</script>
|
386 |
+
"""
|
387 |
|
388 |
print(f"current user_id={user_id}, state={state}")
|
389 |
loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
|