Update app.py
Browse files
app.py
CHANGED
@@ -1742,51 +1742,38 @@ def stitch_story(raw_story: List[dict], theme_id: str, level: str) -> Dict[str,
|
|
1742 |
def show_stitched_story(story_data: Dict[str, str]):
|
1743 |
"""Display the stitched story with translation"""
|
1744 |
try:
|
1745 |
-
st.markdown(""
|
1746 |
-
|
1747 |
-
|
1748 |
-
|
1749 |
-
|
1750 |
-
|
1751 |
-
|
1752 |
-
|
1753 |
-
|
1754 |
-
📖 Your Polished Story | เรื่องราวฉบับสมบูรณ์
|
1755 |
-
</h3>
|
1756 |
-
|
1757 |
-
<div style="
|
1758 |
-
background-color: white;
|
1759 |
-
padding: 15px;
|
1760 |
-
border-radius: 8px;
|
1761 |
-
margin-bottom: 15px;
|
1762 |
-
">
|
1763 |
-
<h4 style="color: #1565c0;">English Version</h4>
|
1764 |
-
<p style="color: #333; font-size: 1.1em; line-height: 1.6;">
|
1765 |
-
{story_data['polished_english']}
|
1766 |
-
</p>
|
1767 |
-
</div>
|
1768 |
-
|
1769 |
-
<div style="
|
1770 |
-
background-color: white;
|
1771 |
-
padding: 15px;
|
1772 |
-
border-radius: 8px;
|
1773 |
-
">
|
1774 |
-
<h4 style="color: #1565c0;">ฉบับภาษาไทย</h4>
|
1775 |
-
<p style="color: #333; font-size: 1.1em; line-height: 1.6;">
|
1776 |
-
{story_data['thai_translation']}
|
1777 |
-
</p>
|
1778 |
-
</div>
|
1779 |
</div>
|
1780 |
-
|
|
|
|
|
1781 |
|
1782 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1783 |
st.markdown("### 💾 บันทึกเรื่องราวฉบับสมบูรณ์")
|
1784 |
save_col1, save_col2 = st.columns(2)
|
1785 |
|
1786 |
with save_col1:
|
1787 |
-
if st.button("📥 บันทึกเป็น PDF",
|
1788 |
-
key="save_stitched_pdf",
|
1789 |
-
use_container_width=True):
|
1790 |
pdf_data = create_bilingual_story_pdf(story_data)
|
1791 |
st.download_button(
|
1792 |
"ดาวน์โหลด PDF",
|
@@ -1797,9 +1784,7 @@ def show_stitched_story(story_data: Dict[str, str]):
|
|
1797 |
)
|
1798 |
|
1799 |
with save_col2:
|
1800 |
-
if st.button("💾 บันทึกข้อความ",
|
1801 |
-
key="save_stitched_text",
|
1802 |
-
use_container_width=True):
|
1803 |
text_data = json.dumps(story_data, ensure_ascii=False, indent=2)
|
1804 |
st.download_button(
|
1805 |
"ดาวน์โหลดข้อความ",
|
@@ -1811,7 +1796,7 @@ def show_stitched_story(story_data: Dict[str, str]):
|
|
1811 |
|
1812 |
except Exception as e:
|
1813 |
logging.error(f"Error showing stitched story: {str(e)}")
|
1814 |
-
st.error("
|
1815 |
|
1816 |
def create_bilingual_story_pdf(story_data: Dict[str, str]) -> bytes:
|
1817 |
"""Create a PDF with both English and Thai versions of the story"""
|
@@ -3097,85 +3082,126 @@ def show_story_stitching_options():
|
|
3097 |
def generate_stitched_story(story: List[dict], style: str, detail_level: str, theme: str, level: str) -> Dict[str, str]:
|
3098 |
"""Generate a polished version of the story based on selected style and detail level"""
|
3099 |
try:
|
3100 |
-
|
3101 |
-
|
3102 |
-
|
3103 |
-
|
3104 |
-
|
3105 |
-
|
3106 |
-
|
3107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3108 |
}
|
3109 |
-
|
3110 |
-
|
3111 |
-
|
3112 |
-
|
3113 |
-
|
3114 |
-
|
3115 |
-
|
3116 |
-
|
3117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3118 |
# Generate English version
|
3119 |
-
|
3120 |
-
|
3121 |
-
|
3122 |
-
|
3123 |
-
|
3124 |
-
|
3125 |
-
|
3126 |
-
|
3127 |
-
}
|
3128 |
-
|
3129 |
-
|
3130 |
-
|
3131 |
-
|
3132 |
-
|
3133 |
-
|
3134 |
-
|
3135 |
-
|
3136 |
-
|
3137 |
-
|
3138 |
-
|
3139 |
-
|
3140 |
-
|
3141 |
|
3142 |
# Generate Thai translation
|
3143 |
-
|
3144 |
-
|
3145 |
-
|
3146 |
-
|
3147 |
-
|
3148 |
-
|
3149 |
-
|
3150 |
-
|
3151 |
-
|
3152 |
-
|
3153 |
-
|
3154 |
-
|
3155 |
-
|
3156 |
-
|
3157 |
-
|
3158 |
-
|
3159 |
-
|
3160 |
-
|
3161 |
-
|
3162 |
-
|
3163 |
-
|
3164 |
-
raise
|
3165 |
|
3166 |
-
|
3167 |
-
'original': story_summary,
|
3168 |
'polished_english': polished_english,
|
3169 |
'thai_translation': thai_translation,
|
3170 |
'style': style_name,
|
3171 |
-
'
|
3172 |
}
|
3173 |
-
|
3174 |
-
logging.info("Story generation completed successfully")
|
3175 |
-
return result
|
3176 |
-
|
3177 |
except Exception as e:
|
3178 |
-
logging.error(f"Error
|
3179 |
raise
|
3180 |
|
3181 |
def show_stitched_result(story_data: Dict[str, str]):
|
|
|
1742 |
def show_stitched_story(story_data: Dict[str, str]):
|
1743 |
"""Display the stitched story with translation"""
|
1744 |
try:
|
1745 |
+
st.markdown("# 📖 Your Polished Story | เรื่องราวฉบับสมบูรณ์")
|
1746 |
+
|
1747 |
+
# English Version
|
1748 |
+
st.markdown("### 🇬🇧 English Version")
|
1749 |
+
st.markdown(
|
1750 |
+
f"""
|
1751 |
+
<div style="background-color: #f8f9fa; padding: 20px; border-radius: 10px;
|
1752 |
+
border-left: 4px solid #1565c0; margin-bottom: 20px;">
|
1753 |
+
{story_data['polished_english']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1754 |
</div>
|
1755 |
+
""",
|
1756 |
+
unsafe_allow_html=True
|
1757 |
+
)
|
1758 |
|
1759 |
+
# Thai Version
|
1760 |
+
st.markdown("### 🇹🇭 ฉบับภาษาไทย")
|
1761 |
+
st.markdown(
|
1762 |
+
f"""
|
1763 |
+
<div style="background-color: #f8f9fa; padding: 20px; border-radius: 10px;
|
1764 |
+
border-left: 4px solid #28a745; margin-bottom: 20px;">
|
1765 |
+
{story_data['thai_translation']}
|
1766 |
+
</div>
|
1767 |
+
""",
|
1768 |
+
unsafe_allow_html=True
|
1769 |
+
)
|
1770 |
+
|
1771 |
+
# Save Options
|
1772 |
st.markdown("### 💾 บันทึกเรื่องราวฉบับสมบูรณ์")
|
1773 |
save_col1, save_col2 = st.columns(2)
|
1774 |
|
1775 |
with save_col1:
|
1776 |
+
if st.button("📥 บันทึกเป็น PDF", key="save_stitched_pdf", use_container_width=True):
|
|
|
|
|
1777 |
pdf_data = create_bilingual_story_pdf(story_data)
|
1778 |
st.download_button(
|
1779 |
"ดาวน์โหลด PDF",
|
|
|
1784 |
)
|
1785 |
|
1786 |
with save_col2:
|
1787 |
+
if st.button("💾 บันทึกข้อความ", key="save_stitched_text", use_container_width=True):
|
|
|
|
|
1788 |
text_data = json.dumps(story_data, ensure_ascii=False, indent=2)
|
1789 |
st.download_button(
|
1790 |
"ดาวน์โหลดข้อความ",
|
|
|
1796 |
|
1797 |
except Exception as e:
|
1798 |
logging.error(f"Error showing stitched story: {str(e)}")
|
1799 |
+
st.error("เกิดข้อผิดพลาดในการแสดงเรื่องราว กรุณาลองใหม่อีกครั้ง")
|
1800 |
|
1801 |
def create_bilingual_story_pdf(story_data: Dict[str, str]) -> bytes:
|
1802 |
"""Create a PDF with both English and Thai versions of the story"""
|
|
|
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 |
+
|
3194 |
+
thai_translation = translation_response.choices[0].message.content.strip()
|
|
|
3195 |
|
3196 |
+
return {
|
|
|
3197 |
'polished_english': polished_english,
|
3198 |
'thai_translation': thai_translation,
|
3199 |
'style': style_name,
|
3200 |
+
'level': level
|
3201 |
}
|
3202 |
+
|
|
|
|
|
|
|
3203 |
except Exception as e:
|
3204 |
+
logging.error(f"Error generating stitched story: {str(e)}")
|
3205 |
raise
|
3206 |
|
3207 |
def show_stitched_result(story_data: Dict[str, str]):
|