service.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from agent.agent import Agent
  2. from db import MongoDao
  3. from data import BrandInfo
  4. import gradio as gr
  5. import json
  6. import pandas as pd
  7. from utils import load_image_from_url, load_image_from_cos
  8. product_dao = MongoDao("vbrand-ec")
  9. license_dao = MongoDao("ProductStandard")
  10. class Service:
  11. product_map = {}
  12. agent = Agent()
  13. @staticmethod
  14. def get_brand_list():
  15. """获取数据库中存储的品牌列表"""
  16. brand_list = product_dao.get_one_field_data("brandName")
  17. brand_list = list(dict.fromkeys(brand_list))
  18. return brand_list
  19. @staticmethod
  20. def get_init_state_product_list(brand_name):
  21. """获取初始状态下的产品列表,并返回列表中第一个产品的信息"""
  22. brand_info = BrandInfo(brand_name)
  23. prodct_titles = [p.title for p in brand_info.product_list]
  24. Service.product_map[brand_name] = brand_info.product_list
  25. # 初始商品列表的第一个商品
  26. init_product = brand_info.product_list[0]
  27. # init_product.images = [load_image_from_url(image_url) for image_url in init_product.images]
  28. return prodct_titles, init_product
  29. @staticmethod
  30. def on_brand_change(brand_name):
  31. """加载选定品牌的所有商品"""
  32. brand_info = BrandInfo(brand_name)
  33. produt_titles = [p.title for p in brand_info.product_list]
  34. Service.product_map[brand_name] = brand_info.product_list
  35. return gr.update(choices=produt_titles, value=produt_titles[0])
  36. @staticmethod
  37. def on_product_change(title, brand_name):
  38. product_list = Service.product_map.get(brand_name, [])
  39. product = next((p for p in product_list if p.title == title), None)
  40. if not product:
  41. return [], [["商品未找到", ""]]
  42. # 加载图片
  43. images = [load_image_from_cos(image_url) for image_url in product.images]
  44. # 产品信息
  45. info = [
  46. ["商品链接", product.url],
  47. ["平台", product.platFormName],
  48. ["商户名称", product.shopTitle],
  49. ["颜色", product.colors],
  50. ["尺寸", product.sizes],
  51. ["价格", product.price],
  52. ]
  53. product_df = pd.DataFrame(info, columns=["", ""])
  54. return images, product_df, product
  55. @staticmethod
  56. def get_license_list(brand_name):
  57. """获取品牌方授权生成的商品列表"""
  58. license_list = []
  59. records = license_dao.get_records_by_query({"BrandName":brand_name})
  60. for record in records:
  61. if "ProductSeries" not in record.keys():
  62. record["ProductSeries"] = ""
  63. product = f"{record['ProductSeries']} {record['Category']}"
  64. if product not in license_list:
  65. license_list.append(product)
  66. return license_list
  67. @staticmethod
  68. def infringement_judgement(brand_name, product):
  69. """侵权判定逻辑"""
  70. # 关键词引流判定
  71. # if brand_name not in product.title:
  72. # # 如果标题中不包含品牌名称,拿到该商品的品牌名称
  73. key_word_judgement = json.loads(Service.agent.brand_key_word_judgement(brand_name, product.title))
  74. # 未授权商品判定
  75. license_list = Service.get_license_list("李宁")
  76. license_judgement = json.loads(Service.agent.license_product_judgement(product.title, license_list))
  77. # 商标侵权判定
  78. logo_judgement = json.loads(Service.agent.image_logo_judgement("./logo/lining.jpg", product.images[0]))
  79. key_word_falg = key_word_judgement["key_word_flag"]
  80. license_judgement_flag = license_judgement["in_list"]
  81. result = f"""
  82. 关键词引流: {key_word_falg}
  83. 是否为授权生产产品: {license_judgement_flag}
  84. 产品LOGO图像判定:
  85. 图像中是否包含logo: {logo_judgement["is_contain_logo"]}
  86. """
  87. if logo_judgement["is_contain_logo"]:
  88. result += f"""是否是指定品牌LOGO: {logo_judgement["is_jugement_logo"]}
  89. LOGO名称: {logo_judgement["brand_name"]}
  90. """
  91. return result
  92. if __name__ == "__main__":
  93. brand_list = Service.get_brand_list()
  94. print(brand_list)