edouardfoussier commited on
Commit
c0df2ba
·
1 Parent(s): 85504aa

fixed sources section

Browse files
Files changed (2) hide show
  1. app.py +19 -9
  2. helpers.py +1 -1
app.py CHANGED
@@ -8,7 +8,7 @@ load_dotenv(override=True)
8
 
9
  from rag.retrieval import search, ensure_ready
10
  from rag.synth import synth_answer_stream
11
- from helpers import _extract_cited_indices, linkify_text_with_sources, _group_sources_md
12
 
13
 
14
  # ---------- Warm-Up ----------
@@ -37,7 +37,7 @@ def bot(history: list[tuple], api_key: str, top_k: int):
37
  Yields (history, sources_markdown) while streaming.
38
  """
39
  if not history:
40
- yield history, "### Sources\n_(none)_"
41
  return
42
 
43
  if api_key:
@@ -51,14 +51,14 @@ def bot(history: list[tuple], api_key: str, top_k: int):
51
  hits = search(user_msg, top_k=k)
52
  except Exception as e:
53
  history[-1] = (user_msg, f"❌ Retrieval error: {e}")
54
- yield history, "### Sources\n_(none)_"
55
  return
56
 
57
  sources_md = sources_markdown(hits[:k])
58
 
59
  # show a small “thinking” placeholder immediately
60
  history[-1] = (user_msg, "⏳ Synthèse en cours…")
61
- yield history, "### 📚 Sources"
62
 
63
  # Streaming LLM
64
  acc = ""
@@ -67,21 +67,31 @@ def bot(history: list[tuple], api_key: str, top_k: int):
67
  acc += chunk or ""
68
  step_hist = deepcopy(history)
69
  step_hist[-1] = (user_msg, acc)
70
- yield step_hist, "### 📚 Sources"
71
  except Exception as e:
72
  history[-1] = (user_msg, f"❌ Synthèse: {e}")
73
- yield history, sources_md
74
  return
75
 
76
  # Finalize + linkify citations
77
  acc_linked = linkify_text_with_sources(acc, hits[:k])
78
  history[-1] = (user_msg, acc_linked)
79
 
 
 
 
 
 
 
80
  # Construit la section sources à partir des citations réelles [n]
81
  used = _extract_cited_indices(acc_linked, k)
 
 
 
 
82
  grouped_sources = _group_sources_md(hits[:k], used)
83
 
84
- yield history, grouped_sources
85
  # yield history, sources_md
86
 
87
 
@@ -137,7 +147,7 @@ with gr.Blocks(theme="soft", fill_height=True) as demo:
137
  bot,
138
  [state, api_key, topk],
139
  [chat, sources],
140
- show_progress="full",
141
  ).then(lambda h: h, chat, state)
142
 
143
  msg_submit = msg.submit(add_user, [msg, state], [msg, state])
@@ -145,7 +155,7 @@ with gr.Blocks(theme="soft", fill_height=True) as demo:
145
  bot,
146
  [state, api_key, topk],
147
  [chat, sources],
148
- show_progress="full",
149
  ).then(lambda h: h, chat, state)
150
 
151
 
 
8
 
9
  from rag.retrieval import search, ensure_ready
10
  from rag.synth import synth_answer_stream
11
+ from helpers import _extract_cited_indices, linkify_text_with_sources, _group_sources_md, is_unknown_answer
12
 
13
 
14
  # ---------- Warm-Up ----------
 
37
  Yields (history, sources_markdown) while streaming.
38
  """
39
  if not history:
40
+ yield history, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
41
  return
42
 
43
  if api_key:
 
51
  hits = search(user_msg, top_k=k)
52
  except Exception as e:
53
  history[-1] = (user_msg, f"❌ Retrieval error: {e}")
54
+ yield history, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
55
  return
56
 
57
  sources_md = sources_markdown(hits[:k])
58
 
59
  # show a small “thinking” placeholder immediately
60
  history[-1] = (user_msg, "⏳ Synthèse en cours…")
61
+ yield history, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
62
 
63
  # Streaming LLM
64
  acc = ""
 
67
  acc += chunk or ""
68
  step_hist = deepcopy(history)
69
  step_hist[-1] = (user_msg, acc)
70
+ yield step_hist, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
71
  except Exception as e:
72
  history[-1] = (user_msg, f"❌ Synthèse: {e}")
73
+ yield history, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
74
  return
75
 
76
  # Finalize + linkify citations
77
  acc_linked = linkify_text_with_sources(acc, hits[:k])
78
  history[-1] = (user_msg, acc_linked)
79
 
80
+ # Decide whether to show sources
81
+ if is_unknown_answer(acc_linked):
82
+ # No sources for unknown / reformulate
83
+ yield history, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
84
+ return
85
+
86
  # Construit la section sources à partir des citations réelles [n]
87
  used = _extract_cited_indices(acc_linked, k)
88
+ if not used:
89
+ yield history, "### 📚 Sources\n_Ici, vous pourrez consulter les sources utilisées pour formuler la réponse._"
90
+ return
91
+
92
  grouped_sources = _group_sources_md(hits[:k], used)
93
 
94
+ yield history, gr_update(visible=True, value=grouped_sources)
95
  # yield history, sources_md
96
 
97
 
 
147
  bot,
148
  [state, api_key, topk],
149
  [chat, sources],
150
+ show_progress="minimal",
151
  ).then(lambda h: h, chat, state)
152
 
153
  msg_submit = msg.submit(add_user, [msg, state], [msg, state])
 
155
  bot,
156
  [state, api_key, topk],
157
  [chat, sources],
158
+ show_progress="minimal",
159
  ).then(lambda h: h, chat, state)
160
 
161
 
helpers.py CHANGED
@@ -10,7 +10,7 @@ def is_unknown_answer(txt: str) -> bool:
10
  patterns = [
11
  "Je suis navré, je n'ai pas trouvé la réponse",
12
  "Je ne sais pas",
13
- "Je ne comprends pas la question"
14
  "Pourriez-vous reformuler",
15
  "je n'ai pas trouvé d'information pertinente",
16
  ]
 
10
  patterns = [
11
  "Je suis navré, je n'ai pas trouvé la réponse",
12
  "Je ne sais pas",
13
+ "Je ne comprends pas"
14
  "Pourriez-vous reformuler",
15
  "je n'ai pas trouvé d'information pertinente",
16
  ]