JoannaKOKO commited on
Commit
7448649
·
verified ·
1 Parent(s): ed92830

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -60,16 +60,19 @@ def generate_caption(image_bytes):
60
  # Function to generate a story from a caption
61
  def generate_story(caption):
62
  try:
63
- prompt = [f"Based on this text:'{caption}', ellaborate a short story with no more than 100 words for young children."]
 
 
 
 
64
  story_output = story_generator(
65
- prompt,
66
  max_length=150,
67
  do_sample=True,
68
  temperature=0.2,
69
- top_p=0.9,
70
- num_return_sequences=1
71
  )
72
- story = story_output[0]["generated_text"]
73
  story = truncate_to_100_words(story)
74
  return story
75
  except Exception as e:
 
60
  # Function to generate a story from a caption
61
  def generate_story(caption):
62
  try:
63
+ device = "cpu"
64
+ prompt = [{"role": "user",
65
+ "content": f"Based on this text:'{caption}', ellaborate a short story with no more than 100 words for young children."}]
66
+ input_text = tokenizer.apply_chat_template(prompt, tokenize=False)
67
+ inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
68
  story_output = story_generator(
69
+ inputs,
70
  max_length=150,
71
  do_sample=True,
72
  temperature=0.2,
73
+ top_p=0.9
 
74
  )
75
+ story = tokenizer.decode(story_output[0]) #story_output[0]["generated_text"]
76
  story = truncate_to_100_words(story)
77
  return story
78
  except Exception as e: