Update app.py
Browse files
app.py
CHANGED
@@ -3082,7 +3082,10 @@ def show_story_stitching_options():
|
|
3082 |
def generate_stitched_story(story: List[dict], style: str, detail_level: str, theme: str, level: str) -> Dict[str, str]:
|
3083 |
"""Generate a polished version of the story based on selected style and detail level"""
|
3084 |
try:
|
3085 |
-
|
|
|
|
|
|
|
3086 |
detail_map = {
|
3087 |
"น้อย": "minimal",
|
3088 |
"ปานกลาง": "moderate",
|
@@ -3090,178 +3093,100 @@ def generate_stitched_story(story: List[dict], style: str, detail_level: str, th
|
|
3090 |
}
|
3091 |
detail_level_en = detail_map.get(detail_level, "moderate")
|
3092 |
|
3093 |
-
#
|
3094 |
style_name = style.split(" - ")[0]
|
3095 |
|
3096 |
-
#
|
3097 |
-
|
3098 |
-
|
3099 |
-
Create a classic fairytale style with:
|
3100 |
-
- Traditional opening and closing phrases
|
3101 |
-
- Magical and whimsical elements
|
3102 |
-
- Clear moral or lesson
|
3103 |
-
- Flowing, storybook language
|
3104 |
-
""",
|
3105 |
-
"Modern Adventure": """
|
3106 |
-
Create a contemporary adventure style with:
|
3107 |
-
- Dynamic pacing
|
3108 |
-
- Modern settings and references
|
3109 |
-
- Engaging action sequences
|
3110 |
-
- Relatable characters
|
3111 |
-
""",
|
3112 |
-
"Poetic Style": """
|
3113 |
-
Create a poetic narrative style with:
|
3114 |
-
- Rich imagery and metaphors
|
3115 |
-
- Rhythmic language
|
3116 |
-
- Evocative descriptions
|
3117 |
-
- Artistic expression
|
3118 |
-
""",
|
3119 |
-
"Simple and Clear": """
|
3120 |
-
Create a clear, straightforward style with:
|
3121 |
-
- Direct language
|
3122 |
-
- Clear sequence of events
|
3123 |
-
- Focused storytelling
|
3124 |
-
- Easy-to-follow structure
|
3125 |
-
"""
|
3126 |
-
}
|
3127 |
-
|
3128 |
-
# Create detail level instructions
|
3129 |
-
detail_instructions = {
|
3130 |
-
"minimal": "Keep descriptions concise and focus on key events.",
|
3131 |
-
"moderate": "Balance description and action with moderate detail.",
|
3132 |
-
"detailed": "Include rich descriptions and elaborate on scenes and characters."
|
3133 |
-
}
|
3134 |
-
|
3135 |
-
# Generate polished version
|
3136 |
-
response = client.chat.completions.create(
|
3137 |
-
model="gpt-4o-mini",
|
3138 |
-
messages=[
|
3139 |
-
{
|
3140 |
-
"role": "system",
|
3141 |
-
"content": f"""
|
3142 |
-
You are a professional children's story editor and translator.
|
3143 |
-
|
3144 |
-
Style Guidelines:
|
3145 |
-
{style_instructions[style_name]}
|
3146 |
-
|
3147 |
-
Detail Level:
|
3148 |
-
{detail_instructions[detail_level_en]}
|
3149 |
-
|
3150 |
-
Educational Level: {level}
|
3151 |
-
|
3152 |
-
Create a polished version that:
|
3153 |
-
1. Maintains the original story's core elements
|
3154 |
-
2. Adapts to the requested style
|
3155 |
-
3. Provides appropriate detail level
|
3156 |
-
4. Ensures age-appropriate language
|
3157 |
-
5. Creates a cohesive narrative flow
|
3158 |
-
"""
|
3159 |
-
},
|
3160 |
-
{
|
3161 |
-
"role": "user",
|
3162 |
-
"content": f"Original story:\n{generate_story_summary(story)}\n\nCreate a polished version:"
|
3163 |
-
}
|
3164 |
-
],
|
3165 |
-
max_tokens=1500,
|
3166 |
-
temperature=0.7
|
3167 |
-
)
|
3168 |
-
|
3169 |
-
polished_english = response.choices[0].message.content.strip()
|
3170 |
-
|
3171 |
-
# Generate Thai translation with matching style
|
3172 |
-
translation_response = client.chat.completions.create(
|
3173 |
-
model="gpt-4o-mini",
|
3174 |
-
messages=[
|
3175 |
-
{
|
3176 |
-
"role": "system",
|
3177 |
-
"content": f"""
|
3178 |
-
You are a professional Thai translator specializing in children's literature.
|
3179 |
-
|
3180 |
-
Create a Thai translation that:
|
3181 |
-
1. Matches the {style_name} style
|
3182 |
-
2. Maintains the same detail level ({detail_level_en})
|
3183 |
-
3. Uses natural, flowing Thai language
|
3184 |
-
4. Preserves the story's tone and feeling
|
3185 |
-
5. Adapts cultural elements appropriately
|
3186 |
-
"""
|
3187 |
-
},
|
3188 |
-
{
|
3189 |
-
"role": "user",
|
3190 |
-
"content": f"Translate this story to Thai:\n{polished_english}"
|
3191 |
-
}
|
3192 |
-
],
|
3193 |
-
max_tokens=1500,
|
3194 |
-
temperature=0.7
|
3195 |
-
)
|
3196 |
-
|
3197 |
-
thai_translation = translation_response.choices[0].message.content.strip()
|
3198 |
|
3199 |
-
|
3200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3201 |
'polished_english': polished_english,
|
3202 |
'thai_translation': thai_translation,
|
3203 |
'style': style_name,
|
3204 |
'detail_level': detail_level_en
|
3205 |
}
|
3206 |
|
|
|
|
|
|
|
3207 |
except Exception as e:
|
3208 |
-
logging.error(f"Error
|
3209 |
raise
|
3210 |
|
3211 |
def show_stitched_result(story_data: Dict[str, str]):
|
3212 |
"""Display the stitched story result with enhanced formatting"""
|
3213 |
try:
|
3214 |
-
st.markdown(""
|
3215 |
-
|
3216 |
-
|
3217 |
-
|
3218 |
-
|
3219 |
-
|
3220 |
-
">
|
3221 |
-
|
3222 |
-
|
3223 |
-
|
3224 |
-
|
3225 |
-
|
3226 |
-
|
3227 |
-
|
3228 |
-
|
3229 |
-
|
3230 |
-
|
3231 |
-
|
3232 |
-
|
3233 |
-
|
3234 |
-
|
3235 |
-
<div style="
|
3236 |
-
color: #333;
|
3237 |
-
font-size: 1.1em;
|
3238 |
-
line-height: 1.6;
|
3239 |
-
white-space: pre-line;
|
3240 |
-
">
|
3241 |
-
{story_data['polished_english']}
|
3242 |
-
</div>
|
3243 |
-
</div>
|
3244 |
-
|
3245 |
-
<div style="
|
3246 |
-
background-color: white;
|
3247 |
-
padding: 20px;
|
3248 |
-
border-radius: 8px;
|
3249 |
-
border: 1px solid #e0e0e0;
|
3250 |
-
">
|
3251 |
-
<h3 style="color: #2e7d32; margin-bottom: 10px;">
|
3252 |
-
ฉบับภาษาไทย
|
3253 |
-
</h3>
|
3254 |
-
<div style="
|
3255 |
-
color: #333;
|
3256 |
-
font-size: 1.1em;
|
3257 |
-
line-height: 1.6;
|
3258 |
-
white-space: pre-line;
|
3259 |
-
">
|
3260 |
-
{story_data['thai_translation']}
|
3261 |
-
</div>
|
3262 |
-
</div>
|
3263 |
-
</div>
|
3264 |
-
""", unsafe_allow_html=True)
|
3265 |
|
3266 |
# Save options
|
3267 |
st.markdown("### 💾 บันทึกเรื่องราว")
|
@@ -3282,6 +3207,7 @@ def show_stitched_result(story_data: Dict[str, str]):
|
|
3282 |
mime="application/pdf",
|
3283 |
key="download_final_pdf"
|
3284 |
)
|
|
|
3285 |
except Exception as e:
|
3286 |
logging.error(f"Error saving PDF: {str(e)}")
|
3287 |
st.error("เกิดข้อผิดพลาดในการสร้างไฟล์ PDF")
|
@@ -3300,6 +3226,7 @@ def show_stitched_result(story_data: Dict[str, str]):
|
|
3300 |
mime="application/json",
|
3301 |
key="download_final_text"
|
3302 |
)
|
|
|
3303 |
except Exception as e:
|
3304 |
logging.error(f"Error saving text: {str(e)}")
|
3305 |
st.error("เกิดข้อผิดพลาดในการบันทึกข้อความ")
|
|
|
3082 |
def generate_stitched_story(story: List[dict], style: str, detail_level: str, theme: str, level: str) -> Dict[str, str]:
|
3083 |
"""Generate a polished version of the story based on selected style and detail level"""
|
3084 |
try:
|
3085 |
+
logging.info("Starting story generation...")
|
3086 |
+
logging.info(f"Input params - Style: {style}, Detail: {detail_level}, Theme: {theme}, Level: {level}")
|
3087 |
+
|
3088 |
+
# แปลง detail level เป็นภาษาอังกฤษ
|
3089 |
detail_map = {
|
3090 |
"น้อย": "minimal",
|
3091 |
"ปานกลาง": "moderate",
|
|
|
3093 |
}
|
3094 |
detail_level_en = detail_map.get(detail_level, "moderate")
|
3095 |
|
3096 |
+
# ดึงชื่อสไตล์
|
3097 |
style_name = style.split(" - ")[0]
|
3098 |
|
3099 |
+
# สร้าง prompt
|
3100 |
+
story_summary = generate_story_summary(story)
|
3101 |
+
logging.info(f"Story summary length: {len(story_summary)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3102 |
|
3103 |
+
# Generate English version
|
3104 |
+
try:
|
3105 |
+
logging.info("Generating English version...")
|
3106 |
+
response = client.chat.completions.create(
|
3107 |
+
model="gpt-4",
|
3108 |
+
messages=[
|
3109 |
+
{
|
3110 |
+
"role": "system",
|
3111 |
+
"content": f"You are a children's story editor. Create a {style_name} style story with {detail_level_en} detail level."
|
3112 |
+
},
|
3113 |
+
{
|
3114 |
+
"role": "user",
|
3115 |
+
"content": f"Original story:\n{story_summary}\n\nCreate a polished version:"
|
3116 |
+
}
|
3117 |
+
],
|
3118 |
+
max_tokens=1000,
|
3119 |
+
temperature=0.7
|
3120 |
+
)
|
3121 |
+
polished_english = response.choices[0].message.content.strip()
|
3122 |
+
logging.info("English version generated successfully")
|
3123 |
+
except Exception as e:
|
3124 |
+
logging.error(f"Error generating English version: {str(e)}")
|
3125 |
+
raise
|
3126 |
+
|
3127 |
+
# Generate Thai translation
|
3128 |
+
try:
|
3129 |
+
logging.info("Generating Thai translation...")
|
3130 |
+
translation_response = client.chat.completions.create(
|
3131 |
+
model="gpt-4o-mini",
|
3132 |
+
messages=[
|
3133 |
+
{
|
3134 |
+
"role": "system",
|
3135 |
+
"content": "You are a Thai translator. Create a natural Thai translation."
|
3136 |
+
},
|
3137 |
+
{
|
3138 |
+
"role": "user",
|
3139 |
+
"content": f"Translate this story to Thai:\n{polished_english}"
|
3140 |
+
}
|
3141 |
+
],
|
3142 |
+
max_tokens=1000,
|
3143 |
+
temperature=0.7
|
3144 |
+
)
|
3145 |
+
thai_translation = translation_response.choices[0].message.content.strip()
|
3146 |
+
logging.info("Thai translation generated successfully")
|
3147 |
+
except Exception as e:
|
3148 |
+
logging.error(f"Error generating Thai translation: {str(e)}")
|
3149 |
+
raise
|
3150 |
+
|
3151 |
+
result = {
|
3152 |
+
'original': story_summary,
|
3153 |
'polished_english': polished_english,
|
3154 |
'thai_translation': thai_translation,
|
3155 |
'style': style_name,
|
3156 |
'detail_level': detail_level_en
|
3157 |
}
|
3158 |
|
3159 |
+
logging.info("Story generation completed successfully")
|
3160 |
+
return result
|
3161 |
+
|
3162 |
except Exception as e:
|
3163 |
+
logging.error(f"Error in story generation: {str(e)}")
|
3164 |
raise
|
3165 |
|
3166 |
def show_stitched_result(story_data: Dict[str, str]):
|
3167 |
"""Display the stitched story result with enhanced formatting"""
|
3168 |
try:
|
3169 |
+
st.markdown("## 📖 Your Polished Story | เรื่องราวฉบับสมบูรณ์")
|
3170 |
+
|
3171 |
+
# English Version
|
3172 |
+
st.markdown("### 🇬🇧 English Version")
|
3173 |
+
st.markdown(
|
3174 |
+
f"""<div style="background-color: #f8f9fa; padding: 20px;
|
3175 |
+
border-radius: 10px; margin: 10px 0; border-left: 4px solid #1565c0;">
|
3176 |
+
{story_data['polished_english']}
|
3177 |
+
</div>""",
|
3178 |
+
unsafe_allow_html=True
|
3179 |
+
)
|
3180 |
+
|
3181 |
+
# Thai Version
|
3182 |
+
st.markdown("### 🇹🇭 ฉบับภาษาไทย")
|
3183 |
+
st.markdown(
|
3184 |
+
f"""<div style="background-color: #f8f9fa; padding: 20px;
|
3185 |
+
border-radius: 10px; margin: 10px 0; border-left: 4px solid #4caf50;">
|
3186 |
+
{story_data['thai_translation']}
|
3187 |
+
</div>""",
|
3188 |
+
unsafe_allow_html=True
|
3189 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3190 |
|
3191 |
# Save options
|
3192 |
st.markdown("### 💾 บันทึกเรื่องราว")
|
|
|
3207 |
mime="application/pdf",
|
3208 |
key="download_final_pdf"
|
3209 |
)
|
3210 |
+
st.success("สร้าง PDF เรียบร้อยแล้ว!")
|
3211 |
except Exception as e:
|
3212 |
logging.error(f"Error saving PDF: {str(e)}")
|
3213 |
st.error("เกิดข้อผิดพลาดในการสร้างไฟล์ PDF")
|
|
|
3226 |
mime="application/json",
|
3227 |
key="download_final_text"
|
3228 |
)
|
3229 |
+
st.success("บันทึกข้อความเรียบร้อยแล้ว!")
|
3230 |
except Exception as e:
|
3231 |
logging.error(f"Error saving text: {str(e)}")
|
3232 |
st.error("เกิดข้อผิดพลาดในการบันทึกข้อความ")
|