nimra2019 commited on
Commit
708bc5a
·
verified ·
1 Parent(s): 70405a6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from diffusers import StableDiffusionInpaintPipeline, StableDiffusionPipeline
3
+ from PIL import Image
4
+ import torch
5
+ import uuid
6
+ import os
7
+
8
+ # ---------------------------------------
9
+ # Load Models (Works on HF Spaces CPU)
10
+ # ---------------------------------------
11
+
12
+ @st.cache_resource
13
+ def load_inpaint_model():
14
+ return StableDiffusionInpaintPipeline.from_pretrained(
15
+ "runwayml/stable-diffusion-inpainting",
16
+ torch_dtype=torch.float32
17
+ )
18
+
19
+ @st.cache_resource
20
+ def load_txt2img_model():
21
+ return StableDiffusionPipeline.from_pretrained(
22
+ "stabilityai/stable-diffusion-2-1",
23
+ torch_dtype=torch.float32
24
+ )
25
+
26
+ # Load both
27
+ inpaint_model = load_inpaint_model()
28
+ txt2img_model = load_txt2img_model()
29
+
30
+
31
+ # ---------------------------------------
32
+ # Streamlit UI
33
+ # ---------------------------------------
34
+
35
+ st.set_page_config(
36
+ page_title="AI Image Editor",
37
+ page_icon="🎨",
38
+ layout="wide"
39
+ )
40
+
41
+ st.title("🎨 AI Image Editor (HuggingFace + Streamlit)")
42
+ st.write("Upload an image and edit it using natural language prompts.")
43
+
44
+ # Sidebar — History Manager
45
+ st.sidebar.header("🕑 Edit History")
46
+ if "history" not in st.session_state:
47
+ st.session_state.history = []
48
+
49
+ if "current_image" not in st.session_state:
50
+ st.session_state.current_image = None
51
+
52
+
53
+ # ---------------------------------------
54
+ # Image Upload
55
+ # ---------------------------------------
56
+
57
+ uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
58
+
59
+ if uploaded_file:
60
+ input_image = Image.open(uploaded_file).convert("RGB")
61
+ st.session_state.current_image = input_image
62
+ st.image(input_image, caption="Original Image", use_column_width=True)
63
+
64
+ # ---------------------------------------
65
+ # Prompt Input
66
+ # ---------------------------------------
67
+
68
+ prompt = st.text_input("Enter editing instruction (e.g., 'remove background', 'add sunset sky', 'cartoon style')")
69
+
70
+ edit_button = st.button("Apply Edit")
71
+
72
+
73
+ # ---------------------------------------
74
+ # Editing Logic
75
+ # ---------------------------------------
76
+
77
+ def save_to_history(image, prompt):
78
+ st.session_state.history.append({
79
+ "prompt": prompt,
80
+ "image": image
81
+ })
82
+
83
+ def display_history():
84
+ for i, step in enumerate(st.session_state.history):
85
+ st.sidebar.image(step["image"], caption=f"Step {i+1}: {step['prompt']}", width=150)
86
+
87
+
88
+ if edit_button and st.session_state.current_image is not None:
89
+
90
+ with st.spinner("Processing..."):
91
+
92
+ # Decide editing type
93
+ if "remove background" in prompt.lower():
94
+ # Replace background with white (simple trick)
95
+ edited_image = st.session_state.current_image.convert("RGBA")
96
+ datas = edited_image.getdata()
97
+ new_data = []
98
+ for item in datas:
99
+ if item[0] > 200 and item[1] > 200 and item[2] > 200:
100
+ new_data.append((255, 255, 255, 0))
101
+ else:
102
+ new_data.append(item)
103
+ edited_image.putdata(new_data)
104
+
105
+ elif "add" in prompt.lower() or "change" in prompt.lower():
106
+ # Use text-to-image model for creative edits
107
+ edited_image = txt2img_model(prompt=prompt).images[0]
108
+
109
+ else:
110
+ # Default = inpainting logic
111
+ mask = Image.new("L", st.session_state.current_image.size, 0)
112
+ edited_image = inpaint_model(
113
+ prompt=prompt,
114
+ image=st.session_state.current_image,
115
+ mask_image=mask
116
+ ).images[0]
117
+
118
+ st.session_state.current_image = edited_image
119
+ save_to_history(edited_image, prompt)
120
+
121
+ st.image(edited_image, caption="Edited Image", use_column_width=True)
122
+
123
+
124
+ # ---------------------------------------
125
+ # Show History
126
+ # ---------------------------------------
127
+
128
+ display_history()
129
+
130
+ # ---------------------------------------
131
+ # Download Button
132
+ # ---------------------------------------
133
+
134
+ if st.session_state.current_image:
135
+ img_name = f"edited_{uuid.uuid4().hex}.png"
136
+ st.download_button(
137
+ label="Download Edited Image",
138
+ data=st.session_state.current_image.save(img_name),
139
+ file_name=img_name,
140
+ mime="image/png"
141
+ )