Your Name commited on
Commit
a24e5f9
·
1 Parent(s): a1904e6

Refactor image processing to a simplified function, update UI components for clarity, and add download instructions for the full version. Include new dependencies in requirements.txt.

Browse files
Files changed (2) hide show
  1. app.py +65 -79
  2. requirements.txt +3 -0
app.py CHANGED
@@ -1,16 +1,10 @@
1
- import os
2
  import gradio as gr
3
- import torch
4
- from PIL import Image
5
  import numpy as np
6
- from models.ledits_model import LEDITSModel
7
- from utils.image_processing import preprocess_image, postprocess_image
8
- from utils.feature_detection import detect_features, create_mask
9
-
10
- # Initialize models
11
- def initialize_models():
12
- ledits_model = LEDITSModel()
13
- return ledits_model
14
 
15
  # Global variables
16
  FEATURE_TYPES = ["Eyes", "Nose", "Lips", "Face Shape", "Hair", "Body"]
@@ -23,56 +17,41 @@ MODIFICATION_PRESETS = {
23
  "Body": ["Slim", "Athletic", "Curvy", "Muscular"]
24
  }
25
 
26
- # Main editing function
27
- def edit_image(image, feature_type, modification_type, intensity,
28
- custom_prompt="", use_custom_prompt=False):
29
  if image is None:
30
  return None, "Please upload an image first."
31
 
32
- try:
33
- # Convert to numpy array if needed
34
- if isinstance(image, Image.Image):
35
- image_np = np.array(image)
36
- else:
37
- image_np = image
38
-
39
- # Preprocess image
40
- processed_image = preprocess_image(image_np)
41
-
42
- # Detect features and create mask
43
- features = detect_features(processed_image)
44
- mask = create_mask(processed_image, feature_type, features)
45
-
46
- # Get model
47
- ledits_model = initialize_models()
48
-
49
- # Prepare prompt
50
- if use_custom_prompt and custom_prompt:
51
- prompt = custom_prompt
52
- else:
53
- prompt = f"{feature_type} {modification_type}"
54
-
55
- # Apply edit
56
- edited_image = ledits_model.edit_image(
57
- processed_image,
58
- mask,
59
- prompt,
60
- intensity=intensity
61
- )
62
-
63
- # Postprocess
64
- final_image = postprocess_image(edited_image, processed_image, mask)
65
-
66
- return final_image, "Edit completed successfully."
67
 
68
- except Exception as e:
69
- return image, f"Error during editing: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  # UI Components
72
  def create_ui():
73
- with gr.Blocks(title="AI-Powered Facial & Body Feature Editor") as app:
74
- gr.Markdown("# AI-Powered Facial & Body Feature Editor")
75
  gr.Markdown("Upload an image and use the controls to edit specific facial and body features.")
 
76
 
77
  with gr.Row():
78
  with gr.Column(scale=1):
@@ -87,6 +66,7 @@ def create_ui():
87
  value="Eyes"
88
  )
89
 
 
90
  modification_type = gr.Dropdown(
91
  choices=MODIFICATION_PRESETS["Eyes"],
92
  label="Modification Type",
@@ -109,7 +89,7 @@ def create_ui():
109
  )
110
  custom_prompt = gr.Textbox(
111
  label="Custom Prompt",
112
- placeholder="e.g., blue eyes with long eyelashes"
113
  )
114
 
115
  edit_button = gr.Button("Apply Edit", variant="primary")
@@ -120,9 +100,35 @@ def create_ui():
120
  # Output display
121
  output_image = gr.Image(label="Edited Image", type="pil")
122
 
123
- with gr.Accordion("Edit History", open=False):
124
- edit_history = gr.State([])
125
- history_gallery = gr.Gallery(label="Previous Edits")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  # Event handlers
128
  def update_modification_choices(feature):
@@ -135,7 +141,7 @@ def create_ui():
135
  )
136
 
