Leo Liu commited on
Commit
5225da1
·
verified ·
1 Parent(s): 79d97c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -52
app.py CHANGED
@@ -1,6 +1,6 @@
1
  # import part
2
- from transformers import pipeline
3
  import streamlit as st
 
4
 
5
  # function part
6
  # img2text
@@ -11,66 +11,51 @@ def img2text(url):
11
 
12
  # text2story
13
  def text2story(text):
14
- story_prompt = (
15
- f"You are an expert in storytelling for children ages 3-10."
16
- f"Based on the image description below:{text}\n"
17
- f"Please create a funny, heartwarming, imaginative fairy tale of no less than 50 words and less than 100 words."
18
- f"Output only the body of the story, do not include other content."
19
- )
20
-
21
- story_text = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B",
22
- max_length=100,
23
- do_sample=True,
24
- top_p=0.95,
25
- temperature=0.7)
26
- generation = story_text(story_prompt)
27
- raw_output = generation[0]['generated_text']
28
-
29
- if raw_output.startswith(story_prompt):
30
- story_text = raw_output[len(story_prompt):].strip()
31
- else:
32
- story_text = raw_output
33
  return story_text
34
 
35
  # text2audio
36
  def text2audio(story_text):
37
- audio_data = pipeline("text-to-audio", model="facebook/musicgen-medium")
 
38
  return audio_data
39
 
40
- # main part
41
- st.set_page_config(page_title="Your Image to Audio Story",
42
- page_icon="🦜")
43
- st.header("Turn Your Image to Audio Story")
44
 
45
- # Upload image here
46
- uploaded_file = st.file_uploader("Select an Image...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- if uploaded_file is not None:
49
- print(uploaded_file)
50
- bytes_data = uploaded_file.getvalue()
51
- with open(uploaded_file.name, "wb") as file:
52
- file.write(bytes_data)
53
- st.image(uploaded_file, caption="Uploaded Image",
54
- use_container_width=True)
55
 
56
- #Stage 1: Image to Text
57
- st.text('Processing img2text...')
58
- scenario = img2text(uploaded_file.name)
59
- st.write(scenario)
60
 
61
- #Stage 2: Text to Story
62
- st.text('Generating a story...')
63
- story = text2story(scenario)
64
- st.write(story)
 
 
65
 
66
- #Stage 3: Story to Audio data
67
- st.text('Generating audio data...')
68
- audio_data =text2audio(story)
69
 
70
- # Play button
71
- if st.button("Play Audio"):
72
- st.audio(audio_data['audio'],
73
- format="audio/wav",
74
- start_time=0,
75
- sample_rate = audio_data['sampling_rate'])
76
- st.audio("kids_playing_audio.wav")
 
1
  # import part
 
2
  import streamlit as st
3
+ from transformers import pipeline
4
 
5
  # function part
6
  # img2text
 
11
 
12
  # text2story
13
  def text2story(text):
14
+ pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
15
+ story_text = pipe(text)[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  return story_text
17
 
18
  # text2audio
19
  def text2audio(story_text):
20
+ pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
21
+ audio_data = pipe(story_text)
22
  return audio_data
23
 
 
 
 
 
24
 
25
+ def main():
26
+ st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
27
+ st.header("Turn Your Image to Audio Story")
28
+ uploaded_file = st.file_uploader("Select an Image...")
29
+
30
+ if uploaded_file is not None:
31
+ print(uploaded_file)
32
+ bytes_data = uploaded_file.getvalue()
33
+ with open(uploaded_file.name, "wb") as file:
34
+ file.write(bytes_data)
35
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
36
+
37
+
38
+ #Stage 1: Image to Text
39
+ st.text('Processing img2text...')
40
+ scenario = img2text(uploaded_file.name)
41
+ st.write(scenario)
42
 
43
+ #Stage 2: Text to Story
44
+ st.text('Generating a story...')
45
+ story = text2story(scenario)
46
+ st.write(story)
 
 
 
47
 
48
+ #Stage 3: Story to Audio data
49
+ st.text('Generating audio data...')
50
+ audio_data =text2audio(story)
 
51
 
52
+ # Play button
53
+ if st.button("Play Audio"):
54
+ st.audio(audio_data['audio'],
55
+ format="audio/wav",
56
+ start_time=0,
57
+ sample_rate = audio_data['sampling_rate'])
58
 
 
 
 
59
 
60
+ if __name__ == "__main__":
61
+ main()