yxccai commited on
Commit
7fc4a4d
·
verified ·
1 Parent(s): 11ceba3

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +31 -0
  2. app.py +71 -0
  3. requirements.txt +4 -0
README.md ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: 文本风格转换API
3
+ emoji: 🔄
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 3.40.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # 文本风格转换API
13
+
14
+ 这是一个基于自定义微调模型的中文文本风格转换应用,可以将书面化、技术性文本转换为自然、口语化的表达方式。
15
+
16
+ ## 功能特点
17
+ - 支持中文医学、化学等专业文本的通俗化改写
18
+ - 实时在线转换
19
+ - 免费API服务
20
+
21
+ ## API调用
22
+ 部署后可通过以下方式调用:
23
+ ```python
24
+ import requests
25
+
26
+ response = requests.post(
27
+ "https://yxccai-text-style-api.hf.space/api/predict",
28
+ json={"data": ["您要转换的文本"]}
29
+ )
30
+ result = response.json()
31
+ print(result["data"][0])
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # 加载模型和tokenizer
6
+ model_name = "yxccai/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()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers>=4.30.0
2
+ torch>=2.0.0
3
+ gradio>=3.40.0
4
+ accelerate>=0.20.0