imseldrith commited on
Commit
041b694
1 Parent(s): fffaf19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -24
app.py CHANGED
@@ -1,25 +1,27 @@
1
  import gradio as gr
2
  import subprocess as sp
3
  import os
 
4
 
5
  os.makedirs("./output", exist_ok=True)
6
 
7
  def run(*args):
8
- source, target, *rest_args = args
9
  if not os.path.exists(source):
10
  return "Source file does not exist"
11
  if not os.path.exists(target):
12
  return "Target file does not exist"
13
 
14
  filename = os.path.basename(target)
15
- output = f"./output/{filename}"
 
16
  frame_processor = rest_args[0]
17
  selected_frame_processors = ' '.join(frame_processor)
18
-
19
  face_analyser_direction = rest_args[1]
20
  face_analyser_age = rest_args[2]
21
  face_analyser_gender = rest_args[3]
22
-
23
  cmd = (
24
  f"python run.py --execution-providers cpu -s {source} -t {target} -o {output} "
25
  f"--frame-processors {selected_frame_processors} "
@@ -29,7 +31,7 @@ def run(*args):
29
  cmd += f" --face-analyser-age {face_analyser_age}"
30
  if face_analyser_gender != 'none':
31
  cmd += f" --face-analyser-gender {face_analyser_gender}"
32
-
33
  if len(rest_args) > 4:
34
  skip_audio = rest_args[4]
35
  keep_fps = rest_args[5]
@@ -40,21 +42,31 @@ def run(*args):
40
  cmd += " --keep-fps"
41
  if keep_temp:
42
  cmd += " --keep-temp"
43
-
44
  try:
45
  print("Started...", cmd)
46
- sp.run(cmd, shell=True, capture_output=True, text=True).stdout
47
-
48
  return output
49
  except Exception as e:
50
  return f"An error occurred: {str(e)}"
51
- def clear_output(output):
52
- if os.path.exists(output):
53
- os.remove(output)
54
- return "Output file deleted"
55
- else:
56
- return "Output file does not exist"
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def get_theme() -> gr.Theme:
60
  return gr.themes.Soft(
@@ -78,7 +90,7 @@ with gr.Blocks(theme=get_theme(), title="DeepFakeAI 1.0.0") as ui:
78
  label = 'FRAME PROCESSORS',
79
  value = ['face_swapper'] # Default value
80
  )
81
-
82
  with gr.Box():
83
  with gr.Column(scale=3):
84
  face_analyser_direction_dropdown = gr.Dropdown(
@@ -96,19 +108,21 @@ with gr.Blocks(theme=get_theme(), title="DeepFakeAI 1.0.0") as ui:
96
  choices = ['none'] + ['male', 'female'],
97
  value = 'none'
98
  )
 
99
  with gr.Tab("Image: "):
100
  source_image = gr.Image(type="filepath", label="SOURCE IMAGE")
101
  target_image = gr.Image(type="filepath", label="TARGET IMAGE")
102
  image_button = gr.Button("START")
103
- clear_button = gr.Button("CLEAR")
104
  image_output = gr.Image(label="OUTPUT")
105
-
 
106
  image_button.click(
107
- run,
108
- inputs=[source_image, target_image, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown],
109
  outputs=image_output
110
  )
111
- clear_button.click(clear_output, inputs=[image_output])
112
 
113
  with gr.Tab("Video: "):
114
  source_image_video = gr.Image(type="filepath", label="SOURCE IMAGE")
@@ -118,13 +132,14 @@ with gr.Blocks(theme=get_theme(), title="DeepFakeAI 1.0.0") as ui:
118
  keep_fps = gr.Checkbox(label="KEEP FPS")
119
  keep_temp = gr.Checkbox(label="KEEP TEMP")
120
  video_button = gr.Button("START")
121
- clear_video_button = gr.Button("CLEAR")
122
  video_output = gr.Video(label="OUTPUT")
 
123
  video_button.click(
124
- run,
125
- inputs=[source_image_video, target_video, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown, skip_audio, keep_fps, keep_temp],
126
  outputs=video_output
127
  )
128
- clear_video_button.click(clear_output, inputs=[video_output])
129
 
130
  ui.launch(debug=True)
 
1
  import gradio as gr
2
  import subprocess as sp
3
  import os
4
+ import uuid
5
 
6
  os.makedirs("./output", exist_ok=True)
7
 
8
  def run(*args):
9
+ source, target, unique_id, *rest_args = args
10
  if not os.path.exists(source):
11
  return "Source file does not exist"
12
  if not os.path.exists(target):
13
  return "Target file does not exist"
14
 
15
  filename = os.path.basename(target)
16
+ os.makedirs(f"./output/{unique_id}",exist_ok=True)
17
+ output = f"./output/{unique_id}/{filename}"
18
  frame_processor = rest_args[0]
19
  selected_frame_processors = ' '.join(frame_processor)
20
+
21
  face_analyser_direction = rest_args[1]
22
  face_analyser_age = rest_args[2]
23
  face_analyser_gender = rest_args[3]
24
+
25
  cmd = (
26
  f"python run.py --execution-providers cpu -s {source} -t {target} -o {output} "
27
  f"--frame-processors {selected_frame_processors} "
 
31
  cmd += f" --face-analyser-age {face_analyser_age}"
32
  if face_analyser_gender != 'none':
33
  cmd += f" --face-analyser-gender {face_analyser_gender}"
34
+
35
  if len(rest_args) > 4:
36
  skip_audio = rest_args[4]
37
  keep_fps = rest_args[5]
 
42
  cmd += " --keep-fps"
43
  if keep_temp:
44
  cmd += " --keep-temp"
45
+
46
  try:
47
  print("Started...", cmd)
48
+ output_text = sp.run(cmd, shell=True, capture_output=True, text=True).stdout
49
+ print(output_text)
50
  return output
51
  except Exception as e:
52
  return f"An error occurred: {str(e)}"
 
 
 
 
 
 
53
 
54
+ def clear_output(unique_id):
55
+ try:
56
+ output_path = f"./output/{unique_id}"
57
+ if os.path.exists(output_path):
58
+ print("Trying to delete ")
59
+ for filename in os.listdir(output_path):
60
+ file_path = os.path.join(output_path, filename)
61
+ if os.path.isfile(file_path):
62
+ os.remove(file_path)
63
+ print(f"Output files in {output_path} are deleted")
64
+ return "Output files for unique_id deleted"
65
+ else:
66
+ print(f"Output files in {output_path} does not exist")
67
+ return "Output directory for (output_path} does not exist"
68
+ except Exception as e:
69
+ return f"An error occurred: {str(e)}"
70
 
71
  def get_theme() -> gr.Theme:
72
  return gr.themes.Soft(
 
90
  label = 'FRAME PROCESSORS',
91
  value = ['face_swapper'] # Default value
92
  )
93
+
94
  with gr.Box():
95
  with gr.Column(scale=3):
96
  face_analyser_direction_dropdown = gr.Dropdown(
 
108
  choices = ['none'] + ['male', 'female'],
109
  value = 'none'
110
  )
111
+ unique_id = gr.Textbox(value=str(uuid.uuid4()), visible=False)
112
  with gr.Tab("Image: "):
113
  source_image = gr.Image(type="filepath", label="SOURCE IMAGE")
114
  target_image = gr.Image(type="filepath", label="TARGET IMAGE")
115
  image_button = gr.Button("START")
116
+ clear_button = gr.ClearButton(value="CLEAR")
117
  image_output = gr.Image(label="OUTPUT")
118
+ clear_button.add(image_output)
119
+
120
  image_button.click(
121
+ run,
122
+ inputs=[source_image, target_image, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown],
123
  outputs=image_output
124
  )
125
+ clear_button.click(fn=clear_output, inputs=unique_id)
126
 
127
  with gr.Tab("Video: "):
128
  source_image_video = gr.Image(type="filepath", label="SOURCE IMAGE")
 
132
  keep_fps = gr.Checkbox(label="KEEP FPS")
133
  keep_temp = gr.Checkbox(label="KEEP TEMP")
134
  video_button = gr.Button("START")
135
+ clear_video_button = gr.ClearButton(value="CLEAR")
136
  video_output = gr.Video(label="OUTPUT")
137
+ clear_video_button.add(video_output)
138
  video_button.click(
139
+ run,
140
+ inputs=[source_image_video, target_video, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown, skip_audio, keep_fps, keep_temp],
141
  outputs=video_output
142
  )
143
+ clear_video_button.click(fn=clear_output, inputs=unique_id)
144
 
145
  ui.launch(debug=True)