Files changed (1) hide show
  1. app.py +92 -55
app.py CHANGED
@@ -1,59 +1,96 @@
1
  import streamlit as st
2
- from diffusers import AutoPipelineForText2Image
3
- import torch
4
  from PIL import Image
5
  import io
6
- import os
7
- import requests
8
-
9
- # --- CONFIG ---
10
- USE_GROQ = False # Set to True when Groq image API is available
11
- GROQ_API_URL = "https://your-groq-image-api.com/generate" # Placeholder
12
-
13
- # Force CPU
14
- os.environ["CUDA_VISIBLE_DEVICES"] = ""
15
-
16
- @st.cache_resource
17
- def load_model():
18
- if USE_GROQ:
19
- return None # Skip local model
20
- pipe = AutoPipelineForText2Image.from_pretrained(
21
- "stabilityai/sd-turbo",
22
- torch_dtype=torch.float32
23
- )
24
- pipe.to("cpu")
25
- return pipe
26
-
27
- def generate_image_local(prompt, guidance_scale):
28
- pipe = load_model()
29
- result = pipe(prompt, guidance_scale=guidance_scale, num_inference_steps=20)
30
- return result.images[0]
31
-
32
- def generate_image_from_groq(prompt):
33
- response = requests.post(GROQ_API_URL, json={"prompt": prompt})
34
- if response.status_code == 200:
35
- image_bytes = io.BytesIO(response.content)
36
- return Image.open(image_bytes)
37
- else:
38
- raise Exception(f"GROQ API failed: {response.text}")
39
-
40
- # UI
41
- st.title("🧠 AI Image Generator (Fast with API / Groq-ready)")
42
-
43
- prompt = st.text_input("Prompt:", "A glowing alien forest with floating orbs, concept art, 8K")
44
- guidance = st.slider("Guidance scale", 1.0, 10.0, 3.0)
45
-
46
- if st.button("Generate"):
47
- with st.spinner("Generating..."):
48
- try:
49
- if USE_GROQ:
50
- image = generate_image_from_groq(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  else:
52
- image = generate_image_local(prompt, guidance)
53
-
54
- st.image(image, caption="Generated Image", use_column_width=True)
55
- buf = io.BytesIO()
56
- image.save(buf, format="PNG")
57
- st.download_button("Download Image", buf.getvalue(), "generated.png", "image/png")
58
- except Exception as e:
59
- st.error(f"Error: {e}")
 
1
  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",
8
+ layout="wide"
9
+ )
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 "uploaded_files" not in st.session_state:
18
+ st.session_state.uploaded_files = []
19
+
20
+ # ---------------- SIDEBAR ----------------
21
+ st.sidebar.header("📁 Upload Files")
22
+
23
+ uploaded_files = st.sidebar.file_uploader(
24
+ "Upload Images / Files",
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
+ # Show uploaded images
34
+ if st.session_state.uploaded_files:
35
+ st.sidebar.subheader("Preview")
36
+ for file in st.session_state.uploaded_files:
37
+ st.sidebar.image(file, caption=file.name, use_container_width=True)
38
+
39
+ # ---------------- CHAT UI ----------------
40
+ st.subheader("💬 Chat with AI Agent")
41
+
42
+ for msg in st.session_state.messages:
43
+ with st.chat_message(msg["role"]):
44
+ st.write(msg["content"])
45
+
46
+ user_input = st.chat_input("Type: generate / edit / analyze image...")
47
+
48
+ # ---------------- RUN AGENT ----------------
49
+ if user_input:
50
+
51
+ st.session_state.messages.append({
52
+ "role": "user",
53
+ "content": user_input
54
+ })
55
+
56
+ with st.chat_message("user"):
57
+ st.write(user_input)
58
+
59
+ with st.chat_message("assistant"):
60
+ with st.spinner("AI Agent thinking..."):
61
+
62
+ result = run_graph(
63
+ user_input,
64
+ uploaded_files=st.session_state.uploaded_files
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
+ st.image(output, caption="Generated / Edited Image", use_container_width=True)
73
+
74
+ buf = io.BytesIO()
75
+ output.save(buf, format="PNG")
76
+
77
+ st.download_button(
78
+ "⬇ Download Image",
79
+ data=buf.getvalue(),
80
+ file_name="result.png",
81
+ mime="image/png"
82
+ )
83
+
84
+ # ---------------- DICT RESULT (ANALYZE / OCR) ----------------
85
+ elif isinstance(output, dict):
86
+
87
+ st.json(output)
88
+
89
+ # ---------------- TEXT RESULT ----------------
90
  else:
91
+ st.write(output)
92
+
93
+ st.session_state.messages.append({
94
+ "role": "assistant",
95
+ "content": str(result.get("result"))
96
+ })