Spaces:
Sleeping
Sleeping
Update app.py
#9
by Muthuraja18 - opened
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import streamlit as st
|
|
| 2 |
from graph import run_graph
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
|
|
|
|
|
|
| 5 |
|
| 6 |
st.set_page_config(
|
| 7 |
page_title="AI Image Studio",
|
|
@@ -10,31 +12,55 @@ st.set_page_config(
|
|
| 10 |
|
| 11 |
st.title("🧠 AI Image Studio (Agents + LangGraph)")
|
| 12 |
|
|
|
|
| 13 |
# ---------------- SESSION STATE ----------------
|
| 14 |
if "messages" not in st.session_state:
|
| 15 |
st.session_state.messages = []
|
| 16 |
|
| 17 |
-
if "
|
| 18 |
-
st.session_state.
|
|
|
|
| 19 |
|
| 20 |
# ---------------- SIDEBAR ----------------
|
| 21 |
st.sidebar.header("📁 Upload Files")
|
| 22 |
|
| 23 |
uploaded_files = st.sidebar.file_uploader(
|
| 24 |
-
"Upload Images
|
| 25 |
type=["png", "jpg", "jpeg"],
|
| 26 |
accept_multiple_files=True
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
if uploaded_files:
|
| 30 |
-
st.session_state.uploaded_files = uploaded_files
|
| 31 |
-
st.sidebar.success(f"{len(uploaded_files)} file(s) uploaded")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
st.sidebar.subheader("Preview")
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# ---------------- CHAT UI ----------------
|
| 40 |
st.subheader("💬 Chat with AI Agent")
|
|
@@ -43,7 +69,9 @@ for msg in st.session_state.messages:
|
|
| 43 |
with st.chat_message(msg["role"]):
|
| 44 |
st.write(msg["content"])
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# ---------------- RUN AGENT ----------------
|
| 49 |
if user_input:
|
|
@@ -61,36 +89,41 @@ if user_input:
|
|
| 61 |
|
| 62 |
result = run_graph(
|
| 63 |
user_input,
|
| 64 |
-
uploaded_files=st.session_state.
|
| 65 |
)
|
| 66 |
|
| 67 |
output = result.get("result")
|
| 68 |
|
| 69 |
-
# ---------------- IMAGE RESULT ----------------
|
| 70 |
-
if hasattr(output, "show") or str(type(output)).find("PIL") != -1:
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
buf = io.BytesIO()
|
| 75 |
output.save(buf, format="PNG")
|
| 76 |
|
| 77 |
st.download_button(
|
| 78 |
-
"⬇ Download
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
|
|
|
| 85 |
elif isinstance(output, dict):
|
| 86 |
|
| 87 |
st.json(output)
|
| 88 |
|
| 89 |
-
|
|
|
|
| 90 |
else:
|
|
|
|
| 91 |
st.write(output)
|
| 92 |
|
|
|
|
| 93 |
st.session_state.messages.append({
|
| 94 |
"role": "assistant",
|
| 95 |
-
"content":
|
| 96 |
})
|
|
|
|
| 2 |
from graph import run_graph
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
st.set_page_config(
|
| 9 |
page_title="AI Image Studio",
|
|
|
|
| 12 |
|
| 13 |
st.title("🧠 AI Image Studio (Agents + LangGraph)")
|
| 14 |
|
| 15 |
+
|
| 16 |
# ---------------- SESSION STATE ----------------
|
| 17 |
if "messages" not in st.session_state:
|
| 18 |
st.session_state.messages = []
|
| 19 |
|
| 20 |
+
if "uploaded_paths" not in st.session_state:
|
| 21 |
+
st.session_state.uploaded_paths = []
|
| 22 |
+
|
| 23 |
|
| 24 |
# ---------------- SIDEBAR ----------------
|
| 25 |
st.sidebar.header("📁 Upload Files")
|
| 26 |
|
| 27 |
uploaded_files = st.sidebar.file_uploader(
|
| 28 |
+
"Upload Images",
|
| 29 |
type=["png", "jpg", "jpeg"],
|
| 30 |
accept_multiple_files=True
|
| 31 |
)
|
| 32 |
|
| 33 |
+
# Convert Streamlit file → REAL FILE PATHS
|
| 34 |
if uploaded_files:
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
paths = []
|
| 37 |
+
|
| 38 |
+
for file in uploaded_files:
|
| 39 |
+
|
| 40 |
+
temp_dir = "uploads"
|
| 41 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 42 |
+
|
| 43 |
+
file_path = os.path.join(temp_dir, file.name)
|
| 44 |
+
|
| 45 |
+
with open(file_path, "wb") as f:
|
| 46 |
+
f.write(file.getbuffer())
|
| 47 |
+
|
| 48 |
+
paths.append(file_path)
|
| 49 |
+
|
| 50 |
+
st.session_state.uploaded_paths = paths
|
| 51 |
+
|
| 52 |
+
st.sidebar.success(f"{len(paths)} files saved")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# Preview
|
| 56 |
+
if st.session_state.uploaded_paths:
|
| 57 |
+
|
| 58 |
st.sidebar.subheader("Preview")
|
| 59 |
+
|
| 60 |
+
for path in st.session_state.uploaded_paths:
|
| 61 |
+
|
| 62 |
+
st.sidebar.image(path, caption=os.path.basename(path))
|
| 63 |
+
|
| 64 |
|
| 65 |
# ---------------- CHAT UI ----------------
|
| 66 |
st.subheader("💬 Chat with AI Agent")
|
|
|
|
| 69 |
with st.chat_message(msg["role"]):
|
| 70 |
st.write(msg["content"])
|
| 71 |
|
| 72 |
+
|
| 73 |
+
user_input = st.chat_input("Generate / Edit / Analyze image...")
|
| 74 |
+
|
| 75 |
|
| 76 |
# ---------------- RUN AGENT ----------------
|
| 77 |
if user_input:
|
|
|
|
| 89 |
|
| 90 |
result = run_graph(
|
| 91 |
user_input,
|
| 92 |
+
uploaded_files=st.session_state.uploaded_paths
|
| 93 |
)
|
| 94 |
|
| 95 |
output = result.get("result")
|
| 96 |
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# ---------------- IMAGE OUTPUT ----------------
|
| 99 |
+
if isinstance(output, Image.Image):
|
| 100 |
+
|
| 101 |
+
st.image(output, caption="Result", use_container_width=True)
|
| 102 |
|
| 103 |
buf = io.BytesIO()
|
| 104 |
output.save(buf, format="PNG")
|
| 105 |
|
| 106 |
st.download_button(
|
| 107 |
+
"⬇ Download",
|
| 108 |
+
buf.getvalue(),
|
| 109 |
+
"result.png",
|
| 110 |
+
"image/png"
|
| 111 |
)
|
| 112 |
|
| 113 |
+
|
| 114 |
+
# ---------------- DICT OUTPUT ----------------
|
| 115 |
elif isinstance(output, dict):
|
| 116 |
|
| 117 |
st.json(output)
|
| 118 |
|
| 119 |
+
|
| 120 |
+
# ---------------- TEXT OUTPUT ----------------
|
| 121 |
else:
|
| 122 |
+
|
| 123 |
st.write(output)
|
| 124 |
|
| 125 |
+
|
| 126 |
st.session_state.messages.append({
|
| 127 |
"role": "assistant",
|
| 128 |
+
"content": "Done"
|
| 129 |
})
|