Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
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")
|