Rathapoom commited on
Commit
140f5be
·
verified ·
1 Parent(s): cb6446c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -81
app.py CHANGED
@@ -3080,114 +3080,92 @@ def show_story_stitching_options():
3080
  st.error("เกิดข้อผิดพลาดในการแสดงตัวเลือกการเรียบเรียง")
3081
 
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
- # Create a comprehensive prompt for the final story
3086
- story_content = " ".join([entry['content'] for entry in story])
3087
-
3088
- style_prompts = {
3089
- "Classic Fairytale": """
3090
- Create a classic fairytale style story that:
3091
- - Uses traditional storytelling elements
3092
- - Has a clear beginning, middle, and end
3093
- - Incorporates descriptive language
3094
- - Maintains a magical, enchanting tone
3095
- """,
3096
- "Modern Adventure": """
3097
- Create a modern adventure story that:
3098
- - Uses contemporary settings and situations
3099
- - Has exciting plot developments
3100
- - Incorporates relatable elements
3101
- - Maintains an engaging, active pace
3102
- """,
3103
- "Simple and Clear": """
3104
- Create a straightforward story that:
3105
- - Uses clear, simple language
3106
- - Has a logical flow of events
3107
- - Focuses on key story elements
3108
- - Maintains easy readability
3109
- """
3110
  }
3111
 
3112
- level_context = {
3113
- 'Beginner': {
3114
- 'instructions': """
3115
- Additional Guidelines:
3116
- - Use simple vocabulary
3117
- - Keep sentences short and clear
3118
- - Use basic tenses
3119
- - Focus on common words and phrases
3120
- """,
3121
- 'max_tokens': 500
3122
- },
3123
- 'Intermediate': {
3124
- 'instructions': """
3125
- Additional Guidelines:
3126
- - Use grade-appropriate vocabulary
3127
- - Mix simple and compound sentences
3128
- - Use common tenses
3129
- - Add some descriptive elements
3130
- """,
3131
- 'max_tokens': 700
3132
- },
3133
- 'Advanced': {
3134
- 'instructions': """
3135
- Additional Guidelines:
3136
- - Use rich vocabulary
3137
- - Vary sentence structures
3138
- - Use multiple tenses
3139
- - Add literary elements and descriptions
3140
- """,
3141
- 'max_tokens': 1000
3142
- }
3143
- }
3144
-
3145
- # Get style name without suffix
3146
- style_name = style.split(" - ")[0] if " - " in style else style
3147
- level_settings = level_context[level]
3148
 
3149
- # Generate English version
3150
  response = client.chat.completions.create(
3151
- model="gpt-4",
3152
  messages=[
3153
  {
3154
  "role": "system",
3155
- "content": f"""
3156
- You are a children's story editor.
3157
- {style_prompts.get(style_name, style_prompts['Simple and Clear'])}
3158
- {level_settings['instructions']}
3159
- Create a polished, cohesive story that preserves the original plot while improving flow and readability.
3160
- """
3161
  },
3162
  {
3163
  "role": "user",
3164
- "content": f"Original story:\n{story_content}\n\nCreate a polished version:"
3165
  }
3166
  ],
3167
- max_tokens=level_settings['max_tokens'],
3168
  temperature=0.7
3169
  )
3170
 
3171
  polished_english = response.choices[0].message.content.strip()
3172
 
3173
- # Generate Thai translation
 
 
 
 
 
 
 
 
 
 
 
 
 
3174
  translation_response = client.chat.completions.create(
3175
  model="gpt-4o-mini",
3176
  messages=[
3177
  {
3178
  "role": "system",
3179
- "content": """
3180
- You are a Thai translator specializing in children's literature.
3181
- Create a natural, engaging Thai translation that maintains the story's essence and style.
3182
- Ensure the translation is appropriate for young readers and flows naturally in Thai.
3183
- """
3184
  },
3185
  {
3186
  "role": "user",
3187
- "content": f"Translate this story to Thai:\n{polished_english}"
3188
  }
3189
  ],
3190
- max_tokens=1000,
3191
  temperature=0.7
3192
  )
3193
 
