Arjun Singh commited on
Commit
7f6532e
·
1 Parent(s): 7aa357c

new faceswapper

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -15,14 +15,14 @@ import numpy
15
  API_KEY = os.getenv('BASETEN_API_KEY')
16
  API_url = os.getenv('BASETEN_COMFY_MODEL_URL')
17
 
18
- def call_api_and_generate_image(negative_prompt, positive_prompt,seed, source_image):
19
  if not API_KEY:
20
  return None, "Error: API key not found in environment variables. Please set BASETEN_API_KEY."
21
 
22
  # Convert the source image to base64
23
- buffered = io.BytesIO()
24
- source_image.save(buffered, format="PNG")
25
- img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
26
 
27
  resp = requests.post(
28
  API_url,
@@ -32,41 +32,46 @@ def call_api_and_generate_image(negative_prompt, positive_prompt,seed, source_im
32
  'negative_prompt': negative_prompt,
33
  'positive_prompt': positive_prompt,
34
  'seed': seed,
35
- 'source_image': img_str
36
  }
37
  }
38
  )
39
 
40
  result = resp.json()
 
41
  base64_data = result['result'][0]['data']
42
  image_data = base64.b64decode(base64_data)
43
  image = Image.open(io.BytesIO(image_data))
44
- return image, json.dumps(result, indent=2)
45
 
46
  def generate_image(positive_prompt, negative_prompt,source_image):
47
  seed = numpy.random.randint(0, 2**32 - 1)
 
 
 
 
48
  try:
49
- return call_api_and_generate_image(negative_prompt, positive_prompt,seed,source_image)
50
  except Exception as e:
51
- return None, f"Error: {str(e)}"
52
 
53
  def clear_fields():
54
- return "", "", None,"" # Clear prompt and outputs
55
 
56
  with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
57
- gr.Markdown("<center><h2>Arjun's Image Generator</h2></center>")
58
- gr.Markdown("Hi there! I'm an AI assistant tasked with generating images.")
59
- prompt = gr.Textbox(label='Positve Prompt', lines=2, max_lines=5, placeholder = 'Describe your image here.')
60
- neg_prompt = gr.Textbox(label='Negative Prompt', lines=2, max_lines=10, value="Low quality, pixelated")
61
- source_image = gr.Image(label='Source Image for Face Swap', type="pil")
62
  with gr.Group():
63
  with gr.Row():
64
  submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
65
  clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
66
  gr.Markdown("<center><h3>AI Response</h3></center>")
67
  image_output_box = gr.Image(type="pil", label="Final Generated Image")
68
- json_output = gr.Textbox(label="API Response JSON", lines=10)
69
- submit_btn.click(fn=generate_image, inputs = [prompt,neg_prompt,source_image], outputs=[image_output_box,json_output])
70
- clear_btn.click(fn=clear_fields,outputs=[prompt,neg_prompt,image_output_box,json_output])
71
 
72
  demo.launch()
 
15
  API_KEY = os.getenv('BASETEN_API_KEY')
16
  API_url = os.getenv('BASETEN_COMFY_MODEL_URL')
17
 
18
+ def call_api_and_generate_image(negative_prompt, positive_prompt,seed,source_image_base64):
19
  if not API_KEY:
20
  return None, "Error: API key not found in environment variables. Please set BASETEN_API_KEY."
21
 
22
  # Convert the source image to base64
23
+ #buffered = io.BytesIO()
24
+ #source_image.save(buffered, format="PNG")
25
+ #img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
26
 
27
  resp = requests.post(
28
  API_url,
 
32
  'negative_prompt': negative_prompt,
33
  'positive_prompt': positive_prompt,
34
  'seed': seed,
35
+ 'source_image_fs': source_image_base64
36
  }
37
  }
38
  )
39
 
40
  result = resp.json()
41
+ print(result)
42
  base64_data = result['result'][0]['data']
43
  image_data = base64.b64decode(base64_data)
44
  image = Image.open(io.BytesIO(image_data))
45
+ return image#, json.dumps(result, indent=2)
46
 
47
  def generate_image(positive_prompt, negative_prompt,source_image):
48
  seed = numpy.random.randint(0, 2**32 - 1)
49
+ source_image_base64 = None
50
+ if source_image is not None:
51
+ with open(source_image, "rb") as image_file:
52
+ source_image_base64 = base64.b64encode(image_file.read()).decode('utf-8')
53
  try:
54
+ return call_api_and_generate_image(negative_prompt, positive_prompt,seed,source_image_base64)
55
  except Exception as e:
56
+ return None#, f"Error: {str(e)}"
57
 
58
  def clear_fields():
59
+ return "", "", None # Clear prompt and outputs
60
 
61
  with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
62
+ gr.Markdown("<center><h2>Arjun's Face Swapper</h2></center>")
63
+ gr.Markdown("Hi there! Specify a man/woman in any setting below and upload your image to see yourself in that setting!")
64
+ prompt = gr.Textbox(label='Positve Prompt', lines=2, max_lines=5, placeholder = 'Describe your image here, for e.g., superman flying in the sky')
65
+ neg_prompt = gr.Textbox(label='Negative Prompt', lines=2, max_lines=10, value="Low quality, pixelated, poor anatomy, bad fingers")
66
+ source_image = gr.Image(label='Source Image for Face Swap', type="filepath")
67
  with gr.Group():
68
  with gr.Row():
69
  submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
70
  clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
71
  gr.Markdown("<center><h3>AI Response</h3></center>")
72
  image_output_box = gr.Image(type="pil", label="Final Generated Image")
73
+ #json_output = gr.Textbox(label="API Response JSON", lines=10)
74
+ submit_btn.click(fn=generate_image, inputs = [prompt,neg_prompt,source_image], outputs=[image_output_box])
75
+ clear_btn.click(fn=clear_fields,outputs=[prompt,neg_prompt,image_output_box])
76
 
77
  demo.launch()