KeywordGacha icon indicating copy to clipboard operation
KeywordGacha copied to clipboard

[Feature Request] 支持用Gradio写一个UI来进行抓取(这样可以部署在hugging face上,还可以白嫖一下ZeroGPU的GPU)

Open PipeYume opened this issue 1 year ago • 2 comments

目前设想的大概UI形状,Process还未实现。

import gradio as gr
import zipfile
import tempfile
from pathlib import Path
def process(file, config):
    # 创建一个临时的ZIP文件
    with tempfile.NamedTemporaryFile(prefix=Path(file.name).stem,suffix=".zip", delete=False) as temp_zip:
        with zipfile.ZipFile(temp_zip.name, 'w') as zipf:
            # 获取上传文件的文件名
            file_name = Path(file.name).name
            # 将上传的TXT文件写入ZIP文件中
            zipf.write(file.name, arcname=file_name)
    # 返回生成的ZIP文件路径
    return temp_zip.name

def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("## Configuration Fields")
        config_fields = {
            "api_key": gr.Textbox(label="API Key", value="", placeholder="Enter API Key"),
            "base_url": gr.Textbox(label="Base URL", value="https://api.deepseek.com", placeholder="Enter Base URL"),
            "model_name": gr.Textbox(label="Model Name", value="deepseek-chat", placeholder="Enter Model Name"),
            "count_threshold": gr.Slider(label="Count Threshold", minimum=1, maximum=30, step=1, value=2),
            "request_timeout": gr.Slider(label="Request Timeout (seconds)", minimum=30, maximum=300, step=10, value=180),
            "request_frequency_threshold": gr.Slider(label="Request Frequency Threshold (requests/second)", minimum=1, maximum=500, step=1, value=1),
            "translate_surface": gr.Checkbox(label="Translate Surface Terms", value=True),
            "translate_context_per": gr.Checkbox(label="Translate Person Name Context", value=False),
            "translate_context_other": gr.Checkbox(label="Translate Other Entity Context", value=False),
        }
        
        with gr.Row():
            file_input = gr.File(label="Upload TXT File", file_types=[".txt"])
            zip_output = gr.File(label="Download ZIP File", file_types=[".zip"])

        def process_file(file, *args):
            config = {}
            for i, key in enumerate(config_fields.keys()):
                config[key] = args[i]
            return process(file, config)
        
        inputs =  [file_input] + list(config_fields.values())
        file_input.change(process_file, inputs=inputs, outputs=zip_output)
    
    demo.launch()

if __name__ == "__main__":
    create_interface()

image

PipeYume avatar Aug 27 '24 07:08 PipeYume

目前设想的大概UI形状,Process还未实现。

import gradio as gr
import zipfile
import tempfile
from pathlib import Path
def process(file, config):
    # 创建一个临时的ZIP文件
    with tempfile.NamedTemporaryFile(prefix=Path(file.name).stem,suffix=".zip", delete=False) as temp_zip:
        with zipfile.ZipFile(temp_zip.name, 'w') as zipf:
            # 获取上传文件的文件名
            file_name = Path(file.name).name
            # 将上传的TXT文件写入ZIP文件中
            zipf.write(file.name, arcname=file_name)
    # 返回生成的ZIP文件路径
    return temp_zip.name

def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("## Configuration Fields")
        config_fields = {
            "api_key": gr.Textbox(label="API Key", value="", placeholder="Enter API Key"),
            "base_url": gr.Textbox(label="Base URL", value="https://api.deepseek.com", placeholder="Enter Base URL"),
            "model_name": gr.Textbox(label="Model Name", value="deepseek-chat", placeholder="Enter Model Name"),
            "count_threshold": gr.Slider(label="Count Threshold", minimum=1, maximum=30, step=1, value=2),
            "request_timeout": gr.Slider(label="Request Timeout (seconds)", minimum=30, maximum=300, step=10, value=180),
            "request_frequency_threshold": gr.Slider(label="Request Frequency Threshold (requests/second)", minimum=1, maximum=500, step=1, value=1),
            "translate_surface": gr.Checkbox(label="Translate Surface Terms", value=True),
            "translate_context_per": gr.Checkbox(label="Translate Person Name Context", value=False),
            "translate_context_other": gr.Checkbox(label="Translate Other Entity Context", value=False),
        }
        
        with gr.Row():
            file_input = gr.File(label="Upload TXT File", file_types=[".txt"])
            zip_output = gr.File(label="Download ZIP File", file_types=[".zip"])

        def process_file(file, *args):
            config = {}
            for i, key in enumerate(config_fields.keys()):
                config[key] = args[i]
            return process(file, config)
        
        inputs =  [file_input] + list(config_fields.values())
        file_input.change(process_file, inputs=inputs, outputs=zip_output)
    
    demo.launch()

if __name__ == "__main__":
    create_interface()

image

感谢建议,等我把韩文模型炼完了研究一下看看

哪里有 ZeroGPU 相关的说明吗,我找了一下好像没找到

neavo avatar Aug 27 '24 16:08 neavo

@neavo https://huggingface.co/new-space?sdk=gradio

huggingface space里 用 gradio创建程序的话,可以用ZeroGPU

PipeYume avatar Aug 27 '24 16:08 PipeYume

已支持 MANUAL_BUILD_v0.20.0

neavo avatar Jun 11 '25 08:06 neavo