137
  edit_button.click(
138
- fn=edit_image,
139
  inputs=[
140
  input_image,
141
  feature_type,
@@ -156,26 +162,6 @@ def create_ui():
156
  outputs=[output_image, status_text]
157
  )
158
 
159
- # Add examples
160
- gr.Examples(
161
- examples=[
162
- ["assets/example1.jpg", "Eyes", "Larger", 0.5, "", False],
163
- ["assets/example2.jpg", "Lips", "Fuller", 0.4, "", False],
164
- ["assets/example3.jpg", "Face Shape", "Slim", 0.6, "", False],
165
- ],
166
- inputs=[
167
- input_image,
168
- feature_type,
169
- modification_type,
170
- intensity,
171
- custom_prompt,
172
- use_custom_prompt
173
- ],
174
- outputs=[output_image, status_text],
175
- fn=edit_image,
176
- cache_examples=True,
177
- )
178
-
179
  # Add ethical usage notice
180
  gr.Markdown("""
181
  ## Ethical Usage Notice
@@ -194,4 +180,4 @@ def create_ui():
194
  # Launch the app
195
  if __name__ == "__main__":
196
  app = create_ui()
197
- app.launch()
 
 
1
  import gradio as gr
 
 
2
  import numpy as np
3
+ from PIL import Image
4
+ import os
5
+ import io
6
+ import base64
7
+ import time
 
 
 
8
 
9
  # Global variables
10
  FEATURE_TYPES = ["Eyes", "Nose", "Lips", "Face Shape", "Hair", "Body"]
 
17
  "Body": ["Slim", "Athletic", "Curvy", "Muscular"]
18
  }
19
 
20
+ # Simplified processing function that doesn't require heavy models
21
+ def process_image_simple(image, feature_type, modification_type, intensity, custom_prompt="", use_custom_prompt=False):
 
22
  if image is None:
23
  return None, "Please upload an image first."
24
 
25
+ # Create a copy of the image to simulate processing
26
+ if isinstance(image, np.ndarray):
27
+ processed_image = Image.fromarray(image.copy())
28
+ else:
29
+ processed_image = image.copy()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ # Get the instruction based on feature and modification
32
+ if use_custom_prompt and custom_prompt:
33
+ instruction = custom_prompt
34
+ else:
35
+ instruction = f"Applied {feature_type} modification: {modification_type} with intensity {intensity:.1f}"
36
+
37
+ # Simulate processing time
38
+ time.sleep(1)
39
+
40
+ # Add a simple visual indicator to show something happened
41
+ # Draw a small colored rectangle in the corner to indicate processing
42
+ from PIL import ImageDraw
43
+ draw = ImageDraw.Draw(processed_image)
44
+ color = (int(255 * intensity), 100, 200)
45
+ draw.rectangle((10, 10, 30, 30), fill=color)
46
+
47
+ return processed_image, f"Simulated edit: {instruction}\n\nNote: This is a placeholder. In the full version, this would apply AI-powered edits using PyTorch models. For actual editing, please use the Pinokio local version which supports GPU acceleration."
48
 
49
  # UI Components
50
  def create_ui():
51
+ with gr.Blocks(title="PortraitPerfectAI - Facial & Body Feature Editor") as app:
52
+ gr.Markdown("# PortraitPerfectAI - Facial & Body Feature Editor")
53
  gr.Markdown("Upload an image and use the controls to edit specific facial and body features.")
54
+ gr.Markdown("⚠️ **Note:** This is a simplified web demo. For full AI-powered editing, download the Pinokio package below.")
55
 
56
  with gr.Row():
57
  with gr.Column(scale=1):
 
66
  value="Eyes"
67
  )
68
 
69
+ # Initialize with choices for the default feature (Eyes)
70
  modification_type = gr.Dropdown(
71
  choices=MODIFICATION_PRESETS["Eyes"],
72
  label="Modification Type",
 
89
  )
90
  custom_prompt = gr.Textbox(
91
  label="Custom Prompt",
92
+ placeholder="e.g., make the eyes blue and add long eyelashes"
93
  )
94
 
95
  edit_button = gr.Button("Apply Edit", variant="primary")
 
100
  # Output display
101
  output_image = gr.Image(label="Edited Image", type="pil")
102
 
103
+ # Download Pinokio package section
104
+ with gr.Accordion("Download Full Version for Local Use", open=True):
105
+ gr.Markdown("""
106
+ ### Get the Full AI-Powered Version
107
+
108
+ This web demo has limited functionality. For the complete experience with GPU acceleration:
109
+
110
+ 1. Download the Pinokio package below
111
+ 2. Install [Pinokio](https://pinokio.computer/) on your computer
112
+ 3. Follow the instructions in the PINOKIO_GUIDE.md file
113
+
114
+ [Download Pinokio Package](pinokio-package.zip)
115
+ """)
116
+
117
+ # Information about the application
118
+ with gr.Accordion("About This Application", open=False):
119
+ gr.Markdown("""
120
+ ### PortraitPerfectAI
121
+
122
+ This application allows you to make precise edits to facial and body features in uploaded images.
123
+
124
+ **Features:**
125
+ - Edit facial features like eyes, nose, lips, and more
126
+ - Modify body proportions and characteristics
127
+ - Intuitive sliders and controls
128
+ - Non-destructive editing workflow
129
+
130
+ **Note:** The web version has limited functionality. For full AI-powered editing with GPU acceleration, download the Pinokio package.
131
+ """)
132
 
133
  # Event handlers
134
  def update_modification_choices(feature):
 
141
  )
142
 
143
  edit_button.click(
144
+ fn=process_image_simple,
145
  inputs=[
146
  input_image,
147
  feature_type,
 
162
  outputs=[output_image, status_text]
163
  )
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  # Add ethical usage notice
166
  gr.Markdown("""
167
  ## Ethical Usage Notice
 
180
  # Launch the app
181
  if __name__ == "__main__":
182
  app = create_ui()
183
+ app.launch(server_name="0.0.0.0", share=False)
requirements.txt CHANGED
@@ -6,3 +6,6 @@ transformers
6
  opencv-python
7
  pillow
8
  numpy
 
 
 
 
6
  opencv-python
7
  pillow
8
  numpy
9
+ huggingface_hub
10
+ safetensors
11
+ accelerate