File size: 3,586 Bytes
9f6868d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import requests


def get_file_size_of_bytes(size_in_bytes):
    if size_in_bytes < 1024:
        return f"{size_in_bytes} B"
    elif size_in_bytes < 1024 ** 2:
        return f"{size_in_bytes / 1024:.2f} KB"
    elif size_in_bytes < 1024 ** 3:
        return f"{size_in_bytes / (1024 ** 2):.2f} MB"
    else:
        return f"{size_in_bytes / (1024 ** 3):.2f} GB"


def delete_file(key, file_id):
    url = f"https://api.stepfun.com/v1/files/{file_id}"
    headers = {"Authorization": f"Bearer {key}"}
    try:
        resp = requests.delete(url, headers=headers, timeout=10)
        if resp.status_code == 200:
            print(f"文件 {file_id} 删除成功")
            return f"文件 {file_id} 删除成功"
        else:
            print(f"文件 {file_id} 删除失败")
            return f"删除失败,状态码:{resp.status_code}\n返回内容:{resp.text}"
    except Exception as e:
        return f"请求异常:{e}"

def query_files(key):
    
    url = "https://api.stepfun.com/v1/files"
    headers = {"Authorization": f"Bearer {key}"}
    try:
        resp = requests.get(url, headers=headers, timeout=10)
        if resp.status_code == 200:
            result = resp.json()
            text = "\n".join([f"{file['filename']} - {get_file_size_of_bytes(file['bytes'])}" for file in result['data']])
            
            return "下方是将会被删除的文件,确认无误后,点击上方的删除按钮进行删除 \n -----------\n"  + text
        else:
            return f"查询失败,状态码:{resp.status_code}\n返回内容:{resp.text}"
    except Exception as e:
        return f"请求异常:{e}"

def clean_files(key):
    url = "https://api.stepfun.com/v1/files"
    headers = {"Authorization": f"Bearer {key}"}
    try:
        resp = requests.get(url, headers=headers, timeout=10)
       
        if resp.status_code == 200:
            result = resp.json()
            text = "\n".join([ delete_file(key,file["id"]) for file in result['data']])
            
            return "下方是将会被删除的文件,确认无误后,点击上方的删除按钮进行删除 \n -----------\n"  + text
        else:
            return f"查询失败,状态码:{resp.status_code}\n返回内容:{resp.text}\n\n可用 curl 命令:\ncurl https://api.stepfun.com/v1/files \\\n-H \"Authorization: Bearer {key}\""
    except Exception as e:
        return f"请求异常:{e}\n\n可用 curl 命令:\ncurl https://api.stepfun.com/v1/files \\\n-H \"Authorization: Bearer {key}\""

    
    return f"清理完成:KEY={key}"

with gr.Blocks(title="StepFun 文件清理工具") as demo:
    text = """
    # StepFun 文件清理工具
    
    ## 计费策略
    可参考[官网文档](https://platform.stepfun.com/docs/pricing/details)
    ## 说明
    1. 输入您的 KEY(可前往 [stepufn.com](https://platform.stepfun.com/interface-key) 获取
    2. 点击查询确认要删除的文件
    3. 点击清理按钮进行清理
    4. 清理完成后,可以再次点击查询确认是否清理成功。
    
"""
    gr.Markdown(text)
    key_input = gr.Textbox(label="StepFun KEY", placeholder="请输入您的 KEY")
    with gr.Row():
        query_btn = gr.Button("查询")
        clean_btn = gr.Button("清理")
    output_box = gr.Textbox(label="输出结果", lines=30)

    query_btn.click(query_files, inputs=key_input, outputs=output_box)
    clean_btn.click(clean_files, inputs=key_input, outputs=output_box)

demo.launch(server_port=7860, share=True,debug=True,server_name="0.0.0.0")