File size: 3,072 Bytes
bf7bb36
d471a4c
bf7bb36
 
d297372
 
 
 
 
 
 
 
bf7bb36
 
 
 
 
 
e73caa2
2f72203
bf7bb36
 
798de62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf7bb36
 
 
 
5320c21
798de62
 
bf7bb36
700a24f
5320c21
 
 
 
 
 
 
 
 
 
bf7bb36
 
 
5320c21
 
798de62
5320c21
6a79721
 
5320c21
 
6a79721
 
 
 
bf7bb36
 
 
 
6a79721
 
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
import os
import sys
import subprocess

if not os.path.exists("ChatGLM-6b-onnx-u8s8"):
    subprocess.run(["git", "lfs", "install"])
    subprocess.run(["git", "clone", "https://huggingface.co/K024/ChatGLM-6b-onnx-u8s8"])
    os.chdir("ChatGLM-6b-onnx-u8s8")
    subprocess.run(["pip", "install", "-r", "requirements.txt"])
    sys.path.append(os.getcwd())
else:
    sys.path.append(os.path.join(os.getcwd(), "ChatGLM-6b-onnx-u8s8"))

from model import ChatGLMModel#, chat_template

model = ChatGLMModel()
# history = []

max_tokens = 2048
temperature = 0.7
top_p = 0.7
top_k = 50
prompt = """
現在有些文本,文本詳細且複雜。 它包含細節,可以縮減和綜合為關鍵要點。 你的任務是提取最重要的概念,重點關注主要思路,提供一個概述而不失去精髓。 你的總結應該:

• 簡潔但足夠充分,可以代表所有重要信息
• 使用正確的句式和連貫的流程
• 捕捉誰、什麼、何時、在哪裡、為什麼和如何
• 盡可能地保留原始風格和風格
• 你必須遵循“摘要”格式:

摘要:
用2至3個句子簡要陳述主要主題和主要發現。

主要要點:
• 要點1 - 最重要的發現或細節
• 要點2 - 第二重要的觀黵
• 要點3 - 第三個重要的信息
• 要點4(可選) - 另一個要點
• 要點5(可選) - 最後的關鍵總結要點

文本:
"""

def sum_chain_l1(text, p_bar):
    docs = []
    for i in p_bar(range(len(text)//2000+1)):
        t = text[i*2000:i*2000+2048]
        if len(t) > 0:
            for answer in model.generate_iterate(prompt+t,
                            max_generated_tokens=max_tokens,
                            top_k=top_k,
                            top_p=top_p,
                            temperature=temperature):
                yield f"{'='*8} {i}/{len(text)//2000+1} {'='*8}\n{answer}"
            docs.append(answer)
    return docs

def sum_chain_l2_deprecated(docs, p_bar):
    hist = docs[0]
    for doc in p_bar(docs[1:]):
        for answer in model.generate_iterate(prompt+"\n"+hist+"\n"+doc,
                    max_generated_tokens=max_tokens,
                    top_k=top_k,
                    top_p=top_p,
                    temperature=temperature):
            yield answer
        hist = answer
    return hist

import gradio as gr

def greet(text, progress=gr.Progress()):
    progress(0, desc="Reading")
    docs = []
    for doc in sum_chain_l1(text, progress.tqdm):
        yield '# drafting summary', doc
    # yield 'stage 1 finished', '\n===== summarized parts =====\n'.join(doc)
    progress(0, desc="Refinement")
    for ans in sum_chain_l2_deprecated(doc, progress.tqdm):
        yield '# refining summary', ans
    return '# final result', ans

gr.Markdown("# Text Summarization\nStage 1 = draft -> Stage 2 = refine -> final result")

iface = gr.Interface(fn=greet,
                     inputs=gr.Textbox(lines=20,
                                       placeholder="Text Here..."),
                     outputs=["text", "text"])
iface.queue(concurrency_count=3).launch()