@@ -3196,7 +3174,7 @@ def generate_stitched_story(story: List[dict], style: str, detail_level: str, th
3196
  return {
3197
  'polished_english': polished_english,
3198
  'thai_translation': thai_translation,
3199
- 'style': style_name,
3200
  'level': level
3201
  }
3202
 
 
3080
  st.error("เกิดข้อผิดพลาดในการแสดงตัวเลือกการเรียบเรียง")
3081
 
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 with balanced additional details"""
3084
  try:
3085
+ # แยกประโยคต่างๆ ตาม role
3086
+ story_parts = {
3087
+ 'starter': next((entry['content'] for entry in story if entry.get('is_starter')), ''),
3088
+ 'user_sentences': [entry['content'] for entry in story if entry['role'] == 'You'],
3089
+ 'ai_responses': [entry['content'] for entry in story if entry['role'] == 'AI' and not entry.get('is_starter')],
3090
+ 'ending': next((entry['content'] for entry in story if entry.get('is_final')), '')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3091
  }
3092
 
3093
+ # สร้าง prompt ที่เน้นการเพิ่มรายละเอียดอย่างสมดุล
3094
+ prompt_template = f"""
3095
+ Rewrite this story into a flowing narrative.
3096
+
3097
+ IMPORTANT RULES:
3098
+ 1. Keep all original sentences and events intact
3099
+ 2. Add appropriate setting details and context
3100
+ 3. Include simple descriptive elements that enhance the story
3101
+ 4. Add natural transitions between events
3102
+ 5. Keep additional details balanced and relevant
3103
+ 6. Maintain the exact sequence of events
3104
+
3105
+ Enhancement Guidelines:
3106
+ - Add setting details (where things happen)
3107
+ - Include character reactions and emotions
3108
+ - Add simple descriptive words for actions
3109
+ - Create smooth transitions between scenes
3110
+ - Keep the magical/whimsical tone of the story
3111
+
3112
+ Original Story Structure:
3113
+ - Beginning: {story_parts['starter']}
3114
+ - Main Story: {' '.join(sum(zip(story_parts['user_sentences'], story_parts['ai_responses']), ()))}
3115
+ - Ending: {story_parts['ending']}
3116
+
3117
+ Style: {style}
3118
+ Level: {level}
3119
+
3120
+ Create an enhanced narrative that preserves all original events while adding appropriate context and descriptions.
3121
+ """
 
 
 
 
 
 
 
3122
 
3123
+ # Generate English version with balanced details
3124
  response = client.chat.completions.create(
3125
+ model="gpt-4o-mini",
3126
  messages=[
3127
  {
3128
  "role": "system",
3129
+ "content": """You are a story editor who enhances stories while preserving their original content.
3130
+ Focus on adding just enough detail to make the story more engaging without overshadowing the original events."""
 
 
 
 
3131
  },
3132
  {
3133
  "role": "user",
3134
+ "content": prompt_template
3135
  }
3136
  ],
 
3137
  temperature=0.7
3138
  )
3139
 
3140
  polished_english = response.choices[0].message.content.strip()
3141
 
3142
+ # Generate Thai translation with similar enhancement approach
3143
+ translation_prompt = f"""
3144
+ Translate this enhanced story to Thai.
3145
+
3146
+ IMPORTANT:
3147
+ 1. Maintain the same level of detail as the English version
3148
+ 2. Keep all story elements and added descriptions
3149
+ 3. Translate naturally while preserving the story's tone
3150
+ 4. Ensure the magical elements come through clearly
3151
+
3152
+ Story to translate:
3153
+ {polished_english}
3154
+ """
3155
+
3156
  translation_response = client.chat.completions.create(
3157
  model="gpt-4o-mini",
3158
  messages=[
3159
  {
3160
  "role": "system",
3161
+ "content": """You are a Thai translator who specializes in children's stories.
3162
+ Maintain the enhanced details while creating a natural Thai narrative."""
 
 
 
3163
  },
3164
  {
3165
  "role": "user",
3166
+ "content": translation_prompt
3167
  }
3168
  ],
 
3169
  temperature=0.7
3170
  )
3171
 
 
3174
  return {
3175
  'polished_english': polished_english,
3176
  'thai_translation': thai_translation,
3177
+ 'style': style,
3178
  'level': level
3179
  }
3180