akhaliq HF Staff commited on
Commit
88d1ce9
·
verified ·
1 Parent(s): 57e2721

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import spaces
5
+
6
+ model_name = "baidu/ERNIE-4.5-21B-A3B-Thinking"
7
+
8
+ # Load the tokenizer and the model
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_name,
12
+ device_map="auto",
13
+ torch_dtype=torch.bfloat16,
14
+ )
15
+
16
+ @spaces.GPU(duration=120)
17
+ def chat(message, history):
18
+ messages = []
19
+ for user_msg, assistant_msg in history:
20
+ if user_msg is not None:
21
+ messages.append({"role": "user", "content": user_msg})
22
+ if assistant_msg is not None:
23
+ messages.append({"role": "assistant", "content": assistant_msg})
24
+ messages.append({"role": "user", "content": message})
25
+
26
+ text = tokenizer.apply_chat_template(
27
+ messages,
28
+ tokenize=False,
29
+ add_generation_prompt=True
30
+ )
31
+ model_inputs = tokenizer([text], add_special_tokens=False, return_tensors="pt").to(model.device)
32
+
33
+ with torch.no_grad():
34
+ generated_ids = model.generate(
35
+ **model_inputs,
36
+ max_new_tokens=1024,
37
+ do_sample=True,
38
+ temperature=0.7,
39
+ )
40
+
41
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
42
+ generate_text = tokenizer.decode(output_ids, skip_special_tokens=True)
43
+
44
+ history.append((message, generate_text))
45
+ return history, ""
46
+
47
+ with gr.Blocks(title="ERNIE Chat") as demo:
48
+ gr.Markdown("# ERNIE-4.5-21B-A3B-Thinking Chat App")
49
+ chatbot = gr.Chatbot(
50
+ height=500,
51
+ type="messages",
52
+ show_copy_button=True,
53
+ avatar_images=("user_icon.png", "bot_icon.png") # Optional: add icons if available
54
+ )
55
+ msg = gr.Textbox(
56
+ placeholder="Type your message here...",
57
+ show_label=False,
58
+ container=True,
59
+ scale=7,
60
+ )
61
+ with gr.Row():
62
+ clear_btn = gr.Button("Clear", variant="secondary")
63
+
64
+ msg.submit(chat, [msg, chatbot], [chatbot, msg])
65
+ clear_btn.click(lambda: ([], ""), None, chatbot, queue=False)
66
+
67
+ if __name__ == "__main__":
68
+ demo.launch()