dinghface commited on
Commit
d40fc8d
·
verified ·
1 Parent(s): 9f89551

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +95 -52
app.py CHANGED
@@ -1,69 +1,112 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
 
 
 
 
 
 
 
 
10
  temperature,
11
  top_p,
12
- hf_token: gr.OAuthToken,
 
 
13
  ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
 
 
 
 
 
 
18
 
19
- messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
 
 
 
 
 
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
24
 
25
- response = ""
 
 
 
 
 
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = 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
- chatbot = 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
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
 
 
 
 
 
 
66
 
 
 
 
 
 
 
67
 
68
  if __name__ == "__main__":
69
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
 
5
+ MODEL_NAME = "dinghface/olmo3-190m-zh-full-continue"
6
 
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ MODEL_NAME,
10
+ torch_dtype=torch.bfloat16,
11
+ device_map="auto",
12
+ )
13
+
14
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
15
+
16
+
17
+ def generate(
18
+ prompt,
19
+ max_new_tokens,
20
  temperature,
21
  top_p,
22
+ top_k,
23
+ repetition_penalty,
24
+ do_sample,
25
  ):
26
+ output = pipe(
27
+ prompt,
28
+ max_new_tokens=int(max_new_tokens),
29
+ do_sample=do_sample,
30
+ temperature=temperature if do_sample else None,
31
+ top_p=top_p if do_sample else None,
32
+ top_k=int(top_k) if do_sample else None,
33
+ repetition_penalty=repetition_penalty,
34
+ )
35
+ return output[0]["generated_text"]
36
 
 
37
 
38
+ EXAMPLES = [
39
+ ["从前有座山,山里有座庙,", 256, 0.8, 0.9, 50, 1.2, True],
40
+ ["人工智能是", 256, 0.7, 0.9, 50, 1.2, True],
41
+ ["今天天气不错,我准备", 256, 0.8, 0.9, 50, 1.2, True],
42
+ ["Python 是一种", 256, 0.7, 0.9, 50, 1.2, True],
43
+ ["春天来了,万物复苏,", 256, 0.9, 0.95, 50, 1.1, True],
44
+ ["在很久很久以前,", 256, 0.85, 0.9, 40, 1.2, True],
45
+ ["The meaning of life is", 256, 0.8, 0.9, 50, 1.2, True],
46
+ ["deep learning is", 256, 0.7, 0.9, 50, 1.2, True],
47
+ ]
48
 
49
+ with gr.Blocks(title="OLMo3-190M-zh Continue Pretrain Demo") as demo:
50
+ gr.Markdown(
51
+ """
52
+ # OLMo3-190M-zh 持续预训练 Demo
53
+ 基于 [OLMo3-190M-zh-full](https://huggingface.co/dinghface/olmo3-190m-zh-full) 进行持续预训练的 190M 参数中文模型。
54
+ 输入一段文字,模型会自动续写。
55
+ """
56
+ )
57
 
58
+ with gr.Row():
59
+ with gr.Column(scale=2):
60
+ prompt = gr.Textbox(
61
+ label="输入提示词",
62
+ placeholder="在这里输入文字,模型会继续往下写...",
63
+ lines=5,
64
+ )
65
+ output = gr.Textbox(label="生成结果", lines=10)
66
+ with gr.Row():
67
+ submit_btn = gr.Button("生成", variant="primary")
68
+ clear_btn = gr.Button("清空")
69
 
70
+ with gr.Column(scale=1):
71
+ do_sample = gr.Checkbox(label="启用采样(关闭则为贪心解码)", value=True)
72
+ max_new_tokens = gr.Slider(
73
+ minimum=16, maximum=1024, value=256, step=16, label="最大生成长度"
74
+ )
75
+ temperature = gr.Slider(
76
+ minimum=0.1, maximum=2.0, value=0.8, step=0.05, label="Temperature(温度)"
77
+ )
78
+ top_p = gr.Slider(
79
+ minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p(核采样)"
80
+ )
81
+ top_k = gr.Slider(
82
+ minimum=1, maximum=200, value=50, step=1, label="Top-k"
83
+ )
84
+ repetition_penalty = gr.Slider(
85
+ minimum=1.0, maximum=2.0, value=1.2, step=0.05, label="重复惩罚"
86
+ )
87
 
88
+ gr.Examples(
89
+ examples=EXAMPLES,
90
+ inputs=[prompt, max_new_tokens, temperature, top_p, top_k, repetition_penalty, do_sample],
91
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
+ gr.Markdown(
94
+ """
95
+ ### 参数说明
96
+ - **Temperature**:越高越随机,越低越确定。1.0 为默认,<1 更保守,>1 更有创意
97
+ - **Top-p**:核采样,从累积概率达到该值的 token 中采样。1.0 不过滤
98
+ - **Top-k**:只从概率最高的 k 个 token 中采样。值越大选择越多
99
+ - **重复惩罚**:>1 时惩罚重复内容,避免循环输出
100
+ - **启用采样**:关闭后使用贪心解码(每次选概率最高的 token),输出确定但单一
101
+ """
102
+ )
103
 
104
+ submit_btn.click(
105
+ fn=generate,
106
+ inputs=[prompt, max_new_tokens, temperature, top_p, top_k, repetition_penalty, do_sample],
107
+ outputs=output,
108
+ )
109
+ clear_btn.click(fn=lambda: ("", ""), inputs=None, outputs=[prompt, output])
110
 
111
  if __name__ == "__main__":
112
  demo.launch()