CR7CAD commited on
Commit
7d60452
·
verified ·
1 Parent(s): 8b48665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -10
app.py CHANGED
@@ -34,27 +34,28 @@ def img2text(image_bytes, _models):
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
  story_generator = _models["story_generator"]
48
 
49
- # Format prompt to encourage short, complete stories
50
  prompt = f"""<|system|>
51
- You are a creative short story writer. Write a complete, coherent story based on the given prompt.
52
- Keep it under 100 words and ensure it has a natural ending.
53
  <|user|>
54
- Write a very short story about: {text}
 
55
  <|assistant|>"""
56
 
57
- # Generate story with parameters tuned for brevity and completeness
58
  response = story_generator(
59
  prompt,
60
  max_new_tokens=100, # Allow enough tokens for a complete story
@@ -114,7 +115,7 @@ if uploaded_file is not None:
114
  caption = img2text(image_bytes, models)
115
  st.write(f"**Caption:** {caption}")
116
 
117
- # Generate story
118
  story = text2story(caption, models)
119
  word_count = len(story.split())
120
  st.write(f"**Story ({word_count} words):**")
 
34
  return result[0]["generated_text"]
35
 
36
  @st.cache_data
37
+ def text2story(caption, _models):
38
+ """Generate a short story from image caption.
39
 
40
  Args:
41
+ caption: Caption describing the image
42
  _models: Dictionary containing loaded models
43
 
44
  Returns:
45
+ A generated story that expands on the image caption
46
  """
47
  story_generator = _models["story_generator"]
48
 
49
+ # Format prompt to ensure the story expands on the image caption
50
  prompt = f"""<|system|>
51
+ You are a creative short story writer. Write a brief, engaging story that expands on the given image caption.
52
+ The story should be under 100 words and have a natural beginning, middle, and end.
53
  <|user|>
54
+ Image caption: "{caption}"
55
+ Create a short story that expands on this image caption and brings it to life.
56
  <|assistant|>"""
57
 
58
+ # Generate story with parameters tuned for brevity and coherence
59
  response = story_generator(
60
  prompt,
61
  max_new_tokens=100, # Allow enough tokens for a complete story
 
115
  caption = img2text(image_bytes, models)
116
  st.write(f"**Caption:** {caption}")
117
 
118
+ # Generate story that expands on the caption
119
  story = text2story(caption, models)
120
  word_count = len(story.split())
121
  st.write(f"**Story ({word_count} words):**")