CR7CAD commited on
Commit
a502e8c
·
verified ·
1 Parent(s): 754622e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py CHANGED
@@ -34,6 +34,49 @@ def text2story(text):
34
  story_text = story_result[0]['generated_text']
35
  story_text = story_text.replace(prompt, "Once upon a time, ")
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # Find a natural ending point (end of sentence) before 100 words
38
  words = story_text.split()
39
  if len(words) > 100:
 
34
  story_text = story_result[0]['generated_text']
35
  story_text = story_text.replace(prompt, "Once upon a time, ")
36
 
37
+ # Find a natural ending point (end of sentence) before 100 words
38
+ words = story_text.split()
39
+ if len(words) > 100:
40
+ # Join the first 100 words
41
+ shortened_text = " ".join(words[:100])
42
+
43
+ # Find the last complete sentence
44
+ last_period = shortened_text.rfind('.')
45
+ last_question = shortened_text.rfind('?')
46
+ last_exclamation = shortened_text.rfind('!')
47
+
48
+ # Find the last sentence ending punctuation
49
+ last_end = max(last_period, last_question, last_exclamation)
50
+
51
+ if last_end > 0:
52
+ # Truncate at the end of the last complete sentence
53
+ story_text = shortened_text[:last_end + 1]
54
+ else:
55
+ # If no sentence ending found, just use the shortened text
56
+ story_text = shortened_text
57
+
58
+ return story_textdef text2story(text):
59
+ # Using a smaller text generation model
60
+ generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
61
+
62
+ # Create a prompt for the story generation
63
+ prompt = f"Write a fun children's story based on this: {text}. The story should be short and end naturally with a conclusion. Once upon a time, "
64
+
65
+ # Generate the story
66
+ story_result = generator(
67
+ prompt,
68
+ max_length=250, # Increased to allow for a complete story
69
+ num_return_sequences=1,
70
+ temperature=0.7,
71
+ top_k=50,
72
+ top_p=0.95,
73
+ do_sample=True
74
+ )
75
+
76
+ # Extract the generated text
77
+ story_text = story_result[0]['generated_text']
78
+ story_text = story_text.replace(prompt, "Once upon a time, ")
79
+
80
  # Find a natural ending point (end of sentence) before 100 words
81
  words = story_text.split()
82
  if len(words) > 100: