import gradio as gr from openai import OpenAI import os import requests import time from utils import uploadImages import json os.environ["OPENAI_API_KEY"] = "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" placidAPIKey = "placid-vwkiebbjlxl9dnhl-t6c89f8b5c0q8b25" client = OpenAI() def createCampaign(template_uuid, title, details, file_url): try: response = requests.post( url="https://api.placid.app/api/rest/images", headers={ "Authorization": f"Bearer {placidAPIKey}", "Content-Type": "application/json", }, json = { "template_uuid": template_uuid, "layers": { "title": { "text": title }, "description": { "text": details }, "screenshot": { "image": file_url } } } ) response_data = response.json() print(response_data) polling_url = response_data['polling_url'] while True: # Make a GET request to the polling URL headers={ "Authorization": f"Bearer {placidAPIKey}", "Content-Type": "application/json", } response = requests.get(polling_url, headers=headers) # Check if the request was successful if response.status_code == 200: data = response.json() # Check the status of the image processing if data['status'] == 'finished': print("Processing finished!") return data["image_url"] elif data['status'] == 'failed': print("Processing failed.") # Handle failure (you may also check data['errors'] for more details) break else: print("Still processing... Status:", data['status']) time.sleep(2) # Wait for 5 seconds before polling again else: print("Failed to poll status. HTTP status code:", response.status_code) break except requests.exceptions.RequestException: print('HTTP Request failed') def predict(productInfo, screenshots): # Step 1: Get campaign copy from openAI system_ins = "You are a professional marketer, \ if user enter a link then extract the information and use it as production context, \ then create a campaign with 6 campaign items based on user input, each with a title and description.\ Output the campaigns in json, it should only contain an array of the campaign items, no root key needed.\ Don't add other info" response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": system_ins}, {"role": "user", "content": productInfo} ] ) campaignCopy = response.choices[0].message.content print(campaignCopy) campaign_json = json.loads(campaignCopy) print(campaign_json) print(type(campaign_json)) campaignItems = campaign_json print("Step 1: Complete") # Step 2: Upload all the images file_urls = uploadImages(screenshots) print("Step 2: Complete") print(file_urls) # Step 3: Create the campain results = [] templates = ["zzpwonwvxudb9", "qpoihnji5reqn", "jasfxwdgopiz5", "tsvhpyqygoq0b", "jinkqm8vixdpi"] for index, file_url in enumerate(file_urls): copy_title = campaignItems[index]['title'] copy_des = campaignItems[index]['description'] url = createCampaign(template_uuid=templates[index], title=copy_title, details=copy_des, file_url=file_url) results.append(url) print("Loading image from") print(results) return results with gr.Blocks() as demo: with gr.Row(): textBox = gr.Text(label="Enter your product info") imageBox = gr.Gallery(label="Upload Screenshots") with gr.Row(): outputGallery = gr.Gallery(label="AppStore Screenshots") # Assuming a button is used to trigger the processing btn_process = gr.Button("Submit") # Function binding btn_process.click(predict, inputs=[textBox, imageBox], outputs=[outputGallery]) demo.launch()