Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- apis/ourdata.py +91 -0
- apis/userdata.py +77 -0
apis/ourdata.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import random
|
| 3 |
+
import asyncio
|
| 4 |
+
import concurrent.futures
|
| 5 |
+
|
| 6 |
+
from helpercodes.image_functions import generate_image, _encode_image_to_base64
|
| 7 |
+
from helpercodes.ourdata_advanced import get_links, generate_gpt_prompt_our_data, get_dataset_dict
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _process_single_style(style_key, keywords_list, data):
|
| 11 |
+
"""
|
| 12 |
+
Processes a single style synchronously:
|
| 13 |
+
1. Samples keywords.
|
| 14 |
+
2. Retrieves relevant images/links from DataFrame.
|
| 15 |
+
3. Generates GPT prompt and final image.
|
| 16 |
+
4. Builds the final response dictionary (title, body, des, images).
|
| 17 |
+
Returns:
|
| 18 |
+
A dictionary with { title, body, des, images } or None if there's an issue.
|
| 19 |
+
"""
|
| 20 |
+
logging.info(f"Working on style '{style_key}'...")
|
| 21 |
+
|
| 22 |
+
if not keywords_list:
|
| 23 |
+
# If style has no keywords, skip
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
sampled_keywords = random.sample(keywords_list, min(len(keywords_list), 5))
|
| 27 |
+
input_keywords = ", ".join(sampled_keywords)
|
| 28 |
+
|
| 29 |
+
# Filter DF to match this style
|
| 30 |
+
df_subset = data[data['styles'] == style_key].copy()
|
| 31 |
+
if df_subset.empty:
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
# Get top-2 relevant images from DF
|
| 35 |
+
results = get_links(df_subset, input_keywords)
|
| 36 |
+
if len(results) < 2:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
# Generate GPT prompt and final image
|
| 40 |
+
prompt = generate_gpt_prompt_our_data(results)
|
| 41 |
+
logging.info(f"Prompt: {prompt}")
|
| 42 |
+
image = generate_image(prompt, '1:1', "None")
|
| 43 |
+
|
| 44 |
+
return image
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
async def process_our_data():
|
| 48 |
+
"""
|
| 49 |
+
Randomly picks 5 keywords from each style (if available),
|
| 50 |
+
finds top-2 relevant images, calls GPT for a prompt, and
|
| 51 |
+
streams the generated images in a Gradio Gallery.
|
| 52 |
+
This version uses multi-threading to process each style concurrently.
|
| 53 |
+
"""
|
| 54 |
+
logging.info("Processing our data...")
|
| 55 |
+
response = []
|
| 56 |
+
|
| 57 |
+
# Get data, style dictionary
|
| 58 |
+
data, style_dict = get_dataset_dict()
|
| 59 |
+
|
| 60 |
+
# We'll gather coroutines so that each style is processed in a thread
|
| 61 |
+
loop = asyncio.get_running_loop()
|
| 62 |
+
|
| 63 |
+
# Collect async tasks here
|
| 64 |
+
tasks = []
|
| 65 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 66 |
+
for style_key, keywords_list in style_dict.items():
|
| 67 |
+
# Schedule the synchronous processing function in a separate thread
|
| 68 |
+
tasks.append(
|
| 69 |
+
loop.run_in_executor(
|
| 70 |
+
executor,
|
| 71 |
+
_process_single_style, # function to run
|
| 72 |
+
style_key, # arguments
|
| 73 |
+
keywords_list,
|
| 74 |
+
data
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
# Await all results
|
| 80 |
+
results = await asyncio.gather(*tasks)
|
| 81 |
+
|
| 82 |
+
# Filter out any None values (cases where we skip)
|
| 83 |
+
for r in results:
|
| 84 |
+
if r is not None:
|
| 85 |
+
response.append(r)
|
| 86 |
+
|
| 87 |
+
return response
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def process_our_data_sync():
|
| 91 |
+
return asyncio.run(process_our_data())
|
apis/userdata.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import asyncio
|
| 3 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 4 |
+
|
| 5 |
+
from helpercodes.image_functions import generate_image
|
| 6 |
+
from helpercodes.user_data import generate_gpt_prompt, _encode_image_to_base64
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def _generate_prompt_and_image(pos: str, neg: str, category: str, story_instruction: str,
|
| 10 |
+
sentiment, image_elements, is_text, text_elements, input_prompt, non_compliant, emotion,
|
| 11 |
+
aspect_ratio, design):
|
| 12 |
+
|
| 13 |
+
# Generate the GPT prompt
|
| 14 |
+
prompt = generate_gpt_prompt(
|
| 15 |
+
pos,
|
| 16 |
+
neg,
|
| 17 |
+
category,
|
| 18 |
+
story_instruction,
|
| 19 |
+
sentiment, image_elements, is_text, text_elements, input_prompt, non_compliant, emotion
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Generate the image from the prompt
|
| 23 |
+
image = generate_image(prompt, aspect_ratio, design)
|
| 24 |
+
|
| 25 |
+
return image
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
async def process_custom_data(pos, category, neg, sentiment, image_elements, is_text, text_elements, input_prompt,
|
| 29 |
+
non_compliant, emotion, aspect_ratio, design):
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
logging.info("Processing custom data...")
|
| 33 |
+
|
| 34 |
+
# The two different instructions
|
| 35 |
+
instruction_var = (
|
| 36 |
+
"which should have new offers, revised text and graphics but style should remain same"
|
| 37 |
+
)
|
| 38 |
+
instruction_same = (
|
| 39 |
+
"which should have new offers and revised text but graphics and style should remain same"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# We'll use asyncio's event loop to run blocking code in a ThreadPoolExecutor
|
| 43 |
+
loop = asyncio.get_running_loop()
|
| 44 |
+
|
| 45 |
+
# Create the thread pool
|
| 46 |
+
with ThreadPoolExecutor() as executor:
|
| 47 |
+
# Schedule both tasks in parallel
|
| 48 |
+
future_var = loop.run_in_executor(
|
| 49 |
+
executor,
|
| 50 |
+
_generate_prompt_and_image,
|
| 51 |
+
pos,
|
| 52 |
+
neg,
|
| 53 |
+
category,
|
| 54 |
+
instruction_var,
|
| 55 |
+
sentiment, image_elements, is_text, text_elements, input_prompt,
|
| 56 |
+
non_compliant, emotion, aspect_ratio, design
|
| 57 |
+
)
|
| 58 |
+
future_same = loop.run_in_executor(
|
| 59 |
+
executor,
|
| 60 |
+
_generate_prompt_and_image,
|
| 61 |
+
pos,
|
| 62 |
+
neg,
|
| 63 |
+
category,
|
| 64 |
+
instruction_same,
|
| 65 |
+
sentiment, image_elements, is_text, text_elements, input_prompt,
|
| 66 |
+
non_compliant, emotion, aspect_ratio, design
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Wait for both images to finish
|
| 70 |
+
image_var, image_same = await asyncio.gather(future_var, future_same)
|
| 71 |
+
|
| 72 |
+
return image_var, image_same
|
| 73 |
+
|
| 74 |
+
def process_user_data_sync(pos, category, neg, sentiment, image_elements, is_text, text_elements, input_prompt,
|
| 75 |
+
non_compliant, emotion, aspect_ratio, design):
|
| 76 |
+
return asyncio.run(process_custom_data(pos, category, neg, sentiment, image_elements, is_text, text_elements, input_prompt,
|
| 77 |
+
non_compliant, emotion, aspect_ratio, design))
|