Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from PIL import Image, ImageDraw | |
| from apis.advancedourdata import process_advanced_data_sync | |
| from apis.ourdata import process_our_data_sync | |
| import os | |
| from apis.userdata import process_user_data_sync | |
| import logging | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s - %(levelname)s - %(message)s" | |
| ) | |
| os.environ["TOKENIZERS_PARALLELISM"] = "false" | |
| def advanced_function(style, sentiment, image_elements, text_elements, design_style, aspect_ratio, | |
| input_image, is_text, non_compliant, emotion): | |
| try: | |
| if is_text == "Yes": | |
| is_text = True | |
| else: | |
| is_text = False | |
| logging.info("Calling process_advanced_data_sync with parameters: style=%s, sentiment=%s, image_elements=%s, text_elements=%s, " | |
| "design_style=%s, aspect_ratio=%s, is_text=%s, non_compliant=%s, emotion=%s", | |
| style, sentiment, image_elements, text_elements, design_style, aspect_ratio, is_text, non_compliant, emotion) | |
| results = process_advanced_data_sync(style, sentiment, image_elements, text_elements, design_style, aspect_ratio, | |
| input_image, is_text, non_compliant, emotion) | |
| img1 = results[0] | |
| img2 = results[1] | |
| logging.info("Advanced function executed successfully.") | |
| return img1, img2 | |
| except Exception as e: | |
| logging.error("Error in advanced_function: %s", str(e), exc_info=True) | |
| return None, None | |
| def our_data_function(): | |
| try: | |
| logging.info("Calling process_our_data_sync") | |
| images = process_our_data_sync() | |
| formatted_links = "\n".join([f"{i+1}. {link}" for i, link in enumerate(images[:10])]) # Format as a numbered list | |
| logging.info("our_data_function executed successfully.") | |
| return formatted_links | |
| except Exception as e: | |
| logging.error("Error in our_data_function: %s", str(e), exc_info=True) | |
| return "" | |
| def your_data_function(pos, category, neg, sentiment, image_elements, is_text, text_elements, | |
| non_compliant, emotion, aspect_ratio, design): | |
| try: | |
| if is_text == "Yes": | |
| is_text = True | |
| else: | |
| is_text = False | |
| logging.info("Calling process_user_data_sync with parameters: pos=%s, category=%s, neg=%s, sentiment=%s, " | |
| "image_elements=%s, is_text=%s, text_elements=%s, non_compliant=%s, emotion=%s, aspect_ratio=%s, design=%s", | |
| pos, category, neg, sentiment, image_elements, is_text, text_elements, non_compliant, emotion, aspect_ratio, design) | |
| img1, img2 = process_user_data_sync(pos, category, neg, sentiment, image_elements, is_text, text_elements, | |
| non_compliant, emotion, aspect_ratio, design) | |
| logging.info("your_data_function executed successfully.") | |
| return img1, img2 | |
| except Exception as e: | |
| logging.error("Error in your_data_function: %s", str(e), exc_info=True) | |
| return None, None | |
| def toggle_text_element(required): | |
| if required == "Yes": | |
| return gr.update(visible=True) | |
| else: | |
| return gr.update(visible=False) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# AdGenesis") | |
| with gr.Tabs(): | |
| # ----- Advanced Tab ----- | |
| with gr.Tab("Advanced"): | |
| with gr.Row(): | |
| style = gr.Dropdown( | |
| choices=[ | |
| "breaking_news", "dealership", "money_cheque", "govt_authority", | |
| "senior_veteran", "license", "holding_boards", "textual", | |
| "single_story", "email_message" | |
| ], | |
| label="Image Layout *", | |
| value="breaking_news" | |
| ) | |
| sentiment = gr.Dropdown( | |
| choices=["Positive", "Negative", "Neutral"], | |
| label="Sentiment *", | |
| value="Positive" | |
| ) | |
| with gr.Row(): | |
| design_style = gr.Dropdown( | |
| choices=["None", "Auto", "General", "Realistic", "Design", "Anime"], | |
| label="Image Design Style", | |
| value="None" | |
| ) | |
| aspect_ratio = gr.Dropdown( | |
| choices=["1:1", "16:9", "4:3"], | |
| label="Aspect Ratio", | |
| value="1:1" | |
| ) | |
| input_image = gr.Image(label="Image", type="pil") | |
| image_elements = gr.Textbox(label="Image Element") | |
| is_text = gr.Radio( | |
| choices=["Yes", "No"], | |
| label="Required Text", | |
| value="Yes" | |
| ) | |
| text_elements = gr.Textbox(label="Text Element") | |
| non_compliant = gr.Textbox(label="Non Compliant") | |
| emotion = gr.Textbox(label="Emotion") | |
| # Toggle visibility of "Text Element" based on "Required Text" selection. | |
| is_text.change(fn=toggle_text_element, inputs=is_text, outputs=text_elements) | |
| advanced_button = gr.Button("Generate Advanced Ad Images") | |
| advanced_output_img1 = gr.Textbox(label="Ad Image 1") | |
| advanced_output_img2 = gr.Textbox(label="Ad Image 2") | |
| advanced_button.click( | |
| fn=advanced_function, | |
| inputs=[style, sentiment, image_elements, text_elements, design_style, aspect_ratio, | |
| input_image, is_text, non_compliant, emotion], | |
| outputs=[advanced_output_img1, advanced_output_img2] | |
| ) | |
| # ----- Win with our data Tab ----- | |
| with gr.Tab("Win with our data"): | |
| gr.Markdown("Click the button below to generate 10 ad images using our data.") | |
| our_data_button = gr.Button("Generate 10 Ad Images") | |
| our_data_gallery = gr.Textbox(label="Generated Ad Images") | |
| # Disable queueing for this click to avoid async issues with Gallery. | |
| our_data_button.click( | |
| fn=our_data_function, | |
| inputs=[], | |
| outputs=our_data_gallery, | |
| queue=False | |
| ) | |
| # ----- Win with your own data Tab ----- | |
| with gr.Tab("Win with your own data"): | |
| gr.Markdown( | |
| "Fill in the fields below (similar to Advanced except for Image Layout) and upload your images.") | |
| with gr.Row(): | |
| positive_image = gr.Image(label="Positive Image *", type="pil") | |
| negative_image = gr.Image(label="Negative Image", type="pil") | |
| category =gr.Textbox(label="Category") | |
| with gr.Row(): | |
| sentiment_your = gr.Dropdown( | |
| choices=["Positive", "Negative", "Neutral"], | |
| label="Sentiment *", | |
| value="Positive" | |
| ) | |
| design_style_your = gr.Dropdown( | |
| choices=["None", "Auto", "General", "Realistic", "Design", "Anime"], | |
| label="Image Design Style", | |
| value="None" | |
| ) | |
| aspect_ratio_your = gr.Dropdown( | |
| choices=["1:1", "16:9", "4:3", "9:16"], | |
| label="Aspect Ratio", | |
| value="1:1" | |
| ) | |
| image_element_your = gr.Textbox(label="Image Element") | |
| is_text = gr.Radio( | |
| choices=["Yes", "No"], | |
| label="Required Text", | |
| value="Yes" | |
| ) | |
| text_element_your = gr.Textbox(label="Text Element") | |
| non_compliant_your = gr.Textbox(label="Non Compliant") | |
| emotion_your = gr.Textbox(label="Emotion") | |
| is_text.change(fn=toggle_text_element, inputs=is_text, outputs=text_element_your) | |
| your_data_button = gr.Button("Generate Your Data Ad Images") | |
| your_data_output_img1 = gr.Textbox(label="Generated Image 1") | |
| your_data_output_img2 = gr.Textbox(label="Generated Image 2") | |
| your_data_button.click( | |
| fn=your_data_function, | |
| inputs=[positive_image, category, negative_image, sentiment_your, image_element_your, is_text, text_element_your, | |
| non_compliant_your, emotion_your, aspect_ratio_your, design_style_your], | |
| outputs=[your_data_output_img1, your_data_output_img2] | |
| ) | |
| demo.launch(share=True) | |