Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,95 +0,0 @@
|
|
1 |
-
install streamlit
|
2 |
-
|
3 |
-
# import part
|
4 |
-
import streamlit as st
|
5 |
-
from transformers import pipeline
|
6 |
-
import textwrap
|
7 |
-
import numpy as np
|
8 |
-
import soundfile as sf
|
9 |
-
import tempfile
|
10 |
-
import os
|
11 |
-
from PIL import Image
|
12 |
-
import string
|
13 |
-
|
14 |
-
# Initialize pipelines with caching
|
15 |
-
@st.cache_resource
|
16 |
-
def load_pipelines():
|
17 |
-
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
18 |
-
storyer = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
|
19 |
-
tts = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
20 |
-
return captioner, storyer, tts
|
21 |
-
|
22 |
-
captioner, storyer, tts = load_pipelines()
|
23 |
-
|
24 |
-
# Function part
|
25 |
-
# Function to generate content from an image
|
26 |
-
def generate_content(image):
|
27 |
-
pil_image = Image.open(image)
|
28 |
-
|
29 |
-
# Generate caption
|
30 |
-
caption = captioner(pil_image)[0]["generated_text"]
|
31 |
-
st.write("**🌟 What's in the picture: 🌟**")
|
32 |
-
st.write(caption)
|
33 |
-
|
34 |
-
# Create prompt for story
|
35 |
-
prompt = (
|
36 |
-
f"Write a funny, interesting children's story that precisely centered on this scene {caption}\nStory:"
|
37 |
-
f"in third-person narrative, that describes this scene exactly: {caption} "
|
38 |
-
f"mention the exact place, location or venue within {caption}"
|
39 |
-
)
|
40 |
-
|
41 |
-
# Generate raw story
|
42 |
-
raw = storyer(
|
43 |
-
prompt,
|
44 |
-
max_new_tokens=150,
|
45 |
-
temperature=0.7,
|
46 |
-
top_p=0.9,
|
47 |
-
no_repeat_ngram_size=2,
|
48 |
-
return_full_text=False
|
49 |
-
)[0]["generated_text"].strip()
|
50 |
-
|
51 |
-
# Define allowed characters to keep (removes symbols like * and ~)
|
52 |
-
allowed_chars = string.ascii_letters + string.digits + " .,!?\"'-"
|
53 |
-
|
54 |
-
# Clean the raw story by keeping only allowed characters
|
55 |
-
clean_raw = ''.join(c for c in raw if c in allowed_chars)
|
56 |
-
|
57 |
-
# Split into words and trim to 100 words
|
58 |
-
words = clean_raw.split()
|
59 |
-
story = " ".join(words[:100])
|
60 |
-
|
61 |
-
st.write("**📖 Your funny story: 📖**")
|
62 |
-
st.write(story)
|
63 |
-
|
64 |
-
# Generate audio from cleaned story
|
65 |
-
chunks = textwrap.wrap(story, width=200)
|
66 |
-
audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])
|
67 |
-
|
68 |
-
# Save audio to temporary file
|
69 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
70 |
-
sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
|
71 |
-
temp_file_path = temp_file.name
|
72 |
-
|
73 |
-
return caption, story, temp_file_path
|
74 |
-
|
75 |
-
# Streamlit UI
|
76 |
-
st.title("✨ Magic Story Maker ✨")
|
77 |
-
st.markdown("Upload a picture to make a funny story and hear it too! 📸")
|
78 |
-
|
79 |
-
uploaded_image = st.file_uploader("Choose your picture", type=["jpg", "jpeg", "png"])
|
80 |
-
|
81 |
-
# Streamlit UI (modified image display section)
|
82 |
-
if uploaded_image is None:
|
83 |
-
st.image("https://example.com/placeholder_image.jpg", caption="Upload your picture here! 📷", use_container_width=True)
|
84 |
-
else:
|
85 |
-
st.image(uploaded_image, caption="Your Picture 🌟", use_container_width=True)
|
86 |
-
|
87 |
-
if st.button("✨ Make My Story! ✨"):
|
88 |
-
if uploaded_image is not None:
|
89 |
-
with st.spinner("🔮 Creating your magical story..."):
|
90 |
-
caption, story, audio_path = generate_content(uploaded_image)
|
91 |
-
st.success("🎉 Your story is ready! 🎉")
|
92 |
-
st.audio(audio_path, format="audio/wav")
|
93 |
-
os.remove(audio_path)
|
94 |
-
else:
|
95 |
-
st.warning("Please upload a picture first! 📸")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|