Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,24 +3,24 @@ from transformers import pipeline
|
|
3 |
|
4 |
# function part
|
5 |
# img2text
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
|
25 |
# text2audio
|
26 |
def text2audio(story_text):
|
@@ -43,13 +43,12 @@ if uploaded_file is not None:
|
|
43 |
|
44 |
#Stage 1: Image to Text
|
45 |
st.text('Processing img2text...')
|
46 |
-
|
47 |
-
|
48 |
|
49 |
#Stage 2: Text to Story
|
50 |
st.text('Generating a story...')
|
51 |
-
|
52 |
-
story = "children playing in the park illustration and making fun of other characters."
|
53 |
st.write(story)
|
54 |
|
55 |
#Stage 3: Story to Audio data
|
|
|
3 |
|
4 |
# function part
|
5 |
# img2text
|
6 |
+
def img2text(url):
|
7 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
8 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
9 |
+
return text
|
10 |
|
11 |
+
# text2story
|
12 |
+
def text2story(text):
|
13 |
+
text_to_story = pipeline("text-generation", model="isarth/distill_gpt2_story_generator",
|
14 |
+
max_length=300, # 增加最大生成长度
|
15 |
+
min_length=100, # 设置最小生成长度
|
16 |
+
do_sample=True, # 启用随机采样
|
17 |
+
temperature=0.9, # 控制随机性(0-1,越大越随机)
|
18 |
+
top_k=50, # 限制候选词数量
|
19 |
+
top_p=0.95, # 核采样参数
|
20 |
+
repetition_penalty=1.2)
|
21 |
+
# story_text = "" # to be completed
|
22 |
+
story_text = text_to_story(text)[0]["generated_text"]
|
23 |
+
return story_text
|
24 |
|
25 |
# text2audio
|
26 |
def text2audio(story_text):
|
|
|
43 |
|
44 |
#Stage 1: Image to Text
|
45 |
st.text('Processing img2text...')
|
46 |
+
scenario = img2text(uploaded_file.name)
|
47 |
+
st.write(scenario)
|
48 |
|
49 |
#Stage 2: Text to Story
|
50 |
st.text('Generating a story...')
|
51 |
+
story = text2story(scenario)
|
|
|
52 |
st.write(story)
|
53 |
|
54 |
#Stage 3: Story to Audio data
|