Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import requests
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
from PIL import Image, ImageOps, ImageEnhance
|
| 7 |
+
|
| 8 |
+
DEEPSIGHT_API_URL = "https://api.deepseek.com/v2/chat/completions"
|
| 9 |
+
DEEPSIGHT_API_KEY = "YOUR_API_KEY"
|
| 10 |
+
|
| 11 |
+
SYSTEM_PROMPT = open("system_prompt.txt").read()
|
| 12 |
+
|
| 13 |
+
def call_deepsight(product_data):
|
| 14 |
+
"""Send product row to DeepSight v2 for structured generation."""
|
| 15 |
+
payload = {
|
| 16 |
+
"model": "deepseek-chat",
|
| 17 |
+
"messages": [
|
| 18 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 19 |
+
{"role": "user", "content": product_data}
|
| 20 |
+
],
|
| 21 |
+
"temperature": 0.2
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
headers = {
|
| 25 |
+
"Content-Type": "application/json",
|
| 26 |
+
"Authorization": f"Bearer {DEEPSIGHT_API_KEY}"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
response = requests.post(DEEPSIGHT_API_URL, json=payload, headers=headers)
|
| 30 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def download_image(url):
|
| 34 |
+
try:
|
| 35 |
+
img_bytes = requests.get(url, timeout=10).content
|
| 36 |
+
return Image.open(io.BytesIO(img_bytes)).convert("RGBA")
|
| 37 |
+
except:
|
| 38 |
+
return None
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def remove_bg(img):
|
| 42 |
+
"""Simple white background fallback if rembg is not installed."""
|
| 43 |
+
# Optional: integrate rembg here if available
|
| 44 |
+
bg = Image.new("RGBA", img.size, "WHITE")
|
| 45 |
+
bg.paste(img, mask=img.split()[3])
|
| 46 |
+
return bg
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def apply_watermark(img, watermark_path="watermark.png"):
|
| 50 |
+
try:
|
| 51 |
+
wm = Image.open(watermark_path).convert("RGBA")
|
| 52 |
+
wm = wm.resize((int(img.size[0] * 0.3), int(img.size[1] * 0.3)))
|
| 53 |
+
img.paste(wm, (img.size[0]-wm.size[0]-10, img.size[1]-wm.size[1]-10), wm)
|
| 54 |
+
except:
|
| 55 |
+
pass
|
| 56 |
+
return img
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def enhance_image(img):
|
| 60 |
+
img = ImageEnhance.Sharpness(img).enhance(1.4)
|
| 61 |
+
img = ImageEnhance.Brightness(img).enhance(1.05)
|
| 62 |
+
return img
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def process_csv(file):
|
| 66 |
+
df = pd.read_csv(file)
|
| 67 |
+
output_rows = []
|
| 68 |
+
output_images = []
|
| 69 |
+
|
| 70 |
+
for idx, row in df.iterrows():
|
| 71 |
+
title = str(row.get("post_title", "")).strip()
|
| 72 |
+
short = str(row.get("post_excerpt", "")).strip()
|
| 73 |
+
long = str(row.get("post_content", "")).strip()
|
| 74 |
+
image_link = row.get("image_link", "")
|
| 75 |
+
|
| 76 |
+
# Create a combined row prompt
|
| 77 |
+
row_prompt = f"""
|
| 78 |
+
Product Title: {title}
|
| 79 |
+
Short Description: {short}
|
| 80 |
+
Long Description: {long}
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
# Call DeepSight for content generation
|
| 84 |
+
ai_output = call_deepsight(row_prompt)
|
| 85 |
+
ai_dict = eval(ai_output) # Expecting strict JSON from DeepSight
|
| 86 |
+
|
| 87 |
+
# Process image
|
| 88 |
+
final_image_path = None
|
| 89 |
+
if isinstance(image_link, str) and image_link.startswith("http"):
|
| 90 |
+
img = download_image(image_link)
|
| 91 |
+
if img:
|
| 92 |
+
img = ImageOps.contain(img, (800, 800))
|
| 93 |
+
img = remove_bg(img)
|
| 94 |
+
img = enhance_image(img)
|
| 95 |
+
img = apply_watermark(img)
|
| 96 |
+
|
| 97 |
+
save_path = f"processed_{idx}.png"
|
| 98 |
+
img.save(save_path, "PNG")
|
| 99 |
+
final_image_path = save_path
|
| 100 |
+
|
| 101 |
+
output_rows.append({
|
| 102 |
+
"seo_title": ai_dict["seo_title"],
|
| 103 |
+
"short_description": ai_dict["short_description"],
|
| 104 |
+
"long_description": ai_dict["long_description"],
|
| 105 |
+
"processed_image": final_image_path
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
result_df = pd.DataFrame(output_rows)
|
| 109 |
+
result_path = "output.csv"
|
| 110 |
+
result_df.to_csv(result_path, index=False)
|
| 111 |
+
|
| 112 |
+
return result_path
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Gradio Interface
|
| 116 |
+
with gr.Blocks() as demo:
|
| 117 |
+
gr.Markdown("# 🚀 WooCommerce Product Optimizer (DeepSight v2)")
|
| 118 |
+
gr.Markdown("Upload your CSV and let DeepSight clean titles, descriptions, and images.")
|
| 119 |
+
|
| 120 |
+
csv_input = gr.File(label="Upload WooCommerce CSV")
|
| 121 |
+
output_csv = gr.File(label="Download Optimized CSV")
|
| 122 |
+
|
| 123 |
+
run_btn = gr.Button("Process CSV")
|
| 124 |
+
|
| 125 |
+
run_btn.click(process_csv, inputs=csv_input, outputs=output_csv)
|
| 126 |
+
|
| 127 |
+
demo.launch()
|