Ashoka74 commited on
Commit
0011649
ยท
verified ยท
1 Parent(s): 8cf8c28

Update app_merged.py

Browse files
Files changed (1) hide show
  1. app_merged.py +58 -1
app_merged.py CHANGED
@@ -22,6 +22,8 @@ import datetime
22
  from pathlib import Path
23
  from io import BytesIO
24
 
 
 
25
 
26
 
27
  from PIL import Image
@@ -716,8 +718,59 @@ def run_rmbg(image):
716
  mask = pred_pil.resize(image_size)
717
  image.putalpha(mask)
718
  return image
 
719
 
 
 
 
720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
721
 
722
  def preprocess_image(image: Image.Image, height=768, width=768):
723
  image = np.array(image)
@@ -1693,7 +1746,10 @@ with gr.Blocks() as app:
1693
 
1694
  with gr.Row():
1695
  with gr.Group():
1696
- prompt = gr.Textbox(label="Prompt")
 
 
 
1697
  bg_source = gr.Radio(choices=[e.value for e in list(BGSource)[2:]],
1698
  value=BGSource.LEFT.value,
1699
  label="Lighting Preference (Initial Latent)", type='value')
@@ -1746,6 +1802,7 @@ with gr.Blocks() as app:
1746
  # outputs=[result_gallery, output_bg],
1747
  # run_on_click=True, examples_per_page=1024
1748
  # )
 
1749
  ips = [extracted_fg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source]
1750
  relight_button.click(fn=process_relight, inputs=ips, outputs=[result_gallery])
1751
  example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)
 
22
  from pathlib import Path
23
  from io import BytesIO
24
 
25
+ import openai
26
+ from openai import OpenAI
27
 
28
 
29
  from PIL import Image
 
718
  mask = pred_pil.resize(image_size)
719
  image.putalpha(mask)
720
  return image
721
+
722
 
723
+ def generate_description(object_description,image, detail="high", max_tokens=250):
724
+ openai_api_key = os.getenv("OPENAI_API_KEY")
725
+ client = OpenAI(api_key=openai_api_key)
726
 
727
+ if image is not None:
728
+ try:
729
+ img = image # No need to open, directly use the PIL Image object
730
+
731
+ buffered = io.BytesIO()
732
+ img.save(buffered, format=IMAGE_FORMAT)
733
+ img_base64 = base64.b64encode(buffered.getvalue()).decode()
734
+
735
+ prompt = f"As if you were describing the interior design, make a detailed caption of this image in one large paragraph. Highlighting textures, furnitures, locations. This object should be included in the description :{object_description}"
736
+
737
+ payload = {
738
+ "model": "gpt-4o-mini",
739
+ "messages": [{
740
+ "role": "user",
741
+ "content": [
742
+ {"type": "text", "text": prompt},
743
+ {"type": "image_url",
744
+ "image_url": {"url": f"data:image/jpeg;base64,{img_base64}", "detail": detail}}
745
+ ]
746
+ }],
747
+ "max_tokens": max_tokens
748
+ }
749
+
750
+ response = client.chat.completions.create(**payload)
751
+ return response.choices[0].message.content
752
+ except Exception as e:
753
+ print(e)
754
+ else:
755
+ try:
756
+ prompt = f"Description: {object_description}. As if you were designing an interior, improve this sentence in one large paragraph. Highlighting textures, furnitures, locations, such that you create a coherent, visually pleasing setting."
757
+
758
+ payload = {
759
+ "model": "gpt-4o-mini",
760
+ "messages": [{
761
+ "role": "user",
762
+ "content": [
763
+ {"type": "text", "text": prompt},
764
+ ]
765
+ }],
766
+ "max_tokens": max_tokens
767
+ }
768
+
769
+ response = client.chat.completions.create(**payload)
770
+ return response.choices[0].message.content
771
+ except Exception as e:
772
+ print(e)
773
+
774
 
775
  def preprocess_image(image: Image.Image, height=768, width=768):
776
  image = np.array(image)
 
1746
 
1747
  with gr.Row():
1748
  with gr.Group():
1749
+ with gr.Column():
1750
+ prompt = gr.Textbox(label="Prompt")
1751
+ augment_prompt = gr.Button(value='Augment Prompt')
1752
+
1753
  bg_source = gr.Radio(choices=[e.value for e in list(BGSource)[2:]],
1754
  value=BGSource.LEFT.value,
1755
  label="Lighting Preference (Initial Latent)", type='value')
 
1802
  # outputs=[result_gallery, output_bg],
1803
  # run_on_click=True, examples_per_page=1024
1804
  # )
1805
+ augment_prompt.click(generate_description, inputs=[prompt, dummy_image_for_prompt_augmentation], outputs=[prompt])
1806
  ips = [extracted_fg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, lowres_denoise, bg_source]
1807
  relight_button.click(fn=process_relight, inputs=ips, outputs=[result_gallery])
1808
  example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)