Eason918 commited on
Commit
006c053
ยท
verified ยท
1 Parent(s): a8b0d91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -5,32 +5,34 @@ from gtts import gTTS
5
  import os
6
  os.system("pip install transformers==4.36.2")
7
 
8
- # ๅŠ ่ผ‰ Hugging Face ็š„ๆจกๅž‹
9
  image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
10
  story_generator = pipeline("text-generation", model="facebook/opt-350m", device=-1)
11
 
12
- # ๅœ–็‰‡ โ†’ ๆ–‡ๅญ—๏ผˆ็”Ÿๆˆๆ่ฟฐ๏ผ‰
13
  def img2text(image_path):
14
  text = image_to_text_model(image_path)[0]["generated_text"]
15
  return text
16
 
17
- # ๆ–‡ๅญ— โ†’ ๆ•…ไบ‹๏ผˆ็”ŸๆˆๅฎŒๆ•ดๆ•…ไบ‹๏ผ‰
18
  def text2story(text):
19
  prompt = f"Write a fun and magical children's story based on this idea: {text}.\n\nOnce upon a time..."
20
  story = story_generator(prompt, max_length=150, do_sample=True, temperature=0.8, top_p=0.9, repetition_penalty=1.2)[0]['generated_text']
21
  return story
22
 
23
- # ๆ•…ไบ‹ โ†’ ่ชž้Ÿณ๏ผˆTTS๏ผ‰
24
  def text2audio_gtts(story_text, filename="story.mp3"):
25
- # ้ฟๅ…ๆ–‡ไปถๅ†ฒ็ช
 
26
  if os.path.exists(filename):
27
  os.remove(filename)
28
 
29
- # ้™ๅˆถ TTS ๆ–‡ๆœฌ้•ทๅบฆ
 
30
  max_chars = 500 # gTTS ๅฏ่ƒฝไธๆ”ฏๆŒ้Ž้•ทๆ–‡ๆœฌ
31
  story_text = story_text[:max_chars]
32
 
33
- # ็”Ÿๆˆ่ฏญ้Ÿณ
34
  tts = gTTS(text=story_text, lang="en")
35
  tts.save(filename)
36
 
@@ -43,33 +45,34 @@ st.header("๐Ÿ“– AI Storyteller: Turn Your Image into a Story with Audio")
43
  uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"])
44
 
45
  if uploaded_file:
46
- # ไฟๅญ˜ๅœ–็‰‡ๅˆฐๆœฌๅœฐ
 
47
  image_path = "uploaded_image.jpg"
48
  with open(image_path, "wb") as f:
49
  f.write(uploaded_file.getbuffer())
50
 
51
- # ่ฎ€ๅ–ไธฆ้กฏ็คบๅœ–็‰‡
52
  image = Image.open(image_path)
53
  st.image(image, caption="Uploaded Image", use_container_width=True)
54
 
55
- # ็”Ÿๆˆๅœ–็‰‡ๆ่ฟฐ
56
  st.text("๐Ÿ” Generating image caption...")
57
- caption = img2text(image_path) # ้€™่ฃๆ”นๆˆๆ–‡ไปถ่ทฏๅพ‘
58
  st.write("**Image Description:**", caption)
59
 
60
- # ็”Ÿๆˆๆ•…ไบ‹
61
  st.text("๐Ÿ“ Generating story...")
62
  story = text2story(caption)
63
  st.write("**Generated Story:**")
64
  st.write(story)
65
 
66
- # ็”Ÿๆˆ่ชž้Ÿณ
67
  st.text("๐Ÿ”Š Generating audio...")
68
  audio_file = text2audio_gtts(story)
69
 
70
- # ๆ’ญๆ”พ้Ÿณ้ ป
71
  st.audio(audio_file, format="audio/mp3")
72
 
73
- # ไธ‹่ผ‰ๆŒ‰้’ฎ
74
  with open(audio_file, "rb") as file:
75
  st.download_button("๐Ÿ“ฅ Download Audio", file, file_name="story.mp3")
 
5
  import os
6
  os.system("pip install transformers==4.36.2")
