import gradio as gr import pandas as pd import requests import io import base64 from PIL import Image, ImageOps, ImageEnhance DEEPSIGHT_API_URL = "https://api.deepseek.com/v2/chat/completions" DEEPSIGHT_API_KEY = "YOUR_API_KEY" SYSTEM_PROMPT = open("system_prompt.txt").read() def call_deepsight(product_data): """Send product row to DeepSight v2 for structured generation.""" payload = { "model": "deepseek-chat", "messages": [ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": product_data} ], "temperature": 0.2 } headers = { "Content-Type": "application/json", "Authorization": f"Bearer {DEEPSIGHT_API_KEY}" } response = requests.post(DEEPSIGHT_API_URL, json=payload, headers=headers) return response.json()["choices"][0]["message"]["content"] def download_image(url): try: img_bytes = requests.get(url, timeout=10).content return Image.open(io.BytesIO(img_bytes)).convert("RGBA") except: return None def remove_bg(img): """Simple white background fallback if rembg is not installed.""" # Optional: integrate rembg here if available bg = Image.new("RGBA", img.size, "WHITE") bg.paste(img, mask=img.split()[3]) return bg def apply_watermark(img, watermark_path="watermark.png"): try: wm = Image.open(watermark_path).convert("RGBA") wm = wm.resize((int(img.size[0] * 0.3), int(img.size[1] * 0.3))) img.paste(wm, (img.size[0]-wm.size[0]-10, img.size[1]-wm.size[1]-10), wm) except: pass return img def enhance_image(img): img = ImageEnhance.Sharpness(img).enhance(1.4) img = ImageEnhance.Brightness(img).enhance(1.05) return img def process_csv(file): df = pd.read_csv(file) output_rows = [] output_images = [] for idx, row in df.iterrows(): title = str(row.get("post_title", "")).strip() short = str(row.get("post_excerpt", "")).strip() long = str(row.get("post_content", "")).strip() image_link = row.get("image_link", "") # Create a combined row prompt row_prompt = f""" Product Title: {title} Short Description: {short} Long Description: {long} """ # Call DeepSight for content generation ai_output = call_deepsight(row_prompt) ai_dict = eval(ai_output) # Expecting strict JSON from DeepSight # Process image final_image_path = None if isinstance(image_link, str) and image_link.startswith("http"): img = download_image(image_link) if img: img = ImageOps.contain(img, (800, 800)) img = remove_bg(img) img = enhance_image(img) img = apply_watermark(img) save_path = f"processed_{idx}.png" img.save(save_path, "PNG") final_image_path = save_path output_rows.append({ "seo_title": ai_dict["seo_title"], "short_description": ai_dict["short_description"], "long_description": ai_dict["long_description"], "processed_image": final_image_path }) result_df = pd.DataFrame(output_rows) result_path = "output.csv" result_df.to_csv(result_path, index=False) return result_path # Gradio Interface with gr.Blocks() as demo: gr.Markdown("# 🚀 WooCommerce Product Optimizer (DeepSight v2)") gr.Markdown("Upload your CSV and let DeepSight clean titles, descriptions, and images.") csv_input = gr.File(label="Upload WooCommerce CSV") output_csv = gr.File(label="Download Optimized CSV") run_btn = gr.Button("Process CSV") run_btn.click(process_csv, inputs=csv_input, outputs=output_csv) demo.launch()