Arjun Singh commited on
Commit
5b51ea4
·
1 Parent(s): 2a4aef9

new face swapper

Browse files
Files changed (2) hide show
  1. app.py +26 -14
  2. app_simple_img_generator.py +66 -0
app.py CHANGED
@@ -15,10 +15,15 @@ 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):
19
  if not API_KEY:
20
  return None, "Error: API key not found in environment variables. Please set BASETEN_API_KEY."
21
 
 
 
 
 
 
22
  resp = requests.post(
23
  API_url,
24
  headers={"Authorization": f"Api-Key {API_KEY}"},
@@ -26,41 +31,48 @@ def call_api_and_generate_image(negative_prompt, positive_prompt,seed):
26
  'workflow_values': {
27
  'negative_prompt': negative_prompt,
28
  'positive_prompt': positive_prompt,
29
- 'seed': seed
 
30
  }
31
  }
32
  )
33
 
34
  result = resp.json()
35
- base64_data = result['result'][0]['data']
36
- image_data = base64.b64decode(base64_data)
37
- image = Image.open(io.BytesIO(image_data))
38
- return image, json.dumps(result, indent=2)
 
 
 
 
 
39
 
40
- def generate_image(positive_prompt, negative_prompt):
41
  seed = numpy.random.randint(0, 2**32 - 1)
42
  try:
43
- return call_api_and_generate_image(negative_prompt, positive_prompt,seed)
44
  except Exception as e:
45
  return None, f"Error: {str(e)}"
46
 
47
  def clear_fields():
48
- return "", "", "" # Clear prompt and output, but not the API key
49
 
50
  with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
51
  gr.Markdown("<center><h2>Arjun's Image Generator</h2></center>")
52
  gr.Markdown("Hi there! I'm an AI assistant tasked with generating images.")
53
  prompt = gr.Textbox(label='Positve Prompt', lines=2, max_lines=5, placeholder = 'Describe your image here.')
54
  neg_prompt = gr.Textbox(label='Negative Prompt', lines=2, max_lines=10, value="Low quality, pixelated")
55
-
56
  with gr.Group():
57
  with gr.Row():
58
  submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
59
  clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
60
  gr.Markdown("<center><h3>AI Response</h3></center>")
61
- image_output = gr.Image(type="pil", label="Generated Image")
62
- json_output = gr.Textbox(label="API Response JSON", lines=10)
63
- submit_btn.click(fn=generate_image, inputs = [prompt,neg_prompt], outputs=[image_output,json_output])
64
- clear_btn.click(fn=clear_fields,outputs=[prompt,image_output,json_output, neg_prompt])
 
65
 
66
  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):
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,
29
  headers={"Authorization": f"Api-Key {API_KEY}"},
 
31
  'workflow_values': {
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_initial = result['result'][0]['data']
42
+ initial_image_data = base64.b64decode(base64_initial)
43
+ initial_image = Image.open(io.BytesIO(initial_image_data))
44
+
45
+ base64_final = result['result'][1]['data']
46
+ final_image_data = base64.b64decode(base64_final)
47
+ final_image = Image.open(io.BytesIO(final_image_data))
48
+
49
+ return initial_image,final_image
50
 
51
+ def generate_image(positive_prompt, negative_prompt,source_image):
52
  seed = numpy.random.randint(0, 2**32 - 1)
53
  try:
54
+ return call_api_and_generate_image(negative_prompt, positive_prompt,seed,source_image)
55
  except Exception as e:
56
  return None, f"Error: {str(e)}"
57
 
58
  def clear_fields():
59
+ return "", "", None, None # Clear prompt and outputs
60
 
61
  with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
62
  gr.Markdown("<center><h2>Arjun's Image Generator</h2></center>")
63
  gr.Markdown("Hi there! I'm an AI assistant tasked with generating images.")
64
  prompt = gr.Textbox(label='Positve Prompt', lines=2, max_lines=5, placeholder = 'Describe your image here.')
65
  neg_prompt = gr.Textbox(label='Negative Prompt', lines=2, max_lines=10, value="Low quality, pixelated")
66
+ source_image = gr.Image(label='Source Image for Face Swap', type="pil")
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
+ initial_image_output = gr.Image(type="pil", label="Initial Generated Image")
73
+ final_image_output = gr.Image(type="pil", label="Final Image After Face Swap")
74
+
75
+ submit_btn.click(fn=generate_image, inputs = [prompt,neg_prompt,source_image], outputs=[initial_image_output,final_image_output])
76
+ clear_btn.click(fn=clear_fields,outputs=[prompt,neg_prompt,initial_image_output,final_image_output])
77
 
78
  demo.launch()
app_simple_img_generator.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import json
7
+ import os
8
+ import numpy
9
+ #from dotenv import load_dotenv
10
+
11
+ #dotenv_path = '/.env'
12
+ #load_dotenv(dotenv_path)
13
+
14
+ # Get the API key from the environment variable
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):
19
+ if not API_KEY:
20
+ return None, "Error: API key not found in environment variables. Please set BASETEN_API_KEY."
21
+
22
+ resp = requests.post(
23
+ API_url,
24
+ headers={"Authorization": f"Api-Key {API_KEY}"},
25
+ json={
26
+ 'workflow_values': {
27
+ 'negative_prompt': negative_prompt,
28
+ 'positive_prompt': positive_prompt,
29
+ 'seed': seed
30
+ }
31
+ }
32
+ )
33
+
34
+ result = resp.json()
35
+ base64_data = result['result'][0]['data']
36
+ image_data = base64.b64decode(base64_data)
37
+ image = Image.open(io.BytesIO(image_data))
38
+ return image, json.dumps(result, indent=2)
39
+
40
+ def generate_image(positive_prompt, negative_prompt):
41
+ seed = numpy.random.randint(0, 2**32 - 1)
42
+ try:
43
+ return call_api_and_generate_image(negative_prompt, positive_prompt,seed)
44
+ except Exception as e:
45
+ return None, f"Error: {str(e)}"
46
+
47
+ def clear_fields():
48
+ return "", "", "" # Clear prompt and output, but not the API key
49
+
50
+ with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
51
+ gr.Markdown("<center><h2>Arjun's Image Generator</h2></center>")
52
+ gr.Markdown("Hi there! I'm an AI assistant tasked with generating images.")
53
+ prompt = gr.Textbox(label='Positve Prompt', lines=2, max_lines=5, placeholder = 'Describe your image here.')
54
+ neg_prompt = gr.Textbox(label='Negative Prompt', lines=2, max_lines=10, value="Low quality, pixelated")
55
+
56
+ with gr.Group():
57
+ with gr.Row():
58
+ submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
59
+ clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
60
+ gr.Markdown("<center><h3>AI Response</h3></center>")
61
+ image_output = gr.Image(type="pil", label="Generated Image")
62
+ json_output = gr.Textbox(label="API Response JSON", lines=10)
63
+ submit_btn.click(fn=generate_image, inputs = [prompt,neg_prompt], outputs=[image_output,json_output])
64
+ clear_btn.click(fn=clear_fields,outputs=[prompt,image_output,json_output, neg_prompt])
65
+
66
+ demo.launch()