Files changed (1) hide show
  1. app.py +191 -40
app.py CHANGED
@@ -1,9 +1,13 @@
 
 
 
1
  import streamlit as st
 
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,17 +16,27 @@ st.set_page_config(
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",
@@ -30,62 +44,176 @@ uploaded_files = st.sidebar.file_uploader(
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")
67
 
68
  for msg in st.session_state.messages:
 
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:
78
 
79
- st.session_state.messages.append({
80
- "role": "user",
81
- "content": user_input
82
- })
 
 
83
 
84
  with st.chat_message("user"):
85
- st.write(user_input)
 
86
 
87
  with st.chat_message("assistant"):
88
- with st.spinner("AI Agent thinking..."):
 
89
 
90
  result = run_graph(
91
  user_input,
@@ -94,36 +222,59 @@ if user_input:
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
- })
 
 
 
1
+ import os
2
+ import io
3
+ from PIL import Image
4
  import streamlit as st
5
+
6
  from graph import run_graph
7
+
8
+ # -------------------------------------------------------
9
+ # PAGE CONFIG
10
+ # -------------------------------------------------------
11
 
12
  st.set_page_config(
13
  page_title="AI Image Studio",
 
16
 
17
  st.title("🧠 AI Image Studio (Agents + LangGraph)")
18
 
19
+ # -------------------------------------------------------
20
+ # SESSION STATE
21
+ # -------------------------------------------------------
22
 
 
23
  if "messages" not in st.session_state:
24
  st.session_state.messages = []
25
 
26
  if "uploaded_paths" not in st.session_state:
27
  st.session_state.uploaded_paths = []
28
 
29
+ if "analysis" not in st.session_state:
30
+ st.session_state.analysis = None
31
 
32
+ if "last_uploaded" not in st.session_state:
33
+ st.session_state.last_uploaded = []
34
+
35
+ # -------------------------------------------------------
36
+ # SIDEBAR
37
+ # -------------------------------------------------------
38
+
39
+ st.sidebar.header("📁 Upload Images")
40
 
41
  uploaded_files = st.sidebar.file_uploader(
42
  "Upload Images",
 
44
  accept_multiple_files=True
45
  )
46
 
47
+ # -------------------------------------------------------
48
+ # SAVE FILES
49
+ # -------------------------------------------------------
50
+
51
  if uploaded_files:
52
 
53
+ os.makedirs("uploads", exist_ok=True)
54
+
55
  paths = []
56
 
57
  for file in uploaded_files:
58
 
59
+ file_path = os.path.join("uploads", file.name)
 
 
 
60
 
61
  with open(file_path, "wb") as f:
62
  f.write(file.getbuffer())
63
 
64
  paths.append(file_path)
65
 
66
+ # Save paths
67
  st.session_state.uploaded_paths = paths
68
 
69
+ # ---------------------------------------------------
70
+ # AUTO ANALYZE ONLY IF NEW IMAGE
71
+ # ---------------------------------------------------
72
+
73
+ if paths != st.session_state.last_uploaded:
74
+
75
+ st.session_state.last_uploaded = paths
76
+
77
+ with st.spinner("🔍 Analyzing image..."):
78
+
79
+ result = run_graph(
80
+ "analyze this image",
81
+ uploaded_files=paths
82
+ )
83
+
84
+ analysis = result.get("result")
85
+
86
+ st.session_state.analysis = analysis
87
+
88
+ if isinstance(analysis, dict):
89
+
90
+ description = analysis.get(
91
+ "description",
92
+ "Image analyzed."
93
+ )
94
+
95
+ resolution = analysis.get("resolution", "")
96
+
97
+ style = analysis.get("style", "")
98
+
99
+ lighting = analysis.get("lighting", "")
100
+
101
+ objects = analysis.get("objects", [])
102
+
103
+ message = f"""📷 **Image Analysis**
104
+
105
+ **Description**
106
+
107
+ {description}
108
+ """
109
 
110
+ if resolution:
111
+ message += f"\n**Resolution:** {resolution}"
112
+
113
+ if style:
114
+ message += f"\n\n**Style:** {style}"
115
+
116
+ if lighting:
117
+ message += f"\n\n**Lighting:** {lighting}"
118
+
119
+ if objects:
120
+ message += f"\n\n**Objects:** {', '.join(objects)}"
121
+
122
+ message += """
123
+
124
+ ---
125
+
126
+ ### What would you like to do?
127
+
128
+ • Remove background
129
+
130
+ • Make it Anime
131
+
132
+ • Make it Pixar style
133
+
134
+ • Enhance quality
135
+
136
+ • Replace objects
137
+
138
+ • Change colors
139
+
140
+ • Add new objects
141
+
142
+ • Extract text
143
+ """
144
+
145
+ st.session_state.messages.append(
146
+ {
147
+ "role": "assistant",
148
+ "content": message
149
+ }
150
+ )
151
+
152
+ else:
153
+
154
+ st.session_state.messages.append(
155
+ {
156
+ "role": "assistant",
157
+ "content": str(analysis)
158
+ }
159
+ )
160
+
161
+ # -------------------------------------------------------
162
+ # SIDEBAR PREVIEW
163
+ # -------------------------------------------------------
164
 
 
165
  if st.session_state.uploaded_paths:
166
 
167
+ st.sidebar.subheader("🖼 Preview")
168
 
169
+ for img in st.session_state.uploaded_paths:
170
 
171
+ st.sidebar.image(
172
+ img,
173
+ caption=os.path.basename(img),
174
+ use_container_width=True
175
+ )
176
 
177
+ # -------------------------------------------------------
178
+ # CHAT
179
+ # -------------------------------------------------------
180
 
 
181
  st.subheader("💬 Chat with AI Agent")
182
 
183
  for msg in st.session_state.messages:
184
+
185
  with st.chat_message(msg["role"]):
 
186
 
187
+ st.markdown(msg["content"])
188
+
189
+ # -------------------------------------------------------
190
+ # USER INPUT
191
+ # -------------------------------------------------------
192
 
193
+ user_input = st.chat_input(
194
+ "Generate / Edit / Analyze image..."
195
+ )
196
 
197
+ # -------------------------------------------------------
198
+ # RUN AGENT
199
+ # -------------------------------------------------------
200
 
 
201
  if user_input:
202
 
203
+ st.session_state.messages.append(
204
+ {
205
+ "role": "user",
206
+ "content": user_input
207
+ }
208
+ )
209
 
210
  with st.chat_message("user"):
211
+
212
+ st.markdown(user_input)
213
 
214
  with st.chat_message("assistant"):
215
+
216
+ with st.spinner("🤖 AI Agent thinking..."):
217
 
218
  result = run_graph(
219
  user_input,
 
222
 
223
  output = result.get("result")
224
 
225
+ assistant_message = ""
226
+
227
+ # ------------------------------------------------
228
+ # IMAGE OUTPUT
229
+ # ------------------------------------------------
230
 
 
231
  if isinstance(output, Image.Image):
232
 
233
+ st.image(
234
+ output,
235
+ caption="Generated Image",
236
+ use_container_width=True
237
+ )
238
+
239
+ buffer = io.BytesIO()
240
 
241
+ output.save(buffer, format="PNG")
 
242
 
243
  st.download_button(
244
+ "⬇ Download Image",
245
+ buffer.getvalue(),
246
  "result.png",
247
  "image/png"
248
  )
249
 
250
+ assistant_message = "✅ Image generated successfully."
251
+
252
+ # ------------------------------------------------
253
+ # DICTIONARY OUTPUT
254
+ # ------------------------------------------------
255
 
 
256
  elif isinstance(output, dict):
257
 
258
  st.json(output)
259
 
260
+ assistant_message = output.get(
261
+ "description",
262
+ str(output)
263
+ )
264
+
265
+ # ------------------------------------------------
266
+ # TEXT OUTPUT
267
+ # ------------------------------------------------
268
 
 
269
  else:
270
 
271
+ st.markdown(str(output))
272
 
273
+ assistant_message = str(output)
274
 
275
+ st.session_state.messages.append(
276
+ {
277
+ "role": "assistant",
278
+ "content": assistant_message
279
+ }
280
+ )