Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,39 +35,40 @@ def img2text(image_bytes, _models):
|
|
| 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 |
-
#
|
| 50 |
-
prompt =
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
<|
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
response = story_generator(
|
| 58 |
prompt,
|
| 59 |
-
max_new_tokens=120, #
|
| 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, #
|
| 64 |
eos_token_id=story_generator.tokenizer.eos_token_id
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# Extract
|
| 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
|
|
|
|
| 35 |
|
| 36 |
@st.cache_data
|
| 37 |
def text2story(caption, _models):
|
| 38 |
+
"""Generate a short story from an image caption.
|
| 39 |
|
| 40 |
Args:
|
| 41 |
+
caption (str): Caption describing the image.
|
| 42 |
+
_models (dict): Dictionary containing loaded models.
|
| 43 |
|
| 44 |
Returns:
|
| 45 |
+
str: A generated story that expands on the image caption.
|
| 46 |
"""
|
| 47 |
story_generator = _models["story_generator"]
|
| 48 |
+
|
| 49 |
+
# Updated prompt includes explicit instructions to incorporate details from the caption
|
| 50 |
+
prompt = (
|
| 51 |
+
"<|system|>\n"
|
| 52 |
+
"You are a creative story generating assistant that always incorporates key details "
|
| 53 |
+
"from the provided image caption into your story.\n"
|
| 54 |
+
"<|user|>\n"
|
| 55 |
+
f"Please generate a short story within 100 words that clearly utilizes the following image caption and expands upon it thoroughly:\n\"{caption}\"\n"
|
| 56 |
+
"<|assistant|>"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Generate story with parameters tuned for creativity and coherence
|
| 60 |
response = story_generator(
|
| 61 |
prompt,
|
| 62 |
+
max_new_tokens=120, # Enough tokens for a complete story
|
| 63 |
do_sample=True,
|
| 64 |
temperature=0.7, # Balanced creativity
|
| 65 |
+
top_p=0.9, # Focus on more likely tokens for coherence
|
| 66 |
+
repetition_penalty=1.2, # Reduce repetitive patterns
|
| 67 |
eos_token_id=story_generator.tokenizer.eos_token_id
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Extract the generated story text by separating the assistant's reply
|
| 71 |
raw_story = response[0]['generated_text']
|
|
|
|
|
|
|
| 72 |
story_text = raw_story.split("<|assistant|>")[-1].strip()
|
| 73 |
|
| 74 |
return story_text
|