Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# filename: okemo_backend.py
|
| 2 |
-
# OkemoLLM backend β Streaming responses + DuckDuckGo + AI summarization
|
| 3 |
|
| 4 |
import os
|
| 5 |
import re
|
|
@@ -271,19 +271,22 @@ def perform_web_search(query: str) -> str:
|
|
| 271 |
return ""
|
| 272 |
|
| 273 |
# ------------------------
|
| 274 |
-
#
|
| 275 |
# ------------------------
|
| 276 |
-
def
|
| 277 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
if not text.strip():
|
| 279 |
return ""
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
return to_ascii(summary.strip())
|
| 287 |
|
| 288 |
# ------------------------
|
| 289 |
# Streaming Generator
|
|
@@ -339,9 +342,11 @@ def on_submit(user_message: str, history: Optional[List[List[str]]], train_input
|
|
| 339 |
if is_search:
|
| 340 |
query = WEB_TAG_RE.sub("", user_message).strip()
|
| 341 |
raw = perform_web_search(query)
|
| 342 |
-
|
|
|
|
|
|
|
| 343 |
|
| 344 |
-
prefix = f"Based on
|
| 345 |
full_prompt = f"{SYSTEM_INSTRUCTION} {prefix}{user_message}"
|
| 346 |
|
| 347 |
current_state = train_input_state.copy()
|
|
@@ -353,7 +358,7 @@ def on_submit(user_message: str, history: Optional[List[List[str]]], train_input
|
|
| 353 |
|
| 354 |
response_stream = generate_response(full_prompt)
|
| 355 |
partial_reply = ""
|
| 356 |
-
search_status = f"π Used
|
| 357 |
|
| 358 |
if not history or (history and history[-1][1] is not None):
|
| 359 |
history.append([user_message, None])
|
|
@@ -375,7 +380,7 @@ def on_submit(user_message: str, history: Optional[List[List[str]]], train_input
|
|
| 375 |
# Gradio Interface
|
| 376 |
# ------------------------
|
| 377 |
with gr.Blocks(title="OkemoLLM") as demo:
|
| 378 |
-
gr.Markdown("# π OkemoLLM β Streaming DuckDuckGo +
|
| 379 |
status = gr.Markdown(f"Ready. Current Checkpoint Iteration: **{start_iteration}**")
|
| 380 |
chat = gr.Chatbot(height=400)
|
| 381 |
|
|
@@ -387,11 +392,11 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 387 |
|
| 388 |
gr.Markdown("---")
|
| 389 |
gr.Markdown("## βοΈ Feedback-Based Training (Learn from Web Context)")
|
| 390 |
-
gr.Markdown("**The training input automatically includes the web
|
| 391 |
|
| 392 |
with gr.Column():
|
| 393 |
# This displays the full input context (System + Web Summary + User Prompt)
|
| 394 |
-
prompt_display = gr.Textbox(label="Full Training Input (System + Web
|
| 395 |
last_response_display = gr.Textbox(label="Model's Last Response", interactive=False)
|
| 396 |
# This will be the Ground Truth/Target for training
|
| 397 |
target_response = gr.Textbox(label="Desired Response (Ground Truth)", placeholder='Enter the correct/better response for the prompt above.', scale=1)
|
|
|
|
| 1 |
# filename: okemo_backend.py
|
| 2 |
+
# OkemoLLM backend β Streaming responses + DuckDuckGo + AI summarization (REMOVED)
|
| 3 |
|
| 4 |
import os
|
| 5 |
import re
|
|
|
|
| 271 |
return ""
|
| 272 |
|
| 273 |
# ------------------------
|
| 274 |
+
# Web Content Preparation (Replaces AI summarization for max retention)
|
| 275 |
# ------------------------
|
| 276 |
+
def prepare_web_content(text: str) -> str:
|
| 277 |
+
"""
|
| 278 |
+
Prepare raw web text by truncating it to a maximal safe length
|
| 279 |
+
(500 characters) to ensure maximum retention while avoiding context overflow.
|
| 280 |
+
"""
|
| 281 |
+
MAX_WEB_CONTENT_LENGTH = 500
|
| 282 |
if not text.strip():
|
| 283 |
return ""
|
| 284 |
+
|
| 285 |
+
prepared_text = to_ascii(text.strip())
|
| 286 |
+
if len(prepared_text) > MAX_WEB_CONTENT_LENGTH:
|
| 287 |
+
prepared_text = prepared_text[:MAX_WEB_CONTENT_LENGTH] + "..."
|
| 288 |
+
|
| 289 |
+
return prepared_text
|
|
|
|
| 290 |
|
| 291 |
# ------------------------
|
| 292 |
# Streaming Generator
|
|
|
|
| 342 |
if is_search:
|
| 343 |
query = WEB_TAG_RE.sub("", user_message).strip()
|
| 344 |
raw = perform_web_search(query)
|
| 345 |
+
# --- MODIFIED: Use prepare_web_content to retain more raw search text ---
|
| 346 |
+
search_summary = prepare_web_content(raw)
|
| 347 |
+
# ------------------------------------------------------------------------
|
| 348 |
|
| 349 |
+
prefix = f"Based on web context: {search_summary}. " if search_summary else ""
|
| 350 |
full_prompt = f"{SYSTEM_INSTRUCTION} {prefix}{user_message}"
|
| 351 |
|
| 352 |
current_state = train_input_state.copy()
|
|
|
|
| 358 |
|
| 359 |
response_stream = generate_response(full_prompt)
|
| 360 |
partial_reply = ""
|
| 361 |
+
search_status = f"π Used web context (retaining up to 500 chars): {search_summary}" if search_summary else "No web info used."
|
| 362 |
|
| 363 |
if not history or (history and history[-1][1] is not None):
|
| 364 |
history.append([user_message, None])
|
|
|
|
| 380 |
# Gradio Interface
|
| 381 |
# ------------------------
|
| 382 |
with gr.Blocks(title="OkemoLLM") as demo:
|
| 383 |
+
gr.Markdown("# π OkemoLLM β Streaming DuckDuckGo + Increased Web Retention")
|
| 384 |
status = gr.Markdown(f"Ready. Current Checkpoint Iteration: **{start_iteration}**")
|
| 385 |
chat = gr.Chatbot(height=400)
|
| 386 |
|
|
|
|
| 392 |
|
| 393 |
gr.Markdown("---")
|
| 394 |
gr.Markdown("## βοΈ Feedback-Based Training (Learn from Web Context)")
|
| 395 |
+
gr.Markdown("**The training input automatically includes the web context (up to 500 chars) and your original prompt.**")
|
| 396 |
|
| 397 |
with gr.Column():
|
| 398 |
# This displays the full input context (System + Web Summary + User Prompt)
|
| 399 |
+
prompt_display = gr.Textbox(label="Full Training Input (System + Web Context + Prompt)", interactive=False)
|
| 400 |
last_response_display = gr.Textbox(label="Model's Last Response", interactive=False)
|
| 401 |
# This will be the Ground Truth/Target for training
|
| 402 |
target_response = gr.Textbox(label="Desired Response (Ground Truth)", placeholder='Enter the correct/better response for the prompt above.', scale=1)
|