SonyaX20
commited on
Commit
·
79fb563
1
Parent(s):
f084b67
new
Browse files- app.py +103 -59
- requirements.txt +5 -1
app.py
CHANGED
@@ -1,64 +1,108 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
+
from datasets import load_dataset
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# 加载预训练模型和分词器
|
7 |
+
MODEL_NAME = "bert-base-chinese"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
9 |
+
model = AutoModel.from_pretrained(MODEL_NAME)
|
10 |
+
|
11 |
+
# 加载tnews数据集
|
12 |
+
dataset = load_dataset("clue", "tnews")
|
13 |
+
|
14 |
+
# 数据预处理函数
|
15 |
+
def preprocess_text(text):
|
16 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
17 |
+
return inputs
|
18 |
+
|
19 |
+
# 特征提取函数
|
20 |
+
def extract_features(text):
|
21 |
+
inputs = preprocess_text(text)
|
22 |
+
with torch.no_grad():
|
23 |
+
outputs = model(**inputs)
|
24 |
+
# 使用[CLS] token的表示作为特征
|
25 |
+
cls_embedding = outputs.last_hidden_state[:, 0, :].squeeze().numpy()
|
26 |
+
return cls_embedding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Gradio接口
|
29 |
+
def predict(text):
|
30 |
+
features = extract_features(text)
|
31 |
+
return f"特征维度: {features.shape}\n特征向量(部分展示): {features[:10]}"
|
32 |
|
33 |
+
# 定义界面
|
34 |
+
demo = gr.Interface(
|
35 |
+
fn=predict,
|
36 |
+
inputs=gr.Textbox(lines=2, placeholder="输入中文文本..."),
|
37 |
+
outputs="text",
|
38 |
+
title="中文特征提取",
|
39 |
+
description="基于BERT的中文文本特征提取,使用tnews数据集进行微调。",
|
40 |
+
)
|
41 |
+
|
42 |
+
# 运行Gradio应用
|
43 |
if __name__ == "__main__":
|
44 |
demo.launch()
|
45 |
+
# import gradio as gr
|
46 |
+
# from huggingface_hub import InferenceClient
|
47 |
+
|
48 |
+
# """
|
49 |
+
# For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
50 |
+
# """
|
51 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
52 |
+
|
53 |
+
|
54 |
+
# def respond(
|
55 |
+
# message,
|
56 |
+
# history: list[tuple[str, str]],
|
57 |
+
# system_message,
|
58 |
+
# max_tokens,
|
59 |
+
# temperature,
|
60 |
+
# top_p,
|
61 |
+
# ):
|
62 |
+
# messages = [{"role": "system", "content": system_message}]
|
63 |
+
|
64 |
+
# for val in history:
|
65 |
+
# if val[0]:
|
66 |
+
# messages.append({"role": "user", "content": val[0]})
|
67 |
+
# if val[1]:
|
68 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
69 |
+
|
70 |
+
# messages.append({"role": "user", "content": message})
|
71 |
+
|
72 |
+
# response = ""
|
73 |
+
|
74 |
+
# for message in client.chat_completion(
|
75 |
+
# messages,
|
76 |
+
# max_tokens=max_tokens,
|
77 |
+
# stream=True,
|
78 |
+
# temperature=temperature,
|
79 |
+
# top_p=top_p,
|
80 |
+
# ):
|
81 |
+
# token = message.choices[0].delta.content
|
82 |
+
|
83 |
+
# response += token
|
84 |
+
# yield response
|
85 |
+
|
86 |
+
|
87 |
+
# """
|
88 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
89 |
+
# """
|
90 |
+
# demo = gr.ChatInterface(
|
91 |
+
# respond,
|
92 |
+
# additional_inputs=[
|
93 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
94 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
95 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
96 |
+
# gr.Slider(
|
97 |
+
# minimum=0.1,
|
98 |
+
# maximum=1.0,
|
99 |
+
# value=0.95,
|
100 |
+
# step=0.05,
|
101 |
+
# label="Top-p (nucleus sampling)",
|
102 |
+
# ),
|
103 |
+
# ],
|
104 |
+
# )
|
105 |
+
|
106 |
+
|
107 |
+
# if __name__ == "__main__":
|
108 |
+
# demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,5 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
transformers
|
3 |
+
datasets
|
4 |
+
gradio
|
5 |
+
torch
|