Spaces:
Running
Running
ChenyuRabbitLove
commited on
Commit
•
f73342b
1
Parent(s):
912d588
chore: modify few features
Browse files- app.py +32 -10
- css/style.css +18 -6
- utils/completion_reward.py +15 -7
- utils/completion_reward_utils.py +1 -1
app.py
CHANGED
@@ -35,19 +35,19 @@ def get_player_info(player_backend_user_id):
|
|
35 |
save_latest_player_data()
|
36 |
with open("latest_player_data.json", "r", encoding="utf-8") as file:
|
37 |
player_info = json.load(file)
|
38 |
-
with open(
|
39 |
datas = json.load(f)
|
40 |
|
41 |
processed_datas = {}
|
42 |
|
43 |
for k, v in datas.items():
|
44 |
-
log = v[
|
45 |
filtered_log = [record for record in log if not record.startswith("恭喜")]
|
46 |
-
processed_log = [record.rsplit(
|
47 |
-
concat_log =
|
48 |
processed_datas[k] = concat_log
|
49 |
|
50 |
-
with open(
|
51 |
json.dump(processed_datas, f)
|
52 |
|
53 |
if player_backend_user_id in player_info:
|
@@ -213,6 +213,9 @@ with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
|
|
213 |
cancel_player_name = gr.Button(
|
214 |
"取消", elem_id="cancel_player_name", visible=False
|
215 |
)
|
|
|
|
|
|
|
216 |
|
217 |
with gr.Row():
|
218 |
player_name_next_step = gr.Button(
|
@@ -277,6 +280,13 @@ with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
|
|
277 |
start_generate_story = gr.Button(
|
278 |
"開始寫作故事", visible=False, elem_id="start_generate_story"
|
279 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
with gr.Row():
|
282 |
bot1 = gr.Chatbot(visible=False)
|
@@ -363,15 +373,25 @@ with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
|
|
363 |
queue=False,
|
364 |
)
|
365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
366 |
confirm_player_name.click(
|
367 |
lambda: (gr.update(interactive=False), gr.update(visible=False)),
|
368 |
None,
|
369 |
[player_name, confirm_player_name],
|
370 |
queue=False,
|
371 |
).then(
|
372 |
-
|
373 |
-
|
374 |
-
[cancel_player_name, player_name_next_step],
|
375 |
queue=False,
|
376 |
).then(
|
377 |
**set_player_name_args
|
@@ -429,14 +449,16 @@ with gr.Blocks(theme=seafoam, css=get_content("css/style.css")) as demo:
|
|
429 |
start_generate_story.click(
|
430 |
lambda: gr.update(visible=False), None, start_generate_story, queue=False
|
431 |
).then(
|
432 |
-
lambda: create_visibility_updates(True,
|
433 |
None,
|
434 |
-
[bot1, bot2, bot3, bot4],
|
435 |
queue=False,
|
436 |
).then(
|
437 |
**get_llm_response_args
|
438 |
).then(
|
439 |
lambda: gr.update(visible=True), None, [select_story], queue=False
|
|
|
|
|
440 |
)
|
441 |
|
442 |
select_story.select(
|
|
|
35 |
save_latest_player_data()
|
36 |
with open("latest_player_data.json", "r", encoding="utf-8") as file:
|
37 |
player_info = json.load(file)
|
38 |
+
with open("latest_player_data.json", "r") as f:
|
39 |
datas = json.load(f)
|
40 |
|
41 |
processed_datas = {}
|
42 |
|
43 |
for k, v in datas.items():
|
44 |
+
log = v["adventure_logs"]
|
45 |
filtered_log = [record for record in log if not record.startswith("恭喜")]
|
46 |
+
processed_log = [record.rsplit(",", 1)[0] for record in filtered_log]
|
47 |
+
concat_log = " ".join(processed_log)
|
48 |
processed_datas[k] = concat_log
|
49 |
|
50 |
+
with open("processed_adventure_logs.json", "w") as f:
|
51 |
json.dump(processed_datas, f)
|
52 |
|
53 |
if player_backend_user_id in player_info:
|
|
|
213 |
cancel_player_name = gr.Button(
|
214 |
"取消", elem_id="cancel_player_name", visible=False
|
215 |
)
|
216 |
+
player_name_too_long = gr.Markdown(
|
217 |
+
"# 暱稱過長,請重新輸入", visible=False, elem_id="player_name_too_long"
|
218 |
+
)
|
219 |
|
220 |
with gr.Row():
|
221 |
player_name_next_step = gr.Button(
|
|
|
280 |
start_generate_story = gr.Button(
|
281 |
"開始寫作故事", visible=False, elem_id="start_generate_story"
|
282 |
)
|
283 |
+
weaving = gr.Button(
|
284 |
+
"星際夥伴努力撰寫故事中...",
|
285 |
+
visible=False,
|
286 |
+
elem_id="weaving",
|
287 |
+
size="lg",
|
288 |
+
interactive=False,
|
289 |
+
)
|
290 |
|
291 |
with gr.Row():
|
292 |
bot1 = gr.Chatbot(visible=False)
|
|
|
373 |
queue=False,
|
374 |
)
|
375 |
|
376 |
+
def check_plyer_name_length(player_name):
|
377 |
+
if len(player_name) > 10:
|
378 |
+
return (
|
379 |
+
gr.update(visible=True),
|
380 |
+
gr.update(visible=False),
|
381 |
+
gr.update(visible=True),
|
382 |
+
)
|
383 |
+
else:
|
384 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(False)
|
385 |
+
|
386 |
confirm_player_name.click(
|
387 |
lambda: (gr.update(interactive=False), gr.update(visible=False)),
|
388 |
None,
|
389 |
[player_name, confirm_player_name],
|
390 |
queue=False,
|
391 |
).then(
|
392 |
+
check_plyer_name_length,
|
393 |
+
player_name,
|
394 |
+
[cancel_player_name, player_name_next_step, player_name_too_long],
|
395 |
queue=False,
|
396 |
).then(
|
397 |
**set_player_name_args
|
|
|
449 |
start_generate_story.click(
|
450 |
lambda: gr.update(visible=False), None, start_generate_story, queue=False
|
451 |
).then(
|
452 |
+
lambda: create_visibility_updates(True, 5),
|
453 |
None,
|
454 |
+
[bot1, bot2, bot3, bot4, weaving],
|
455 |
queue=False,
|
456 |
).then(
|
457 |
**get_llm_response_args
|
458 |
).then(
|
459 |
lambda: gr.update(visible=True), None, [select_story], queue=False
|
460 |
+
).then(
|
461 |
+
lambda: gr.update(visible=False), None, [weaving], queue=False
|
462 |
)
|
463 |
|
464 |
select_story.select(
|
css/style.css
CHANGED
@@ -331,15 +331,21 @@ input[type="range"]::-ms-track {
|
|
331 |
}
|
332 |
|
333 |
.selected {
|
334 |
-
color: #fff;
|
335 |
-
background: #12d2ab;
|
336 |
-
border: 2px solid #02b28f;
|
337 |
-
border-radius: 8px;
|
338 |
}
|
339 |
|
340 |
.message {
|
341 |
-
background: rgba(0,0,0,0.05);
|
342 |
-
border: None;
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
}
|
344 |
|
345 |
.code_wrap {
|
@@ -349,6 +355,12 @@ input[type="range"]::-ms-track {
|
|
349 |
margin: 1vh 1vw 1vh 1vw;
|
350 |
}
|
351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
352 |
#select_story {
|
353 |
display: flex;
|
354 |
justify-content: space-evenly;
|
|
|
331 |
}
|
332 |
|
333 |
.selected {
|
334 |
+
color: #fff !important;
|
335 |
+
background: #12d2ab !important;
|
336 |
+
border: 2px solid #02b28f !important;
|
337 |
+
border-radius: 8px !important;
|
338 |
}
|
339 |
|
340 |
.message {
|
341 |
+
background: rgba(0,0,0,0.05) !important;
|
342 |
+
border: None !important;
|
343 |
+
}
|
344 |
+
|
345 |
+
#processing {
|
346 |
+
margin: 20vh 10vw;
|
347 |
+
height: 30vh;
|
348 |
+
font-size: 2rem;
|
349 |
}
|
350 |
|
351 |
.code_wrap {
|
|
|
355 |
margin: 1vh 1vw 1vh 1vw;
|
356 |
}
|
357 |
|
358 |
+
#certificate [aria-label="Download"]{
|
359 |
+
background: #12d2ab !important;
|
360 |
+
color: #fff !important;
|
361 |
+
border: 5px solid #02b28f !important;
|
362 |
+
}
|
363 |
+
|
364 |
#select_story {
|
365 |
display: flex;
|
366 |
justify-content: space-evenly;
|
utils/completion_reward.py
CHANGED
@@ -35,6 +35,7 @@ gcs_client = storage.Client(
|
|
35 |
credentials=creds, project=service_account_info_dict["project_id"]
|
36 |
)
|
37 |
|
|
|
38 |
class CompletionReward:
|
39 |
def __init__(self):
|
40 |
self.player_backend_user_id = None
|
@@ -129,7 +130,7 @@ class CompletionReward:
|
|
129 |
)
|
130 |
self.player_certificate_url = public_url
|
131 |
|
132 |
-
return gr.Image(public_url, visible=True)
|
133 |
|
134 |
def to_dict(self):
|
135 |
return {
|
@@ -318,13 +319,16 @@ class AWSAgent:
|
|
318 |
class GoogleAgent:
|
319 |
from google.cloud import aiplatform
|
320 |
from vertexai.preview.generative_models import GenerativeModel
|
|
|
321 |
SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
|
322 |
service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
|
323 |
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
|
324 |
|
325 |
-
creds = Credentials.from_service_account_info(
|
|
|
|
|
326 |
aiplatform.init(
|
327 |
-
project=
|
328 |
service_account=service_account_info_dict,
|
329 |
credentials=creds,
|
330 |
)
|
@@ -349,11 +353,15 @@ class GoogleAgent:
|
|
349 |
while retry_attempts < 5:
|
350 |
try:
|
351 |
logging.info("Google Generating response...")
|
352 |
-
model_response = self.gemini_pro_model.generate_content(
|
353 |
-
|
|
|
|
|
354 |
chinese_converter = OpenCC("s2tw")
|
355 |
|
356 |
-
return chinese_converter.convert(
|
|
|
|
|
357 |
|
358 |
except Exception as e:
|
359 |
retry_attempts += 1
|
@@ -499,7 +507,7 @@ class ImageProcessor:
|
|
499 |
body_x, body_y = left + 20, title_y + 60 # Adjust position as needed
|
500 |
|
501 |
for line in paragraph.split("\n"):
|
502 |
-
wrapped_lines = textwrap.wrap(line, width=
|
503 |
for wrapped_line in wrapped_lines:
|
504 |
draw.text((body_x, body_y), wrapped_line, font=body_font, fill="black")
|
505 |
body_y += 30
|
|
|
35 |
credentials=creds, project=service_account_info_dict["project_id"]
|
36 |
)
|
37 |
|
38 |
+
|
39 |
class CompletionReward:
|
40 |
def __init__(self):
|
41 |
self.player_backend_user_id = None
|
|
|
130 |
)
|
131 |
self.player_certificate_url = public_url
|
132 |
|
133 |
+
return gr.Image(public_url, visible=True, elem_id="certificate")
|
134 |
|
135 |
def to_dict(self):
|
136 |
return {
|
|
|
319 |
class GoogleAgent:
|
320 |
from google.cloud import aiplatform
|
321 |
from vertexai.preview.generative_models import GenerativeModel
|
322 |
+
|
323 |
SERVICE_ACCOUNT_INFO = os.getenv("GBQ_TOKEN")
|
324 |
service_account_info_dict = json.loads(SERVICE_ACCOUNT_INFO)
|
325 |
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
|
326 |
|
327 |
+
creds = Credentials.from_service_account_info(
|
328 |
+
service_account_info_dict, scopes=SCOPES
|
329 |
+
)
|
330 |
aiplatform.init(
|
331 |
+
project="junyiacademy",
|
332 |
service_account=service_account_info_dict,
|
333 |
credentials=creds,
|
334 |
)
|
|
|
353 |
while retry_attempts < 5:
|
354 |
try:
|
355 |
logging.info("Google Generating response...")
|
356 |
+
model_response = self.gemini_pro_model.generate_content(
|
357 |
+
f"{system_prompt}, 以下是我的冒險故事 ```{user_log}```"
|
358 |
+
)
|
359 |
+
|
360 |
chinese_converter = OpenCC("s2tw")
|
361 |
|
362 |
+
return chinese_converter.convert(
|
363 |
+
model_response.candidates[0].content.parts[0].text
|
364 |
+
)
|
365 |
|
366 |
except Exception as e:
|
367 |
retry_attempts += 1
|
|
|
507 |
body_x, body_y = left + 20, title_y + 60 # Adjust position as needed
|
508 |
|
509 |
for line in paragraph.split("\n"):
|
510 |
+
wrapped_lines = textwrap.wrap(line, width=70)
|
511 |
for wrapped_line in wrapped_lines:
|
512 |
draw.text((body_x, body_y), wrapped_line, font=body_font, fill="black")
|
513 |
body_y += 30
|
utils/completion_reward_utils.py
CHANGED
@@ -57,7 +57,7 @@ def check_is_in_completion_reward(player_backend_user_id):
|
|
57 |
gr.update(visible=False),
|
58 |
gr.update(visible=False),
|
59 |
gr.update(visible=False),
|
60 |
-
gr.Image(value, visible=True),
|
61 |
)
|
62 |
else:
|
63 |
return (
|
|
|
57 |
gr.update(visible=False),
|
58 |
gr.update(visible=False),
|
59 |
gr.update(visible=False),
|
60 |
+
gr.Image(value, visible=True, elem_id="certificate"),
|
61 |
)
|
62 |
else:
|
63 |
return (
|