mayf commited on
Commit
e537b6d
·
verified ·
1 Parent(s): 982555a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -7,6 +7,7 @@ st.set_page_config(
7
  )
8
 
9
  # Other imports AFTER Streamlit config
 
10
  import time
11
  import tempfile
12
  from PIL import Image
@@ -33,11 +34,11 @@ def load_models():
33
  device_map="auto",
34
  trust_remote_code=True,
35
  torch_dtype="auto",
36
- max_new_tokens=150,
37
  temperature=0.7,
38
  top_p=0.85,
39
  repetition_penalty=1.15,
40
- eos_token_id=151645 # Qwen3's specific EOS token
41
  )
42
 
43
  return captioner, storyteller
@@ -69,10 +70,10 @@ if uploaded_image:
69
  # Create story prompt
70
  story_prompt = (
71
  f"<|im_start|>system\n"
72
- f"You are a children's book author. Create a 50-100 word story based on this image description: {image_caption}\n"
73
  "Use simple language, friendly characters, and a positive lesson.<|im_end|>\n"
74
  f"<|im_start|>user\n"
75
- f"Write a short, child-friendly story with a clear beginning, middle, and end.<|im_end|>\n"
76
  f"<|im_start|>assistant\n"
77
  )
78
 
@@ -82,25 +83,38 @@ if uploaded_image:
82
  story_result = story_pipe(
83
  story_prompt,
84
  do_sample=True,
85
- num_return_sequences=1
 
86
  )
87
  generation_time = time.time() - start_time
88
- st.text(f"⏱ Generation time: {generation_time:.1f}s")
89
 
90
  # Process output
91
  raw_story = story_result[0]['generated_text']
 
 
92
  clean_story = raw_story.split("<|im_start|>assistant\n")[-1]
 
93
  clean_story = clean_story.replace("<|im_end|>", "").strip()
 
 
 
 
 
 
 
 
94
 
95
- # Ensure proper story formatting
96
  final_story = []
97
  for sentence in clean_story.split(". "):
98
- if not sentence: continue
 
 
99
  if not sentence.endswith('.'):
100
  sentence += '.'
101
  final_story.append(sentence[0].upper() + sentence[1:])
102
 
103
- final_story = " ".join(final_story).replace("..", ".")[:600] # Character limit safeguard
104
 
105
  # Display story
106
  st.subheader("✨ Your Magical Story")
 
7
  )
8
 
9
  # Other imports AFTER Streamlit config
10
+ import re
11
  import time
12
  import tempfile
13
  from PIL import Image
 
34
  device_map="auto",
35
  trust_remote_code=True,
36
  torch_dtype="auto",
37
+ max_new_tokens=250,
38
  temperature=0.7,
39
  top_p=0.85,
40
  repetition_penalty=1.15,
41
+ eos_token_id=151645
42
  )
43
 
44
  return captioner, storyteller
 
70
  # Create story prompt
71
  story_prompt = (
72
  f"<|im_start|>system\n"
73
+ f"You are a children's book author. Create a 100-150 word story based on: {image_caption}\n"
74
  "Use simple language, friendly characters, and a positive lesson.<|im_end|>\n"
75
  f"<|im_start|>user\n"
76
+ f"Write a child-friendly story with a clear beginning, middle, and end.<|im_end|>\n"
77
  f"<|im_start|>assistant\n"
78
  )
79
 
 
83
  story_result = story_pipe(
84
  story_prompt,
85
  do_sample=True,
86
+ num_return_sequences=1,
87
+ pad_token_id=151645
88
  )
89
  generation_time = time.time() - start_time
 
90
 
91
  # Process output
92
  raw_story = story_result[0]['generated_text']
93
+
94
+ # Clean up story text
95
  clean_story = raw_story.split("<|im_start|>assistant\n")[-1]
96
+ clean_story = clean_story.split("<|im_start|>")[0] # Remove any new turns
97
  clean_story = clean_story.replace("<|im_end|>", "").strip()
98
+
99
+ # Remove assistant mentions using regex
100
+ clean_story = re.sub(
101
+ r'^(assistant[:>]?\s*)+',
102
+ '',
103
+ clean_story,
104
+ flags=re.IGNORECASE
105
+ ).strip()
106
 
107
+ # Format story punctuation
108
  final_story = []
109
  for sentence in clean_story.split(". "):
110
+ sentence = sentence.strip()
111
+ if not sentence:
112
+ continue
113
  if not sentence.endswith('.'):
114
  sentence += '.'
115
  final_story.append(sentence[0].upper() + sentence[1:])
116
 
117
+ final_story = " ".join(final_story).replace("..", ".")[:800]
118
 
119
  # Display story
120
  st.subheader("✨ Your Magical Story")