|
|
import openai |
|
|
import io |
|
|
import os |
|
|
import matplotlib.pyplot as plt |
|
|
from PIL import Image |
|
|
import requests |
|
|
from io import BytesIO |
|
|
import base64 |
|
|
from IPython.display import display, clear_output, Image as IPyImage |
|
|
import json |
|
|
import gradio as gr |
|
|
import PIL.Image |
|
|
import io |
|
|
import numpy as np |
|
|
|
|
|
from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate |
|
|
from dotenv import load_dotenv |
|
|
import sys |
|
|
import os |
|
|
from io import StringIO |
|
|
import re |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
template = """Assistant is a large language model trained by OpenAI. |
|
|
|
|
|
Assistant is designed to generate catchy texts for Facebook Ads post. Assistant will receive some details about a product such as type, color, price, and potential customers. |
|
|
|
|
|
Based on these parameters it will generate the following 4 fields - |
|
|
|
|
|
primary: max 125 characters |
|
|
|
|
|
headline: max 27 characters |
|
|
|
|
|
description: max 27 characters |
|
|
|
|
|
Each field should be totally different with minimal repetition. Do not repeatedly use phrases that sound to sales like. Output your responses as a string with the keys being 'primary', 'headline', and 'description' and getting a new Line after each key. Be strict about this format. Just directly output responses nothing else, no leading words, etc. |
|
|
|
|
|
Consider this product : {human_input} |
|
|
""" |
|
|
|
|
|
prompt = PromptTemplate( |
|
|
input_variables=["human_input"], |
|
|
template=template |
|
|
) |
|
|
|
|
|
chatgpt_chain = LLMChain( |
|
|
llm=OpenAI(temperature=1), |
|
|
prompt=prompt, |
|
|
verbose=True, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload_and_process(image, promptText, promptImg, lens_option, n_images, size): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
responses = chatgpt_chain.predict(human_input=promptText) |
|
|
print(responses) |
|
|
index = responses.find("Output:") |
|
|
|
|
|
|
|
|
if index != -1: |
|
|
responses = responses[:index] + responses[index + len("Output:"):] |
|
|
split_pairs = responses.split(" ") |
|
|
pk ="" |
|
|
hk ="" |
|
|
dk = "" |
|
|
primary = "" |
|
|
headline = "" |
|
|
description = "" |
|
|
|
|
|
|
|
|
key_value_dict = {} |
|
|
key = None |
|
|
value = "" |
|
|
for pair in split_pairs: |
|
|
if ":" in pair: |
|
|
if key is not None: |
|
|
key_value_dict[key.strip()] = value.strip() |
|
|
key, value = pair.split(":", 1) |
|
|
else: |
|
|
value += " " + pair.strip() |
|
|
|
|
|
|
|
|
if key is not None: |
|
|
key_value_dict[key.strip()] = value.strip() |
|
|
|
|
|
|
|
|
for key, value in key_value_dict.items(): |
|
|
print(key + ": " + value) |
|
|
items = list(key_value_dict.items()) |
|
|
if items[0][0] in'primary': |
|
|
primary=items[0][1] |
|
|
elif items[0][0] in 'headline': |
|
|
headline=items[0][1] |
|
|
elif items[0][0] in 'description': |
|
|
description=items[0][1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
text_output = f"<h2>{headline}</h2><h3>{primary}</h3><p>{description}</p>" |
|
|
|
|
|
return text_output |
|
|
|
|
|
lens_options = [ |
|
|
"Sigma 85 mm f/1.4 (good for portrait)", |
|
|
"Sigma 85 mm f/8(sharper background)", |
|
|
"Sigma 24 mm f/8 (wider angle)", |
|
|
"Nikon D810 | ISO 64 | focal length 20 mm (Voigtländer 20 mm f3.5) | aperture f/9 | exposure time 1/40 Sec (DRI)", |
|
|
"Canon EOS 1000D, ƒ/3.5, focal length: 18.0 mm, exposure time: 1/5, ISO 400" |
|
|
] |
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=upload_and_process, |
|
|
inputs=[ |
|
|
gr.inputs.Image(type="pil", label="Upload Image", image_mode='RGB', tool="editor", source="upload"), |
|
|
gr.inputs.Textbox(default='Enter product name, price, target customer, etc.', label="Text Prompt"), |
|
|
gr.inputs.Textbox(default='Enter the desired image to be generated', label="Image Prompt"), |
|
|
gr.inputs.Dropdown(choices=lens_options, label="Lens Option"), |
|
|
gr.inputs.Slider(minimum=1, maximum=10, default=4, step=1, label="No. of Images"), |
|
|
gr.inputs.Textbox(default='512x512', label="Size"), |
|
|
], |
|
|
outputs=[ |
|
|
gr.outputs.HTML(label="Generated Text"), |
|
|
|
|
|
], |
|
|
title="Facebook Ad Creation", |
|
|
) |
|
|
|
|
|
iface.launch() |