Update app.py
Browse files
app.py
CHANGED
@@ -1825,8 +1825,42 @@ def stitch_story(raw_story: List[dict], theme_id: str, level: str) -> Dict[str,
|
|
1825 |
logging.error(f"Error in story stitching: {str(e)}")
|
1826 |
raise
|
1827 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1828 |
def show_stitched_story(story_data: Dict[str, str]):
|
1829 |
-
"""Display the stitched story with translation"""
|
1830 |
try:
|
1831 |
st.markdown("# 📖 Your Polished Story | เรื่องราวฉบับสมบูรณ์")
|
1832 |
|
@@ -1842,6 +1876,27 @@ def show_stitched_story(story_data: Dict[str, str]):
|
|
1842 |
unsafe_allow_html=True
|
1843 |
)
|
1844 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1845 |
# Thai Version
|
1846 |
st.markdown("### 🇹🇭 ฉบับภาษาไทย")
|
1847 |
st.markdown(
|
@@ -1854,13 +1909,14 @@ def show_stitched_story(story_data: Dict[str, str]):
|
|
1854 |
unsafe_allow_html=True
|
1855 |
)
|
1856 |
|
1857 |
-
# Save Options
|
1858 |
st.markdown("### 💾 บันทึกเรื่องราวฉบับสมบูรณ์")
|
1859 |
save_col1, save_col2 = st.columns(2)
|
1860 |
|
1861 |
with save_col1:
|
1862 |
if st.button("📥 บันทึกเป็น PDF", key="save_stitched_pdf", use_container_width=True):
|
1863 |
-
|
|
|
1864 |
st.download_button(
|
1865 |
"ดาวน์โหลด PDF",
|
1866 |
data=pdf_data,
|
@@ -1871,10 +1927,14 @@ def show_stitched_story(story_data: Dict[str, str]):
|
|
1871 |
|
1872 |
with save_col2:
|
1873 |
if st.button("💾 บันทึกข้อความ", key="save_stitched_text", use_container_width=True):
|
1874 |
-
text_data =
|
|
|
|
|
|
|
|
|
1875 |
st.download_button(
|
1876 |
"ดาวน์โหลดข้อความ",
|
1877 |
-
data=
|
1878 |
file_name=f"my_story_{datetime.now().strftime('%Y%m%d_%H%M')}.json",
|
1879 |
mime="application/json",
|
1880 |
key="download_stitched_text"
|
@@ -1884,8 +1944,8 @@ def show_stitched_story(story_data: Dict[str, str]):
|
|
1884 |
logging.error(f"Error showing stitched story: {str(e)}")
|
1885 |
st.error("เกิดข้อผิดพลาดในการแสดงเรื่องราว กรุณาลองใหม่อีกครั้ง")
|
1886 |
|
1887 |
-
def create_bilingual_story_pdf(story_data: Dict[str, str]) -> bytes:
|
1888 |
-
"""Create a PDF with both English and Thai versions of the story"""
|
1889 |
try:
|
1890 |
buffer = io.BytesIO()
|
1891 |
doc = SimpleDocTemplate(
|
@@ -1896,7 +1956,7 @@ def create_bilingual_story_pdf(story_data: Dict[str, str]) -> bytes:
|
|
1896 |
topMargin=72,
|
1897 |
bottomMargin=72
|
1898 |
)
|
1899 |
-
|
1900 |
# Create styles
|
1901 |
styles = getSampleStyleSheet()
|
1902 |
title_style = ParagraphStyle(
|
@@ -1929,6 +1989,18 @@ def create_bilingual_story_pdf(story_data: Dict[str, str]) -> bytes:
|
|
1929 |
elements.append(Paragraph("My Story - JoyStory", title_style))
|
1930 |
elements.append(Spacer(1, 20))
|
1931 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1932 |
# Add English version
|
1933 |
elements.append(Paragraph("English Version", heading_style))
|
1934 |
elements.append(Paragraph(story_data['polished_english'], body_style))
|
|
|
1825 |
logging.error(f"Error in story stitching: {str(e)}")
|
1826 |
raise
|
1827 |
|
1828 |
+
def generate_story_illustration(story_summary: str) -> str:
|
1829 |
+
"""Generate illustration for the story using DALL-E 3"""
|
1830 |
+
try:
|
1831 |
+
# Prepare the prompt for DALL-E
|
1832 |
+
illustration_prompt = f"""
|
1833 |
+
Create a children's book illustration for this story:
|
1834 |
+
{story_summary}
|
1835 |
+
|
1836 |
+
Style requirements:
|
1837 |
+
- Cute and child-friendly illustration
|
1838 |
+
- Soft, warm colors
|
1839 |
+
- Storybook art style
|
1840 |
+
- Clear and simple composition
|
1841 |
+
- Safe for children
|
1842 |
+
- No text in the image
|
1843 |
+
"""
|
1844 |
+
|
1845 |
+
# Generate image
|
1846 |
+
response = client.images.generate(
|
1847 |
+
model="dall-e-3",
|
1848 |
+
prompt=illustration_prompt,
|
1849 |
+
size="1024x1024",
|
1850 |
+
quality="standard",
|
1851 |
+
n=1,
|
1852 |
+
)
|
1853 |
+
|
1854 |
+
image_url = response.data[0].url
|
1855 |
+
logging.info("Story illustration generated successfully")
|
1856 |
+
return image_url
|
1857 |
+
|
1858 |
+
except Exception as e:
|
1859 |
+
logging.error(f"Error generating story illustration: {str(e)}")
|
1860 |
+
return None
|
1861 |
+
|
1862 |
def show_stitched_story(story_data: Dict[str, str]):
|
1863 |
+
"""Display the stitched story with translation and illustration"""
|
1864 |
try:
|
1865 |
st.markdown("# 📖 Your Polished Story | เรื่องราวฉบับสมบูรณ์")
|
1866 |
|
|
|
1876 |
unsafe_allow_html=True
|
1877 |
)
|
1878 |
|
1879 |
+
# Generate and display illustration
|
1880 |
+
with st.spinner("🎨 กำลังวาดภาพประกอบ..."):
|
1881 |
+
illustration_url = generate_story_illustration(story_data['polished_english'])
|
1882 |
+
if illustration_url:
|
1883 |
+
st.markdown("### 🎨 Story Illustration | ภาพประกอบเรื่องราว")
|
1884 |
+
st.image(
|
1885 |
+
illustration_url,
|
1886 |
+
caption="AI-generated illustration for your story",
|
1887 |
+
use_column_width=True
|
1888 |
+
)
|
1889 |
+
|
1890 |
+
# Add download button for illustration
|
1891 |
+
st.markdown(f"""
|
1892 |
+
<div style="text-align: center; margin: 10px 0;">
|
1893 |
+
<a href="{illustration_url}" download="story_illustration.png"
|
1894 |
+
target="_blank" style="text-decoration: none;">
|
1895 |
+
📥 ดาวน์โหลดภาพประกอบ
|
1896 |
+
</a>
|
1897 |
+
</div>
|
1898 |
+
""", unsafe_allow_html=True)
|
1899 |
+
|
1900 |
# Thai Version
|
1901 |
st.markdown("### 🇹🇭 ฉบับภาษาไทย")
|
1902 |
st.markdown(
|
|
|
1909 |
unsafe_allow_html=True
|
1910 |
)
|
1911 |
|
1912 |
+
# Save Options with illustration
|
1913 |
st.markdown("### 💾 บันทึกเรื่องราวฉบับสมบูรณ์")
|
1914 |
save_col1, save_col2 = st.columns(2)
|
1915 |
|
1916 |
with save_col1:
|
1917 |
if st.button("📥 บันทึกเป็น PDF", key="save_stitched_pdf", use_container_width=True):
|
1918 |
+
# Add illustration to PDF if available
|
1919 |
+
pdf_data = create_bilingual_story_pdf(story_data, illustration_url)
|
1920 |
st.download_button(
|
1921 |
"ดาวน์โหลด PDF",
|
1922 |
data=pdf_data,
|
|
|
1927 |
|
1928 |
with save_col2:
|
1929 |
if st.button("💾 บันทึกข้อความ", key="save_stitched_text", use_container_width=True):
|
1930 |
+
text_data = {
|
1931 |
+
**story_data,
|
1932 |
+
'illustration_url': illustration_url
|
1933 |
+
}
|
1934 |
+
json_str = json.dumps(text_data, ensure_ascii=False, indent=2)
|
1935 |
st.download_button(
|
1936 |
"ดาวน์โหลดข้อความ",
|
1937 |
+
data=json_str,
|
1938 |
file_name=f"my_story_{datetime.now().strftime('%Y%m%d_%H%M')}.json",
|
1939 |
mime="application/json",
|
1940 |
key="download_stitched_text"
|
|
|
1944 |
logging.error(f"Error showing stitched story: {str(e)}")
|
1945 |
st.error("เกิดข้อผิดพลาดในการแสดงเรื่องราว กรุณาลองใหม่อีกครั้ง")
|
1946 |
|
1947 |
+
def create_bilingual_story_pdf(story_data: Dict[str, str], illustration_url: str = None) -> bytes:
|
1948 |
+
"""Create a PDF with both English and Thai versions of the story and illustration"""
|
1949 |
try:
|
1950 |
buffer = io.BytesIO()
|
1951 |
doc = SimpleDocTemplate(
|
|
|
1956 |
topMargin=72,
|
1957 |
bottomMargin=72
|
1958 |
)
|
1959 |
+
|
1960 |
# Create styles
|
1961 |
styles = getSampleStyleSheet()
|
1962 |
title_style = ParagraphStyle(
|
|
|
1989 |
elements.append(Paragraph("My Story - JoyStory", title_style))
|
1990 |
elements.append(Spacer(1, 20))
|
1991 |
|
1992 |
+
# Add illustration if available
|
1993 |
+
if illustration_url:
|
1994 |
+
try:
|
1995 |
+
# Download and add illustration
|
1996 |
+
response = requests.get(illustration_url)
|
1997 |
+
img_data = io.BytesIO(response.content)
|
1998 |
+
img = Image(img_data, width=400, height=400)
|
1999 |
+
elements.append(img)
|
2000 |
+
elements.append(Spacer(1, 20))
|
2001 |
+
except Exception as e:
|
2002 |
+
logging.error(f"Error adding illustration to PDF: {str(e)}")
|
2003 |
+
|
2004 |
# Add English version
|
2005 |
elements.append(Paragraph("English Version", heading_style))
|
2006 |
elements.append(Paragraph(story_data['polished_english'], body_style))
|