deeme commited on
Commit
75eef29
·
verified ·
1 Parent(s): 78ce7ef

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +14 -0
  2. README.md +6 -6
  3. access_token.txt +0 -0
  4. app.py +90 -0
  5. at.py +70 -0
  6. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+ COPY ./access_token.txt /code/access_token.txt
7
+ COPY ./app.py /code/app.py
8
+ COPY ./at.py /code/at.py
9
+
10
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
11
+
12
+ EXPOSE 7860
13
+
14
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: At
3
- emoji: 🔥
4
- colorFrom: pink
5
  colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: at
3
+ emoji: 🌍
4
+ colorFrom: yellow
5
  colorTo: indigo
6
  sdk: docker
7
  pinned: false
8
+ license: mit
9
+ app_port: 7860
10
+ ---
access_token.txt ADDED
File without changes
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import schedule
3
+ import time
4
+ import os
5
+
6
+ import threading
7
+ from fastapi import FastAPI
8
+ app = FastAPI()
9
+
10
+ # 从环境变量获取基础 URL
11
+ BASE_URL = os.getenv('BASE_URL', "")
12
+
13
+ def check_available_tokens():
14
+ """
15
+ 检查可用的 Tokens 数量
16
+ """
17
+ try:
18
+ response = requests.get(f"{BASE_URL}")
19
+ if response.status_code == 200:
20
+ content = response.text
21
+ if '当前可用 Tokens 数量:<span class="text-blue-600">0' in content:
22
+ return True
23
+ return False
24
+ except Exception as e:
25
+ print(f"检查 Tokens 时发生错误: {e}")
26
+ return False
27
+
28
+ def read_and_prepare_tokens(file_path):
29
+ """
30
+ 从文件读取tokens并使用\r\n连接
31
+ """
32
+ try:
33
+ # 适配 Hugging Face Spaces 的文件路径
34
+ if os.path.exists(file_path):
35
+ with open(file_path, 'r') as file:
36
+ # 去除每行的空白字符,过滤掉空行
37
+ tokens = [line.strip() for line in file if line.strip()]
38
+ else:
39
+ # 如果文件不存在,尝试在 /home/user 目录下查找
40
+ alternative_path = os.path.join('/home/user', file_path)
41
+ with open(alternative_path, 'r') as file:
42
+ tokens = [line.strip() for line in file if line.strip()]
43
+
44
+ # 使用\r\n连接tokens
45
+ combined_token = '\r\n'.join(tokens)
46
+ return combined_token
47
+
48
+ except FileNotFoundError:
49
+ print(f"错误:文件 {file_path} 未找到")
50
+ return None
51
+ except Exception as e:
52
+ print(f"读取文件发生错误: {e}")
53
+ return None
54
+
55
+ def post_tokens():
56
+ """
57
+ 读取tokens并发送单次POST请求
58
+ """
59
+ url = f"{BASE_URL}/upload"
60
+
61
+ # 读取并准备tokens
62
+ combined_token = read_and_prepare_tokens('access_token.txt')
63
+
64
+ if not combined_token:
65
+ print("没有可用的tokens")
66
+ # 触发一次at.py环境变量读取生成access_token.txt
67
+ os.system('python at.py')
68
+ return
69
+
70
+ try:
71
+ payload = {"text": combined_token}
72
+ response = requests.post(url, data=payload)
73
+
74
+ #print(f"响应状态码: {response.status_code}")
75
+ print(f"响应内容: {response.text}")
76
+
77
+ except Exception as e:
78
+ print(f"请求发生错误: {e}")
79
+
80
+ @app.get("/")
81
+ async def main():
82
+ # 首先检查是否需要发送 tokens
83
+ if not check_available_tokens():
84
+ print("当前仍有可用 Tokens,无需发送")
85
+ return
86
+ return {"message": post_tokens()}
87
+
88
+ if __name__ == "__main__":
89
+ import uvicorn
90
+ uvicorn.run(app, host="0.0.0.0", port=7860)
at.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+
4
+ # 定义常量
5
+ TOKEN_URL_NEO = 'https://token.oaifree.com/api/auth/refresh'
6
+ TOKEN_URL_OAI = 'https://auth0.openai.com/oauth/token'
7
+
8
+ def get_access_token(refresh_token, mode):
9
+ """
10
+ 获取 access token 的函数
11
+
12
+ :param refresh_token: refresh token
13
+ :param mode: 'neo' 或 'oai'
14
+ :return: access token 或错误信息
15
+ """
16
+ url = TOKEN_URL_OAI if mode == 'oai' else TOKEN_URL_NEO
17
+
18
+ if mode == 'neo':
19
+ headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
20
+ data = f'refresh_token={refresh_token}'
21
+ else:
22
+ headers = {'Content-Type': 'application/json'}
23
+ data = json.dumps({
24
+ 'redirect_uri': "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
25
+ 'grant_type': "refresh_token",
26
+ 'client_id': "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
27
+ 'refresh_token': refresh_token
28
+ })
29
+
30
+ try:
31
+ response = requests.post(url, headers=headers, data=data)
32
+ result = response.json()
33
+
34
+ if 'access_token' in result:
35
+ return result['access_token']
36
+ else:
37
+ return f"获取失败:{result.get('detail', '未知错误')}"
38
+
39
+ except requests.RequestException as e:
40
+ return f"请求错误:{str(e)}"
41
+
42
+ def batch_get_access_tokens(input_file='refresh_token.txt', output_file='access_token.txt'):
43
+ """
44
+ 批量获取 access tokens
45
+
46
+ :param input_file: 包含 refresh tokens 的输入文件
47
+ :param output_file: 写入 access tokens 的输出文件
48
+ """
49
+ # 从环境变量读取 refresh tokens
50
+ refresh_tokens = os.getenv('REFRESH_TOKENS', '')
51
+ if refresh_tokens:
52
+ refresh_tokens = refresh_tokens.split(',')
53
+ else:
54
+ # 如果没有环境变量,使用默认文件读取
55
+ with open(input_file, 'r') as f_in:
56
+ refresh_tokens = [line.strip() for line in f_in]
57
+
58
+ with open(output_file, 'w') as f_out:
59
+ for refresh_token in refresh_tokens:
60
+ # 默认使用 'neo' 模式,可以根据需要修改
61
+ access_token = get_access_token(refresh_token, mode='oai')
62
+
63
+ # 写入 access token
64
+ f_out.write(f"{access_token}\n")
65
+
66
+ print(f"Access tokens 已写入 {output_file}")
67
+
68
+ # 主程序入口
69
+ if __name__ == "__main__":
70
+ batch_get_access_tokens()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ requests
2
+ schedule
3
+ fastapi
4
+ uvicorn