sku_search.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import json
  3. import gradio as gr
  4. from utils.chat import process_sellpoint,process_title
  5. from pdf_parser import pdf2db
  6. def search_json_files(filename):
  7. directory = "./database/meta"
  8. for file in os.listdir(directory):
  9. if file.endswith('.json') and filename in file:
  10. with open(os.path.join(directory, file), 'r') as f:
  11. data = json.load(f)
  12. return data["商品图像"], data["价格"], data["色号"], data["成分"], data["关键词"], data["商品细节"]
  13. def build_ui():
  14. with gr.Blocks() as demo:
  15. with gr.Tab("商品信息搜索"):
  16. with gr.Column():
  17. with gr.Row():
  18. with gr.Column():
  19. search_id = gr.Textbox(label="商品ID", value="1CNC6N230")
  20. id_image = gr.Image(label="商品图")
  21. with gr.Column():
  22. id_price = gr.Textbox(label="价格", value="¥1999")
  23. id_color = gr.Textbox(label="颜色", value="深花灰")
  24. id_ingredient = gr.Textbox(label="成分", value="羊毛")
  25. id_selling_point = gr.Textbox(label="商品卖点", value="落肩直身宽松行,一手长经典款初胎羊毛")
  26. id_details = gr.Textbox(label="商品细节", value="全羊毛双面呢,采用初剪细支羊毛精制而成,呢面绒毛均匀顺直,光泽温和高贵,手感细腻,挺括而富有弹性")
  27. search_button = gr.Button("Search")
  28. search_button.click(fn=search_json_files, inputs=[search_id], outputs=[id_image,id_price, id_color, id_ingredient, id_selling_point, id_details])
  29. with gr.Tab("PDF文件解析"):
  30. with gr.Row():
  31. file_input = gr.File(label="选择PDF文件", file_types=[".pdf"], file_count="single")
  32. parse_btn = gr.Button("开始解析", variant="primary")
  33. text_output = gr.Textbox(label="解析进度", placeholder="解析进度", lines=20, max_lines=50)
  34. parse_btn.click(fn=pdf2db, inputs=file_input, outputs=text_output)
  35. with gr.Tab("商品卖点生成"):
  36. gr.Markdown("## 图库里若有商品图,可只填款号")
  37. with gr.Row():
  38. with gr.Column():
  39. id = gr.Textbox(label="商品ID")
  40. input_image = gr.Image(label="商品图(选填)", type="pil")
  41. input_info = gr.Textbox(label="输入的卖点(选填)")
  42. output_ch_info = gr.Textbox(label="输出的中文卖点")
  43. output_en_info = gr.Textbox(label="输出的英文卖点")
  44. output_ky_info = gr.Textbox(label="输出的三个关键卖点")
  45. with gr.Column():
  46. product_image = gr.Image(label="商品图")
  47. product_price = gr.Textbox(label="价格")
  48. product_color = gr.Textbox(label="颜色")
  49. product_ingredient = gr.Textbox(label="成分")
  50. product_selling_point = gr.Textbox(label="商品卖点")
  51. product_details = gr.Textbox(label="商品细节")
  52. sell_point_button = gr.Button("生成卖点")
  53. sell_point_button.click(fn=process_sellpoint,
  54. inputs=[id, input_image, input_info],
  55. outputs=[output_ch_info, output_en_info, output_ky_info,
  56. product_image, product_price, product_color,
  57. product_ingredient, product_selling_point, product_details])
  58. with gr.Tab("生成卖点标题"):
  59. gr.Markdown("## 图库里若有商品图,可只填款号")
  60. with gr.Row():
  61. with gr.Column():
  62. p_id = gr.Textbox(label="商品ID")
  63. input_info = gr.Textbox(label="输入商品描述(可选)")
  64. output_title_en = gr.Textbox(label="输出的英文标题")
  65. output_title_ch = gr.Textbox(label="输出的中文标题")
  66. sell_title_button = gr.Button("生成卖点")
  67. sell_title_button.click(fn=process_title,
  68. inputs=[p_id,input_info],
  69. outputs=[output_title_en,output_title_ch])
  70. return demo
  71. if __name__ == "__main__":
  72. demo=build_ui()
  73. demo.launch(server_name='0.0.0.0', server_port=3333)
  74. # from flask import Flask, render_template, request, redirect, url_for
  75. # import json
  76. # import os
  77. # app = Flask(__name__)
  78. # def add_product_to_json(product_data):
  79. # """将商品信息添加到JSON文件"""
  80. # json_file_path = './database/meta/1B1L9H030.json' # 请根据实际情况调整路径
  81. # with open(json_file_path, 'r+', encoding='utf-8') as file:
  82. # data = json.load(file)
  83. # data.append(product_data) # 假设数据是一个列表
  84. # file.seek(0)
  85. # json.dump(data, file, ensure_ascii=False, indent=4)
  86. # @app.route('/', methods=['GET', 'POST'])
  87. # def index():
  88. # if request.method == 'POST':
  89. # product_data = {
  90. # '商品名称': request.form['name'],
  91. # '商品价格': request.form['price'],
  92. # '商品描述': request.form['description'],
  93. # '商品图像': request.form['image_url']
  94. # }
  95. # add_product_to_json(product_data)
  96. # return redirect(url_for('index'))
  97. # return render_template('index.html')
  98. # if __name__ == "__main__":
  99. # app.run(debug=True)