Spaces:
Running
Running
oceansweep
commited on
Commit
•
f8bbcd6
1
Parent(s):
31334e0
Upload 2 files
Browse files
App_Function_Libraries/Gradio_UI/RAG_QA_Chat_tab.py
CHANGED
@@ -15,7 +15,7 @@ import gradio as gr
|
|
15 |
from App_Function_Libraries.Books.Book_Ingestion_Lib import read_epub
|
16 |
from App_Function_Libraries.DB.DB_Manager import DatabaseError, get_paginated_files, add_media_with_keywords
|
17 |
from App_Function_Libraries.PDF.PDF_Ingestion_Lib import extract_text_and_format_from_pdf
|
18 |
-
from App_Function_Libraries.RAG.RAG_Libary_2 import generate_answer
|
19 |
from App_Function_Libraries.RAG.RAG_QA_Chat import search_database, rag_qa_chat
|
20 |
# Eventually... FIXME
|
21 |
from App_Function_Libraries.RAG.RAG_QA_Chat import load_chat_history, save_chat_history
|
@@ -31,7 +31,7 @@ def create_rag_qa_chat_tab():
|
|
31 |
with gr.Row():
|
32 |
with gr.Column(scale=1):
|
33 |
context_source = gr.Radio(
|
34 |
-
["
|
35 |
label="Context Source",
|
36 |
value="Existing File"
|
37 |
)
|
@@ -104,7 +104,7 @@ def create_rag_qa_chat_tab():
|
|
104 |
|
105 |
loading_indicator = gr.HTML(visible=False)
|
106 |
|
107 |
-
def rag_qa_chat_wrapper(message, history, context_source,
|
108 |
convert_to_text, keywords, api_choice):
|
109 |
try:
|
110 |
logging.info(f"Starting rag_qa_chat_wrapper with message: {message}")
|
@@ -128,8 +128,9 @@ def create_rag_qa_chat_tab():
|
|
128 |
logging.info(f"First question, no rephrasing: {message}")
|
129 |
|
130 |
if context_source == "Existing File":
|
131 |
-
|
132 |
-
|
|
|
133 |
elif context_source == "Search Database":
|
134 |
context = f"media_id:{search_results.split('(ID: ')[1][:-1]}"
|
135 |
logging.info(f"Using search result with context: {context}")
|
|
|
15 |
from App_Function_Libraries.Books.Book_Ingestion_Lib import read_epub
|
16 |
from App_Function_Libraries.DB.DB_Manager import DatabaseError, get_paginated_files, add_media_with_keywords
|
17 |
from App_Function_Libraries.PDF.PDF_Ingestion_Lib import extract_text_and_format_from_pdf
|
18 |
+
from App_Function_Libraries.RAG.RAG_Libary_2 import generate_answer, enhanced_rag_pipeline
|
19 |
from App_Function_Libraries.RAG.RAG_QA_Chat import search_database, rag_qa_chat
|
20 |
# Eventually... FIXME
|
21 |
from App_Function_Libraries.RAG.RAG_QA_Chat import load_chat_history, save_chat_history
|
|
|
31 |
with gr.Row():
|
32 |
with gr.Column(scale=1):
|
33 |
context_source = gr.Radio(
|
34 |
+
["All Files in the Database", "Search Database", "Upload File"],
|
35 |
label="Context Source",
|
36 |
value="Existing File"
|
37 |
)
|
|
|
104 |
|
105 |
loading_indicator = gr.HTML(visible=False)
|
106 |
|
107 |
+
def rag_qa_chat_wrapper(message, history, context_source, search_results, file_upload,
|
108 |
convert_to_text, keywords, api_choice):
|
109 |
try:
|
110 |
logging.info(f"Starting rag_qa_chat_wrapper with message: {message}")
|
|
|
128 |
logging.info(f"First question, no rephrasing: {message}")
|
129 |
|
130 |
if context_source == "Existing File":
|
131 |
+
# Use the enhanced_rag_pipeline to search the entire database
|
132 |
+
context = enhanced_rag_pipeline(rephrased_question, api_choice)
|
133 |
+
logging.info(f"Using enhanced_rag_pipeline for database search")
|
134 |
elif context_source == "Search Database":
|
135 |
context = f"media_id:{search_results.split('(ID: ')[1][:-1]}"
|
136 |
logging.info(f"Using search result with context: {context}")
|
App_Function_Libraries/Gradio_UI/View_DB_Items_tab.py
CHANGED
@@ -10,7 +10,7 @@ import gradio as gr
|
|
10 |
#
|
11 |
# Local Imports
|
12 |
from App_Function_Libraries.DB.DB_Manager import view_database, get_all_document_versions, \
|
13 |
-
|
14 |
from App_Function_Libraries.DB.SQLite_DB import get_document_version
|
15 |
from App_Function_Libraries.Utils.Utils import get_database_path, format_text_with_line_breaks
|
16 |
#
|
@@ -124,6 +124,27 @@ def create_prompt_view_tab():
|
|
124 |
)
|
125 |
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
def create_view_all_with_versions_tab():
|
128 |
with gr.TabItem("View All Items"):
|
129 |
gr.Markdown("# View All Database Entries with Version Selection")
|
@@ -141,7 +162,9 @@ def create_view_all_with_versions_tab():
|
|
141 |
with gr.Column(scale=1):
|
142 |
pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
|
143 |
with gr.Column(scale=2):
|
144 |
-
|
|
|
|
|
145 |
|
146 |
item_mapping = gr.State({})
|
147 |
|
@@ -162,80 +185,94 @@ def create_view_all_with_versions_tab():
|
|
162 |
gr.update(interactive=not next_disabled),
|
163 |
gr.update(interactive=not prev_disabled),
|
164 |
gr.update(visible=False, choices=[]),
|
165 |
-
"",
|
166 |
new_item_mapping)
|
167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
def display_item_details(selected_item, item_mapping):
|
169 |
-
if selected_item and item_mapping:
|
170 |
media_id = item_mapping[selected_item]
|
171 |
-
prompt, summary,
|
172 |
versions = get_all_document_versions(media_id)
|
173 |
-
version_choices = [f"Version {v['version_number']} ({v['created_at']})" for v in versions]
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
|
|
178 |
|
179 |
-
|
180 |
-
|
181 |
-
<strong>Prompt:</strong><br>{formatted_prompt}<br><br>
|
182 |
-
<strong>Summary:</strong><br>{formatted_summary}<br><br>
|
183 |
-
<strong>Content (first 500 characters):</strong><br>{formatted_content}...
|
184 |
-
"""
|
185 |
|
186 |
return (
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
190 |
|
191 |
def update_version_content(selected_item, item_mapping, selected_version):
|
192 |
-
if selected_item and item_mapping and selected_version:
|
193 |
media_id = item_mapping[selected_item]
|
194 |
version_number = int(selected_version.split()[1].split('(')[0])
|
195 |
version_data = get_document_version(media_id, version_number)
|
196 |
|
197 |
if 'error' not in version_data:
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
return
|
206 |
-
return
|
207 |
|
208 |
view_button.click(
|
209 |
fn=update_page,
|
210 |
inputs=[page_number, entries_per_page],
|
211 |
outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
|
212 |
-
version_dropdown,
|
213 |
)
|
214 |
|
215 |
next_page_button.click(
|
216 |
fn=lambda page, entries: update_page(page + 1, entries),
|
217 |
inputs=[page_number, entries_per_page],
|
218 |
outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
|
219 |
-
version_dropdown,
|
220 |
)
|
221 |
|
222 |
previous_page_button.click(
|
223 |
fn=lambda page, entries: update_page(max(1, page - 1), entries),
|
224 |
inputs=[page_number, entries_per_page],
|
225 |
outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
|
226 |
-
version_dropdown,
|
227 |
)
|
228 |
|
229 |
items_output.change(
|
230 |
fn=display_item_details,
|
231 |
inputs=[items_output, item_mapping],
|
232 |
-
outputs=[version_dropdown,
|
233 |
)
|
234 |
|
235 |
version_dropdown.change(
|
236 |
fn=update_version_content,
|
237 |
inputs=[items_output, item_mapping, version_dropdown],
|
238 |
-
outputs=[
|
239 |
)
|
240 |
|
241 |
|
|
|
10 |
#
|
11 |
# Local Imports
|
12 |
from App_Function_Libraries.DB.DB_Manager import view_database, get_all_document_versions, \
|
13 |
+
fetch_paginated_data, fetch_item_details, get_latest_transcription
|
14 |
from App_Function_Libraries.DB.SQLite_DB import get_document_version
|
15 |
from App_Function_Libraries.Utils.Utils import get_database_path, format_text_with_line_breaks
|
16 |
#
|
|
|
124 |
)
|
125 |
|
126 |
|
127 |
+
def format_as_html(content, title):
|
128 |
+
escaped_content = html.escape(content)
|
129 |
+
formatted_content = escaped_content.replace('\n', '<br>')
|
130 |
+
return f"""
|
131 |
+
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">
|
132 |
+
<h3>{title}</h3>
|
133 |
+
<div style="max-height: 300px; overflow-y: auto;">
|
134 |
+
{formatted_content}
|
135 |
+
</div>
|
136 |
+
</div>
|
137 |
+
"""
|
138 |
+
|
139 |
+
def extract_prompt_and_summary(content: str):
|
140 |
+
# Implement this function based on how prompt and summary are stored in your DocumentVersions content
|
141 |
+
# This is a placeholder implementation
|
142 |
+
parts = content.split('\n\n', 2)
|
143 |
+
prompt = parts[0] if len(parts) > 0 else "No prompt available."
|
144 |
+
summary = parts[1] if len(parts) > 1 else "No summary available."
|
145 |
+
return prompt, summary
|
146 |
+
|
147 |
+
|
148 |
def create_view_all_with_versions_tab():
|
149 |
with gr.TabItem("View All Items"):
|
150 |
gr.Markdown("# View All Database Entries with Version Selection")
|
|
|
162 |
with gr.Column(scale=1):
|
163 |
pagination_info = gr.Textbox(label="Pagination Info", interactive=False)
|
164 |
with gr.Column(scale=2):
|
165 |
+
prompt_output = gr.Textbox(label="Prompt Used", visible=True)
|
166 |
+
summary_output = gr.HTML(label="Summary", visible=True)
|
167 |
+
transcription_output = gr.HTML(label="Transcription", visible=True)
|
168 |
|
169 |
item_mapping = gr.State({})
|
170 |
|
|
|
185 |
gr.update(interactive=not next_disabled),
|
186 |
gr.update(interactive=not prev_disabled),
|
187 |
gr.update(visible=False, choices=[]),
|
188 |
+
"", "", "",
|
189 |
new_item_mapping)
|
190 |
|
191 |
+
def format_as_html(content, title):
|
192 |
+
if content is None:
|
193 |
+
content = "No content available."
|
194 |
+
escaped_content = html.escape(str(content))
|
195 |
+
formatted_content = escaped_content.replace('\n', '<br>')
|
196 |
+
return f"""
|
197 |
+
<div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;">
|
198 |
+
<h3>{title}</h3>
|
199 |
+
<div style="max-height: 300px; overflow-y: auto;">
|
200 |
+
{formatted_content}
|
201 |
+
</div>
|
202 |
+
</div>
|
203 |
+
"""
|
204 |
+
|
205 |
def display_item_details(selected_item, item_mapping):
|
206 |
+
if selected_item and item_mapping and selected_item in item_mapping:
|
207 |
media_id = item_mapping[selected_item]
|
208 |
+
prompt, summary, transcription = fetch_item_details(media_id)
|
209 |
versions = get_all_document_versions(media_id)
|
|
|
210 |
|
211 |
+
# Filter out duplicate versions and sort them
|
212 |
+
unique_versions = list(set((v['version_number'], v['created_at']) for v in versions))
|
213 |
+
unique_versions.sort(key=lambda x: x[0], reverse=True)
|
214 |
+
version_choices = [f"Version {v[0]} ({v[1]})" for v in unique_versions]
|
215 |
|
216 |
+
summary_html = format_as_html(summary, "Summary")
|
217 |
+
transcription_html = format_as_html(transcription, "Transcription")
|
|
|
|
|
|
|
|
|
218 |
|
219 |
return (
|
220 |
+
gr.update(visible=True, choices=version_choices,
|
221 |
+
value=version_choices[0] if version_choices else None),
|
222 |
+
prompt if prompt is not None else "",
|
223 |
+
summary_html,
|
224 |
+
transcription_html
|
225 |
+
)
|
226 |
+
return gr.update(visible=False, choices=[]), "", "", ""
|
227 |
|
228 |
def update_version_content(selected_item, item_mapping, selected_version):
|
229 |
+
if selected_item and item_mapping and selected_item in item_mapping and selected_version:
|
230 |
media_id = item_mapping[selected_item]
|
231 |
version_number = int(selected_version.split()[1].split('(')[0])
|
232 |
version_data = get_document_version(media_id, version_number)
|
233 |
|
234 |
if 'error' not in version_data:
|
235 |
+
content = version_data['content']
|
236 |
+
prompt, summary = extract_prompt_and_summary(content)
|
237 |
+
transcription = get_latest_transcription(media_id)
|
238 |
+
|
239 |
+
summary_html = format_as_html(summary, "Summary")
|
240 |
+
transcription_html = format_as_html(transcription, "Transcription")
|
241 |
+
|
242 |
+
return prompt if prompt is not None else "", summary_html, transcription_html
|
243 |
+
return gr.update(value=selected_item), gr.update(), gr.update()
|
244 |
|
245 |
view_button.click(
|
246 |
fn=update_page,
|
247 |
inputs=[page_number, entries_per_page],
|
248 |
outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
|
249 |
+
version_dropdown, prompt_output, summary_output, transcription_output, item_mapping]
|
250 |
)
|
251 |
|
252 |
next_page_button.click(
|
253 |
fn=lambda page, entries: update_page(page + 1, entries),
|
254 |
inputs=[page_number, entries_per_page],
|
255 |
outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
|
256 |
+
version_dropdown, prompt_output, summary_output, transcription_output, item_mapping]
|
257 |
)
|
258 |
|
259 |
previous_page_button.click(
|
260 |
fn=lambda page, entries: update_page(max(1, page - 1), entries),
|
261 |
inputs=[page_number, entries_per_page],
|
262 |
outputs=[items_output, pagination_info, page_number, next_page_button, previous_page_button,
|
263 |
+
version_dropdown, prompt_output, summary_output, transcription_output, item_mapping]
|
264 |
)
|
265 |
|
266 |
items_output.change(
|
267 |
fn=display_item_details,
|
268 |
inputs=[items_output, item_mapping],
|
269 |
+
outputs=[version_dropdown, prompt_output, summary_output, transcription_output]
|
270 |
)
|
271 |
|
272 |
version_dropdown.change(
|
273 |
fn=update_version_content,
|
274 |
inputs=[items_output, item_mapping, version_dropdown],
|
275 |
+
outputs=[prompt_output, summary_output, transcription_output]
|
276 |
)
|
277 |
|
278 |
|