Spaces:
Running
Running
Upload 5 files
Browse files
README.md
CHANGED
|
@@ -23,6 +23,10 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
| 23 |
|
| 24 |
[](https://huggingface.co/spaces/malt666/abacus_chat_proxy?duplicate=true)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
## 🚀 快速开始
|
| 27 |
|
| 28 |
### Hugging Face一键部署
|
|
@@ -31,13 +35,14 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
| 31 |
2. 登录你的Hugging Face账号(如果还没有,需要注册一个)
|
| 32 |
3. 在弹出的页面中设置你的Space名称
|
| 33 |
4. 创建完Space后,在Space的Settings -> Repository Secrets中添加以下配置:
|
| 34 |
-
- `covid_1`: 你的会话ID
|
| 35 |
- `cookie_1`: 你的cookies字符串
|
| 36 |
- `password`: (可选)访问密码
|
| 37 |
5. 等待自动部署完成即可
|
| 38 |
-
6. **获取API链接**:部署成功后,点击右上角的三个点按钮,在弹出的选项卡里面点击"Embed this Space",然后在弹出的"Embed this Space"界面里的"Direct URL"就是你的访问链接,你可以用这个链接调用API
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
|
| 42 |
#### Windows用户
|
| 43 |
|
|
@@ -66,12 +71,10 @@ chmod +x start.sh
|
|
| 66 |
3. 在Space的设置中连接你的GitHub仓库
|
| 67 |
4. 在Space的设置中添加以下Secrets:
|
| 68 |
- 第1组配置:
|
| 69 |
-
- `covid_1`: 第1个会话ID
|
| 70 |
- `cookie_1`: 第1个cookies字符串
|
| 71 |
- 第2组配置(如果需要):
|
| 72 |
-
- `covid_2`: 第2个会话ID
|
| 73 |
- `cookie_2`: 第2个cookies字符串
|
| 74 |
-
- 更多配置以此类推(`
|
| 75 |
- `password`: (可选)访问密码
|
| 76 |
5. Space会自动部署,服务将在 `https://你的空间名-你的用户名.hf.space` 上运行
|
| 77 |
|
|
@@ -99,15 +102,13 @@ PyJWT==2.8.0
|
|
| 99 |
在Docker或云平台部署时,需要配置以下环境变量:
|
| 100 |
|
| 101 |
- 必需的配置(至少需要一组):
|
| 102 |
-
- `
|
| 103 |
-
- `
|
| 104 |
- 以此类推...
|
| 105 |
- 可选配置:
|
| 106 |
- `password`: 访问密码
|
| 107 |
|
| 108 |
## 🔒 安全说明
|
| 109 |
|
| 110 |
-
- 配置文件中的敏感信息请妥善保管
|
| 111 |
- 建议在部署到Hugging Face时设置访问密码
|
| 112 |
-
- 不要将包含敏感信息的配置文件提交到公开仓库
|
| 113 |
- 在Hugging Face上配置时,请使用Secrets来存储敏感信息
|
|
|
|
| 23 |
|
| 24 |
[](https://huggingface.co/spaces/malt666/abacus_chat_proxy?duplicate=true)
|
| 25 |
|
| 26 |
+
## ⚠️ 警告
|
| 27 |
+
|
| 28 |
+
**本地部署方式已失效!**为了适配hugging face,本项目的本地部署方式已不再可用。目前只能通过Hugging Face Spaces部署来使用本代理服务。请使用下方的Hugging Face一键部署方法。
|
| 29 |
+
|
| 30 |
## 🚀 快速开始
|
| 31 |
|
| 32 |
### Hugging Face一键部署
|
|
|
|
| 35 |
2. 登录你的Hugging Face账号(如果还没有,需要注册一个)
|
| 36 |
3. 在弹出的页面中设置你的Space名称
|
| 37 |
4. 创建完Space后,在Space的Settings -> Repository Secrets中添加以下配置:
|
|
|
|
| 38 |
- `cookie_1`: 你的cookies字符串
|
| 39 |
- `password`: (可选)访问密码
|
| 40 |
5. 等待自动部署完成即可
|
| 41 |
+
6. **获取API链接**:部署成功后,点击右上角的三个点按钮,在弹出的选项卡里面点击"Embed this Space",然后在弹出的"Embed this Space"界面里的"Direct URL"就是你的访问链接,你可以用这个链接调用API和查看使用情况
|
| 42 |
+
|
| 43 |
+
### 本地运行(已失效)
|
| 44 |
|
| 45 |
+
> ⚠️ 以下本地运行方法已失效,仅作参考。请使用Hugging Face部署方式。
|
| 46 |
|
| 47 |
#### Windows用户
|
| 48 |
|
|
|
|
| 71 |
3. 在Space的设置中连接你的GitHub仓库
|
| 72 |
4. 在Space的设置中添加以下Secrets:
|
| 73 |
- 第1组配置:
|
|
|
|
| 74 |
- `cookie_1`: 第1个cookies字符串
|
| 75 |
- 第2组配置(如果需要):
|
|
|
|
| 76 |
- `cookie_2`: 第2个cookies字符串
|
| 77 |
+
- 更多配置以此类推(`cookie_3`...)
|
| 78 |
- `password`: (可选)访问密码
|
| 79 |
5. Space会自动部署,服务将在 `https://你的空间名-你的用户名.hf.space` 上运行
|
| 80 |
|
|
|
|
| 102 |
在Docker或云平台部署时,需要配置以下环境变量:
|
| 103 |
|
| 104 |
- 必需的配置(至少需要一组):
|
| 105 |
+
- `cookie_1`: 第1组配置
|
| 106 |
+
- `cookie_2`: 第2组配置(可选)
|
| 107 |
- 以此类推...
|
| 108 |
- 可选配置:
|
| 109 |
- `password`: 访问密码
|
| 110 |
|
| 111 |
## 🔒 安全说明
|
| 112 |
|
|
|
|
| 113 |
- 建议在部署到Hugging Face时设置访问密码
|
|
|
|
| 114 |
- 在Hugging Face上配置时,请使用Secrets来存储敏感信息
|
app.py
CHANGED
|
@@ -58,7 +58,7 @@ total_tokens = {
|
|
| 58 |
|
| 59 |
# 模型调用记录
|
| 60 |
model_usage_records = [] # 每次调用详细记录
|
| 61 |
-
MODEL_USAGE_RECORDS_FILE = "model_usage_records.json" #
|
| 62 |
|
| 63 |
# 计算点信息
|
| 64 |
compute_points = {
|
|
@@ -995,64 +995,61 @@ def index():
|
|
| 995 |
return redirect(url_for('dashboard'))
|
| 996 |
|
| 997 |
|
| 998 |
-
#
|
| 999 |
-
def num_tokens_from_string(
|
| 1000 |
-
"""
|
| 1001 |
try:
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
return
|
| 1006 |
except Exception as e:
|
| 1007 |
-
#
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
return estimated_tokens
|
| 1011 |
|
| 1012 |
# 更新模型使用统计
|
| 1013 |
def update_model_stats(model, prompt_tokens, completion_tokens):
|
|
|
|
| 1014 |
global model_usage_stats, total_tokens, model_usage_records
|
| 1015 |
|
| 1016 |
-
#
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
# 转换为北京时间 (UTC+8)
|
| 1020 |
-
beijing_time = utc_now + timedelta(hours=8)
|
| 1021 |
-
call_time = beijing_time.strftime('%Y-%m-%d %H:%M:%S') # 北京时间
|
| 1022 |
-
|
| 1023 |
-
record = {
|
| 1024 |
-
"model": model,
|
| 1025 |
-
"call_time": call_time,
|
| 1026 |
-
"prompt_tokens": prompt_tokens,
|
| 1027 |
-
"completion_tokens": completion_tokens,
|
| 1028 |
-
"calculation_method": "tiktoken" if any(x in model.lower() for x in ["gpt", "claude"]) or model in ["llama-3", "mistral", "gemma"] else "estimate"
|
| 1029 |
-
}
|
| 1030 |
-
model_usage_records.append(record)
|
| 1031 |
-
|
| 1032 |
-
# 限制记录数量,保留最新的500条
|
| 1033 |
-
if len(model_usage_records) > 500:
|
| 1034 |
-
model_usage_records.pop(0)
|
| 1035 |
-
|
| 1036 |
-
# 保存调用记录到本地文件
|
| 1037 |
-
save_model_usage_records()
|
| 1038 |
|
| 1039 |
-
#
|
| 1040 |
if model not in model_usage_stats:
|
| 1041 |
model_usage_stats[model] = {
|
| 1042 |
"count": 0,
|
| 1043 |
"prompt_tokens": 0,
|
| 1044 |
-
"completion_tokens": 0
|
| 1045 |
-
"total_tokens": 0
|
| 1046 |
}
|
| 1047 |
|
|
|
|
| 1048 |
model_usage_stats[model]["count"] += 1
|
| 1049 |
model_usage_stats[model]["prompt_tokens"] += prompt_tokens
|
| 1050 |
model_usage_stats[model]["completion_tokens"] += completion_tokens
|
| 1051 |
-
model_usage_stats[model]["total_tokens"] += (prompt_tokens + completion_tokens)
|
| 1052 |
|
|
|
|
| 1053 |
total_tokens["prompt"] += prompt_tokens
|
| 1054 |
total_tokens["completion"] += completion_tokens
|
| 1055 |
total_tokens["total"] += (prompt_tokens + completion_tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1056 |
|
| 1057 |
|
| 1058 |
# 获取计算点信息
|
|
@@ -1294,10 +1291,32 @@ if SPACE_URL:
|
|
| 1294 |
print("注意:Hugging Face生成的URL会自动将空间名称中的下划线(_)替换为连字符(-)")
|
| 1295 |
|
| 1296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1297 |
if __name__ == "__main__":
|
| 1298 |
# 启动保活线程
|
| 1299 |
threading.Thread(target=keep_alive, daemon=True).start()
|
| 1300 |
|
|
|
|
|
|
|
|
|
|
| 1301 |
# 加载历史模型调用记录
|
| 1302 |
load_model_usage_records()
|
| 1303 |
|
|
|
|
| 58 |
|
| 59 |
# 模型调用记录
|
| 60 |
model_usage_records = [] # 每次调用详细记录
|
| 61 |
+
MODEL_USAGE_RECORDS_FILE = "/app/model_usage_records.json" # 调用记录保存文件,使用/app目录确保HF中持久化
|
| 62 |
|
| 63 |
# 计算点信息
|
| 64 |
compute_points = {
|
|
|
|
| 995 |
return redirect(url_for('dashboard'))
|
| 996 |
|
| 997 |
|
| 998 |
+
# 使用tiktoken计算字符串的token数量
|
| 999 |
+
def num_tokens_from_string(text):
|
| 1000 |
+
"""使用tiktoken计算字符串的token数量"""
|
| 1001 |
try:
|
| 1002 |
+
# 使用cl100k_base编码器,这是gpt-4和gpt-3.5-turbo使用的编码器
|
| 1003 |
+
enc = tiktoken.get_encoding("cl100k_base")
|
| 1004 |
+
tokens = enc.encode(text)
|
| 1005 |
+
return len(tokens)
|
| 1006 |
except Exception as e:
|
| 1007 |
+
# 如果出错,使用字符长度作为粗略估计(大约每4个字符1个token)
|
| 1008 |
+
print(f"计算token数量出错: {e},使用估算方法")
|
| 1009 |
+
return len(text) // 4
|
|
|
|
| 1010 |
|
| 1011 |
# 更新模型使用统计
|
| 1012 |
def update_model_stats(model, prompt_tokens, completion_tokens):
|
| 1013 |
+
"""更新模型使用统计数据"""
|
| 1014 |
global model_usage_stats, total_tokens, model_usage_records
|
| 1015 |
|
| 1016 |
+
# 获取北京时间
|
| 1017 |
+
beijing_now = datetime.utcnow() + timedelta(hours=8)
|
| 1018 |
+
call_time = beijing_now.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1019 |
|
| 1020 |
+
# 首次使用该模型时初始化统计
|
| 1021 |
if model not in model_usage_stats:
|
| 1022 |
model_usage_stats[model] = {
|
| 1023 |
"count": 0,
|
| 1024 |
"prompt_tokens": 0,
|
| 1025 |
+
"completion_tokens": 0
|
|
|
|
| 1026 |
}
|
| 1027 |
|
| 1028 |
+
# 更新模型统计
|
| 1029 |
model_usage_stats[model]["count"] += 1
|
| 1030 |
model_usage_stats[model]["prompt_tokens"] += prompt_tokens
|
| 1031 |
model_usage_stats[model]["completion_tokens"] += completion_tokens
|
|
|
|
| 1032 |
|
| 1033 |
+
# 更新总token统计
|
| 1034 |
total_tokens["prompt"] += prompt_tokens
|
| 1035 |
total_tokens["completion"] += completion_tokens
|
| 1036 |
total_tokens["total"] += (prompt_tokens + completion_tokens)
|
| 1037 |
+
|
| 1038 |
+
# 添加使用记录
|
| 1039 |
+
usage_record = {
|
| 1040 |
+
"call_time": call_time,
|
| 1041 |
+
"model": model,
|
| 1042 |
+
"prompt_tokens": prompt_tokens,
|
| 1043 |
+
"completion_tokens": completion_tokens,
|
| 1044 |
+
"total_tokens": prompt_tokens + completion_tokens,
|
| 1045 |
+
"calculation_method": "tiktoken"
|
| 1046 |
+
}
|
| 1047 |
+
model_usage_records.append(usage_record)
|
| 1048 |
+
|
| 1049 |
+
# 保存记录到文件,确保数据持久化
|
| 1050 |
+
save_model_usage_records()
|
| 1051 |
+
|
| 1052 |
+
print(f"模型使用记录: {model}, 输入: {prompt_tokens}, 输出: {completion_tokens}, 时间: {call_time}")
|
| 1053 |
|
| 1054 |
|
| 1055 |
# 获取计算点信息
|
|
|
|
| 1291 |
print("注意:Hugging Face生成的URL会自动将空间名称中的下划线(_)替换为连字符(-)")
|
| 1292 |
|
| 1293 |
|
| 1294 |
+
# 定期保存token使用统计
|
| 1295 |
+
def auto_save_stats():
|
| 1296 |
+
"""定期自动保存token使用统计数据的线程函数"""
|
| 1297 |
+
while True:
|
| 1298 |
+
try:
|
| 1299 |
+
# 每10分钟保存一次
|
| 1300 |
+
time.sleep(600)
|
| 1301 |
+
# 保存模型使用记录
|
| 1302 |
+
save_model_usage_records()
|
| 1303 |
+
print(f"已自动保存模型使用记录: {datetime.utcnow() + timedelta(hours=8)}")
|
| 1304 |
+
|
| 1305 |
+
# 每小时尝试重新加载记录,确保数据不丢失
|
| 1306 |
+
if datetime.utcnow().minute < 10: # 每小时的前10分钟执行
|
| 1307 |
+
print("尝试重新加载模型使用记录...")
|
| 1308 |
+
load_model_usage_records()
|
| 1309 |
+
except Exception as e:
|
| 1310 |
+
print(f"自动保存/加载模型使用记录出错: {e}")
|
| 1311 |
+
|
| 1312 |
+
|
| 1313 |
if __name__ == "__main__":
|
| 1314 |
# 启动保活线程
|
| 1315 |
threading.Thread(target=keep_alive, daemon=True).start()
|
| 1316 |
|
| 1317 |
+
# 启动自动保存统计数据的线程
|
| 1318 |
+
threading.Thread(target=auto_save_stats, daemon=True).start()
|
| 1319 |
+
|
| 1320 |
# 加载历史模型调用记录
|
| 1321 |
load_model_usage_records()
|
| 1322 |
|