Spaces:
Build error
Build error
Update app.py
Browse files
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(
|
| 38 |
-
"""Generate a short story from
|
| 39 |
|
| 40 |
Args:
|
| 41 |
-
|
| 42 |
_models: Dictionary containing loaded models
|
| 43 |
|
| 44 |
Returns:
|
| 45 |
-
A generated story
|
| 46 |
"""
|
| 47 |
story_generator = _models["story_generator"]
|
| 48 |
|
| 49 |
-
# Format prompt to
|
| 50 |
prompt = f"""<|system|>
|
| 51 |
-
You are a creative short story writer. Write a
|
| 52 |
-
|
| 53 |
<|user|>
|
| 54 |
-
|
|
|
|
| 55 |
<|assistant|>"""
|
| 56 |
|
| 57 |
-
# Generate story with parameters tuned for brevity and
|
| 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):**")
|