Spaces:
Runtime error
Runtime error
liuwei
commited on
Commit
•
1abeaa7
1
Parent(s):
ec9bbbe
init
Browse files- app.py +36 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline, set_seed
|
3 |
+
|
4 |
+
# 设置全局随机种子,确保每次生成的结果相同
|
5 |
+
set_seed(42)
|
6 |
+
|
7 |
+
def app():
|
8 |
+
# 创建Streamlit应用程序
|
9 |
+
st.title("使用gpt2的文本生成")
|
10 |
+
input_text = st.text_input("请输入您要生成的文本,只支持英文", value="")
|
11 |
+
maxlen = st.text_input("请输入生成文本的最大长度,越长越慢,不要超过1000", value="30")
|
12 |
+
button_generate = st.button("生成")
|
13 |
+
output_text = st.empty()
|
14 |
+
|
15 |
+
def generate_text(input_text):
|
16 |
+
# 加载预训练模型
|
17 |
+
generator = pipeline("text-generation", model="/Users/admin/mypy/hugging-models/gpt2")
|
18 |
+
|
19 |
+
# 生成文本
|
20 |
+
output = generator(input_text, max_length=int(maxlen), num_return_sequences=1)
|
21 |
+
|
22 |
+
# 提取生成的文本
|
23 |
+
generated_text = output[0]["generated_text"].strip()
|
24 |
+
|
25 |
+
return generated_text
|
26 |
+
|
27 |
+
if button_generate:
|
28 |
+
# 生成文本
|
29 |
+
generated_text = generate_text(input_text)
|
30 |
+
|
31 |
+
# 显示生成的文本
|
32 |
+
output_text.success(generated_text)
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
# 运行应用程序
|
36 |
+
app()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
streamlit
|