File size: 2,402 Bytes
9c7b232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import time
import json

def print_log(msg):
    print("[%s]%s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), msg))

def make_post_request(api_endpoint, endpoint_path, payload):
    url = f"{api_endpoint}{endpoint_path}"
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}

    try:
        response = requests.post(url, headers=headers, data=payload)
        return response
    except Exception as e:
        print_log(str(e))
        return None

def getapikey(url, username, password):
    if not url or not username or not password:
        return "URL, username, and password are required."
    api_endpoint = f"{url}"
    payload = f"username={username}&password={password}&prompt=login"
    response = make_post_request(api_endpoint, "/api/auth/platform/login", payload)
    return json.loads(response.text)["login_info"]["user"]["session"]["sensitive_id"]

def get_refresh_token(url, username, password):
    if not url or not username or not password:
        return "URL, username, and password are required."
    api_endpoint = f"{url}"
    payload = f"username={username}&password={password}"
    response = make_post_request(api_endpoint, "/api/auth/login2", payload)
    return json.loads(response.text)["refresh_token"]

with gr.Blocks() as iface:
    gr.Markdown("## Get OpenAI API sess Key")
    gr.Markdown("For example, if the address for your pandora-next is `https://dongsiqie-pdrn1.hf.space` and the value of your proxy_api_prefix attribute is aa12345678, then the URL below should be filled in as `https://dongsiqie-pdrn1.hf.space/aa12345678`")
    url_input = gr.Textbox(label="url", value="", placeholder="youUrl/proxy_api_prefix")
    username_input = gr.Textbox(label="username")
    password_input = gr.Textbox(type='password', label="password")    
    get_api_key_button = gr.Button("Get Sess Key")
    get_refresh_token_button = gr.Button("Get Refresh Token")
    response_output = gr.Markdown(label="Response")
    gr.Markdown("You can check the remaining quota for sess-api on this website: https://dongsiqie-openai-credit-grants.hf.space")
    get_api_key_button.click(getapikey, inputs=[url_input, username_input, password_input], outputs=response_output)
    get_refresh_token_button.click(get_refresh_token, inputs=[url_input, username_input, password_input], outputs=response_output)

iface.launch()