Spaces:
Running
Running
Add external HF Space integration for web search and code execution
Browse files- Enable dynamic URL fetching by default (hidden from UI)
- Add web search functionality using external HuggingFace Spaces
- Add code execution capability via external HF Spaces
- Implement automatic pattern detection for search and code requests
- Add graceful fallbacks when external spaces unavailable
- Integrate results into LLM context for enhanced responses
- Fix lambda function compatibility issue
app.py
CHANGED
@@ -100,7 +100,7 @@ def enhanced_fetch_url_content(url, enable_search_validation=False):
|
|
100 |
element.decompose()
|
101 |
|
102 |
# Extract main content preferentially
|
103 |
-
main_content = soup.find('main') or soup.find('article') or soup.find('div', class_=lambda x: x and 'content' in x.lower()
|
104 |
text = main_content.get_text()
|
105 |
|
106 |
# Enhanced text cleaning
|
@@ -895,7 +895,7 @@ def update_sandbox_preview(config_data):
|
|
895 |
|
896 |
return preview_text, preview_html
|
897 |
|
898 |
-
def on_preview_combined(name, description, system_prompt, enable_research_assistant, model, temperature, max_tokens, examples_text, enable_dynamic_urls, enable_vector_rag, enable_code_execution):
|
899 |
"""Generate configuration and return preview updates"""
|
900 |
if not name or not name.strip():
|
901 |
return (
|
@@ -928,6 +928,7 @@ def on_preview_combined(name, description, system_prompt, enable_research_assist
|
|
928 |
'enable_dynamic_urls': enable_dynamic_urls,
|
929 |
'enable_vector_rag': enable_vector_rag,
|
930 |
'enable_code_execution': enable_code_execution,
|
|
|
931 |
'examples_text': examples_text,
|
932 |
'preview_ready': True
|
933 |
}
|
@@ -1078,6 +1079,29 @@ def preview_chat_response(message, history, config_data, url1="", url2="", url3=
|
|
1078 |
if dynamic_context_parts:
|
1079 |
dynamic_context = "\n".join(dynamic_context_parts)
|
1080 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1081 |
# Check for code execution request if enabled
|
1082 |
code_execution_result = ""
|
1083 |
if config_data.get('enable_code_execution'):
|
@@ -1103,7 +1127,7 @@ def preview_chat_response(message, history, config_data, url1="", url2="", url3=
|
|
1103 |
break
|
1104 |
|
1105 |
# Build enhanced system prompt with all contexts
|
1106 |
-
enhanced_system_prompt = config_data.get('system_prompt', '') + grounding_context + rag_context + dynamic_context + code_execution_result
|
1107 |
|
1108 |
# Build messages array for the API
|
1109 |
messages = [{"role": "system", "content": enhanced_system_prompt}]
|
@@ -1193,7 +1217,7 @@ def export_preview_conversation(history):
|
|
1193 |
|
1194 |
return gr.update(value=temp_file, visible=True)
|
1195 |
|
1196 |
-
def on_generate(name, description, system_prompt, enable_research_assistant, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, enable_vector_rag, rag_tool_state, enable_code_execution):
|
1197 |
if not name or not name.strip():
|
1198 |
return gr.update(value="Error: Please provide a Space Title", visible=True), gr.update(visible=False)
|
1199 |
|
@@ -1707,6 +1731,13 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1707 |
outputs=[code_execution_space]
|
1708 |
)
|
1709 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1710 |
|
1711 |
|
1712 |
# Connect the URL management buttons
|
@@ -1739,7 +1770,7 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1739 |
# Connect the generate button
|
1740 |
generate_btn.click(
|
1741 |
on_generate,
|
1742 |
-
inputs=[name, description, system_prompt, enable_research_assistant, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, enable_vector_rag, rag_tool_state, enable_code_execution],
|
1743 |
outputs=[status, download_file, sandbox_state]
|
1744 |
)
|
1745 |
|
@@ -1857,7 +1888,7 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1857 |
# Connect cross-tab functionality after all components are defined
|
1858 |
preview_btn.click(
|
1859 |
on_preview_combined,
|
1860 |
-
inputs=[name, description, system_prompt, enable_research_assistant, model, temperature, max_tokens, examples_text, enable_dynamic_urls, enable_vector_rag, enable_code_execution],
|
1861 |
outputs=[preview_config_state, preview_status_comp, preview_chat_section_comp, config_display_comp]
|
1862 |
)
|
1863 |
|
|
|
100 |
element.decompose()
|
101 |
|
102 |
# Extract main content preferentially
|
103 |
+
main_content = soup.find('main') or soup.find('article') or soup.find('div', class_=lambda x: bool(x and 'content' in x.lower())) or soup
|
104 |
text = main_content.get_text()
|
105 |
|
106 |
# Enhanced text cleaning
|
|
|
895 |
|
896 |
return preview_text, preview_html
|
897 |
|
898 |
+
def on_preview_combined(name, description, system_prompt, enable_research_assistant, model, temperature, max_tokens, examples_text, enable_dynamic_urls, enable_vector_rag, enable_code_execution, enable_web_search):
|
899 |
"""Generate configuration and return preview updates"""
|
900 |
if not name or not name.strip():
|
901 |
return (
|
|
|
928 |
'enable_dynamic_urls': enable_dynamic_urls,
|
929 |
'enable_vector_rag': enable_vector_rag,
|
930 |
'enable_code_execution': enable_code_execution,
|
931 |
+
'enable_web_search': enable_web_search,
|
932 |
'examples_text': examples_text,
|
933 |
'preview_ready': True
|
934 |
}
|
|
|
1079 |
if dynamic_context_parts:
|
1080 |
dynamic_context = "\n".join(dynamic_context_parts)
|
1081 |
|
1082 |
+
# Check for web search request if enabled
|
1083 |
+
web_search_result = ""
|
1084 |
+
if config_data.get('enable_web_search'):
|
1085 |
+
# Simple patterns to detect search requests
|
1086 |
+
search_patterns = [
|
1087 |
+
r'search for\s+(.+)',
|
1088 |
+
r'find\s+(.+)',
|
1089 |
+
r'look up\s+(.+)',
|
1090 |
+
r'what is\s+(.+)',
|
1091 |
+
r'who is\s+(.+)',
|
1092 |
+
r'how to\s+(.+)',
|
1093 |
+
r'latest\s+(.+)',
|
1094 |
+
r'recent\s+(.+)'
|
1095 |
+
]
|
1096 |
+
|
1097 |
+
for pattern in search_patterns:
|
1098 |
+
match = re.search(pattern, message, re.IGNORECASE)
|
1099 |
+
if match:
|
1100 |
+
search_query = match.group(1).strip()
|
1101 |
+
search_result = perform_web_search(search_query, "Web search requested")
|
1102 |
+
web_search_result = f"\n\n{search_result}\n\n"
|
1103 |
+
break
|
1104 |
+
|
1105 |
# Check for code execution request if enabled
|
1106 |
code_execution_result = ""
|
1107 |
if config_data.get('enable_code_execution'):
|
|
|
1127 |
break
|
1128 |
|
1129 |
# Build enhanced system prompt with all contexts
|
1130 |
+
enhanced_system_prompt = config_data.get('system_prompt', '') + grounding_context + rag_context + dynamic_context + web_search_result + code_execution_result
|
1131 |
|
1132 |
# Build messages array for the API
|
1133 |
messages = [{"role": "system", "content": enhanced_system_prompt}]
|
|
|
1217 |
|
1218 |
return gr.update(value=temp_file, visible=True)
|
1219 |
|
1220 |
+
def on_generate(name, description, system_prompt, enable_research_assistant, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, enable_vector_rag, rag_tool_state, enable_code_execution, enable_web_search):
|
1221 |
if not name or not name.strip():
|
1222 |
return gr.update(value="Error: Please provide a Space Title", visible=True), gr.update(visible=False)
|
1223 |
|
|
|
1731 |
outputs=[code_execution_space]
|
1732 |
)
|
1733 |
|
1734 |
+
# Connect the web search checkbox
|
1735 |
+
enable_web_search.change(
|
1736 |
+
toggle_web_search,
|
1737 |
+
inputs=[enable_web_search],
|
1738 |
+
outputs=[web_search_space]
|
1739 |
+
)
|
1740 |
+
|
1741 |
|
1742 |
|
1743 |
# Connect the URL management buttons
|
|
|
1770 |
# Connect the generate button
|
1771 |
generate_btn.click(
|
1772 |
on_generate,
|
1773 |
+
inputs=[name, description, system_prompt, enable_research_assistant, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, enable_vector_rag, rag_tool_state, enable_code_execution, enable_web_search],
|
1774 |
outputs=[status, download_file, sandbox_state]
|
1775 |
)
|
1776 |
|
|
|
1888 |
# Connect cross-tab functionality after all components are defined
|
1889 |
preview_btn.click(
|
1890 |
on_preview_combined,
|
1891 |
+
inputs=[name, description, system_prompt, enable_research_assistant, model, temperature, max_tokens, examples_text, enable_dynamic_urls, enable_vector_rag, enable_code_execution, enable_web_search],
|
1892 |
outputs=[preview_config_state, preview_status_comp, preview_chat_section_comp, config_display_comp]
|
1893 |
)
|
1894 |
|