Explorar el Código

解决重复插入问题

xueyiming hace 2 meses
padre
commit
67313a9e77
Se han modificado 1 ficheros con 17 adiciones y 11 borrados
  1. 17 11
      utils/keywords_utils.py

+ 17 - 11
utils/keywords_utils.py

@@ -22,14 +22,20 @@ class KeywordSummaryTask:
         for keyword in keywords:
             keyword_data = db_helper.get(KeywordData, keyword=keyword)
             if keyword_data is None:
-                new_keyword_data = KeywordData(keyword=keyword)
-                keyword_data = db_helper.add(new_keyword_data)
+                try:
+                    new_keyword_data = KeywordData(keyword=keyword)
+                    keyword_data = db_helper.add(new_keyword_data)
+                except Exception as e:
+                    return
             keyword_with_content_chunk = db_helper.get(KeywordWithContentChunk, keyword_id=keyword_data.id,
-                                                            content_chunk_id=content_chunk.id)
+                                                       content_chunk_id=content_chunk.id)
             if keyword_with_content_chunk is None:
-                keyword_with_content_chunk = KeywordWithContentChunk(keyword_id=keyword_data.id,
-                                                                     content_chunk_id=content_chunk.id)
-                db_helper.add(keyword_with_content_chunk)
+                try:
+                    keyword_with_content_chunk = KeywordWithContentChunk(keyword_id=keyword_data.id,
+                                                                         content_chunk_id=content_chunk.id)
+                    db_helper.add(keyword_with_content_chunk)
+                except Exception as e:
+                    return
             if keyword_with_content_chunk.keyword_clustering_status == 0:
                 try:
                     keyword_clustering = db_helper.get(KeywordClustering, keyword_id=keyword_data.id)
@@ -42,15 +48,15 @@ class KeywordSummaryTask:
                         new_keyword_summary = update_keyword_summary_prompt(keyword_clustering.keyword_summary, keyword,
                                                                             content_chunk.text)
                         db_helper.update(KeywordClustering, filters={"id": keyword_clustering.id},
-                                              updates={"keyword_summary": new_keyword_summary})
+                                         updates={"keyword_summary": new_keyword_summary})
                     db_helper.update(KeywordWithContentChunk, filters={"id": keyword_with_content_chunk.id},
-                                          updates={"keyword_clustering_status": 1})
+                                     updates={"keyword_clustering_status": 1})
                 except Exception as e:
                     print(e)
                     db_helper.update(KeywordWithContentChunk, filters={"id": keyword_with_content_chunk.id},
-                                          updates={"keyword_clustering_status": 2})
+                                     updates={"keyword_clustering_status": 2})
         db_helper.update(ContentChunks, filters={"id": content_chunk.id},
-                              updates={"keywords_status": 1})
+                         updates={"keywords_status": 1})
 
     # 使用线程池处理文本列表
     def process_texts_concurrently(self):
@@ -83,4 +89,4 @@ class KeywordSummaryTask:
 
 if __name__ == '__main__':
     keyword_summary_task = KeywordSummaryTask()
-    keyword_summary_task.process_texts_concurrently()
+    keyword_summary_task.process_texts_concurrently()