Spaces:
Build error
Build error
雷娃
commited on
Commit
·
4ef74d7
1
Parent(s):
229772a
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# load model and tokenizer
|
7 |
+
model_name = "inclusionAI/Ling-lite-1.5"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
torch_dtype="auto",
|
12 |
+
device_map="auto",
|
13 |
+
trust_remote_code=True
|
14 |
+
).eval()
|
15 |
+
|
16 |
+
# define chat function
|
17 |
+
def chat(user_input, max_new_tokens=512):
|
18 |
+
# chat history
|
19 |
+
messages = [
|
20 |
+
{"role": "system", "content": "You are Ling, an assistant created by inclusionAI"},
|
21 |
+
{"role": "user", "content": user_input}
|
22 |
+
]
|
23 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
24 |
+
|
25 |
+
# encode the input prompt
|
26 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
27 |
+
|
28 |
+
# generate response
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = model.generate(
|
31 |
+
**inputs,
|
32 |
+
max_new_tokens=max_new_tokens,
|
33 |
+
pad_token_id=tokenizer.eos_token_id
|
34 |
+
)
|
35 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[-1]:], skip_special_tokens=True)
|
36 |
+
return response
|
37 |
+
|
38 |
+
# Construct Gradio Interface
|
39 |
+
interface = gr.Interface(
|
40 |
+
fn=chat,
|
41 |
+
inputs=[
|
42 |
+
gr.Textbox(lines=5, label="输入你的问题"),
|
43 |
+
gr.Slider(minimum=100, maximum=1024, step=50, label="生成长度")
|
44 |
+
],
|
45 |
+
outputs=gr.Textbox(label="模型回复"),
|
46 |
+
title="Ling-lite-1.5 MoE 模型 Demo",
|
47 |
+
description="基于 [inclusionAI/Ling-lite-1.5](https://huggingface.co/inclusionAI/Ling-lite-1.5) 的对话式文本生成演示。",
|
48 |
+
examples=[
|
49 |
+
["介绍大型语言模型的基本概念", 512],
|
50 |
+
["如何解决数学问题中的长上下文依赖?", 768]
|
51 |
+
]
|
52 |
+
)
|
53 |
+
|
54 |
+
# launch Gradion Service
|
55 |
+
interface.launch()
|