namuisam commited on
Commit
3b09e61
·
verified ·
1 Parent(s): bad441f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -3
app.py CHANGED
@@ -1,21 +1,49 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
- # Function definitions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def img2text(url):
6
  image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
7
  text = image_to_text_model(url)[0]["generated_text"]
8
  return text
9
 
10
  def text2story(text):
11
- # Updated prompt to generate a kid-friendly story without repeating the instructions.
12
  prompt = (
13
  "Write a creative, engaging story for kids aged 3-10 based on the scenario below. "
14
  "Do not include these instructions in your answer. "
15
  "Scenario: " + text
16
  )
17
  text_generation_model = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
18
- story_text = text_generation_model(
19
  prompt,
20
  min_length=50,
21
  max_length=100,
@@ -23,6 +51,9 @@ def text2story(text):
23
  early_stopping=True,
24
  top_p=0.4
25
  )[0]["generated_text"]
 
 
 
26
  return story_text
27
 
28
  def text2audio(story_text):
 
1
  import streamlit as st
2
  from transformers import pipeline
3
 
4
+ def remove_instructions(text):
5
+ """
6
+ Removes any lines that contain prompt instructions from the model output.
7
+ This is a naive approach that looks for specific keywords/phrases
8
+ and discards them or trims them from the final text.
9
+ """
10
+ lines = text.split("\n")
11
+ filtered_lines = []
12
+
13
+ for line in lines:
14
+ # Skip lines that contain certain instruction phrases
15
+ if "Write a creative, engaging story for kids aged 3-10" in line:
16
+ continue
17
+ if "Do not include these instructions" in line:
18
+ continue
19
+
20
+ # If the line contains 'Scenario:', remove everything up to and including 'Scenario:'
21
+ # so only the actual story part remains.
22
+ if "Scenario:" in line:
23
+ scenario_index = line.find("Scenario:")
24
+ # Remove the prompt portion
25
+ line = line[scenario_index + len("Scenario: "):].strip()
26
+
27
+ filtered_lines.append(line)
28
+
29
+ # Rejoin the filtered lines into a final text
30
+ final_text = "\n".join(filtered_lines).strip()
31
+ return final_text
32
+
33
  def img2text(url):
34
  image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
35
  text = image_to_text_model(url)[0]["generated_text"]
36
  return text
37
 
38
  def text2story(text):
39
+ # You can keep or adjust the prompt as you see fit.
40
  prompt = (
41
  "Write a creative, engaging story for kids aged 3-10 based on the scenario below. "
42
  "Do not include these instructions in your answer. "
43
  "Scenario: " + text
44
  )
45
  text_generation_model = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
46
+ raw_story = text_generation_model(
47
  prompt,
48
  min_length=50,
49
  max_length=100,
 
51
  early_stopping=True,
52
  top_p=0.4
53
  )[0]["generated_text"]
54
+
55
+ # Post-process to remove any repeated instructions
56
+ story_text = remove_instructions(raw_story)
57
  return story_text
58
 
59
  def text2audio(story_text):