File size: 1,467 Bytes
fe124ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f6cc6b
3ea73d7
fe124ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import cache
import requests
import os
from typing import Any
from pgsoft.pgdate.date_utils import beijing
import json


def post_to_pgai_helper(command: str) -> Any:
    myobj = {
        "code": os.getenv("pgai_code"), 
        "command": command,
    }

    header = {'accept': 'application/json', 'Content-Type': 'application/json'}
    url_base = "https://steveagi-pgai.hf.space/games/watermelon/nlp"
    url_read = f"{url_base}/r"

    try:
        res = requests.post(url_read, headers=header, json=myobj)
        if res.status_code == 200:
            print(res.text)
            return res.text
        else:
            print(res.text)
            return None
    except Exception as e:
        print(e)
        return None
    

from_cache = True
# @cache
def call_pgai(service, game, functionality, nlp_command, url, hf_token):
    calling_start = beijing()
    print(f"calling ai starts at {calling_start}")
    try:
        res = post_to_pgai_helper(nlp_command)
    except Exception as e:
        return (
            f"{type(e)}, {str(e)}. \nyou may want to make "
            + "sure your hf_token is correct"
        )
    calling_end = beijing()
    timecost = calling_end.timestamp() - calling_start.timestamp()
    print(f"calling ai ends at {calling_end}, costs {timecost:.2f}s")
    global from_cache
    from_cache = False
    if res is not None:
        outp = json.loads(res)
        return outp
    else:
        return None