Dendup commited on
Commit
536293f
1 Parent(s): 541ec2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -1,9 +1,13 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
- import requests
4
 
5
- # Load the model
6
- story_generator = pipeline('text-generation', model='gpt2')
 
 
 
 
 
7
 
8
  st.title('Interactive Short-Story Creator')
9
 
@@ -13,12 +17,33 @@ genre = st.selectbox(
13
  ('Fantasy', 'Sci-Fi', 'Mystery', 'Horror', 'Adventure', 'Romance')
14
  )
15
 
16
- # Text area for user input
 
 
17
  user_input = st.text_area("Start your story here...", "Once upon a time")
18
 
 
 
 
 
19
  if st.button('Generate Story Continuation'):
20
  with st.spinner('AI is writing the story...'):
21
- prompt = f"A {genre.lower()} story. {user_input}"
22
- generated_story = story_generator(prompt, max_length= 500)
23
- story_text = generated_story[0]['generated_text']
24
- st.text_area("Here's the continuation of your story:", story_text, height=250)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
 
3
 
4
+ # Load the model with caching
5
+ @st.cache(allow_output_mutation=True)
6
+ def load_model():
7
+ model = pipeline('text-generation', model='gpt2')
8
+ return model
9
+
10
+ story_generator = load_model()
11
 
12
  st.title('Interactive Short-Story Creator')
13
 
 
17
  ('Fantasy', 'Sci-Fi', 'Mystery', 'Horror', 'Adventure', 'Romance')
18
  )
19
 
20
+ # Additional inputs for a more tailored story
21
+ character = st.text_input("Enter a main character name:")
22
+ setting = st.text_input("Enter a setting (e.g., a magical forest, a spaceship):")
23
  user_input = st.text_area("Start your story here...", "Once upon a time")
24
 
25
+ # Advanced configuration options
26
+ max_length = st.slider("Select the length of the story continuation:", 100, 1000, 500)
27
+ temperature = st.slider("Adjust creativity of the story:", 0.7, 1.5, 1.0)
28
+
29
  if st.button('Generate Story Continuation'):
30
  with st.spinner('AI is writing the story...'):
31
+ # Improved prompt construction with user inputs
32
+ prompt = f"Write a story in the style of {genre.lower()}. It is set in {setting} and revolves around {character}. Begin with: '{user_input}'"
33
+
34
+ try:
35
+ generated_story = story_generator(prompt, max_length=max_length, temperature=temperature)
36
+ story_text = generated_story[0]['generated_text']
37
+ st.text_area("Here's the continuation of your story:", story_text, height=250)
38
+ except Exception as e:
39
+ st.error(f"Error generating story: {str(e)}")
40
+
41
+ # Collecting user feedback
42
+ feedback = st.text_area("Feedback on the generated story:", "")
43
+ if st.button('Submit Feedback'):
44
+ st.write("Thanks for your feedback!")
45
+
46
+ # Optional: Add visual elements based on the genre
47
+ if genre == 'Fantasy':
48
+ st.image('path_to_fantasy_image.jpg', caption='Fantasy Realm')
49
+ # Add similar blocks for other genres with appropriate images and captions