Update src/streamlit_app.py
Browse files15 unmodified lines16171819202119 unmodified lines4142434445464748495051119 unmodified lines171172173174175176177178179180181182183184185109 unmodified lines2952962972982993003013023033043052 unmodified lines308309310311312313314447 unmodified lines76276376476576676776815 unmodified linesimport aiohttpfrom openpyxl.utils import get_column_letterst.set_page_config(page_title="竞品对比", layout="wide")st.title("竞品对比")19 unmodified lines品牌、品牌链接、卖家所属地、ASIN、大类排名、小类排名、月销量、月销售额、价格、变体数、评分数、评分、上架时间、重量、尺寸等""")# 创建图片目录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):"""调用Dify API获取产品卖点对比"""119 unmodified linesdef extract_images_from_excel(excel_file_path):"""从Excel文件中提取嵌入的图片"""# 创建临时目录temp_dir = tempfile.mkdtemp()try:# 将xlsx文件解压到临时目录with zipfile.ZipFile(excel_file_path, 'r') as zip_ref:zip_ref.extractall(temp_dir)# 查找media文件夹中的图片media_dir = os.path.join(temp_dir, 'xl', 'media')if not os.path.exists(media_dir):return {}109 unmodified linesreturn {}finally:# 清理临时文件shutil.rmtree(temp_dir)# 修改上传文件处理逻辑中的临时文件存储位置temp_dir = tempfile.gettempdir()os.environ["TMPDIR"] = temp_diruploaded_file = st.file_uploader("上传Excel文件(包含ASIN及产品信息的数据表)", type=["xlsx"],help="请上传包含ASIN列的Excel文件,文件应包含产品信息如品牌、价格、销量等数据。系统将自动提取嵌入的产品图片。")2 unmodified linestry:# 使用唯一文件名避免冲突unique_id = str(uuid.uuid4())temp_file = f"temp_upload_{unique_id}.xlsx"# 保存上传的文件到临时位置with open(temp_file, "wb") as f:447 unmodified linestry:os.remove(temp_file)except:pass # 忽略最终的错误except:pass # 忽略任何清理错误else:15 unmodified lines161718192021222324252627282930313233343519 unmodified lines555657585960119 unmodified lines180181182183184185186187188189190191192193194195109 unmodified lines3053063073083093103112 unmodified lines314315316317318319320447 unmodified lines76876977077177277377415 unmodified linesimport aiohttpfrom openpyxl.utils import get_column_letter# 定义适用于Hugging Face Spaces的安全路径# 在HF Spaces中,/tmp目录是可写的TEMP_DIR = "/tmp"if not os.path.exists(TEMP_DIR):os.makedirs(TEMP_DIR, exist_ok=True)# 创建图片目录在/tmp下image_dir = os.path.join(TEMP_DIR, "extracted_images")if not os.path.exists(image_dir):os.makedirs(image_dir, exist_ok=True)# 设置环境变量os.environ["TMPDIR"] = TEMP_DIRst.set_page_config(page_title="竞品对比", layout="wide")st.title("竞品对比")19 unmodified lines品牌、品牌链接、卖家所属地、ASIN、大类排名、小类排名、月销量、月销售额、价格、变体数、评分数、评分、上架时间、重量、尺寸等""")# AI卖点解析功能async def fetch_selling_points(session, product_info, product_quantity):"""调用Dify API获取产品卖点对比"""119 unmodified linesdef extract_images_from_excel(excel_file_path):"""从Excel文件中提取嵌入的图片"""# 创建临时目录extract_temp_dir = os.path.join(TEMP_DIR, f"excel_extract_{uuid.uuid4().hex}")os.makedirs(extract_temp_dir, exist_ok=True)try:# 将xlsx文件解压到临时目录with zipfile.ZipFile(excel_file_path, 'r') as zip_ref:zip_ref.extractall(extract_temp_dir)# 查找media文件夹中的图片media_dir = os.path.join(extract_temp_dir, 'xl', 'media')if not os.path.exists(media_dir):return {}109 unmodified linesreturn {}finally:# 清理临时文件shutil.rmtree(extract_temp_dir)uploaded_file = st.file_uploader("上传Excel文件(包含ASIN及产品信息的数据表)", type=["xlsx"],help="请上传包含ASIN列的Excel文件,文件应包含产品信息如品牌、价格、销量等数据。系统将自动提取嵌入的产品图片。")2 unmodified linestry:# 使用唯一文件名避免冲突unique_id = str(uuid.uuid4())temp_file = os.path.join(TEMP_DIR, f"temp_upload_{unique_id}.xlsx")# 保存上传的文件到临时位置with open(temp_file, "wb") as f:447 unmodified linestry:os.remove(temp_file)except:st.warning("无法删除临时文件,但这不影响程序功能")except:pass # 忽略任何清理错误else:
src/streamlit_app.py+21-15