ching3 commited on
Commit
c8ac2e5
·
verified ·
1 Parent(s): dee550e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -64
app.py CHANGED
@@ -1,67 +1,28 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- # Load model directly
4
- from transformers import AutoModelForCausalLM
5
- client = AutoModelForCausalLM.from_pretrained("openbmb/MiniCPM3-4B", trust_remote_code=True)
6
-
7
- """
8
- 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
9
- """
10
- # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
11
-
12
-
13
- def respond(
14
- message,
15
- history: list[tuple[str, str]],
16
- system_message,
17
- max_tokens,
18
- temperature,
19
- top_p,
20
- ):
21
- messages = [{"role": "system", "content": system_message}]
22
-
23
- for val in history:
24
- if val[0]:
25
- messages.append({"role": "user", "content": val[0]})
26
- if val[1]:
27
- messages.append({"role": "assistant", "content": val[1]})
28
-
29
- messages.append({"role": "user", "content": message})
30
-
31
- response = ""
32
-
33
- for message in client.chat_completion(
34
- messages,
35
- max_tokens=max_tokens,
36
- stream=True,
37
- temperature=temperature,
38
- top_p=top_p,
39
- ):
40
- token = message.choices[0].delta.content
41
-
42
- response += token
43
- yield response
44
-
45
-
46
- """
47
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
48
- """
49
- demo = gr.ChatInterface(
50
- respond,
51
- additional_inputs=[
52
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
53
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
54
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
55
- gr.Slider(
56
- minimum=0.1,
57
- maximum=1.0,
58
- value=0.95,
59
- step=0.05,
60
- label="Top-p (nucleus sampling)",
61
- ),
62
- ],
63
  )
64
 
65
-
66
- if __name__ == "__main__":
67
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # 使用 MiniCPM-3 模型和分词器
6
+ model_name = "IDEA-CCNL/Wenzhong-GPT2-3.5B" # 此为 MiniCPM-3 公开版模型的名称,可以替换成你的模型名称
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ # 定义生成回复的函数
11
+ def generate_response(input_text):
12
+ inputs = tokenizer.encode(input_text, return_tensors="pt")
13
+ # 为生成模型配置生成参数
14
+ outputs = model.generate(inputs, max_length=200, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95)
15
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
16
+ return response
17
+
18
+ # 使用 Gradio 创建聊天界面
19
+ iface = gr.Interface(
20
+ fn=generate_response,
21
+ inputs="text",
22
+ outputs="text",
23
+ title="MiniCPM-3 中文聊天机器人",
24
+ description="这是一个基于 MiniCPM-3 的简单聊天机器人,可以进行中文对话"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
27
+ # 启动 Gradio 应用
28
+ iface.launch()