7
 
8
+ # ๐ŸŽฏ Load Hugging Face Models | ๅŠ ่ผ‰ Hugging Face ๆจกๅž‹
9
  image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
10
  story_generator = pipeline("text-generation", model="facebook/opt-350m", device=-1)
11
 
12
+ # Image to Text (Generate Caption) | ๅœ–็‰‡ โ†’ ๆ–‡ๅญ—๏ผˆ็”Ÿๆˆๆ่ฟฐ๏ผ‰
13
  def img2text(image_path):
14
  text = image_to_text_model(image_path)[0]["generated_text"]
15
  return text
16
 
17
+ # Text to Story (Generate a Complete Story) | ๆ–‡ๅญ— โ†’ ๆ•…ไบ‹๏ผˆ็”ŸๆˆๅฎŒๆ•ดๆ•…ไบ‹๏ผ‰
18
  def text2story(text):
19
  prompt = f"Write a fun and magical children's story based on this idea: {text}.\n\nOnce upon a time..."
20
  story = story_generator(prompt, max_length=150, do_sample=True, temperature=0.8, top_p=0.9, repetition_penalty=1.2)[0]['generated_text']
21
  return story
22
 
23
+ # Story to Speech (TTS) | ๆ•…ไบ‹ โ†’ ่ชž้Ÿณ๏ผˆTTS๏ผ‰
24
  def text2audio_gtts(story_text, filename="story.mp3"):
25
+
26
+ # Avoid filename conflicts | ้ฟๅ…ๆ–‡ไปถๅ†ฒ็ช
27
  if os.path.exists(filename):
28
  os.remove(filename)
29
 
30
+
31
+ # Limit text length (to prevent gTTS crashes)| ้™ๅˆถ TTS ๆ–‡ๆœฌ้•ทๅบฆ๏ผˆ้ฟๅ… gTTS ๅดฉๆฝฐ๏ผ‰
32
  max_chars = 500 # gTTS ๅฏ่ƒฝไธๆ”ฏๆŒ้Ž้•ทๆ–‡ๆœฌ
33
  story_text = story_text[:max_chars]
34
 
35
+ # Generate Speech | ็”Ÿๆˆ่ฏญ้Ÿณ
36
  tts = gTTS(text=story_text, lang="en")
37
  tts.save(filename)
38
 
 
45
  uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"])
46
 
47
  if uploaded_file:
48
+
49
+ # Save image locally | ไฟๅญ˜ๅœ–็‰‡ๅˆฐๆœฌๅœฐ
50
  image_path = "uploaded_image.jpg"
51
  with open(image_path, "wb") as f:
52
  f.write(uploaded_file.getbuffer())
53
 
54
+ # Load and Display Image | ่ฎ€ๅ–ไธฆ้กฏ็คบๅœ–็‰‡
55
  image = Image.open(image_path)
56
  st.image(image, caption="Uploaded Image", use_container_width=True)
57
 
58
+ # Generate Image Caption | ็”Ÿๆˆๅœ–็‰‡ๆ่ฟฐ
59
  st.text("๐Ÿ” Generating image caption...")
60
+ caption = img2text(image_path)
61
  st.write("**Image Description:**", caption)
62
 
63
+ # Generate Story | ็”Ÿๆˆๆ•…ไบ‹
64
  st.text("๐Ÿ“ Generating story...")
65
  story = text2story(caption)
66
  st.write("**Generated Story:**")
67
  st.write(story)
68
 
69
+ # Generate Audio | ็”Ÿๆˆ่ชž้Ÿณ
70
  st.text("๐Ÿ”Š Generating audio...")
71
  audio_file = text2audio_gtts(story)
72
 
73
+ # Play Audio | ๆ’ญๆ”พ้Ÿณ้ ป
74
  st.audio(audio_file, format="audio/mp3")
75
 
76
+ # Download Audio | ไธ‹่ผ‰ๆŒ‰้’ฎ
77
  with open(audio_file, "rb") as file:
78
  st.download_button("๐Ÿ“ฅ Download Audio", file, file_name="story.mp3")