Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer
|
| 3 |
+
from optimum.onnxruntime import ORTModelForCausalLM, ORTOptions
|
| 4 |
+
|
| 5 |
+
# 选超轻量模型:微软Phi-3-mini(仅38亿参数,推理极快)
|
| 6 |
+
model_name = "microsoft/Phi-3-mini-4k-instruct-ONNX"
|
| 7 |
+
|
| 8 |
+
# 开启INT8量化+动态批处理,CPU计算量直接减半
|
| 9 |
+
options = ORTOptions(enable_int8=True, enable_dynamic_quantization=True)
|
| 10 |
+
model = ORTModelForCausalLM.from_pretrained(model_name, from_transformers=True, ort_options=options)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
def generate_text(input_texts):
|
| 14 |
+
inputs = tokenizer(input_texts, return_tensors="pt", padding=True, truncation=True, max_length=32)
|
| 15 |
+
outputs = model.generate(
|
| 16 |
+
**inputs,
|
| 17 |
+
max_new_tokens=8, # 生成长度砍到极致
|
| 18 |
+
temperature=0.1, # 温度调低,减少随机计算
|
| 19 |
+
do_sample=False,
|
| 20 |
+
num_beams=1,
|
| 21 |
+
early_stopping=True # 到句号就停,不做无用功
|
| 22 |
+
)
|
| 23 |
+
return tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 24 |
+
|
| 25 |
+
# 界面支持多行输入(批量处理请求,CPU利用率拉满)
|
| 26 |
+
iface = gr.Interface(fn=generate_text, inputs=gr.Textbox(multiline=True), outputs="text")
|
| 27 |
+
iface.launch()
|