Spaces:
Runtime error
Runtime error
Upload 3 files
Browse filesFiles changed (3)
- DockerfileAdded+7
- book_reader_app.pyAdded+95
- requirements.txtAdded+9
1234567FROM python:3.11WORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txtCOPY book_reader_app.py .EXPOSE 8501CMD ["streamlit", "run", "book_reader_app.py", "--server.port=8501", "--server.address=0.0.0.0"]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import streamlit as stfrom google import genaifrom gtts import gTTSfrom PIL import Imageimport io
st.set_page_config(page_title="๋ง๋ฒ์ ์ฑ
์ฝ๊ธฐ ์นด๋ฉ๋ผ", layout="centered", page_icon="๐")
# Custom CSS for Kids UIst.markdown(""" <style> .main { background-color: #FFF8F0; } h1 { color: #FF6B6B; font-family: 'Comic Sans MS', 'Chalkboard SE', sans-serif; text-align: center; } h3 { color: #4ECDC4; text-align: center; } .stCameraInput { border-radius: 15px; border: 4px dashed #FFD93D; padding: 10px; } .stAudio { margin-top: 20px; } .story-box { background-color: white; padding: 20px; border-radius: 15px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); border-left: 8px solid #FFD93D; font-size: 20px; line-height: 1.6; color: #333; } </style>""", unsafe_allow_html=True)
st.title("๐ ๋ง๋ฒ์ ์ฑ
์ฝ๊ธฐ ์นด๋ฉ๋ผ ๐ท")st.markdown("### ์ฑ
์ ์ฐฐ์นต! ์ฐ์ด์ฃผ๋ฉด ๋ง๋ฒ์ฌ ์์ ์ด ๊ธ์ ์ฝ์ด์ค์ โจ")
# Sidebar for configurationwith st.sidebar: st.header("โ๏ธ ๋ถ๋ชจ๋ ์ค์ ๋ฉ๋ด") gemini_api_key = st.text_input("Gemini API Key", type="password", placeholder="AIzaSy...") st.info("API ํค๋ฅผ ์
๋ ฅํด์ผ ๋ง๋ฒ์ฌ ์์ ์ด ์ฑ
์ ์ฝ์ ์ ์์ด์!")
# Initialize session state for storing the current generated text and audioif "story_text" not in st.session_state: st.session_state.story_text = ""if "audio_bytes" not in st.session_state: st.session_state.audio_bytes = None
# Camera Inputcamera_image = st.camera_input("์นด๋ฉ๋ผ๋ก ์ฑ
์ ์ฐ์ด๋ณด์ธ์!")
if camera_image is not None: if not gemini_api_key: st.warning("โ ๏ธ ์ฌ์ด๋๋ฐ(์ผ์ชฝ)์ Gemini API ํค๋ฅผ ๋จผ์ ์
๋ ฅํด์ฃผ์ธ์!") else: st.success("์ฌ์ง ์ฐฐ์นต! ์์ ์๊ฒ ์ฌ์ง์ ์ ๋ฌํ์ด์. ์ ์๋ง ๊ธฐ๋ค๋ ค์ฃผ์ธ์ ๐งโโ๏ธ") # This prevents re-running the API call if the image hasn't changed if "last_image" not in st.session_state or st.session_state.last_image != camera_image.getvalue(): st.session_state.last_image = camera_image.getvalue() with st.spinner("๋ง๋ฒ์ฌ ์์ ์ด ์ฑ
์ ์ฝ๊ณ ์์ด์... ๋พฐ๋ก๋กฑ! โจ"): try: # 1. OCR + Contextualization with Gemini 2.5 Flash client = genai.Client(api_key=gemini_api_key) image = Image.open(camera_image) prompt = """ ์ด ์ด๋ฏธ์ง์ ์๋ ์ฑ
์ ๋ด์ฉ์ ์ ํํ๊ฒ ์ฝ์ด์ค. ์์ด์๊ฒ ๋ํ์ฑ
์ ์ฝ์ด์ฃผ๋ ๊ฒ์ฒ๋ผ ์น์ ํ๊ณ ๋ค์ ํ ๋งํฌ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ฐ๊ฟ์ ๋ค๋ ค์ค. ๋ด์ฉ์ ์ด์ธ๋ฆฌ๋ ๊ท์ฌ์ด ์ด๋ชจ์ง๋ ์ค๊ฐ์ค๊ฐ ๋ฃ์ด์ค. ์ด๋ฏธ์ง์ ํ
์คํธ๊ฐ ์๋ค๋ฉด "์, ๊ธ์๊ฐ ์ ๋ณด์ฌ์! ๋ค์ ํ ๋ฒ ์ฐ์ด์ค๋?" ๋ผ๊ณ ๋งํด์ค. """ response = client.models.generate_content( model='gemini-2.5-flash', contents=[prompt, image] ) st.session_state.story_text = response.text # 2. Text-to-Speech with gTTS tts = gTTS(text=st.session_state.story_text, lang='ko', slow=False) audio_fp = io.BytesIO() tts.write_to_fp(audio_fp) st.session_state.audio_bytes = audio_fp.getvalue() except Exception as e: st.error(f"์! ๋ง๋ฒ์ฌ ์์ ์ด ์ค์ํ์ด์: {e}") st.session_state.story_text = "" st.session_state.audio_bytes = None
# Display Outputif st.session_state.story_text and st.session_state.audio_bytes: st.markdown("### ๐๏ธ ๋ง๋ฒ์ฌ ์์ ์ ๋ชฉ์๋ฆฌ๋ฅผ ๋ค์ด๋ณด์ธ์!") st.audio(st.session_state.audio_bytes, format='audio/mp3', autoplay=True) st.markdown("### ๐ ์์ ์ด ์ฝ์ด์ค ๋ด์ฉ") st.markdown(f'<div class="story-box">{st.session_state.story_text}</div>', unsafe_allow_html=True)123456789streamlitpandasgoogle-genaigoogle-cloud-aiplatformplotlymatplotlibgoogle-api-python-clientgTTSPillowDockerfileAdded+7
book_reader_app.pyAdded+95
requirements.txtAdded+9