Spaces:
Running on Zero
Running on Zero
Add @spaces.GPU decoration for ZeroGPU Hugging Face Space compatibility
Browse files
app.py
CHANGED
|
@@ -1,12 +1,18 @@
|
|
| 1 |
# IMPORTANT: spaces must be imported at the very top for Hugging Face Spaces / ZeroGPU
|
| 2 |
try:
|
| 3 |
import spaces
|
|
|
|
| 4 |
except Exception:
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
"""Production Document Question Answering (DQA) - Gradio Web Interface.
|
| 8 |
|
| 9 |
-
Configured for native Hugging Face Spaces deployment.
|
| 10 |
Features:
|
| 11 |
- Auto-loads sample documents for instant demonstration.
|
| 12 |
- Multi-document upload: PDF (layout-aware, OCR fallback), DOCX, TXT.
|
|
@@ -98,6 +104,7 @@ def get_status_markdown() -> str:
|
|
| 98 |
"""
|
| 99 |
|
| 100 |
|
|
|
|
| 101 |
def process_uploaded_files(files) -> str:
|
| 102 |
"""Processes newly uploaded files and adds them to the index."""
|
| 103 |
global indexed_filenames
|
|
@@ -143,6 +150,38 @@ def clear_index_action() -> str:
|
|
| 143 |
return get_status_markdown()
|
| 144 |
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
def chat_response(
|
| 147 |
message: str,
|
| 148 |
history: List[Dict[str, str]],
|
|
@@ -158,10 +197,10 @@ def chat_response(
|
|
| 158 |
):
|
| 159 |
"""
|
| 160 |
Main conversational QA handler:
|
| 161 |
-
1.
|
| 162 |
-
2.
|
| 163 |
-
3. Streams response tokens.
|
| 164 |
-
4.
|
| 165 |
"""
|
| 166 |
if not message or not message.strip():
|
| 167 |
yield history, "No query provided.", "No query provided."
|
|
@@ -205,20 +244,18 @@ def chat_response(
|
|
| 205 |
conv_history.append({"role": "user", "content": str(turn[0])})
|
| 206 |
conv_history.append({"role": "assistant", "content": str(turn[1])})
|
| 207 |
|
| 208 |
-
#
|
| 209 |
-
ret_res =
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
rerank_top_k=int(top_k),
|
| 213 |
mode=mode,
|
| 214 |
-
|
| 215 |
-
|
|
|
|
| 216 |
)
|
| 217 |
|
| 218 |
retrieved_chunks = ret_res["chunks"]
|
| 219 |
scores = ret_res["scores"]
|
| 220 |
-
rewritten_query = ret_res.get("rewritten_query", message)
|
| 221 |
-
sub_queries = ret_res.get("sub_queries", [message])
|
| 222 |
|
| 223 |
# Format source citations markdown
|
| 224 |
citations_md = "### π Retrieved Evidence Passages & Citations\n\n"
|
|
@@ -238,13 +275,6 @@ def chat_response(
|
|
| 238 |
{"role": "assistant", "content": ""}
|
| 239 |
]
|
| 240 |
|
| 241 |
-
# Generate answer
|
| 242 |
-
raw_answer, verification = generator.generate_answer(
|
| 243 |
-
query=rewritten_query,
|
| 244 |
-
context_chunks=retrieved_chunks,
|
| 245 |
-
verify=enable_verification
|
| 246 |
-
)
|
| 247 |
-
|
| 248 |
# Stream output word by word for interactive response
|
| 249 |
words = raw_answer.split(" ")
|
| 250 |
streamed_text = ""
|
|
@@ -253,7 +283,6 @@ def chat_response(
|
|
| 253 |
updated_history[-1]["content"] = streamed_text
|
| 254 |
if i % 2 == 0 or i == len(words) - 1:
|
| 255 |
time.sleep(0.015)
|
| 256 |
-
# Yield progressive updates
|
| 257 |
yield updated_history, citations_md, "β³ Verifying claims..."
|
| 258 |
|
| 259 |
# Format verification audit markdown
|
|
@@ -276,12 +305,6 @@ def chat_response(
|
|
| 276 |
|
| 277 |
|
| 278 |
# ---------------- GRADIO UI LAYOUT ----------------
|
| 279 |
-
custom_theme = gr.themes.Soft(
|
| 280 |
-
primary_hue="blue",
|
| 281 |
-
secondary_hue="indigo",
|
| 282 |
-
neutral_hue="slate"
|
| 283 |
-
)
|
| 284 |
-
|
| 285 |
with gr.Blocks(title="Document QA System") as demo:
|
| 286 |
gr.Markdown("""
|
| 287 |
# π Production Document Question Answering (DQA)
|
|
|
|
| 1 |
# IMPORTANT: spaces must be imported at the very top for Hugging Face Spaces / ZeroGPU
|
| 2 |
try:
|
| 3 |
import spaces
|
| 4 |
+
gpu_decorator = spaces.GPU
|
| 5 |
except Exception:
|
| 6 |
+
def gpu_decorator(fn=None, **kwargs):
|
| 7 |
+
if fn is not None:
|
| 8 |
+
return fn
|
| 9 |
+
def wrapper(f):
|
| 10 |
+
return f
|
| 11 |
+
return wrapper
|
| 12 |
|
| 13 |
"""Production Document Question Answering (DQA) - Gradio Web Interface.
|
| 14 |
|
| 15 |
+
Configured for native Hugging Face Spaces deployment (CPU & ZeroGPU).
|
| 16 |
Features:
|
| 17 |
- Auto-loads sample documents for instant demonstration.
|
| 18 |
- Multi-document upload: PDF (layout-aware, OCR fallback), DOCX, TXT.
|
|
|
|
| 104 |
"""
|
| 105 |
|
| 106 |
|
| 107 |
+
@gpu_decorator
|
| 108 |
def process_uploaded_files(files) -> str:
|
| 109 |
"""Processes newly uploaded files and adds them to the index."""
|
| 110 |
global indexed_filenames
|
|
|
|
| 150 |
return get_status_markdown()
|
| 151 |
|
| 152 |
|
| 153 |
+
@gpu_decorator
|
| 154 |
+
def run_neural_pipeline(
|
| 155 |
+
message: str,
|
| 156 |
+
conv_history: list,
|
| 157 |
+
mode: str,
|
| 158 |
+
top_k: int,
|
| 159 |
+
enable_decomposition: bool,
|
| 160 |
+
enable_verification: bool
|
| 161 |
+
):
|
| 162 |
+
"""
|
| 163 |
+
Executes dense/hybrid search, cross-encoder reranking, and verification
|
| 164 |
+
inside the ZeroGPU execution context.
|
| 165 |
+
"""
|
| 166 |
+
ret_res = retriever.retrieve(
|
| 167 |
+
query=message,
|
| 168 |
+
initial_top_k=20,
|
| 169 |
+
rerank_top_k=int(top_k),
|
| 170 |
+
mode=mode,
|
| 171 |
+
conversation_history=conv_history,
|
| 172 |
+
decompose=enable_decomposition
|
| 173 |
+
)
|
| 174 |
+
retrieved_chunks = ret_res["chunks"]
|
| 175 |
+
rewritten_query = ret_res.get("rewritten_query", message)
|
| 176 |
+
|
| 177 |
+
raw_answer, verification = generator.generate_answer(
|
| 178 |
+
query=rewritten_query,
|
| 179 |
+
context_chunks=retrieved_chunks,
|
| 180 |
+
verify=enable_verification
|
| 181 |
+
)
|
| 182 |
+
return ret_res, raw_answer, verification
|
| 183 |
+
|
| 184 |
+
|
| 185 |
def chat_response(
|
| 186 |
message: str,
|
| 187 |
history: List[Dict[str, str]],
|
|
|
|
| 197 |
):
|
| 198 |
"""
|
| 199 |
Main conversational QA handler:
|
| 200 |
+
1. Updates LLM backend configuration.
|
| 201 |
+
2. Runs neural retrieval, reranking, and answer generation.
|
| 202 |
+
3. Streams response tokens to the UI.
|
| 203 |
+
4. Displays evidence citations and claim verification audit.
|
| 204 |
"""
|
| 205 |
if not message or not message.strip():
|
| 206 |
yield history, "No query provided.", "No query provided."
|
|
|
|
| 244 |
conv_history.append({"role": "user", "content": str(turn[0])})
|
| 245 |
conv_history.append({"role": "assistant", "content": str(turn[1])})
|
| 246 |
|
| 247 |
+
# Execute neural retrieval + rerank + generation via ZeroGPU context
|
| 248 |
+
ret_res, raw_answer, verification = run_neural_pipeline(
|
| 249 |
+
message=message,
|
| 250 |
+
conv_history=conv_history,
|
|
|
|
| 251 |
mode=mode,
|
| 252 |
+
top_k=int(top_k),
|
| 253 |
+
enable_decomposition=enable_decomposition,
|
| 254 |
+
enable_verification=enable_verification
|
| 255 |
)
|
| 256 |
|
| 257 |
retrieved_chunks = ret_res["chunks"]
|
| 258 |
scores = ret_res["scores"]
|
|
|
|
|
|
|
| 259 |
|
| 260 |
# Format source citations markdown
|
| 261 |
citations_md = "### π Retrieved Evidence Passages & Citations\n\n"
|
|
|
|
| 275 |
{"role": "assistant", "content": ""}
|
| 276 |
]
|
| 277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
# Stream output word by word for interactive response
|
| 279 |
words = raw_answer.split(" ")
|
| 280 |
streamed_text = ""
|
|
|
|
| 283 |
updated_history[-1]["content"] = streamed_text
|
| 284 |
if i % 2 == 0 or i == len(words) - 1:
|
| 285 |
time.sleep(0.015)
|
|
|
|
| 286 |
yield updated_history, citations_md, "β³ Verifying claims..."
|
| 287 |
|
| 288 |
# Format verification audit markdown
|
|
|
|
| 305 |
|
| 306 |
|
| 307 |
# ---------------- GRADIO UI LAYOUT ----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
with gr.Blocks(title="Document QA System") as demo:
|
| 309 |
gr.Markdown("""
|
| 310 |
# π Production Document Question Answering (DQA)
|