dtm95 commited on
Commit
560fa5d
β€’
1 Parent(s): d6ee855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -1,22 +1,13 @@
1
  import streamlit as st
2
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
 
3
 
4
  # Set up the Streamlit app
5
  st.title("πŸ§šβ€β™€οΈ Magic Story Buddy πŸ“š")
6
  st.markdown("Let's create a magical story just for you!")
7
 
8
- def initialize_model():
9
- # Load the tokenizer and model
10
- tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-2.7B")
11
- model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-2.7B")
12
-
13
- # Create the pipeline for text generation
14
- text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
15
-
16
- return text_generator
17
-
18
- # Initialize the model and pipeline
19
- pipe = pipeline("text-generation", model="hakurei/lit-6B")
20
 
21
  # User input
22
  child_name = st.text_input("What's your name, young storyteller?")
@@ -27,10 +18,19 @@ story_theme = st.selectbox("What would you like your story to be about?",
27
  story_length = st.slider("How long should the story be?", 50, 200, 100)
28
  include_moral = st.checkbox("Include a moral lesson?")
29
 
30
- def generate_story(prompt, max_length=500):
31
- messages = [{"role": "user", "content": prompt}]
32
- result = pipe(messages, max_length=max_length, do_sample=True, temperature=0.7)
33
- return result[0]['generated_text']
 
 
 
 
 
 
 
 
 
34
 
35
  if st.button("Create My Story!"):
36
  if child_name and story_theme:
@@ -48,7 +48,7 @@ if st.button("Create My Story!"):
48
  prompt += "This story teaches us that "
49
 
50
  # Generate the story
51
- story = generate_story(prompt, max_length=story_length)
52
 
53
  # Display the story
54
  st.markdown("## Your Magical Story")
 
1
  import streamlit as st
2
+ import openai
3
+ import os
4
 
5
  # Set up the Streamlit app
6
  st.title("πŸ§šβ€β™€οΈ Magic Story Buddy πŸ“š")
7
  st.markdown("Let's create a magical story just for you!")
8
 
9
+ # Set up OpenAI API key
10
+ openai.api_key = os.getenv("sk-proj-tWGLTJozNuRBlh6RGqFAT3BlbkFJQy7dSGpukXfwuwueZGIt")
 
 
 
 
 
 
 
 
 
 
11
 
12
  # User input
13
  child_name = st.text_input("What's your name, young storyteller?")
 
18
  story_length = st.slider("How long should the story be?", 50, 200, 100)
19
  include_moral = st.checkbox("Include a moral lesson?")
20
 
21
+ def generate_story(prompt, max_tokens=500):
22
+ response = openai.ChatCompletion.create(
23
+ model="gpt-3.5-turbo",
24
+ messages=[
25
+ {"role": "system", "content": "You are a friendly storyteller for children."},
26
+ {"role": "user", "content": prompt}
27
+ ],
28
+ max_tokens=max_tokens,
29
+ n=1,
30
+ stop=None,
31
+ temperature=0.7,
32
+ )
33
+ return response.choices[0].message['content'].strip()
34
 
35
  if st.button("Create My Story!"):
36
  if child_name and story_theme:
 
48
  prompt += "This story teaches us that "
49
 
50
  # Generate the story
51
+ story = generate_story(prompt, max_tokens=story_length * 2) # Multiply by 2 as tokens != words
52
 
53
  # Display the story
54
  st.markdown("## Your Magical Story")