chat.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import sys
  2. # print(sys.path)
  3. # sys.path.append("/data/data/luosy/project/sku_search")
  4. import time
  5. import os
  6. from PIL import Image
  7. import uuid
  8. from prompt import *
  9. from llm import *
  10. import json
  11. from conf import *
  12. import re
  13. history_list=[]
  14. plugins = {
  15. "ch_en_selling_points":get_ch_en_selling_points,
  16. "en_ch_selling_points":get_en_ch_selling_points,
  17. "ch_en_selling_title":get_ch_en_selling_title,
  18. "en_ch_selling_points_his":get_en_ch_selling_points_his,
  19. "TextControl_his":TextControl_his
  20. }
  21. def search_json_files(filename):
  22. directory = "/data/data/luosy/project/sku_search/database/meta"
  23. data = {}
  24. # im=Image.new('RGB', (800, 800), 'white')
  25. for file in os.listdir(directory):
  26. if file.endswith('.json') and filename in file:
  27. with open(os.path.join(directory, file), 'r') as f:
  28. data = json.load(f)
  29. if data =={}:
  30. return None,"暂无数据","暂无数据","暂无数据","暂无数据","暂无数据"
  31. return data["商品图像"], data["价格"], data["色号"], data["成分"], data["关键词"], data["商品细节"]
  32. def is_valid_response(response):
  33. """
  34. 检查响应是否是有效的JSON格式且包含所需字段
  35. """
  36. try:
  37. response_dict = json.loads(response)
  38. required_fields = ["服装描述(中文)", "服装描述(英文)", "3个关键点(英文)"]
  39. return all(field in response_dict for field in required_fields)
  40. except Exception as e:
  41. return False
  42. def extract_response_values(response):
  43. """
  44. 从响应中提取各个字段的值
  45. Args:
  46. response: JSON格式的响应字符串
  47. Returns:
  48. tuple: (中文描述, 英文描述, 关键点)
  49. """
  50. try:
  51. response_dict = json.loads(response)
  52. cn_desc = response_dict.get("服装描述(中文)", "")
  53. en_desc = response_dict.get("服装描述(英文)", "")
  54. key_points = response_dict.get("3个关键点(英文)", "")
  55. return cn_desc, en_desc, key_points
  56. except Exception as e:
  57. return "", "", ""
  58. # def doubao_request(pre_signed_url_output, info,method, max_retries=1):
  59. # """
  60. # 带重试机制的豆包请求
  61. # Args:
  62. # pre_signed_url_output: 预签名URL
  63. # info: 商品信息
  64. # max_retries: 最大重试次数
  65. # Returns:
  66. # tuple: (中文描述, 英文描述, 关键点)
  67. # """
  68. # info = plugins[method](info)
  69. # for attempt in range(max_retries):
  70. # try:
  71. # result = doubao_MMM_request( pre_signed_url_output, info)
  72. # if is_valid_response(result):
  73. # ch,en,key=extract_response_values(result)
  74. # if len(en)>350 and method=="ch_en_selling_points":
  75. # sysp,usrp=TextControl(en)
  76. # en=get_lm_text(sysp,usrp)
  77. # return ch,en,key
  78. # except Exception as e:
  79. # print(f"Attempt {attempt + 1} failed with error: {str(e)}")
  80. # if attempt == max_retries - 1:
  81. # raise e
  82. # raise Exception("Failed to get valid response after maximum retries")
  83. # def doubao_generate(image,info,method):
  84. # if not os.path.exists(MMMConfig.root_path):
  85. # os.makedirs(MMMConfig.root_path)
  86. # file_name=f"{uuid.uuid4()}.jpg"
  87. # original_file=os.path.join(MMMConfig.root_path,file_name)
  88. # # 添加这一行,将RGBA转换为RGB
  89. # if image.mode == 'RGBA':
  90. # image = image.convert('RGB')
  91. # image.save(original_file)
  92. # compressed_file = original_file
  93. # object_key = file_name
  94. # quality = compress_image(original_file, compressed_file)
  95. # pre_signed_url_output = upload_tos(compressed_file,object_key)
  96. # if pre_signed_url_output and os.path.exists(compressed_file):
  97. # os.remove(compressed_file)
  98. # return doubao_request(pre_signed_url_output,info,method)
  99. # def generate_text(id,image=None,info="",method="ch_en_selling_points"):
  100. # if len(id) == 9:
  101. # id_image,id_price, id_color, id_ingredient, id_selling_point, id_details=search_json_files(id)
  102. # else:
  103. # id_image,id_price, id_color, id_ingredient, id_selling_point, id_details=None,None,None,None,None,None
  104. # info=id_details+info if id_details else info
  105. # if image == None and id_image != None:
  106. # image=Image.open(id_image)
  107. # ch_sen,en_sen,key_point=doubao_generate(image,info,method)
  108. # return ch_sen,en_sen,key_point,id_image,id_price, id_color, id_ingredient, id_selling_point, id_details
  109. # def gen_title(id,info=None,method="ch_en_selling_title"):
  110. # info_new=None
  111. # if len(id) == 9:
  112. # info_new=generate_text(id)[1]
  113. # if info_new ==None and info !=None:
  114. # info_new=info
  115. # sysp,usrp = plugins[method](info_new)
  116. # res=get_lm_text(sysp,usrp)
  117. # res_dict = json.loads(res)
  118. # # print(res)
  119. # return res_dict["en_tile"],res_dict["ch_title"]
  120. def format_history(strings, indent=" "):
  121. result = ""
  122. for i, string in enumerate(strings, start=1):
  123. # 拼接序号、缩进和字符串,并添加换行符
  124. result += f"{indent}{i}. {string}\n"
  125. return result
  126. def get_history():
  127. global history_list
  128. if len(history_list)==0:
  129. history=''
  130. else:
  131. history=format_history(history_list)
  132. return history
  133. def add_history(input,max_num=20):
  134. global history_list
  135. text = re.split(r'[,\.\!\?\;\:]+', input)
  136. text=text[0].strip()
  137. history_list.insert(0, text)
  138. if len(history_list)>max_num:
  139. history_list=history_list[:max_num]
  140. def generate_text(plm_info,img,graphic_label=None,method="en_ch_selling_points_his",plat="ali",model_name="mm_tyqw"):
  141. # his="""1.Embrace the spirit of freedom with this Hanma denim\n
  142. # 2.Meet Aria, your new wardrobe staple that exudes elegance and comfort\n
  143. # 3.Embrace the elegance of this 16 momme mulberry silk wide-leg pant\n
  144. # 4.Meet your new best friend, the ultra-lightweight and versatile 'Transformer' down jacket\n
  145. # 5.Embrace the allure of this enchanting blouse, where every detail whispers sophistication\n
  146. # 6.Meet Gloria\n
  147. # 7.Discover Gloria
  148. # 8.Step into the spotlight with Gloria"""
  149. his=get_history()
  150. print(his)
  151. if graphic_label:
  152. tags_sen=",".join(graphic_label)
  153. plm_info="\n' '以下是该衣服的关键点:"+tags_sen
  154. if plat=="ali":
  155. key=ali_ky
  156. model=ali_model[model_name]
  157. else:
  158. key=doubao_ky
  159. model=doubao_model[model_name]
  160. usrp = plugins[method](plm_info,his)
  161. # print(usrp)
  162. llm=llm_request(*key,model)
  163. for _ in range(3):
  164. try:
  165. result = llm.llm_mm_request(usrp,img)
  166. print(result)
  167. if is_valid_response(result):
  168. ch,en,kw=extract_response_values(result)
  169. add_history(en)
  170. break
  171. except Exception as e:
  172. print(f"Attempt failed with error: {str(e)}")
  173. # result={"descr":en,"keywords":kw}
  174. return en,kw
  175. def generate_text_new(plm_info,img,graphic_label=None,method="TextControl_his",plat="ali",model_name1="mm_tyqw",model_name2="text_qwen"):
  176. his=get_history()
  177. print(his)
  178. if graphic_label:
  179. plm_info+="\n以下是该衣服的关键点:"+graphic_label
  180. if plat=="ali":
  181. key=ali_ky
  182. model1=ali_model[model_name1]
  183. model2=ali_model[model_name2]
  184. else:
  185. key=doubao_ky
  186. model=doubao_model[model_name1]
  187. sim_prompt="如果图片里有文字的话,请结合图片里的衣服和文本信息进行描述一下衣服,还要具体到衣服的风格,细节,版型,设计等"
  188. # print(usrp)
  189. llm_mm=llm_request(*key,model1)
  190. llm_lm=llm_request(*key,model2)
  191. res1 = llm_mm.llm_mm_request(sim_prompt,img)
  192. sys_prompt,usr_prompt = plugins[method](res1,his)
  193. print(usr_prompt)
  194. for _ in range(3):
  195. try:
  196. # print(sys_prompt,usr_prompt)
  197. result=llm_lm.llm_text_request(usr_prompt,sys_prompt)
  198. print(result)
  199. if is_valid_response(result):
  200. ch,en,kw=extract_response_values(result)
  201. add_history(en)
  202. break
  203. except Exception as e:
  204. print(f"Attempt failed with error: {str(e)}")
  205. # result={"descr":en,"keywords":kw}
  206. return ch,en,kw
  207. def gen_title(info,tags=None,referencr_title=None,method="ch_en_selling_title",plat="doubao",model_name="text_ds"):
  208. if tags:
  209. tags_sen=",".join(tags)
  210. info="\n' '以下是该衣服的关键点:"+tags_sen
  211. if referencr_title:
  212. info="\n' '请以这条标题样例的结构作为借鉴来写这条标题:"+referencr_title
  213. sysp,usrp = plugins[method](info)
  214. if plat=="ali":
  215. key=ali_ky
  216. model=ali_model[model_name]
  217. else:
  218. key=doubao_ky
  219. model=doubao_model[model_name]
  220. llm=llm_request(*key,model)
  221. res=llm.llm_text_request(usrp,sysp)
  222. res_dict = json.loads(res)
  223. return {"title":res_dict["en_tile"]}
  224. # @app.route('/title', methods=['POST'])
  225. # def request_title():
  226. # try:
  227. # spu = request.form.get("spu", None)
  228. # referencr_title = request.form.get("referencr_title", None)
  229. # desc = request.form.get("desc")
  230. # if not desc:
  231. # return jsonify({"error": "desc is required"}), 400
  232. # tags=request.form.get("tags", None)
  233. # result=gen_title(desc,tags,referencr_title)
  234. # response = {
  235. # "spu": spu,
  236. # "result": result
  237. # }
  238. # return jsonify(response), 200
  239. # except Exception as e:
  240. # return jsonify({"error": str(e)}), 500
  241. # @app.route('/description', methods=['POST'])
  242. # def request_description():
  243. # try:
  244. # spu = request.form.get("spu", None)
  245. # reference_url = request.form.get("reference_url", None)
  246. # graphic_label = request.form.get("graphic_label", None)
  247. # img = request.form.get("img")
  248. # if not img:
  249. # return jsonify({"error": "img is required"}), 400
  250. # plm_info = request.form.get("plm_info")
  251. # if not plm_info:
  252. # return jsonify({"error": "plm_info is required"}), 400
  253. # result=generate_text(plm_info,img,graphic_label)
  254. # response = {
  255. # "spu": spu,
  256. # "result": result
  257. # }
  258. # return jsonify(response), 200
  259. # except Exception as e:
  260. # return jsonify({"error": str(e)}), 500
  261. if __name__ == "__main__":
  262. id_image,id_price, id_color, id_ingredient, id_selling_point, id_details=search_json_files("1A6H4K7V0")
  263. id_image=id_image[2:]
  264. id_image=os.path.join("/data/data/luosy/project/sku_search",id_image)
  265. print(id_details,id_image)
  266. for _ in range(3):
  267. result=generate_text_new('1、16mm平纹香云纱;2、香蕉裤型;3、裤身立体收省;4、后腰包橡筋;5、两侧插袋;两边插袋里面有网布贴袋,一大一小,大的可套手机,小的可套耳机或卡片;6、真门牌,大溪地贝壳扣',"/data/data/Mia/product_env_project/gen_sellpoint/企业微信截图_17439887354277.png")
  268. print(result)
  269. # from tqdm import tqdm
  270. # def image_to_base64(image):
  271. # # 将Image对象转换为BytesIO对象
  272. # image_io = io.BytesIO()
  273. # image.save(image_io, format='PNG')
  274. # image_io.seek(0)
  275. # # 使用base64编码
  276. # image_base64 = base64.b64encode(image_io.read()).decode('utf-8')
  277. # return image_base64
  278. # def create_html_with_base64_images(root, output_html):
  279. # with open(output_html, 'w', encoding='utf-8') as html_file:
  280. # html_file.write('<!DOCTYPE html>\n<html>\n<head>\n<title>Images in Table</title>\n')
  281. # html_file.write('<meta charset="UTF-8">\n') # 添加字符编码声明
  282. # html_file.write('<style>\n')
  283. # html_file.write('table {\nborder-collapse: collapse;\nwidth: 100%;\n}\n')
  284. # html_file.write('table, th, td {\nborder: 1px solid black;\n}\n')
  285. # html_file.write('img {\nmax-width: 100%;\nheight: auto;\ndisplay: block;\nmargin-left: auto;\nmargin-right: auto;\n}\n')
  286. # html_file.write('</style>\n')
  287. # html_file.write('</head>\n<body>\n')
  288. # html_file.write('<table>\n')
  289. # html_file.write('<tr>\n')
  290. # html_file.write('<th>输入的图片</th>\n') # 第一列:索引
  291. # html_file.write('<th>输入的描述</th>\n') # 第二列:标题
  292. # html_file.write('<th>输出的商品详情</th>\n') # 第二列:标题
  293. # html_file.write('<th>输出的商品详情(翻译)</th>\n') # 第三列:图表
  294. # html_file.write('<th>输出的卖点</th>\n') # 第三列:图表
  295. # # for i in range(1, 100): # 添加序号列1到13
  296. # # html_file.write(f'<th>{i}</th>\n')
  297. # html_file.write('</tr>\n')
  298. # for file in tqdm(os.listdir(root)[:100], desc="Processing", unit="iter"):
  299. # if '.ipynb_checkpoints' in file:
  300. # continue
  301. # file_path = os.path.join(root, file)
  302. # with open(file_path, 'r') as f:
  303. # data = json.load(f)
  304. # if data and "商品图像" in data.keys():
  305. # id_image,id_details=data["商品图像"][2:], data["商品细节"]
  306. # else:
  307. # continue
  308. # id_image=os.path.join("/data/data/luosy/project/sku_search",id_image)
  309. # img_base64 = image_to_base64(Image.open(id_image))
  310. # ch,en,kw=generate_text_new(id_details,id_image)
  311. # html_file.write('<tr>\n')
  312. # # html_file.write(f'<td>{index+1}</td>\n') # 添加序号
  313. # # html_file.write('<td>\n')
  314. # # html_file.write(f'<img src="data:image/png;base64,{frame_title_img}" alt="Image">\n')
  315. # # html_file.write('</td>\n')
  316. # html_file.write('<td>\n')
  317. # html_file.write(f'<img src="data:image/png;base64,{img_base64}" alt="Image">\n')
  318. # html_file.write('</td>\n')
  319. # html_file.write(f'<td>{id_details}</td>\n') # 添加序号
  320. # html_file.write(f'<td>{en}</td>\n') # 添加序号
  321. # html_file.write(f'<td>{ch}</td>\n') # 添加序号
  322. # html_file.write(f'<td>{kw}</td>\n') # 添加序号
  323. # # html_file.write('</td>\n')
  324. # # for img in image_data:
  325. # # html_file.write('<td>\n')
  326. # # html_file.write(f'<img src="data:image/jpeg;base64,{img}" alt="Image" style="max-width: 100px; max-height: 100px; margin: 5px;">\n')
  327. # # html_file.write('</td>\n')
  328. # # html_file.write('</td>\n')
  329. # html_file.write('</tr>\n')
  330. # html_file.write('</table>\n')
  331. # html_file.write('</body>\n</html>')
  332. # root='/data/data/luosy/project/sku_search/database/meta'
  333. # create_html_with_base64_images(root, "out——qw_v6.html")
  334. # app.run(host="0.0.0.0",port=2222,debug=True)
  335. # print(gen_title(info= "This sweatshirt is a wardrobe essential with its simple yet stylish design and 3D heart pattern that adds a fun visual pop. <b>The unisex design is perfect for couples</b>, and it pairs effortlessly with jeans, cargo pants, or a pleated skirt. Ideal for school, work, or casual outings, it's comfortable and trendy all day long!"))
  336. # from PIL import Image
  337. # img1=Image.open("/data/data/luosy/project/sku_search/temp_img/企业微信截图_17372766091671.png")
  338. # ch_sen,en_sen,key_point,id_image,id_price, id_color, id_ingredient, id_selling_point, id_details=generate_text("",img1,"""-With elastic waistband
  339. # -With hairband
  340. # -X-line fit
  341. # 1.腰部橡筋 2.袖子橡
  342. # 筋 3.前中绳子可调
  343. # 节大小""")
  344. # print(len(en_sen),end=" ")
  345. # print(ch_sen,en_sen,key_point)
  346. # ###############################
  347. # img2=Image.open("/data/data/luosy/project/sku_search/temp_img/企业微信截图_17389065463149[1](1).png")
  348. # ch_sen,en_sen,key_point,id_image,id_price, id_color, id_ingredient, id_selling_point, id_details=generate_text("",img2,"""-Washable wool
  349. # -Unisex
  350. # -With silver threads
  351. # 1.后中开衩;2.双扣可调节袖袢;3.暗门筒设计,天然果实扣;4.可水洗羊毛含银葱人字纹面料;5.里面左右两侧均有内袋,左侧最外层内袋是手机袋,防丢失""")
  352. # print(len(en_sen),end=" ")
  353. # print(ch_sen,en_sen,key_point)
  354. # ###############################
  355. # img3=Image.open("/data/data/luosy/project/sku_search/temp_img/企业微信截图_17392379937637.png")
  356. # ch_sen,en_sen,key_point,id_image,id_price, id_color, id_ingredient, id_selling_point, id_details=generate_text("",img3,"""-Acetate
  357. # -With pockets
  358. # -Workwear
  359. # 1.描述二醋酸面料:2.扣子为镶钻布包扣;3.半裙后腰包橡筋;4.半裙有
  360. # 侧插袋;5.半裙有侧开隐形拉链,这是两件套套装""")
  361. # print(len(en_sen),end=" ")
  362. # print(ch_sen,en_sen,key_point)