Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
# 加载模型和tokenizer
|
| 6 |
-
model_name = "您的用户名/text-style-converter"
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
model_name,
|
| 10 |
-
torch_dtype=torch.float16,
|
| 11 |
-
device_map="auto"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
def convert_text_style(input_text):
|
| 15 |
-
"""文本风格转换函数"""
|
| 16 |
-
if not input_text.strip():
|
| 17 |
-
return "请输入要转换的文本"
|
| 18 |
-
|
| 19 |
-
prompt = f"""以下是一个文本风格转换任务,请将书面化、技术性的输入文本转换为自然、口语化的表达方式。
|
| 20 |
-
|
| 21 |
-
### 输入文本:
|
| 22 |
-
{input_text}
|
| 23 |
-
|
| 24 |
-
### 输出文本:
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 28 |
-
|
| 29 |
-
with torch.no_grad():
|
| 30 |
-
outputs = model.generate(
|
| 31 |
-
inputs.input_ids,
|
| 32 |
-
attention_mask=inputs.attention_mask,
|
| 33 |
-
max_new_tokens=500,
|
| 34 |
-
temperature=0.7,
|
| 35 |
-
do_sample=True,
|
| 36 |
-
pad_token_id=tokenizer.eos_token_id
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
-
|
| 41 |
-
# 提取生成的部分
|
| 42 |
-
if "### 输出文本:" in full_response:
|
| 43 |
-
response = full_response.split("### 输出文本:")[-1].strip()
|
| 44 |
-
else:
|
| 45 |
-
response = full_response
|
| 46 |
-
|
| 47 |
-
return response
|
| 48 |
-
|
| 49 |
-
# 创建Gradio接口
|
| 50 |
-
iface = gr.Interface(
|
| 51 |
-
fn=convert_text_style,
|
| 52 |
-
inputs=gr.Textbox(
|
| 53 |
-
label="输入文本",
|
| 54 |
-
placeholder="请输入需要转换为口语化的书面文本...",
|
| 55 |
-
lines=5
|
| 56 |
-
),
|
| 57 |
-
outputs=gr.Textbox(
|
| 58 |
-
label="输出文本",
|
| 59 |
-
lines=5
|
| 60 |
-
),
|
| 61 |
-
title="中文文本风格转换API",
|
| 62 |
-
description="将书面化、技术性文本转换为自然、口语化表达",
|
| 63 |
-
examples=[
|
| 64 |
-
["乙醇的检测方法包括以下几项: 1. 酸碱度检查:取20ml乙醇加20ml水,加2滴酚酞指示剂应无色。"],
|
| 65 |
-
["本品为薄膜衣片,除去包衣后显橙红色至暗红色。"]
|
| 66 |
-
]
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# 启动应用
|
| 70 |
-
if __name__ == "__main__":
|
| 71 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|