Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
llm = pipeline("text-generation", model="gpt2") # 替换为你喜欢的模型
|
| 5 |
+
|
| 6 |
+
def run_chain(prompts):
|
| 7 |
+
results = []
|
| 8 |
+
context = ""
|
| 9 |
+
for p in prompts:
|
| 10 |
+
prompt = p.replace("{input}", context)
|
| 11 |
+
response = llm(prompt, max_new_tokens=100)[0]["generated_text"]
|
| 12 |
+
# 提取新增部分
|
| 13 |
+
new_output = response[len(prompt):].strip()
|
| 14 |
+
results.append(new_output)
|
| 15 |
+
context = new_output
|
| 16 |
+
return results
|
| 17 |
+
|
| 18 |
+
with gr.Blocks() as demo:
|
| 19 |
+
prompt_boxes = []
|
| 20 |
+
output_boxes = []
|
| 21 |
+
|
| 22 |
+
with gr.Row():
|
| 23 |
+
prompt_list = gr.State([])
|
| 24 |
+
out_list = gr.State([])
|
| 25 |
+
|
| 26 |
+
def add_prompt(prompt_list, out_list):
|
| 27 |
+
prompt_list.append("")
|
| 28 |
+
out_list.append("")
|
| 29 |
+
return prompt_list, out_list
|
| 30 |
+
|
| 31 |
+
add_btn = gr.Button("➕ 添加一个 Prompt")
|
| 32 |
+
run_btn = gr.Button("🚀 运行 Chain")
|
| 33 |
+
|
| 34 |
+
prompts_display = gr.Textbox(lines=15, label="Prompt Chain (用{input}引用上一轮输出)", interactive=True, elem_id="prompt_chain")
|
| 35 |
+
outputs_display = gr.Textbox(lines=15, label="模型输出", interactive=False)
|
| 36 |
+
|
| 37 |
+
def run_all(prompts_str):
|
| 38 |
+
prompts = prompts_str.strip().split("\n---\n")
|
| 39 |
+
outputs = run_chain(prompts)
|
| 40 |
+
return "\n---\n".join(outputs)
|
| 41 |
+
|
| 42 |
+
add_btn.click(fn=add_prompt, inputs=[prompt_list, out_list], outputs=[prompt_list, out_list])
|
| 43 |
+
run_btn.click(fn=run_all, inputs=prompts_display, outputs=outputs_display)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|