Update src/streamlit_app.py
Browse files41 unmodified lines4243444545464747484950131 unmodified lines18218318418518618718818919019140 unmodified lines23223323423523623723123223823924024124224324424524 unmodified lines27027127227327427526326427627727827928028128228318 unmodified lines30230330430530630728929030830931031131231331431557 unmodified lines373374375376377378379380381382383384385386387388389390286 unmodified lines6776786796806816516826836846852 unmodified lines68868969069169269369441 unmodified lines""")
# 创建图片目录image_dir = "extracted_images"image_dir = os.path.join(tempfile.gettempdir(), "extracted_images")if not os.path.exists(image_dir): os.makedirs(image_dir) os.makedirs(image_dir, exist_ok=True)
# AI卖点解析功能async def fetch_selling_points(session, product_info, product_quantity):131 unmodified lines media_dir = os.path.join(temp_dir, 'xl', 'media') if not os.path.exists(media_dir): return {} # 确保image_dir存在 if not os.path.exists(image_dir): os.makedirs(image_dir, exist_ok=True) # 加载Excel以获取ASIN列 df = pd.read_excel(excel_file_path, engine='openpyxl')40 unmodified lines except OSError: # 文件可能被占用 base, ext = os.path.splitext(target_path) target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}" # 使用PIL打开并保存图片,避免直接文件复制 try: img = PILImage.open(img_path) shutil.copy2(img_path, target_path) asin_to_image[asin] = target_path img.save(target_path) asin_to_image[asin] = target_path except Exception as e: st.warning(f"保存图片时出错: {str(e)}") continue # 如果前面的方法没有成功提取所有ASIN的图片 if len(asin_to_image) != len(asins):24 unmodified lines except OSError: # 文件可能被占用 base, ext = os.path.splitext(target_path) target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}" # 使用PIL打开并保存图片,避免直接文件复制 try: img = PILImage.open(img_path) shutil.copy2(img_path, target_path) asin_to_image[asin] = target_path img.save(target_path) asin_to_image[asin] = target_path except Exception as e: st.warning(f"保存图片时出错: {str(e)}") continue except Exception as e: pass # 忽略错误,尝试下一种方法 18 unmodified lines except OSError: base, ext = os.path.splitext(target_path) target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}" # 使用PIL打开并保存图片,避免直接文件复制 try: img = PILImage.open(img_path) shutil.copy2(img_path, target_path) asin_to_image[asin] = target_path img.save(target_path) asin_to_image[asin] = target_path except Exception as e: st.warning(f"保存图片时出错: {str(e)}") continue return asin_to_image 57 unmodified lines if final_selected_asins: # 提取Excel文件中的图片 try: st.info(f"开始提取图片,临时目录: {tempfile.gettempdir()}") asin_to_image = extract_images_from_excel(temp_file) st.success(f"成功提取 {len(asin_to_image)} 个ASIN对应的图片") # 显示提取的图片路径 if asin_to_image: with st.expander("查看图片路径信息"): for asin, path in list(asin_to_image.items())[:5]: # 仅显示前5个 st.text(f"ASIN: {asin}, 图片路径: {path}, 文件存在: {os.path.exists(path)}") except Exception as e: st.warning(f"处理图片时出现问题: {str(e)}") st.warning(f"错误类型: {type(e).__name__}") asin_to_image = {} # 过滤选中的ASIN数据286 unmodified lines paste_y = (max_size[1] - pil_img.height) // 2 background.paste(pil_img, (paste_x, paste_y)) # 使用临时目录创建调整大小后的图片 temp_img_id = str(uuid.uuid4()) resized_img_path = f"temp_resized_{temp_img_id}.png" resized_img_path = os.path.join(tempfile.gettempdir(), f"temp_resized_{temp_img_id}.png") background.save(resized_img_path) resized_img_paths.append(resized_img_path) 2 unmodified lines img_xl.height = 100 ws.add_image(img_xl, f"{col_letter_img}{image_row_idx_excel}") except Exception as e_img: st.error(f"处理图片时出错: {str(e_img)}") pass # Ignore image adding errors # Image cleanup loop will be at the end
src/streamlit_app.py+41-9