hskwon7 commited on
Commit
adb445d
·
verified ·
1 Parent(s): 57d546d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -1
app.py CHANGED
@@ -1,5 +1,67 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  def main():
5
  sentiment_pipeline = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
@@ -17,4 +79,5 @@ def main():
17
  st.write(f"Confidence: {confidence:.2f}")
18
 
19
  if __name__ == "__main__":
20
- main()
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from PIL import Image
4
+ import io
5
+ from gtts import gTTS
6
+
7
+ # Page config
8
+ st.title("🖼️ → 📖 Image-to-Story Demo")
9
+ st.write("Upload an image and watch as it’s captioned, turned into a short story, and even read aloud!")
10
+
11
+ # Load and cache pipelines
12
+ @st.cache_resource
13
+ def load_captioner():
14
+ return pipeline("image-to-text", model="unography/blip-large-long-cap")
15
+
16
+ @st.cache_resource
17
+ def load_story_gen():
18
+ return pipeline(
19
+ "text-generation",
20
+ model="gpt2",
21
+ tokenizer="gpt2"
22
+ )
23
+
24
+ captioner = load_captioner()
25
+ story_gen = load_story_gen()
26
+
27
+ # 1) Image upload
28
+ uploaded = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
29
+ if uploaded:
30
+ img = Image.open(uploaded)
31
+ st.image(img, use_column_width=True)
32
+
33
+ # 2) Generate caption
34
+ with st.spinner("Generating caption…"):
35
+ caps = captioner(img)
36
+ # `caps` is a list of dicts like [{"generated_text": "..."}]
37
+ caption = caps[0]["generated_text"]
38
+ st.write("**Caption:**", caption)
39
+
40
+ # 3) Generate story from caption
41
+ with st.spinner("Spinning up a story…"):
42
+ story_out = story_gen(
43
+ caption,
44
+ max_length=200,
45
+ num_return_sequences=1,
46
+ do_sample=True,
47
+ top_p=0.9
48
+ )
49
+ story = story_out[0]["generated_text"]
50
+ st.write("**Story:**", story)
51
+
52
+ # 4) Play story as audio
53
+ if st.button("🔊 Play Story Audio"):
54
+ with st.spinner("Generating audio…"):
55
+ tts = gTTS(text=story, lang="en")
56
+ buf = io.BytesIO()
57
+ tts.write_to_fp(buf)
58
+ buf.seek(0)
59
+ st.audio(buf.read(), format="audio/mp3")
60
+
61
+
62
+ """
63
+ import streamlit as st
64
+ from transformers import pipeline
65
 
66
  def main():
67
  sentiment_pipeline = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
 
79
  st.write(f"Confidence: {confidence:.2f}")
80
 
81
  if __name__ == "__main__":
82
+ main()
83
+ """