GH111 commited on
Commit
a0d022f
·
1 Parent(s): 2c74df0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from responsivevoice import ResponsiveVoice
4
+ from PIL import Image
5
+ from diffusers import StableDiffusionPipeline
6
+
7
+ # Text-to-speech model
8
+ tts_model = ResponsiveVoice()
9
+
10
+ # Text-to-image pipeline
11
+ sd_pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
12
+
13
+ # Story generation model
14
+ generator = pipeline("text-generation", model="gpt2")
15
+
16
+ def generate_story(prompt):
17
+ """
18
+ Generates a story based on user input.
19
+
20
+ Args:
21
+ prompt: A string containing the starting prompt for the story.
22
+
23
+ Returns:
24
+ A string containing the generated story.
25
+ """
26
+ story = generator(prompt, max_length=1024)[0]["generated_text"]
27
+ return story
28
+
29
+ def generate_image(prompt):
30
+ """
31
+ Generates an image based on user input.
32
+
33
+ Args:
34
+ prompt: A string containing the description for the image.
35
+
36
+ Returns:
37
+ A PIL Image object.
38
+ """
39
+ image = sd_pipeline(prompt).images[0]
40
+ return image
41
+
42
+ # Streamlit app
43
+ st.title("Hugging Face Storytelling App")
44
+
45
+ # Prompt input
46
+ prompt = st.text_input("Start your story with...")
47
+
48
+ # Text-to-speech button
49
+ if st.button("Speak the Story"):
50
+ with st.spinner("Speaking the story..."):
51
+ tts_model.speak(generate_story(prompt))
52
+
53
+ # Text-to-text button
54
+ if st.button("Generate Text from Prompt"):
55
+ with st.spinner("Generating text..."):
56
+ new_text = generate_story(prompt)
57
+ st.write(new_text)
58
+
59
+ # Text-to-image button
60
+ if st.button("Generate Image from Prompt"):
61
+ with st.spinner("Generating image..."):
62
+ image = generate_image(prompt)
63
+ st.image(image)
64
+
65
+ # Questions to answer
66
+ st.write("**Think about your story and answer these questions:**")
67
+ st.text_input("What is the main character's name?")
68
+ st.text_input("Where does the story take place?")
69
+ st.text_input("What is the problem the character faces?")
70
+ st.text_input("How does the character solve the problem?")
71
+
72
+ # Disclaimer
73
+ st.write("* This app is still under development and may not always generate accurate or coherent results.")
74
+ st.write("* Please be mindful of the content generated by the AI models.")