Spaces:
Running
Running
ganchengguang
commited on
Commit
•
d07888f
1
Parent(s):
3165aa7
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# 加载本地模型和tokenizer
|
5 |
+
model_name = "ganchengguang/OIELLM-8B-Instruction" # 替换为你的模型名称
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# 定义语言和选项的映射
|
10 |
+
options = {
|
11 |
+
'English': {'NER': '/NER/', 'Sentimentrw': '/Sentiment related word/', 'Sentimentadjn': '/Sentiment Adj and N/', 'Sentimentadj': '/Sentiment Adj/', 'Sentimentn': '/Sentiment N/', 'Relation': '/relation extraction/', 'Event': '/event extraction/'},
|
12 |
+
'中文': {'NER': '/实体命名识别/', 'Sentimentrw': '/感情分析关联单词/', 'Sentimentadjn': '/感情分析形容词名词/', 'Sentimentadj': '/感情分析形容词/', 'Sentimentn': '/感情分析名词/', 'Relation': '/关系抽取/', 'Event': '/事件抽取/'},
|
13 |
+
'日本語': {'NER': '/固有表現抽出/', 'Sentimentrw': '/感情分析関連単語/', 'Sentimentadjn': '/感情分析形容詞名詞/', 'Sentimentadj': '/感情分析形容詞/', 'Sentimentn': '/感情分析名詞/', 'Relation': '/関係抽出/', 'Event': '/事件抽出/'}
|
14 |
+
}
|
15 |
+
|
16 |
+
# 定义聊天函数
|
17 |
+
def respond(message, language, task, system_message, max_tokens, temperature, top_p):
|
18 |
+
# 初始化对话历史
|
19 |
+
messages = [{"role": "system", "content": system_message}]
|
20 |
+
messages.append({"role": "user", "content": message + " " + options[language][task]})
|
21 |
+
|
22 |
+
# 编码输入
|
23 |
+
inputs = tokenizer(messages, return_tensors="pt", padding=True, truncation=True)
|
24 |
+
# 生成回复
|
25 |
+
outputs = model.generate(
|
26 |
+
inputs["input_ids"],
|
27 |
+
max_length=max_tokens,
|
28 |
+
temperature=temperature,
|
29 |
+
top_p=top_p,
|
30 |
+
do_sample=True
|
31 |
+
)
|
32 |
+
# 解码回复
|
33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
34 |
+
yield response
|
35 |
+
|
36 |
+
# 更新任务选项的函数
|
37 |
+
def update_tasks(language):
|
38 |
+
return gr.update(choices=list(options[language].keys()))
|
39 |
+
|
40 |
+
# 创建Gradio接口
|
41 |
+
demo = gr.ChatInterface(
|
42 |
+
respond,
|
43 |
+
inputs=[
|
44 |
+
gr.Textbox(label="Input Text"),
|
45 |
+
gr.Dropdown(label="Language", choices=list(options.keys()), value="English"),
|
46 |
+
gr.Dropdown(label="Task", choices=list(options['English'].keys())),
|
47 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
48 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
49 |
+
gr.Slider(minimum=0.1, maximum4.0, value=0.7, step=0.1, label="Temperature"),
|
50 |
+
gr.Slider(
|
51 |
+
minimum=0.1,
|
52 |
+
maximum=1.0,
|
53 |
+
value=0.95,
|
54 |
+
step=0.05,
|
55 |
+
label="Top-p (nucleus sampling)"
|
56 |
+
),
|
57 |
+
],
|
58 |
+
live=True
|
59 |
+
)
|
60 |
+
|
61 |
+
# 设置语言选择框的动态更新
|
62 |
+
demo.components[1].change(update_tasks, inputs=demo.components[1], outputs=demo.components[2])
|
63 |
+
|
64 |
+
if __name__ == "__main__":
|
65 |
+
demo.launch()
|