Update src/streamlit_app.py
Browse files15 unmodified lines161718192021222324252627282930313233343519 unmodified lines5556574445464748585960119 unmodified lines180181182174183184185186187188179189190191182192193194195109 unmodified lines3053063072982993003013023083093103112 unmodified lines314315316311317318319320447 unmodified lines76876977076577177277377415 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_DIR
st.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() 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(temp_dir) zip_ref.extractall(extract_temp_dir) # 查找media文件夹中的图片 media_dir = os.path.join(temp_dir, 'xl', 'media') media_dir = os.path.join(extract_temp_dir, 'xl', 'media') if not os.path.exists(media_dir): return {} 109 unmodified lines return {} finally: # 清理临时文件 shutil.rmtree(temp_dir)
# 修改上传文件处理逻辑中的临时文件存储位置temp_dir = tempfile.gettempdir()os.environ["TMPDIR"] = temp_dir shutil.rmtree(extract_temp_dir)
uploaded_file = st.file_uploader("上传Excel文件(包含ASIN及产品信息的数据表)", type=["xlsx"], help="请上传包含ASIN列的Excel文件,文件应包含产品信息如品牌、价格、销量等数据。系统将自动提取嵌入的产品图片。")2 unmodified lines try: # 使用唯一文件名避免冲突 unique_id = str(uuid.uuid4()) temp_file = f"temp_upload_{unique_id}.xlsx" temp_file = os.path.join(TEMP_DIR, f"temp_upload_{unique_id}.xlsx") # 保存上传的文件到临时位置 with open(temp_file, "wb") as f:447 unmodified lines try: os.remove(temp_file) except: pass # 忽略最终的错误 st.warning("无法删除临时文件,但这不影响程序功能") except: pass # 忽略任何清理错误else:src/streamlit_app.py+21-15