File size: 2,384 Bytes
d877f5a 74b5ebb d877f5a 5046452 d877f5a 761ae49 703e6fc b4cd98c f9d919d fe124ca b4cd98c 111b72c 0e6afde d877f5a 651f405 74b5ebb ff0123b 74b5ebb ff0123b 651f405 74b5ebb 5d5cb75 89a35b5 5bf3b30 74b5ebb b4cd98c 74b5ebb cb39903 b4cd98c 703e6fc b4cd98c f9d919d b4cd98c 9a20d2b b4cd98c f9d919d 9a20d2b f9d919d 74b5ebb 761ae49 74b5ebb d877f5a 761ae49 b4cd98c 74b5ebb d877f5a b4cd98c 111b72c d877f5a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import json
import os
from datetime import datetime
from zoneinfo import ZoneInfo
import gradio as gr
from extract import extract
import call_logger
from pgsoft.pgdate.date_utils import beijing
import call_pgai
from utils import normalize_text
#######################
# proxy version
#######################
proxy_version = "1.0.0-2025-05-26-a"
t = datetime.now()
t = t.astimezone(ZoneInfo("Asia/Shanghai"))
print(f"[Beijing]: {t.replace(microsecond=0)}")
t = t.astimezone(ZoneInfo("America/Los_Angeles"))
print(f"[Seattle]: {t.replace(microsecond=0)}")
identity = os.environ.get("identity", "local")
print(f"identity: {identity}")
db_token = os.environ.get("db_token")
if db_token:
print(db_token[:5])
game_list = [
"matchn",
"house",
"watermelon",
"snake",
]
def run(info, game, nlp_command):
"""event handler"""
# reuse hf_token field as json string
user, source, username, _ = extract(info)
if nlp_command is None:
return "command is required"
nlp_command = normalize_text(nlp_command)
if nlp_command == "":
return "invalid command"
service_start = beijing()
print(f"[{service_start}] service starts >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(f"[{user}] [{game}] [{nlp_command}]")
call_pgai.from_cache = True
outp = call_pgai.call_pgai(nlp_command, game)
if outp is None:
return "no output"
if isinstance(outp, str):
return outp
# add proxy version info to the output
outp["timestamp"] = beijing().__str__()
outp["proxy-version"] = proxy_version
outp["user"] = user
outp["username"] = username
outp["game"] = game
outp["source"] = source
outp["cache"] = call_pgai.from_cache
call_logger.call_logger(outp, identity, db_token)
service_end = beijing()
timecost = service_end.timestamp() - service_start.timestamp()
print(
f"[{service_end}] service ends, costs {timecost:.2f}s "
+ "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n"
)
return json.dumps(outp, indent=4)
demo = gr.Interface(
fn=run,
inputs=[
"text",
gr.Radio(
game_list,
value=game_list[0],
info="Which game you want the AI to support?",
),
"text",
],
outputs="text",
title="Pgai",
allow_flagging="never",
)
demo.launch()
|