CR7CAD commited on
Commit
93643da
·
verified ·
1 Parent(s): 863e283

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -35
app.py CHANGED
@@ -33,46 +33,43 @@ def img2text(image_bytes, _models):
33
  result = _models["image_captioner"](pil_img)
34
  return result[0]["generated_text"]
35
 
36
- # Generate story from text - improved to extract just the story response
37
  @st.cache_data
38
  def text2story(text, _models):
39
- """Generate a story from text - using the original implementation"""
40
- prompt = f"Write a short children's story based on this: {text}. The story should have a clear beginning, middle, and end. Keep it under 150 words. "
41
 
42
- # Generate the story
43
- story_result = _models["story_generator"](
44
- prompt,
45
- max_length=300,
46
- num_return_sequences=1,
47
- temperature=0.7,
48
- do_sample=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  )
50
-
51
- full_text = story_result[0]['generated_text']
52
-
53
- # If the generated text includes the prompt at the beginning, remove it.
54
- if full_text.startswith(prompt):
55
- story_text = full_text[len(prompt):].strip()
56
- else:
57
- story_text = full_text
58
-
59
- # Find natural ending points (end of sentences)
60
- periods = [i for i, char in enumerate(story_text) if char == '.']
61
- question_marks = [i for i, char in enumerate(story_text) if char == '?']
62
- exclamation_marks = [i for i, char in enumerate(story_text) if char == '!']
63
-
64
- all_endings = sorted(periods + question_marks + exclamation_marks)
65
-
66
- if all_endings:
67
- min_story_length = 100
68
- suitable_endings = [i for i in all_endings if i >= min_story_length]
69
 
70
- if suitable_endings:
71
- if len(suitable_endings) > 2:
72
- return story_text[:suitable_endings[2]+1]
73
- else:
74
- return story_text[:suitable_endings[-1]+1]
75
-
76
  return story_text
77
 
78
  # Text-to-speech function
 
33
  result = _models["image_captioner"](pil_img)
34
  return result[0]["generated_text"]
35
 
 
36
  @st.cache_data
37
  def text2story(text, _models):
38
+ """Generate a short story from text prompt.
 
39
 
40
+ Args:
41
+ text: Input text prompt
42
+ _models: Dictionary containing loaded models
43
+
44
+ Returns:
45
+ A generated story under 100 words that ends naturally
46
+ """
47
+
48
+ # Format prompt to encourage short, complete stories
49
+ prompt = f"""<|system|>
50
+ You are a creative short story writer. Write a complete, coherent story based on the given prompt.
51
+ Keep it under 100 words and ensure it has a natural ending.
52
+ <|user|>
53
+ Write a very short story about: {text}
54
+ <|assistant|>"""
55
+
56
+ # Generate story with parameters tuned for brevity and completeness
57
+ response = story_generator(
58
+ prompt,
59
+ max_new_tokens=250, # Allow enough tokens for a complete story
60
+ do_sample=True,
61
+ temperature=0.7, # Balanced creativity
62
+ top_p=0.9, # Focus on more likely tokens
63
+ repetition_penalty=1.2, # Avoid repetitive patterns
64
+ eos_token_id=story_generator.tokenizer.eos_token_id
65
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Extract just the generated story text
68
+ raw_story = response[0]['generated_text']
69
+
70
+ # Parse out just the assistant's response from the conversation format
71
+ story_text = raw_story.split("<|assistant|>")[-1].strip()
72
+
73
  return story_text
74
 
75
  # Text-to-speech function