Leo Liu
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -16,39 +16,10 @@ def img2text(url):
|
|
16 |
|
17 |
# text2story
|
18 |
def text2story(text):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
# 初始化生成管道
|
24 |
-
pipe = pipeline(
|
25 |
-
"text-generation",
|
26 |
-
model="pranavpsv/genre-story-generator-v2",
|
27 |
-
max_new_tokens=180,
|
28 |
-
min_new_tokens=130,
|
29 |
-
temperature=0.7,
|
30 |
-
pad_token_id=50256 # 添加模型专用参数
|
31 |
-
)
|
32 |
-
|
33 |
-
# 生成原始文本
|
34 |
-
raw_output = pipe(full_prompt, return_full_text=False)[0]['generated_text']
|
35 |
-
|
36 |
-
# 增强版清洗逻辑
|
37 |
-
def clean_output(generated_text):
|
38 |
-
# 使用正则表达式匹配提示词块
|
39 |
-
prompt_pattern = re.compile(
|
40 |
-
r'\[PROMPT_START\].*?\[PROMPT_END\]',
|
41 |
-
re.DOTALL # 匹配多行内容
|
42 |
-
)
|
43 |
-
# 移除整个提示词块
|
44 |
-
cleaned = re.sub(prompt_pattern, '', generated_text)
|
45 |
-
|
46 |
-
# 二次清理残留内容
|
47 |
-
cleaned = re.sub(r'^Write a children.*?\n', '', cleaned) # 处理可能的开头残留
|
48 |
-
cleaned = re.sub(r'Requirements:.*?\n', '', cleaned) # 移除要求残留
|
49 |
-
return cleaned.strip()
|
50 |
-
|
51 |
-
return clean_output(raw_output)
|
52 |
|
53 |
|
54 |
# text2audio
|
|
|
16 |
|
17 |
# text2story
|
18 |
def text2story(text):
|
19 |
+
prompt = f"Write a children's story for ages 3-10 based on: {text}"
|
20 |
+
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2", max_new_tokens=160, min_new_tokens=130, num_return_sequences=1)
|
21 |
+
story_text = pipe(prompt, return_full_text=False)[0]['generated_text']
|
22 |
+
return story_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
|
25 |
# text2audio
|