LCA commited on
Commit
dcad6c6
·
verified ·
1 Parent(s): 2fd6167

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -8,6 +8,7 @@ from sentence_transformers import SentenceTransformer
8
  from huggingface_hub import InferenceClient
9
  from datasets import load_dataset
10
  import json
 
11
 
12
  DATASET_REPO = "LCA/HACKATHON_PARTS"
13
 
@@ -64,6 +65,26 @@ def rechercher_article(articleSource):
64
 
65
  return article
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  def respond(message):
68
  print(" ------------------ ")
69
  print(message)
@@ -105,16 +126,18 @@ def respond(message):
105
 
106
  print("---- retour de l'analyse")
107
  print(full_response)
108
- print("")
 
 
109
 
110
  # If you expect a JSON response, you can try to parse it here
111
  # import json
112
  # try:
113
  order = {}
114
  try:
115
- data = json.loads(full_response)
116
  articles = []
117
- for article in data.get("articles", []):
118
  found_article = rechercher_article(article)
119
  articles.append(found_article)
120
  order["articles"] = articles
 
8
  from huggingface_hub import InferenceClient
9
  from datasets import load_dataset
10
  import json
11
+ import re
12
 
13
  DATASET_REPO = "LCA/HACKATHON_PARTS"
14
 
 
65
 
66
  return article
67
 
68
+ def extract_json_from_response(response):
69
+ """
70
+ Extrait le JSON d'une chaîne de texte contenant potentiellement du texte en vrac.
71
+ Retourne un objet Python (dict) ou None si extraction impossible.
72
+ """
73
+
74
+ # Recherche la première accolade ouvrante et fermante pour délimiter le JSON
75
+ match = re.search(r'({.*})', response, re.DOTALL)
76
+ if match:
77
+ json_str = match.group(1)
78
+ try:
79
+ return json.loads(json_str)
80
+ except Exception as e:
81
+ print("Erreur lors du parsing JSON extrait:", e)
82
+ return None
83
+ else:
84
+ print("Aucun JSON trouvé dans la réponse.")
85
+ return None
86
+
87
+
88
  def respond(message):
89
  print(" ------------------ ")
90
  print(message)
 
126
 
127
  print("---- retour de l'analyse")
128
  print(full_response)
129
+ print("--")
130
+ json_response = extract_json_from_response(full_response)
131
+ print(json_response)
132
 
133
  # If you expect a JSON response, you can try to parse it here
134
  # import json
135
  # try:
136
  order = {}
137
  try:
138
+
139
  articles = []
140
+ for article in json_response.get("articles", []):
141
  found_article = rechercher_article(article)
142
  articles.append(found_article)
143
  order["articles"] = articles