JSY8 commited on
Commit
ae82db7
·
verified ·
1 Parent(s): 8b8075d

Delete app.py

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