Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,6 @@ def log_debug(message):
|
|
| 18 |
full_message = f"[{timestamp}] {message}"
|
| 19 |
debug_messages.append(full_message)
|
| 20 |
print(full_message) # Print to console
|
| 21 |
-
# Keep only the last 20 messages
|
| 22 |
if len(debug_messages) > 20:
|
| 23 |
debug_messages.pop(0)
|
| 24 |
return "\n".join(debug_messages)
|
|
@@ -38,32 +37,40 @@ if not os.path.exists(MODEL_CACHE_DIR):
|
|
| 38 |
log_debug("Initializing RAG model...")
|
| 39 |
try:
|
| 40 |
rag = RAGWithCitations(model_path_or_name=MODEL_CACHE_DIR)
|
| 41 |
-
|
|
|
|
| 42 |
if hasattr(rag, "model"):
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
log_debug(f"β Model initialization failed: {str(e)}")
|
| 47 |
raise
|
| 48 |
|
| 49 |
-
# Model configuration
|
| 50 |
-
if hasattr(rag, "tokenizer"):
|
| 51 |
-
rag.tokenizer.pad_token = rag.tokenizer.eos_token
|
| 52 |
-
if hasattr(rag, "model"):
|
| 53 |
-
rag.model.config.pad_token_id = rag.tokenizer.eos_token_id
|
| 54 |
-
rag.model.generation_config.do_sample = True
|
| 55 |
-
rag.model.config.use_cache = True
|
| 56 |
-
|
| 57 |
def extract_text_from_pdf_url(url, debug_state):
|
| 58 |
"""Extract text from PDF with debug logging"""
|
| 59 |
debug_state = log_debug(f"π Fetching PDF: {url[:60]}...")
|
| 60 |
try:
|
| 61 |
-
start_time = time.time()
|
| 62 |
response = requests.get(url, timeout=30)
|
| 63 |
response.raise_for_status()
|
| 64 |
-
load_time = time.time() - start_time
|
| 65 |
-
debug_state = log_debug(f"β³ PDF downloaded in {load_time:.2f}s (size: {len(response.content)/1024:.1f}KB)")
|
| 66 |
-
|
| 67 |
doc = fitz.open(stream=response.content, filetype="pdf")
|
| 68 |
text = ""
|
| 69 |
for page in doc:
|
|
@@ -88,12 +95,14 @@ def generate_answer(query, pdf_urls_str, debug_state=""):
|
|
| 88 |
sources = []
|
| 89 |
feedback = "### PDF Load Report:\n"
|
| 90 |
|
| 91 |
-
debug_state = log_debug(f"Processing {len(pdf_urls)} PDF URLs...")
|
| 92 |
-
|
| 93 |
for url in pdf_urls:
|
| 94 |
text, debug_state = extract_text_from_pdf_url(url, debug_state)
|
| 95 |
if not text.startswith("[Error"):
|
| 96 |
-
sources.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
feedback += f"- β
Loaded: {url[:80]}\n"
|
| 98 |
else:
|
| 99 |
feedback += f"- β Failed: {url[:80]}\n"
|
|
@@ -103,20 +112,20 @@ def generate_answer(query, pdf_urls_str, debug_state=""):
|
|
| 103 |
return feedback + "\nNo valid PDFs processed", debug_state
|
| 104 |
|
| 105 |
debug_state = log_debug(f"π§ Generating answer using {len(sources)} sources...")
|
| 106 |
-
start_time = time.time()
|
| 107 |
|
| 108 |
try:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
answer = response.get('raw_response', 'No response generated')
|
| 114 |
backend = response.get('backend_used', 'unknown')
|
| 115 |
|
| 116 |
-
debug_state = log_debug(f"π‘ Answer
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
full_output = f"{feedback}\n\n### Answer:\n{answer}\n\n_Generated in {gen_time:.2f}s using {backend}_"
|
| 120 |
return full_output, debug_state
|
| 121 |
|
| 122 |
except Exception as e:
|
|
@@ -141,13 +150,9 @@ with gr.Blocks(title="Pleias RAG QA", css="""
|
|
| 141 |
padding: 10px;
|
| 142 |
border-radius: 5px;
|
| 143 |
}
|
| 144 |
-
.debug-title {
|
| 145 |
-
font-weight: bold;
|
| 146 |
-
margin-bottom: 5px;
|
| 147 |
-
}
|
| 148 |
""") as demo:
|
| 149 |
|
| 150 |
-
gr.Markdown("
|
| 151 |
|
| 152 |
with gr.Row():
|
| 153 |
with gr.Column():
|
|
@@ -159,15 +164,13 @@ with gr.Blocks(title="Pleias RAG QA", css="""
|
|
| 159 |
with gr.Column():
|
| 160 |
output = gr.Markdown(label="Model Response")
|
| 161 |
if DEBUG:
|
| 162 |
-
gr.Markdown("### Debug Console", elem_classes=["debug-title"])
|
| 163 |
debug_console = gr.Textbox(
|
| 164 |
-
label="",
|
| 165 |
interactive=False,
|
| 166 |
lines=15,
|
| 167 |
elem_classes=["debug-console"]
|
| 168 |
)
|
| 169 |
|
| 170 |
-
# Handle submission
|
| 171 |
submit_btn.click(
|
| 172 |
fn=generate_answer,
|
| 173 |
inputs=[question, pdf_urls] + ([debug_console] if DEBUG else []),
|
|
|
|
| 18 |
full_message = f"[{timestamp}] {message}"
|
| 19 |
debug_messages.append(full_message)
|
| 20 |
print(full_message) # Print to console
|
|
|
|
| 21 |
if len(debug_messages) > 20:
|
| 22 |
debug_messages.pop(0)
|
| 23 |
return "\n".join(debug_messages)
|
|
|
|
| 37 |
log_debug("Initializing RAG model...")
|
| 38 |
try:
|
| 39 |
rag = RAGWithCitations(model_path_or_name=MODEL_CACHE_DIR)
|
| 40 |
+
|
| 41 |
+
# Fix the warnings by properly configuring generation parameters
|
| 42 |
if hasattr(rag, "model"):
|
| 43 |
+
# Configure tokenizer
|
| 44 |
+
if hasattr(rag, "tokenizer"):
|
| 45 |
+
if rag.tokenizer.pad_token is None:
|
| 46 |
+
rag.tokenizer.pad_token = rag.tokenizer.eos_token
|
| 47 |
+
rag.tokenizer.padding_side = "left" # For batch generation
|
| 48 |
+
|
| 49 |
+
# Configure model generation settings
|
| 50 |
+
rag.model.config.pad_token_id = rag.tokenizer.pad_token_id
|
| 51 |
+
rag.model.generation_config.pad_token_id = rag.tokenizer.pad_token_id
|
| 52 |
+
|
| 53 |
+
# Fix the do_sample/top_p warning
|
| 54 |
+
rag.model.generation_config.do_sample = True
|
| 55 |
+
rag.model.generation_config.top_p = 0.95 # Explicitly set to match warning
|
| 56 |
+
|
| 57 |
+
# Configure attention mask handling
|
| 58 |
+
rag.model.config.use_cache = True
|
| 59 |
+
|
| 60 |
+
log_debug("β
Model loaded successfully with configuration:")
|
| 61 |
+
log_debug(f" - Pad token: {rag.tokenizer.pad_token} (ID: {rag.tokenizer.pad_token_id})")
|
| 62 |
+
log_debug(f" - Generation config: {rag.model.generation_config}")
|
| 63 |
+
|
| 64 |
except Exception as e:
|
| 65 |
log_debug(f"β Model initialization failed: {str(e)}")
|
| 66 |
raise
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def extract_text_from_pdf_url(url, debug_state):
|
| 69 |
"""Extract text from PDF with debug logging"""
|
| 70 |
debug_state = log_debug(f"π Fetching PDF: {url[:60]}...")
|
| 71 |
try:
|
|
|
|
| 72 |
response = requests.get(url, timeout=30)
|
| 73 |
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
| 74 |
doc = fitz.open(stream=response.content, filetype="pdf")
|
| 75 |
text = ""
|
| 76 |
for page in doc:
|
|
|
|
| 95 |
sources = []
|
| 96 |
feedback = "### PDF Load Report:\n"
|
| 97 |
|
|
|
|
|
|
|
| 98 |
for url in pdf_urls:
|
| 99 |
text, debug_state = extract_text_from_pdf_url(url, debug_state)
|
| 100 |
if not text.startswith("[Error"):
|
| 101 |
+
sources.append({
|
| 102 |
+
"text": text,
|
| 103 |
+
"metadata": {"source": url},
|
| 104 |
+
"attention_mask": [1] * len(text.split()) # Simple attention mask
|
| 105 |
+
})
|
| 106 |
feedback += f"- β
Loaded: {url[:80]}\n"
|
| 107 |
else:
|
| 108 |
feedback += f"- β Failed: {url[:80]}\n"
|
|
|
|
| 112 |
return feedback + "\nNo valid PDFs processed", debug_state
|
| 113 |
|
| 114 |
debug_state = log_debug(f"π§ Generating answer using {len(sources)} sources...")
|
|
|
|
| 115 |
|
| 116 |
try:
|
| 117 |
+
# Generate with proper attention handling
|
| 118 |
+
response = rag.generate(
|
| 119 |
+
query,
|
| 120 |
+
sources,
|
| 121 |
+
attention_mask=True # Ensure attention masks are used
|
| 122 |
+
)
|
| 123 |
|
| 124 |
answer = response.get('raw_response', 'No response generated')
|
| 125 |
backend = response.get('backend_used', 'unknown')
|
| 126 |
|
| 127 |
+
debug_state = log_debug(f"π‘ Answer generated using {backend}")
|
| 128 |
+
full_output = f"{feedback}\n\n### Answer:\n{answer}\n\n_Generated using {backend}_"
|
|
|
|
|
|
|
| 129 |
return full_output, debug_state
|
| 130 |
|
| 131 |
except Exception as e:
|
|
|
|
| 150 |
padding: 10px;
|
| 151 |
border-radius: 5px;
|
| 152 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
""") as demo:
|
| 154 |
|
| 155 |
+
gr.Markdown("# Retrieval Generation from PDF files with a 350MB Pocket Size Model from Pleias")
|
| 156 |
|
| 157 |
with gr.Row():
|
| 158 |
with gr.Column():
|
|
|
|
| 164 |
with gr.Column():
|
| 165 |
output = gr.Markdown(label="Model Response")
|
| 166 |
if DEBUG:
|
|
|
|
| 167 |
debug_console = gr.Textbox(
|
| 168 |
+
label="Debug Console",
|
| 169 |
interactive=False,
|
| 170 |
lines=15,
|
| 171 |
elem_classes=["debug-console"]
|
| 172 |
)
|
| 173 |
|
|
|
|
| 174 |
submit_btn.click(
|
| 175 |
fn=generate_answer,
|
| 176 |
inputs=[question, pdf_urls] + ([debug_console] if DEBUG else []),
|