Update src/streamlit_app.py
Browse files41 unmodified lines424344454647484950131 unmodified lines18218318418518618740 unmodified lines22822923023123223323423524 unmodified lines26026126226326426526626718 unmodified lines28628728828929029129229357 unmodified lines351352353354355356357358359360286 unmodified lines6476486496506516526536542 unmodified lines65765865966066166241 unmodified lines""")# 创建图片目录image_dir = "extracted_images"if not os.path.exists(image_dir):os.makedirs(image_dir)# AI卖点解析功能async def fetch_selling_points(session, product_info, product_quantity):131 unmodified linesmedia_dir = os.path.join(temp_dir, 'xl', 'media')if not os.path.exists(media_dir):return {}# 加载Excel以获取ASIN列df = pd.read_excel(excel_file_path, engine='openpyxl')40 unmodified linesexcept OSError: # 文件可能被占用base, ext = os.path.splitext(target_path)target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}"shutil.copy2(img_path, target_path)asin_to_image[asin] = target_path# 如果前面的方法没有成功提取所有ASIN的图片if len(asin_to_image) != len(asins):24 unmodified linesexcept OSError: # 文件可能被占用base, ext = os.path.splitext(target_path)target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}"shutil.copy2(img_path, target_path)asin_to_image[asin] = target_pathexcept Exception as e:pass # 忽略错误,尝试下一种方法18 unmodified linesexcept OSError:base, ext = os.path.splitext(target_path)target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}"shutil.copy2(img_path, target_path)asin_to_image[asin] = target_pathreturn asin_to_image57 unmodified linesif final_selected_asins:# 提取Excel文件中的图片try:asin_to_image = extract_images_from_excel(temp_file)st.success(f"成功提取 {len(asin_to_image)} 个ASIN对应的图片")except Exception as e:st.warning(f"处理图片时出现问题: {str(e)}")asin_to_image = {}# 过滤选中的ASIN数据286 unmodified linespaste_y = (max_size[1] - pil_img.height) // 2background.paste(pil_img, (paste_x, paste_y))temp_img_id = str(uuid.uuid4())resized_img_path = f"temp_resized_{temp_img_id}.png"background.save(resized_img_path)resized_img_paths.append(resized_img_path)2 unmodified linesimg_xl.height = 100ws.add_image(img_xl, f"{col_letter_img}{image_row_idx_excel}")except Exception as e_img:pass # Ignore image adding errors# Image cleanup loop will be at the end41 unmodified lines424344454647484950131 unmodified lines18218318418518618718818919019140 unmodified lines23223323423523623723823924024124224324424524 unmodified lines27027127227327427527627727827928028128228318 unmodified lines30230330430530630730830931031131231331431557 unmodified lines373374375376377378379380381382383384385386387388389390286 unmodified lines6776786796806816826836846852 unmodified lines68868969069169269369441 unmodified lines""")# 创建图片目录image_dir = os.path.join(tempfile.gettempdir(), "extracted_images")if not os.path.exists(image_dir):os.makedirs(image_dir, exist_ok=True)# AI卖点解析功能async def fetch_selling_points(session, product_info, product_quantity):131 unmodified linesmedia_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 linesexcept OSError: # 文件可能被占用base, ext = os.path.splitext(target_path)target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}"# 使用PIL打开并保存图片,避免直接文件复制try:img = PILImage.open(img_path)img.save(target_path)asin_to_image[asin] = target_pathexcept Exception as e:st.warning(f"保存图片时出错: {str(e)}")continue# 如果前面的方法没有成功提取所有ASIN的图片if len(asin_to_image) != len(asins):24 unmodified linesexcept OSError: # 文件可能被占用base, ext = os.path.splitext(target_path)target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}"# 使用PIL打开并保存图片,避免直接文件复制try:img = PILImage.open(img_path)img.save(target_path)asin_to_image[asin] = target_pathexcept Exception as e:st.warning(f"保存图片时出错: {str(e)}")continueexcept Exception as e:pass # 忽略错误,尝试下一种方法18 unmodified linesexcept OSError:base, ext = os.path.splitext(target_path)target_path = f"{base}_{uuid.uuid4().hex[:6]}{ext}"# 使用PIL打开并保存图片,避免直接文件复制try:img = PILImage.open(img_path)img.save(target_path)asin_to_image[asin] = target_pathexcept Exception as e:st.warning(f"保存图片时出错: {str(e)}")continuereturn asin_to_image57 unmodified linesif 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 linespaste_y = (max_size[1] - pil_img.height) // 2background.paste(pil_img, (paste_x, paste_y))# 使用临时目录创建调整大小后的图片temp_img_id = str(uuid.uuid4())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 linesimg_xl.height = 100ws.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