Spaces:
Sleeping
Sleeping
Revert App.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
# app.py for HF Spaces: Gradio UI for Multi-Book Paranormal Text Repository
|
| 2 |
# (With Codex Tree v0.5: Hierarchical categorization)
|
| 3 |
import gradio as gr
|
|
@@ -148,8 +150,8 @@ semantic_pipeline.add_component("retriever_embedding", retriever_embedding_sem)
|
|
| 148 |
semantic_pipeline.connect("embedder.embedding", "retriever_embedding.query_embedding")
|
| 149 |
logger.info(f"Semantic pipeline components: {semantic_pipeline.graph.nodes.keys()}")
|
| 150 |
# Helper: Run search (updated for book filter via meta['book'], added debug logs for doc counts)
|
| 151 |
-
def search_stories(query, source_filter=None, type_filter=None, search_mode="Both", top_k=1000, min_score=0.2
|
| 152 |
-
logger.info(f"Searching for query: {query}, source: {source_filter}, type: {type_filter}, mode: {search_mode}, min_score: {min_score}
|
| 153 |
filters = {"operator": "AND", "conditions": []}
|
| 154 |
if source_filter and source_filter != "All Sources":
|
| 155 |
filters["conditions"].append({"field": "book", "operator": "==", "value": source_filter})
|
|
@@ -216,13 +218,6 @@ def search_stories(query, source_filter=None, type_filter=None, search_mode="Bot
|
|
| 216 |
elif title in grouped:
|
| 217 |
grouped[title]["score"] = max(grouped[title]["score"], doc.score)
|
| 218 |
sorted_results = sorted(grouped.values(), key=lambda x: x["score"], reverse=True)
|
| 219 |
-
if category_filter != "All":
|
| 220 |
-
filtered_results = []
|
| 221 |
-
for r in sorted_results:
|
| 222 |
-
has_assignment = r['title'] in stories_dict
|
| 223 |
-
if (category_filter == "Assigned" and has_assignment) or (category_filter == "Unassigned" and not has_assignment):
|
| 224 |
-
filtered_results.append(r)
|
| 225 |
-
sorted_results = filtered_results
|
| 226 |
if search_mode == "Exact":
|
| 227 |
sorted_results = [{**r, "search_query": query} for r in sorted_results]
|
| 228 |
logger.info(f"Search returned {len(sorted_results)} results for query: {query}")
|
|
@@ -863,14 +858,7 @@ def remove_category(l1, l2, l3, l4, l5, l6, tree, sel):
|
|
| 863 |
path = [p for p in [l1, l2, l3, l4, l5, l6] if p]
|
| 864 |
if not path:
|
| 865 |
return tree
|
| 866 |
-
|
| 867 |
-
tree = remove_from_path(tree, path, title)
|
| 868 |
-
paths = find_paths_for_title(tree, title)
|
| 869 |
-
if not paths:
|
| 870 |
-
global stories_dict
|
| 871 |
-
if title in stories_dict:
|
| 872 |
-
del stories_dict[title]
|
| 873 |
-
return tree
|
| 874 |
# Helper: Update current categories display
|
| 875 |
def update_current_categories(selected, tree):
|
| 876 |
if not selected:
|
|
@@ -914,7 +902,7 @@ def save_and_status(tree):
|
|
| 914 |
save_codex_tree(tree)
|
| 915 |
return "Changes saved. Space may restart shortly."
|
| 916 |
# Helper: Update results
|
| 917 |
-
def update_results(query, source, type_filter, search_mode, min_score
|
| 918 |
try:
|
| 919 |
mode_map = {
|
| 920 |
"Keywords (Exact/Phrase Matches)": "Keywords",
|
|
@@ -923,7 +911,7 @@ def update_results(query, source, type_filter, search_mode, min_score, category_
|
|
| 923 |
"Exact (Word/Phrase)": "Exact"
|
| 924 |
}
|
| 925 |
mode = mode_map.get(search_mode, "Both")
|
| 926 |
-
results = search_stories(query, source, type_filter, mode, min_score=min_score
|
| 927 |
choices = [
|
| 928 |
f"{r['title']} (Score: {r['score']:.2f}) (Book: {r['book_slug'].replace('_', ' ').title()}) (Pages: {r['pages']}, Keywords: {r['keywords']})"
|
| 929 |
for r in results
|
|
@@ -1079,7 +1067,6 @@ def gradio_ui():
|
|
| 1079 |
source_filter = gr.Dropdown(label="Source", choices=sources, value="All Sources")
|
| 1080 |
type_filter = gr.Dropdown(label="Type", choices=["Story", "Non-Story", "Both"], value="Both")
|
| 1081 |
search_mode = gr.Dropdown(label="Search Mode", choices=["Keywords (Exact/Phrase Matches)", "Semantic (Conceptual Similarity)", "Both (Hybrid)", "Exact (Word/Phrase)"], value="Both (Hybrid)")
|
| 1082 |
-
category_filter = gr.Dropdown(label="Category Filter", choices=["All", "Assigned", "Unassigned"], value="All")
|
| 1083 |
min_score_input = gr.Number(label="Min Score Threshold", value=0.1, minimum=0.0, step=0.05)
|
| 1084 |
search_btn = gr.Button("Search")
|
| 1085 |
# Main Row: Three Columns
|
|
@@ -1126,7 +1113,7 @@ def gradio_ui():
|
|
| 1126 |
export_list_btn = gr.Dropdown(label="Export List", choices=["MD", "PDF", "Word"], interactive=True)
|
| 1127 |
export_list_output = gr.HTML(value="", show_label=False)
|
| 1128 |
# Event Handlers (updated for book_slug)
|
| 1129 |
-
search_btn.click(update_results, [query, source_filter, type_filter, search_mode, min_score_input
|
| 1130 |
results_radio.change(
|
| 1131 |
select_story, [results_state, results_radio], [selected_story, edited_start, edited_end, edited_keywords]
|
| 1132 |
).then(
|
|
|
|
| 1 |
+
Please review the following code. nothing to do right now but familarize yourself with it:
|
| 2 |
+
|
| 3 |
# app.py for HF Spaces: Gradio UI for Multi-Book Paranormal Text Repository
|
| 4 |
# (With Codex Tree v0.5: Hierarchical categorization)
|
| 5 |
import gradio as gr
|
|
|
|
| 150 |
semantic_pipeline.connect("embedder.embedding", "retriever_embedding.query_embedding")
|
| 151 |
logger.info(f"Semantic pipeline components: {semantic_pipeline.graph.nodes.keys()}")
|
| 152 |
# Helper: Run search (updated for book filter via meta['book'], added debug logs for doc counts)
|
| 153 |
+
def search_stories(query, source_filter=None, type_filter=None, search_mode="Both", top_k=1000, min_score=0.2):
|
| 154 |
+
logger.info(f"Searching for query: {query}, source: {source_filter}, type: {type_filter}, mode: {search_mode}, min_score: {min_score}")
|
| 155 |
filters = {"operator": "AND", "conditions": []}
|
| 156 |
if source_filter and source_filter != "All Sources":
|
| 157 |
filters["conditions"].append({"field": "book", "operator": "==", "value": source_filter})
|
|
|
|
| 218 |
elif title in grouped:
|
| 219 |
grouped[title]["score"] = max(grouped[title]["score"], doc.score)
|
| 220 |
sorted_results = sorted(grouped.values(), key=lambda x: x["score"], reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
if search_mode == "Exact":
|
| 222 |
sorted_results = [{**r, "search_query": query} for r in sorted_results]
|
| 223 |
logger.info(f"Search returned {len(sorted_results)} results for query: {query}")
|
|
|
|
| 858 |
path = [p for p in [l1, l2, l3, l4, l5, l6] if p]
|
| 859 |
if not path:
|
| 860 |
return tree
|
| 861 |
+
return remove_from_path(tree, path, sel['title'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 862 |
# Helper: Update current categories display
|
| 863 |
def update_current_categories(selected, tree):
|
| 864 |
if not selected:
|
|
|
|
| 902 |
save_codex_tree(tree)
|
| 903 |
return "Changes saved. Space may restart shortly."
|
| 904 |
# Helper: Update results
|
| 905 |
+
def update_results(query, source, type_filter, search_mode, min_score):
|
| 906 |
try:
|
| 907 |
mode_map = {
|
| 908 |
"Keywords (Exact/Phrase Matches)": "Keywords",
|
|
|
|
| 911 |
"Exact (Word/Phrase)": "Exact"
|
| 912 |
}
|
| 913 |
mode = mode_map.get(search_mode, "Both")
|
| 914 |
+
results = search_stories(query, source, type_filter, mode, min_score=min_score)
|
| 915 |
choices = [
|
| 916 |
f"{r['title']} (Score: {r['score']:.2f}) (Book: {r['book_slug'].replace('_', ' ').title()}) (Pages: {r['pages']}, Keywords: {r['keywords']})"
|
| 917 |
for r in results
|
|
|
|
| 1067 |
source_filter = gr.Dropdown(label="Source", choices=sources, value="All Sources")
|
| 1068 |
type_filter = gr.Dropdown(label="Type", choices=["Story", "Non-Story", "Both"], value="Both")
|
| 1069 |
search_mode = gr.Dropdown(label="Search Mode", choices=["Keywords (Exact/Phrase Matches)", "Semantic (Conceptual Similarity)", "Both (Hybrid)", "Exact (Word/Phrase)"], value="Both (Hybrid)")
|
|
|
|
| 1070 |
min_score_input = gr.Number(label="Min Score Threshold", value=0.1, minimum=0.0, step=0.05)
|
| 1071 |
search_btn = gr.Button("Search")
|
| 1072 |
# Main Row: Three Columns
|
|
|
|
| 1113 |
export_list_btn = gr.Dropdown(label="Export List", choices=["MD", "PDF", "Word"], interactive=True)
|
| 1114 |
export_list_output = gr.HTML(value="", show_label=False)
|
| 1115 |
# Event Handlers (updated for book_slug)
|
| 1116 |
+
search_btn.click(update_results, [query, source_filter, type_filter, search_mode, min_score_input], [results_state, results_radio, stories_count])
|
| 1117 |
results_radio.change(
|
| 1118 |
select_story, [results_state, results_radio], [selected_story, edited_start, edited_end, edited_keywords]
|
| 1119 |
).then(
|