File size: 1,777 Bytes
8f788f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a986bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Request, Header
from gradio_client import Client
from datetime import datetime

app = FastAPI()


@app.api_route("/", methods=["GET", "POST"])
async def ping():
    return {"pong": datetime.now().isoformat()}


@app.get("/private-policy")
async def ping():
    return "We dont store your data. All data returned is public!"

@app.api_route("/gapi", methods=["GET", "POST"])
async def InvokeGradioApi(request: Request, x_hf_token: str = Header(default=None)):
    if request.method == "POST":
        data = await request.json()
    else:
        data = dict(request.query_params)

    # Extrai SpaceName e AuthKey
    space_name = data.pop("space", None)
    auth_key = data.pop("auth", None) or x_hf_token

    if not space_name:
        return {"error": "Missing required parameter: space"}

    print("Creating client", space_name);
    client = Client(space_name, hf_token=auth_key if auth_key else None)

    print("Data:")
    print(data);

    try:
        result = client.predict(**data)
        return result
    except Exception as e:
        return {"error": str(e)}



@app.get("/SearchScripts")
async def SearchScript(request: Request, x_hf_token: str = Header(default=None)):
    data = dict(request.query_params)
        
    auth_key = data.pop("auth", None) or x_hf_token
    client = Client('rrg92/sqlserver-lib-assistant', hf_token=auth_key if auth_key else None)
    
    search_text = data['text'];
    
    print("search", search_text);
    
    try:
        result = client.predict(message=search_text, api_name="/SearchFiles")
        return result
    except Exception as e:
        print(e);
        return {"error": "Some error happened. Admin must check logs"}