Spaces:
Running
Running
Remove research template fields and simplify toggle functionality
Browse files- Remove Role and Purpose, Intended Audience, Key Tasks, and Additional Context fields
- Simplify research assistant toggle to only handle system prompt and dynamic URLs
- Remove field change handlers and helper functions for deleted fields
- Update function signatures and event handlers
- Add comprehensive debugging for preview chat response issues
app.py
CHANGED
@@ -605,7 +605,7 @@ def process_documents(files, current_rag_tool):
|
|
605 |
|
606 |
try:
|
607 |
# Initialize RAG tool if not exists
|
608 |
-
if not current_rag_tool:
|
609 |
current_rag_tool = RAGTool()
|
610 |
|
611 |
# Process files
|
@@ -742,9 +742,9 @@ def on_preview_combined(name, description, system_prompt, enable_research_assist
|
|
742 |
}
|
743 |
|
744 |
# Generate preview displays
|
745 |
-
preview_text = f"""**Preview
|
746 |
-
|
747 |
-
Your assistant "{name}" is configured and ready to test.
|
748 |
|
749 |
**Configuration:**
|
750 |
- **Model:** {model}
|
@@ -756,7 +756,7 @@ Your assistant "{name}" is configured and ready to test.
|
|
756 |
**System Prompt:**
|
757 |
{final_system_prompt[:200]}{'...' if len(final_system_prompt) > 200 else ''}
|
758 |
|
759 |
-
|
760 |
|
761 |
config_display = f"""### Current Configuration
|
762 |
|
@@ -906,6 +906,14 @@ def preview_chat_response(message, history, config_data, url1="", url2="", url3=
|
|
906 |
# Add current message
|
907 |
messages.append({"role": "user", "content": message})
|
908 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
909 |
# Make API request to OpenRouter
|
910 |
response = requests.post(
|
911 |
url="https://openrouter.ai/api/v1/chat/completions",
|
@@ -913,20 +921,35 @@ def preview_chat_response(message, history, config_data, url1="", url2="", url3=
|
|
913 |
"Authorization": f"Bearer {api_key}",
|
914 |
"Content-Type": "application/json"
|
915 |
},
|
916 |
-
json=
|
917 |
-
|
918 |
-
"messages": messages,
|
919 |
-
"temperature": config_data.get('temperature', 0.7),
|
920 |
-
"max_tokens": config_data.get('max_tokens', 500)
|
921 |
-
}
|
922 |
)
|
923 |
|
924 |
if response.status_code == 200:
|
925 |
-
|
926 |
-
|
927 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
928 |
else:
|
929 |
-
assistant_response = f"[Preview Error] API Error: {response.status_code} - {response.text}"
|
930 |
|
931 |
except Exception as e:
|
932 |
assistant_response = f"[Preview Error] {str(e)}"
|
@@ -954,7 +977,7 @@ def export_preview_conversation(history):
|
|
954 |
|
955 |
return gr.update(value=temp_file, visible=True)
|
956 |
|
957 |
-
def on_generate(name, description, system_prompt, enable_research_assistant,
|
958 |
if not name or not name.strip():
|
959 |
return gr.update(value="Error: Please provide a Space Title", visible=True), gr.update(visible=False)
|
960 |
|
@@ -1198,63 +1221,21 @@ def remove_chat_urls(count):
|
|
1198 |
else:
|
1199 |
return (gr.update(), gr.update(), gr.update(), gr.update(), count)
|
1200 |
|
1201 |
-
|
1202 |
-
|
|
|
1203 |
if enable_research:
|
1204 |
combined_prompt = "You are a search tool that provides link-grounded information through web fetching, limiting source criteria to DOI-verified articles from academic databases and official sources. Use https://libkey.io/ to cross-reference and validate article DOIs for inclusion. This tool is designed for students and researchers conducting academic inquiry. Additional responsibilities include analyzing academic sources, fact-checking claims with evidence, providing properly cited research summaries, and helping users navigate scholarly information. Ground all responses in provided URL contexts and any additional URLs you're instructed to fetch. Never rely on memory for factual claims."
|
1205 |
return (
|
1206 |
-
gr.update(visible=True), # Show research detailed fields
|
1207 |
gr.update(value=combined_prompt), # Update main system prompt
|
1208 |
-
gr.update(value=
|
1209 |
-
gr.update(value="This tool is designed for students and researchers conducting academic inquiry."),
|
1210 |
-
gr.update(value="Analyze academic sources, fact-check claims with evidence, provide properly cited research summaries, and help users navigate scholarly information."),
|
1211 |
-
gr.update(value="Ground all responses in provided URL contexts and any additional URLs you're instructed to fetch. Never rely on memory for factual claims."),
|
1212 |
-
gr.update(value=True), # Enable dynamic URL fetching for research template
|
1213 |
-
gr.update(visible=False), # Hide custom categories fields
|
1214 |
-
gr.update(value=False) # Disable custom categories checkbox
|
1215 |
)
|
1216 |
else:
|
1217 |
return (
|
1218 |
-
gr.update(visible=False), # Hide research detailed fields
|
1219 |
gr.update(value=""), # Clear main system prompt when disabling
|
1220 |
-
gr.update(value=
|
1221 |
-
gr.update(value=""), # Clear audience field
|
1222 |
-
gr.update(value=""), # Clear tasks field
|
1223 |
-
gr.update(value=""), # Clear context field
|
1224 |
-
gr.update(value=False), # Disable dynamic URL setting
|
1225 |
-
gr.update(visible=False), # Hide custom fields when research is disabled
|
1226 |
-
gr.update() # Keep custom categories checkbox as is
|
1227 |
)
|
1228 |
|
1229 |
-
def update_system_prompt_from_fields(role_purpose, intended_audience, key_tasks, additional_context):
|
1230 |
-
"""Update the main system prompt field when research assistant fields change"""
|
1231 |
-
parts = []
|
1232 |
-
if role_purpose and role_purpose.strip():
|
1233 |
-
parts.append(role_purpose.strip())
|
1234 |
-
if intended_audience and intended_audience.strip():
|
1235 |
-
parts.append(intended_audience.strip())
|
1236 |
-
if key_tasks and key_tasks.strip():
|
1237 |
-
parts.append(key_tasks.strip())
|
1238 |
-
if additional_context and additional_context.strip():
|
1239 |
-
parts.append(additional_context.strip())
|
1240 |
-
|
1241 |
-
combined = " ".join(parts)
|
1242 |
-
return gr.update(value=combined)
|
1243 |
-
|
1244 |
-
def toggle_custom_categories(enable_custom, current_research_enabled):
|
1245 |
-
"""Toggle visibility of custom categories fields"""
|
1246 |
-
if enable_custom:
|
1247 |
-
return (
|
1248 |
-
gr.update(visible=True), # Show custom categories fields
|
1249 |
-
gr.update(visible=False), # Hide research assistant fields
|
1250 |
-
gr.update(value=False) # Disable research assistant checkbox
|
1251 |
-
)
|
1252 |
-
else:
|
1253 |
-
return (
|
1254 |
-
gr.update(visible=False), # Hide custom categories fields
|
1255 |
-
gr.update(visible=False), # Hide research fields when custom is disabled
|
1256 |
-
gr.update() # Keep research assistant checkbox as is
|
1257 |
-
)
|
1258 |
|
1259 |
# Create Gradio interface with proper tab structure
|
1260 |
with gr.Blocks(title="Chat U/I Helper") as demo:
|
@@ -1320,91 +1301,13 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1320 |
)
|
1321 |
|
1322 |
# Assistant configuration options
|
1323 |
-
|
1324 |
-
|
1325 |
-
|
1326 |
-
|
1327 |
-
|
1328 |
-
)
|
1329 |
-
|
1330 |
-
enable_custom_categories = gr.Checkbox(
|
1331 |
-
label="Use Custom Categories",
|
1332 |
-
value=False,
|
1333 |
-
info="Enable structured fields for defining your assistant"
|
1334 |
-
)
|
1335 |
|
1336 |
-
# Detailed fields for research assistant (initially hidden)
|
1337 |
-
with gr.Column(visible=False) as research_detailed_fields:
|
1338 |
-
gr.Markdown("*The system prompt above will be automatically populated with these fields when enabled*")
|
1339 |
-
|
1340 |
-
role_purpose = gr.Textbox(
|
1341 |
-
label="Role and Purpose",
|
1342 |
-
placeholder="You are a research assistant that...",
|
1343 |
-
lines=2,
|
1344 |
-
value="",
|
1345 |
-
info="Define what the assistant is and its primary function"
|
1346 |
-
)
|
1347 |
-
|
1348 |
-
intended_audience = gr.Textbox(
|
1349 |
-
label="Intended Audience",
|
1350 |
-
placeholder="This assistant is designed for undergraduate students...",
|
1351 |
-
lines=2,
|
1352 |
-
value="",
|
1353 |
-
info="Specify who will be using this assistant and their context"
|
1354 |
-
)
|
1355 |
-
|
1356 |
-
key_tasks = gr.Textbox(
|
1357 |
-
label="Key Tasks",
|
1358 |
-
placeholder="Your main responsibilities include...",
|
1359 |
-
lines=3,
|
1360 |
-
value="",
|
1361 |
-
info="List the specific tasks and capabilities the assistant should focus on"
|
1362 |
-
)
|
1363 |
-
|
1364 |
-
additional_context = gr.Textbox(
|
1365 |
-
label="Additional Context",
|
1366 |
-
placeholder="Remember to always...",
|
1367 |
-
lines=2,
|
1368 |
-
value="",
|
1369 |
-
info="Any additional instructions, constraints, or behavioral guidelines"
|
1370 |
-
)
|
1371 |
|
1372 |
-
# Custom categories fields (initially hidden)
|
1373 |
-
with gr.Column(visible=False) as custom_categories_fields:
|
1374 |
-
gr.Markdown("#### Custom Assistant Categories")
|
1375 |
-
gr.Markdown("*The system prompt above will be automatically populated with these fields when enabled*")
|
1376 |
-
|
1377 |
-
custom_role_purpose = gr.Textbox(
|
1378 |
-
label="Role and Purpose",
|
1379 |
-
placeholder="Define what the assistant is and its primary function",
|
1380 |
-
lines=2,
|
1381 |
-
value="",
|
1382 |
-
info="Define what the assistant is and its primary function"
|
1383 |
-
)
|
1384 |
-
|
1385 |
-
custom_intended_audience = gr.Textbox(
|
1386 |
-
label="Intended Audience",
|
1387 |
-
placeholder="Specify who will be using this assistant and their context",
|
1388 |
-
lines=2,
|
1389 |
-
value="",
|
1390 |
-
info="Specify who will be using this assistant and their context"
|
1391 |
-
)
|
1392 |
-
|
1393 |
-
custom_key_tasks = gr.Textbox(
|
1394 |
-
label="Key Tasks",
|
1395 |
-
placeholder="List the specific tasks and capabilities the assistant should focus on",
|
1396 |
-
lines=3,
|
1397 |
-
value="",
|
1398 |
-
info="List the specific tasks and capabilities the assistant should focus on"
|
1399 |
-
)
|
1400 |
-
|
1401 |
-
custom_additional_context = gr.Textbox(
|
1402 |
-
label="Additional Context",
|
1403 |
-
placeholder="Any additional instructions, constraints, or behavioral guidelines",
|
1404 |
-
lines=2,
|
1405 |
-
value="",
|
1406 |
-
info="Any additional instructions, constraints, or behavioral guidelines"
|
1407 |
-
)
|
1408 |
|
1409 |
examples_text = gr.Textbox(
|
1410 |
label="Example Prompts (one per line)",
|
@@ -1504,35 +1407,16 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1504 |
status = gr.Markdown(visible=False)
|
1505 |
download_file = gr.File(label="Download your zip package", visible=False)
|
1506 |
|
|
|
|
|
1507 |
# Connect the research assistant checkbox
|
1508 |
enable_research_assistant.change(
|
1509 |
toggle_research_assistant,
|
1510 |
-
inputs=[enable_research_assistant
|
1511 |
-
outputs=[
|
1512 |
)
|
1513 |
|
1514 |
-
# Connect the custom categories checkbox
|
1515 |
-
enable_custom_categories.change(
|
1516 |
-
toggle_custom_categories,
|
1517 |
-
inputs=[enable_custom_categories, enable_research_assistant],
|
1518 |
-
outputs=[custom_categories_fields, research_detailed_fields, enable_research_assistant]
|
1519 |
-
)
|
1520 |
|
1521 |
-
# Connect research assistant fields to update main system prompt
|
1522 |
-
for field in [role_purpose, intended_audience, key_tasks, additional_context]:
|
1523 |
-
field.change(
|
1524 |
-
update_system_prompt_from_fields,
|
1525 |
-
inputs=[role_purpose, intended_audience, key_tasks, additional_context],
|
1526 |
-
outputs=[system_prompt]
|
1527 |
-
)
|
1528 |
-
|
1529 |
-
# Connect custom categories fields to update main system prompt
|
1530 |
-
for field in [custom_role_purpose, custom_intended_audience, custom_key_tasks, custom_additional_context]:
|
1531 |
-
field.change(
|
1532 |
-
update_system_prompt_from_fields,
|
1533 |
-
inputs=[custom_role_purpose, custom_intended_audience, custom_key_tasks, custom_additional_context],
|
1534 |
-
outputs=[system_prompt]
|
1535 |
-
)
|
1536 |
|
1537 |
# Connect the URL management buttons
|
1538 |
add_url_btn.click(
|
@@ -1564,12 +1448,12 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1564 |
# Connect the generate button
|
1565 |
generate_btn.click(
|
1566 |
on_generate,
|
1567 |
-
inputs=[name, description, system_prompt, enable_research_assistant,
|
1568 |
outputs=[status, download_file, sandbox_state]
|
1569 |
)
|
1570 |
|
1571 |
|
1572 |
-
with gr.Tab("
|
1573 |
gr.Markdown("# Sandbox Preview")
|
1574 |
gr.Markdown("Preview your generated assistant exactly as it will appear in the deployed HuggingFace Space.")
|
1575 |
|
@@ -1676,7 +1560,7 @@ with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
1676 |
outputs=[preview_url3, preview_url4, preview_add_url_btn, preview_remove_url_btn, preview_url_count]
|
1677 |
)
|
1678 |
|
1679 |
-
with gr.Tab("Support
|
1680 |
create_support_docs()
|
1681 |
|
1682 |
# Connect cross-tab functionality after all components are defined
|
|
|
605 |
|
606 |
try:
|
607 |
# Initialize RAG tool if not exists
|
608 |
+
if not current_rag_tool and RAGTool is not None:
|
609 |
current_rag_tool = RAGTool()
|
610 |
|
611 |
# Process files
|
|
|
742 |
}
|
743 |
|
744 |
# Generate preview displays
|
745 |
+
preview_text = f"""🎉 **Preview Successfully Rendered!**
|
746 |
+
|
747 |
+
Your assistant "{name}" is now configured and ready to test in the Sandbox Preview tab.
|
748 |
|
749 |
**Configuration:**
|
750 |
- **Model:** {model}
|
|
|
756 |
**System Prompt:**
|
757 |
{final_system_prompt[:200]}{'...' if len(final_system_prompt) > 200 else ''}
|
758 |
|
759 |
+
✨ **Next Steps:** Switch to the "Sandbox Preview" tab to test your assistant with real conversations before generating the deployment package."""
|
760 |
|
761 |
config_display = f"""### Current Configuration
|
762 |
|
|
|
906 |
# Add current message
|
907 |
messages.append({"role": "user", "content": message})
|
908 |
|
909 |
+
# Debug: Log the request being sent
|
910 |
+
request_payload = {
|
911 |
+
"model": config_data.get('model', 'google/gemini-2.0-flash-001'),
|
912 |
+
"messages": messages,
|
913 |
+
"temperature": config_data.get('temperature', 0.7),
|
914 |
+
"max_tokens": config_data.get('max_tokens', 500)
|
915 |
+
}
|
916 |
+
|
917 |
# Make API request to OpenRouter
|
918 |
response = requests.post(
|
919 |
url="https://openrouter.ai/api/v1/chat/completions",
|
|
|
921 |
"Authorization": f"Bearer {api_key}",
|
922 |
"Content-Type": "application/json"
|
923 |
},
|
924 |
+
json=request_payload,
|
925 |
+
timeout=30
|
|
|
|
|
|
|
|
|
926 |
)
|
927 |
|
928 |
if response.status_code == 200:
|
929 |
+
try:
|
930 |
+
response_data = response.json()
|
931 |
+
|
932 |
+
# Check if response has expected structure
|
933 |
+
if 'choices' not in response_data or not response_data['choices']:
|
934 |
+
assistant_response = f"[Preview Debug] No choices in API response. Response: {response_data}"
|
935 |
+
elif 'message' not in response_data['choices'][0]:
|
936 |
+
assistant_response = f"[Preview Debug] No message in first choice. Response: {response_data}"
|
937 |
+
elif 'content' not in response_data['choices'][0]['message']:
|
938 |
+
assistant_response = f"[Preview Debug] No content in message. Response: {response_data}"
|
939 |
+
else:
|
940 |
+
assistant_content = response_data['choices'][0]['message']['content']
|
941 |
+
|
942 |
+
# Debug: Check if content is empty
|
943 |
+
if not assistant_content or assistant_content.strip() == "":
|
944 |
+
assistant_response = f"[Preview Debug] Empty content from API. Messages sent: {len(messages)} messages, last user message: '{message}', model: {request_payload['model']}"
|
945 |
+
else:
|
946 |
+
# Add preview indicator
|
947 |
+
assistant_response = f"[Preview Mode] {assistant_content}"
|
948 |
+
|
949 |
+
except (KeyError, IndexError, json.JSONDecodeError) as e:
|
950 |
+
assistant_response = f"[Preview Error] Failed to parse API response: {str(e)}. Raw response: {response.text[:500]}"
|
951 |
else:
|
952 |
+
assistant_response = f"[Preview Error] API Error: {response.status_code} - {response.text[:500]}"
|
953 |
|
954 |
except Exception as e:
|
955 |
assistant_response = f"[Preview Error] {str(e)}"
|
|
|
977 |
|
978 |
return gr.update(value=temp_file, visible=True)
|
979 |
|
980 |
+
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):
|
981 |
if not name or not name.strip():
|
982 |
return gr.update(value="Error: Please provide a Space Title", visible=True), gr.update(visible=False)
|
983 |
|
|
|
1221 |
else:
|
1222 |
return (gr.update(), gr.update(), gr.update(), gr.update(), count)
|
1223 |
|
1224 |
+
|
1225 |
+
def toggle_research_assistant(enable_research):
|
1226 |
+
"""Toggle research assistant system prompt"""
|
1227 |
if enable_research:
|
1228 |
combined_prompt = "You are a search tool that provides link-grounded information through web fetching, limiting source criteria to DOI-verified articles from academic databases and official sources. Use https://libkey.io/ to cross-reference and validate article DOIs for inclusion. This tool is designed for students and researchers conducting academic inquiry. Additional responsibilities include analyzing academic sources, fact-checking claims with evidence, providing properly cited research summaries, and helping users navigate scholarly information. Ground all responses in provided URL contexts and any additional URLs you're instructed to fetch. Never rely on memory for factual claims."
|
1229 |
return (
|
|
|
1230 |
gr.update(value=combined_prompt), # Update main system prompt
|
1231 |
+
gr.update(value=True) # Enable dynamic URL fetching for research template
|
|
|
|
|
|
|
|
|
|
|
|
|
1232 |
)
|
1233 |
else:
|
1234 |
return (
|
|
|
1235 |
gr.update(value=""), # Clear main system prompt when disabling
|
1236 |
+
gr.update(value=False) # Disable dynamic URL setting
|
|
|
|
|
|
|
|
|
|
|
|
|
1237 |
)
|
1238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1239 |
|
1240 |
# Create Gradio interface with proper tab structure
|
1241 |
with gr.Blocks(title="Chat U/I Helper") as demo:
|
|
|
1301 |
)
|
1302 |
|
1303 |
# Assistant configuration options
|
1304 |
+
enable_research_assistant = gr.Checkbox(
|
1305 |
+
label="Research Template",
|
1306 |
+
value=False,
|
1307 |
+
info="Enable to use pre-configured research assistant settings"
|
1308 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1311 |
|
1312 |
examples_text = gr.Textbox(
|
1313 |
label="Example Prompts (one per line)",
|
|
|
1407 |
status = gr.Markdown(visible=False)
|
1408 |
download_file = gr.File(label="Download your zip package", visible=False)
|
1409 |
|
1410 |
+
|
1411 |
+
|
1412 |
# Connect the research assistant checkbox
|
1413 |
enable_research_assistant.change(
|
1414 |
toggle_research_assistant,
|
1415 |
+
inputs=[enable_research_assistant],
|
1416 |
+
outputs=[system_prompt, enable_dynamic_urls]
|
1417 |
)
|
1418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
1419 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1420 |
|
1421 |
# Connect the URL management buttons
|
1422 |
add_url_btn.click(
|
|
|
1448 |
# Connect the generate button
|
1449 |
generate_btn.click(
|
1450 |
on_generate,
|
1451 |
+
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],
|
1452 |
outputs=[status, download_file, sandbox_state]
|
1453 |
)
|
1454 |
|
1455 |
|
1456 |
+
with gr.Tab("Preview"):
|
1457 |
gr.Markdown("# Sandbox Preview")
|
1458 |
gr.Markdown("Preview your generated assistant exactly as it will appear in the deployed HuggingFace Space.")
|
1459 |
|
|
|
1560 |
outputs=[preview_url3, preview_url4, preview_add_url_btn, preview_remove_url_btn, preview_url_count]
|
1561 |
)
|
1562 |
|
1563 |
+
with gr.Tab("Support"):
|
1564 |
create_support_docs()
|
1565 |
|
1566 |
# Connect cross-tab functionality after all components are defined
|