File size: 4,191 Bytes
24ff201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sys
import time
import warnings
from pathlib import Path


# 配置hugface环境
from huggingface_hub import hf_hub_download
import gradio as gr
import os
import glob
import json

# os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
# torch.set_float32_matmul_precision("high")



def instruct_generate(
    img_path: str = " ",
    prompt: str = "What food do lamas eat?",
    input: str = "",
    max_new_tokens: int = 100,
    top_k: int = 200,
    temperature: float = 0.8,
) -> None:
    """Generates a response based on a given instruction and an optional input.
    This script will only work with checkpoints from the instruction-tuned LLaMA-Adapter model.
    See `finetune_adapter.py`.

    Args:
        prompt: The prompt/instruction (Alpaca style).
        adapter_path: Path to the checkpoint with trained adapter weights, which are the output of
            `finetune_adapter.py`.
        input: Optional input (Alpaca style).
        pretrained_path: The path to the checkpoint with pretrained LLaMA weights.
        tokenizer_path: The tokenizer path to load.
        quantize: Whether to quantize the model and using which method:
            ``"llm.int8"``: LLM.int8() mode,
            ``"gptq.int4"``: GPTQ 4-bit mode.
        max_new_tokens: The number of generation steps to take.
        top_k: The number of top most probable tokens to consider in the sampling process.
        temperature: A value controlling the randomness of the sampling process. Higher values result in more random
    """
    output = [prompt, input, max_new_tokens, top_k, temperature]
    print(output)
    return output

# 配置具体参数

example_path = "example.json"
# 1024如果不够, 调整为512
max_seq_len = 1024
max_batch_size = 1

with open(example_path, 'r') as f:
    content = f.read()
    example_dict = json.loads(content)


def create_instruct_demo():
    with gr.Blocks() as instruct_demo:
        with gr.Row():
            with gr.Column():
                scene_img = gr.Image(label='Scene', type='filepath')
                object_list = gr.Textbox(
                    lines=2, label="Input")

                instruction = gr.Textbox(
                    lines=2, label="Instruction")
                max_len = gr.Slider(minimum=1, maximum=512,
                                    value=128, label="Max length")
                with gr.Accordion(label='Advanced options', open=False):
                    temp = gr.Slider(minimum=0, maximum=1,
                                     value=0.8, label="Temperature")
                    top_k = gr.Slider(minimum=100, maximum=300,
                                      value=200, label="Top k")

                run_botton = gr.Button("Run")

            with gr.Column():
                outputs = gr.Textbox(lines=10, label="Output")

        inputs = [instruction, object_list, max_len, top_k, temp]

        # 接下来设定具体的example格式
        examples_img_list = glob.glob("caption_demo/*.png")
        examples = []
        for example_img_one in examples_img_list:
            scene_name = os.path.basename(example_img_one).split(".")[0]
            example_object_list = example_dict[scene_name]["input"]
            example_instruction = example_dict[scene_name]["instruction"]
            example_one = [example_img_one, example_object_list, example_instruction, 512, 0.8, 200]
            examples.append(example_one)

        gr.Examples(
            examples=examples,
            inputs=inputs,
            outputs=outputs,
            fn=instruct_generate,
            cache_examples=os.getenv('SYSTEM') == 'spaces'
        )
        run_botton.click(fn=instruct_generate, inputs=inputs, outputs=outputs)
    return instruct_demo


# Please refer to our [arXiv paper](https://arxiv.org/abs/2303.16199) and [github](https://github.com/ZrrSkywalker/LLaMA-Adapter) for more details.
description = """
# TaPA
The official demo for **Embodied Task Planning with Large Language Models**.
"""

with gr.Blocks(css='style.css') as demo:
    gr.Markdown(description)
    with gr.TabItem("Instruction-Following"):
        create_instruct_demo()

demo.queue(api_open=True, concurrency_count=1).launch()