dongsiqie commited on
Commit
9c7b232
·
verified ·
1 Parent(s): b8be67c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import time
4
+ import json
5
+
6
+ def print_log(msg):
7
+ print("[%s]%s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), msg))
8
+
9
+ def make_post_request(api_endpoint, endpoint_path, payload):
10
+ url = f"{api_endpoint}{endpoint_path}"
11
+ headers = {'Content-Type': 'application/x-www-form-urlencoded'}
12
+
13
+ try:
14
+ response = requests.post(url, headers=headers, data=payload)
15
+ return response
16
+ except Exception as e:
17
+ print_log(str(e))
18
+ return None
19
+
20
+ def getapikey(url, username, password):
21
+ if not url or not username or not password:
22
+ return "URL, username, and password are required."
23
+ api_endpoint = f"{url}"
24
+ payload = f"username={username}&password={password}&prompt=login"
25
+ response = make_post_request(api_endpoint, "/api/auth/platform/login", payload)
26
+ return json.loads(response.text)["login_info"]["user"]["session"]["sensitive_id"]
27
+
28
+ def get_refresh_token(url, username, password):
29
+ if not url or not username or not password:
30
+ return "URL, username, and password are required."
31
+ api_endpoint = f"{url}"
32
+ payload = f"username={username}&password={password}"
33
+ response = make_post_request(api_endpoint, "/api/auth/login2", payload)
34
+ return json.loads(response.text)["refresh_token"]
35
+
36
+ with gr.Blocks() as iface:
37
+ gr.Markdown("## Get OpenAI API sess Key")
38
+ 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`")
39
+ url_input = gr.Textbox(label="url", value="", placeholder="youUrl/proxy_api_prefix")
40
+ username_input = gr.Textbox(label="username")
41
+ password_input = gr.Textbox(type='password', label="password")
42
+ get_api_key_button = gr.Button("Get Sess Key")
43
+ get_refresh_token_button = gr.Button("Get Refresh Token")
44
+ response_output = gr.Markdown(label="Response")
45
+ gr.Markdown("You can check the remaining quota for sess-api on this website: https://dongsiqie-openai-credit-grants.hf.space")
46
+ get_api_key_button.click(getapikey, inputs=[url_input, username_input, password_input], outputs=response_output)
47
+ get_refresh_token_button.click(get_refresh_token, inputs=[url_input, username_input, password_input], outputs=response_output)
48
+
49
+ iface.launch()