Bondya commited on
Commit
e698bdd
ยท
verified ยท
1 Parent(s): 8322a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -38
app.py CHANGED
@@ -7,21 +7,19 @@ import os
7
 
8
  def img2text(img_path):
9
  captioner = pipeline(
10
- "image-to-text",
11
- model="nlpconnect/vit-gpt2-image-captioning",
12
- device_map="auto"
13
  )
14
  result = captioner(img_path)
15
  return result[0]["generated_text"]
16
 
17
  def text2story(scenario):
18
  generator = pipeline(
19
- "text-generation",
20
- model="gpt2",
21
- trust_remote_code=True,
22
- device_map="auto", # ๅฟ…้กปๆทปๅŠ 
23
- torch_dtype=torch.float16,
24
- max_length=100,
25
  num_return_sequences=1
26
  )
27
  prompt = f"Create a children's story based on: {scenario}"
@@ -29,17 +27,22 @@ def text2story(scenario):
29
  return story
30
 
31
  def text2audio(story_text):
32
- tts = gTTS(text=story_text, lang="en")
33
- audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
34
  tts.save(audio_file.name)
35
  return audio_file.name
36
 
37
-
38
  def main():
39
- st.set_page_config(page_title="Image to Story")
 
 
 
40
  st.header("Upload Your Image")
41
 
42
- uploaded_file = st.file_uploader("Choose image", type=["jpg", "png", "jpeg"])
 
 
 
43
 
44
  if uploaded_file:
45
  temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name)
@@ -48,38 +51,28 @@ def main():
48
 
49
  st.image(uploaded_file)
50
 
51
- # ๅ›พ็‰‡่ฝฌๆ–‡ๅญ—ๅค„็†็Šถๆ€
52
- with st.status("๐Ÿ–ผ๏ธ Processing img2text...", expanded=True) as status:
53
  scenario = img2text(temp_img)
54
- status.update(label="โœ… Image analysis completed!", state="complete")
55
 
56
- st.write("Image Caption:", scenario)
57
-
58
- # ๆ•…ไบ‹็”Ÿๆˆๅค„็†็Šถๆ€
59
- with st.status("๐Ÿ“ Generating a story...", expanded=True) as status:
60
  story = text2story(scenario)
61
- status.update(label="๐Ÿ“– Story generation completed!", state="complete")
62
-
63
- st.subheader("Story")
64
- st.write(story)
65
 
66
- # ้Ÿณ้ข‘็”Ÿๆˆๅค„็†็Šถๆ€
67
- with st.status("๐Ÿ”Š Converting to audio...", expanded=True) as status:
68
  audio_path = text2audio(story)
69
- status.update(label="๐ŸŽง Audio conversion completed!", state="complete")
70
 
71
- st.audio(audio_path)
 
 
72
 
 
73
  os.unlink(temp_img)
74
  os.unlink(audio_path)
75
 
76
-
77
- if st.button("๐ŸŽต Play Audio Story"):
78
- st.audio(
79
- audio_data["audio"],
80
- format="audio/mp3",
81
- start_time=0,
82
- sample_rate=audio_data["sampling_rate"]
83
- )
84
- if __name__ == "__main__":
85
  main()
 
7
 
8
  def img2text(img_path):
9
  captioner = pipeline(
10
+ 'image-to-text',
11
+ model='nlpconnect/vit-gpt2-image-captioning',
12
+ device_map='auto'
13
  )
14
  result = captioner(img_path)
15
  return result[0]["generated_text"]
16
 
17
  def text2story(scenario):
18
  generator = pipeline(
19
+ 'text-generation',
20
+ model='gpt2',
21
+ device_map='auto',
22
+ max_length=200,
 
 
23
  num_return_sequences=1
24
  )
25
  prompt = f"Create a children's story based on: {scenario}"
 
27
  return story
28
 
29
  def text2audio(story_text):
30
+ tts = gTTS(text=story_text, lang='en')
31
+ audio_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
32
  tts.save(audio_file.name)
33
  return audio_file.name
34
 
 
35
  def main():
36
+ st.set_page_config(
37
+ page_title='Image to Story',
38
+ page_icon='๐Ÿ“–'
39
+ )
40
  st.header("Upload Your Image")
41
 
42
+ uploaded_file = st.file_uploader(
43
+ "Choose Image",
44
+ type=['jpg', 'png', 'jpeg']
45
+ )
46
 
47
  if uploaded_file:
48
  temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name)
 
51
 
52
  st.image(uploaded_file)
53
 
54
+ # ๅ›พ็‰‡ๅˆ†ๆž
55
+ with st.status('๐Ÿ–ผ๏ธ Processing image...'):
56
  scenario = img2text(temp_img)
57
+ st.write("Image Caption:", scenario)
58
 
59
+ # ็”Ÿๆˆๆ•…ไบ‹
60
+ with st.status('๐Ÿ“– Generating story...'):
 
 
61
  story = text2story(scenario)
62
+ st.subheader('Story')
63
+ st.write(story)
 
 
64
 
65
+ # ็”Ÿๆˆ้Ÿณ้ข‘
66
+ with st.status('๐Ÿ”Š Converting audio...'):
67
  audio_path = text2audio(story)
 
68
 
69
+ # ๆทปๅŠ ๆ’ญๆ”พๆŒ‰้’ฎ๏ผˆๆ–ฐๅขž้ƒจๅˆ†๏ผ‰
70
+ if st.button('โ–ถ๏ธ Play Audio Story'):
71
+ st.audio(audio_path, format="audio/mp3")
72
 
73
+ # ๆธ…็†ๆ–‡ไปถ
74
  os.unlink(temp_img)
75
  os.unlink(audio_path)
76
 
77
+ if __name__ == '__main__':
 
 
 
 
 
 
 
 
78
  main()