yongyeol commited on
Commit
5df15e0
Β·
verified Β·
1 Parent(s): c886cb9

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +61 -39
src/streamlit_app.py CHANGED
@@ -1,40 +1,62 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ from io import BytesIO
6
+
7
+ # ───────────────────────────────
8
+ # CONFIG
9
+ # ───────────────────────────────
10
+ st.set_page_config(page_title="동화 μ‚½ν™” 생성기", layout="wide")
11
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-Kontext-dev"
12
+ headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
13
+
14
+ # ───────────────────────────────
15
+ # UTILS
16
+ # ───────────────────────────────
17
+ def query_flux(prompt, image_bytes=None):
18
+ inputs = {"prompt": prompt}
19
+ if image_bytes:
20
+ inputs["image"] = base64.b64encode(image_bytes).decode("utf-8")
21
+
22
+ response = requests.post(API_URL, headers=headers, json=inputs)
23
+ if response.status_code == 200:
24
+ image_data = base64.b64decode(response.json()["image"])
25
+ return Image.open(BytesIO(image_data))
26
+ else:
27
+ st.error(f"Error: {response.status_code} - {response.text}")
28
+ return None
29
+
30
+ # ───────────────────────────────
31
+ # UI
32
+ # ───────────────────────────────
33
+ st.title("πŸ“š 동화 μ‚½ν™” 생성기 (FLUX.1 Kontext)")
34
+ st.markdown("각 μž₯면의 μ„€λͺ…을 μž…λ ₯ν•˜λ©΄, 이야기 흐름에 λ§žλŠ” μ‚½ν™”λ₯Ό μžλ™μœΌλ‘œ μƒμ„±ν•©λ‹ˆλ‹€.")
35
+
36
+ with st.expander("λ“±μž₯인물 및 μŠ€νƒ€μΌ 정보 (선택)"):
37
+ character_prompt = st.text_area("λ“±μž₯인물 μ„€λͺ… (ex. λΉ¨κ°„ 망토λ₯Ό μ“΄ μ†Œλ…€, νšŒμƒ‰ λŠ‘λŒ€ λ“±)", height=100)
38
+ reference_image = st.file_uploader("μ°Έμ‘° 이미지 μ—…λ‘œλ“œ (선택)", type=["jpg", "png"])
39
+
40
+ st.markdown("### πŸ“ 동화 λ‚΄μš©μ„ 5개 μž₯면으둜 λ‚˜λˆ„μ–΄ μž…λ ₯ν•˜μ„Έμš”")
41
+
42
+ scene_prompts = []
43
+ for i in range(5):
44
+ text = st.text_area(f"μž₯λ©΄ {i+1} μ„€λͺ…", key=f"scene_{i}", height=80)
45
+ scene_prompts.append(text)
46
+
47
+ if st.button("🎨 μ‚½ν™” μƒμ„±ν•˜κΈ°"):
48
+ if not any(scene_prompts):
49
+ st.warning("μ΅œμ†Œ ν•˜λ‚˜ μ΄μƒμ˜ μž₯λ©΄ μ„€λͺ…이 ν•„μš”ν•©λ‹ˆλ‹€.")
50
+ else:
51
+ ref_bytes = reference_image.read() if reference_image else None
52
+
53
+ with st.spinner("이미지λ₯Ό 생성 μ€‘μž…λ‹ˆλ‹€..."):
54
+ cols = st.columns(5)
55
+ for i, prompt in enumerate(scene_prompts):
56
+ if not prompt.strip():
57
+ continue
58
+
59
+ full_prompt = f"{character_prompt}. {prompt}" if character_prompt else prompt
60
+ img = query_flux(full_prompt, ref_bytes)
61
+ if img:
62
+ cols[i].image(img, caption=f"μž₯λ©΄ {i+1}")