import os import json import gradio as gr from utils.chat import process_sellpoint,process_title from pdf_parser import pdf2db def search_json_files(filename): directory = "./database/meta" for file in os.listdir(directory): if file.endswith('.json') and filename in file: with open(os.path.join(directory, file), 'r') as f: data = json.load(f) return data["商品图像"], data["价格"], data["色号"], data["成分"], data["关键词"], data["商品细节"] def build_ui(): with gr.Blocks() as demo: with gr.Tab("商品信息搜索"): with gr.Column(): with gr.Row(): with gr.Column(): search_id = gr.Textbox(label="商品ID", value="1CNC6N230") id_image = gr.Image(label="商品图") with gr.Column(): id_price = gr.Textbox(label="价格", value="¥1999") id_color = gr.Textbox(label="颜色", value="深花灰") id_ingredient = gr.Textbox(label="成分", value="羊毛") id_selling_point = gr.Textbox(label="商品卖点", value="落肩直身宽松行,一手长经典款初胎羊毛") id_details = gr.Textbox(label="商品细节", value="全羊毛双面呢,采用初剪细支羊毛精制而成,呢面绒毛均匀顺直,光泽温和高贵,手感细腻,挺括而富有弹性") search_button = gr.Button("Search") search_button.click(fn=search_json_files, inputs=[search_id], outputs=[id_image,id_price, id_color, id_ingredient, id_selling_point, id_details]) with gr.Tab("PDF文件解析"): with gr.Row(): file_input = gr.File(label="选择PDF文件", file_types=[".pdf"], file_count="single") parse_btn = gr.Button("开始解析", variant="primary") text_output = gr.Textbox(label="解析进度", placeholder="解析进度", lines=20, max_lines=50) parse_btn.click(fn=pdf2db, inputs=file_input, outputs=text_output) with gr.Tab("商品卖点生成"): gr.Markdown("## 图库里若有商品图,可只填款号") with gr.Row(): with gr.Column(): id = gr.Textbox(label="商品ID") input_image = gr.Image(label="商品图(选填)", type="pil") input_info = gr.Textbox(label="输入的卖点(选填)") output_ch_info = gr.Textbox(label="输出的中文卖点") output_en_info = gr.Textbox(label="输出的英文卖点") output_ky_info = gr.Textbox(label="输出的三个关键卖点") with gr.Column(): product_image = gr.Image(label="商品图") product_price = gr.Textbox(label="价格") product_color = gr.Textbox(label="颜色") product_ingredient = gr.Textbox(label="成分") product_selling_point = gr.Textbox(label="商品卖点") product_details = gr.Textbox(label="商品细节") sell_point_button = gr.Button("生成卖点") sell_point_button.click(fn=process_sellpoint, inputs=[id, input_image, input_info], outputs=[output_ch_info, output_en_info, output_ky_info, product_image, product_price, product_color, product_ingredient, product_selling_point, product_details]) with gr.Tab("生成卖点标题"): gr.Markdown("## 图库里若有商品图,可只填款号") with gr.Row(): with gr.Column(): p_id = gr.Textbox(label="商品ID") input_info = gr.Textbox(label="输入商品描述(可选)") output_title_en = gr.Textbox(label="输出的英文标题") output_title_ch = gr.Textbox(label="输出的中文标题") sell_title_button = gr.Button("生成卖点") sell_title_button.click(fn=process_title, inputs=[p_id,input_info], outputs=[output_title_en,output_title_ch]) return demo if __name__ == "__main__": demo=build_ui() demo.launch(server_name='0.0.0.0', server_port=3333) # from flask import Flask, render_template, request, redirect, url_for # import json # import os # app = Flask(__name__) # def add_product_to_json(product_data): # """将商品信息添加到JSON文件""" # json_file_path = './database/meta/1B1L9H030.json' # 请根据实际情况调整路径 # with open(json_file_path, 'r+', encoding='utf-8') as file: # data = json.load(file) # data.append(product_data) # 假设数据是一个列表 # file.seek(0) # json.dump(data, file, ensure_ascii=False, indent=4) # @app.route('/', methods=['GET', 'POST']) # def index(): # if request.method == 'POST': # product_data = { # '商品名称': request.form['name'], # '商品价格': request.form['price'], # '商品描述': request.form['description'], # '商品图像': request.form['image_url'] # } # add_product_to_json(product_data) # return redirect(url_for('index')) # return render_template('index.html') # if __name__ == "__main__": # app.run(debug=True)