Spaces:
Running
Running
milwright
commited on
Commit
·
be0e3cc
1
Parent(s):
a3853b4
Remove title and description fields from UI and deployment package
Browse files
app.py
CHANGED
|
@@ -713,137 +713,14 @@ def get_grounding_context(urls):
|
|
| 713 |
return "\n\n" + "\n\n".join(context_parts) + "\n\n"
|
| 714 |
return ""
|
| 715 |
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
# Ensure short_description is a proper string and within HF's 60-character limit
|
| 719 |
-
description = config.get('description', '') or 'AI chat interface'
|
| 720 |
-
if len(description) > 60:
|
| 721 |
-
short_desc = description[:57] + '...' # 57 + 3 = 60 characters total
|
| 722 |
-
else:
|
| 723 |
-
short_desc = description
|
| 724 |
-
|
| 725 |
-
return f"""---
|
| 726 |
-
title: {config['name']}
|
| 727 |
-
emoji: 🤖
|
| 728 |
-
colorFrom: blue
|
| 729 |
-
colorTo: red
|
| 730 |
-
sdk: gradio
|
| 731 |
-
sdk_version: 5.38.0
|
| 732 |
-
app_file: app.py
|
| 733 |
-
pinned: false
|
| 734 |
-
license: mit
|
| 735 |
-
short_description: "{short_desc}"
|
| 736 |
-
---
|
| 737 |
-
|
| 738 |
-
# {config['name']}
|
| 739 |
-
|
| 740 |
-
{config['description']}
|
| 741 |
-
|
| 742 |
-
## Quick Deploy to HuggingFace Spaces
|
| 743 |
-
|
| 744 |
-
### Step 1: Create the Space
|
| 745 |
-
1. Go to https://huggingface.co/spaces
|
| 746 |
-
2. Click "Create new Space"
|
| 747 |
-
3. Choose a name for your Space
|
| 748 |
-
4. Select **Gradio** as the SDK
|
| 749 |
-
5. Set visibility (Public/Private)
|
| 750 |
-
6. Click "Create Space"
|
| 751 |
-
|
| 752 |
-
### Step 2: Upload Files
|
| 753 |
-
1. In your new Space, click "Files" tab
|
| 754 |
-
2. Upload these files from the zip:
|
| 755 |
-
- `app.py`
|
| 756 |
-
- `requirements.txt`
|
| 757 |
-
3. Wait for "Building" to complete
|
| 758 |
-
|
| 759 |
-
### Step 3: Add API Key
|
| 760 |
-
1. Go to Settings (gear icon)
|
| 761 |
-
2. Click "Variables and secrets"
|
| 762 |
-
3. Click "New secret"
|
| 763 |
-
4. Name: `{config['api_key_var']}`
|
| 764 |
-
5. Value: Your OpenRouter API key
|
| 765 |
-
6. Click "Add"
|
| 766 |
-
|
| 767 |
-
{f'''### Step 4: Configure Access Control
|
| 768 |
-
Your Space is configured with access code protection. Students will need to enter the access code to use the chatbot.
|
| 769 |
-
|
| 770 |
-
1. Go to Settings (gear icon)
|
| 771 |
-
2. Click "Variables and secrets"
|
| 772 |
-
3. Click "New secret"
|
| 773 |
-
4. Name: `SPACE_ACCESS_CODE`
|
| 774 |
-
5. Value: `{config['access_code']}`
|
| 775 |
-
6. Click "Add"
|
| 776 |
-
|
| 777 |
-
**Important**: The access code is now stored securely as an environment variable and is not visible in your app code.
|
| 778 |
-
|
| 779 |
-
To disable access protection:
|
| 780 |
-
1. Go to Settings → Variables and secrets
|
| 781 |
-
2. Delete the `SPACE_ACCESS_CODE` secret
|
| 782 |
-
3. The Space will rebuild automatically with no access protection
|
| 783 |
-
|
| 784 |
-
''' if config['access_code'] else ''}
|
| 785 |
-
|
| 786 |
-
### Step {4 if not config['access_code'] else 5}: Get Your API Key
|
| 787 |
-
1. Go to https://openrouter.ai/keys
|
| 788 |
-
2. Sign up/login if needed
|
| 789 |
-
3. Click "Create Key"
|
| 790 |
-
4. Copy the key (starts with `sk-or-`)
|
| 791 |
-
|
| 792 |
-
### Step {5 if not config['access_code'] else 6}: Test Your Space
|
| 793 |
-
- Go back to "App" tab
|
| 794 |
-
- Your Space should be running!
|
| 795 |
-
- Try the example prompts or ask a question
|
| 796 |
-
|
| 797 |
-
## Configuration
|
| 798 |
-
|
| 799 |
-
- **Model**: {config['model']}
|
| 800 |
-
- **Temperature**: {config['temperature']}
|
| 801 |
-
- **Max Tokens**: {config['max_tokens']}
|
| 802 |
-
- **API Key Variable**: {config['api_key_var']}"""
|
| 803 |
-
|
| 804 |
-
# Add optional configuration items
|
| 805 |
-
if config['access_code']:
|
| 806 |
-
readme_content += f"""
|
| 807 |
-
- **Access Code**: {config['access_code']} (Students need this to access the chatbot)"""
|
| 808 |
-
|
| 809 |
-
if config.get('enable_dynamic_urls'):
|
| 810 |
-
readme_content += """
|
| 811 |
-
- **Dynamic URL Fetching**: Enabled (Assistant can fetch URLs mentioned in conversations)"""
|
| 812 |
-
|
| 813 |
-
readme_content += f"""
|
| 814 |
-
|
| 815 |
-
## Customization
|
| 816 |
-
|
| 817 |
-
To modify your Space:
|
| 818 |
-
1. Edit `app.py` in your Space
|
| 819 |
-
2. Update configuration variables at the top
|
| 820 |
-
3. Changes deploy automatically
|
| 821 |
-
|
| 822 |
-
## Troubleshooting
|
| 823 |
-
|
| 824 |
-
- **"Please set your {config['api_key_var']}"**: Add the secret in Space settings
|
| 825 |
-
- **Error 401**: Invalid API key or no credits
|
| 826 |
-
- **Error 429**: Rate limit - wait and try again
|
| 827 |
-
- **Build failed**: Check requirements.txt formatting
|
| 828 |
-
|
| 829 |
-
## More Help
|
| 830 |
-
|
| 831 |
-
- HuggingFace Spaces: https://huggingface.co/docs/hub/spaces
|
| 832 |
-
- OpenRouter Docs: https://openrouter.ai/docs
|
| 833 |
-
- Gradio Docs: https://gradio.app/docs
|
| 834 |
-
|
| 835 |
-
---
|
| 836 |
-
|
| 837 |
-
Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} with Chat U/I Helper
|
| 838 |
-
"""
|
| 839 |
-
|
| 840 |
-
return readme_content
|
| 841 |
|
| 842 |
def create_requirements():
|
| 843 |
"""Generate requirements.txt with latest versions"""
|
| 844 |
return "gradio>=5.38.0\nrequests>=2.32.3\nbeautifulsoup4>=4.12.3\npython-dotenv>=1.0.0"
|
| 845 |
|
| 846 |
-
def generate_zip(
|
| 847 |
"""Generate deployable zip file"""
|
| 848 |
|
| 849 |
# Process examples
|
|
@@ -867,8 +744,6 @@ def generate_zip(name, description, system_prompt, model, api_key_var, temperatu
|
|
| 867 |
|
| 868 |
# Create config
|
| 869 |
config = {
|
| 870 |
-
'name': name,
|
| 871 |
-
'description': description,
|
| 872 |
'system_prompt': system_prompt,
|
| 873 |
'model': model,
|
| 874 |
'api_key_var': api_key_var,
|
|
@@ -881,21 +756,16 @@ def generate_zip(name, description, system_prompt, model, api_key_var, temperatu
|
|
| 881 |
|
| 882 |
# Generate files
|
| 883 |
app_content = SPACE_TEMPLATE.format(**config)
|
| 884 |
-
# Pass empty access_code to README since user will configure it in HF Spaces
|
| 885 |
-
readme_config = config.copy()
|
| 886 |
-
readme_config['access_code'] = "" # Always empty since user configures in HF Spaces
|
| 887 |
-
readme_content = create_readme(readme_config)
|
| 888 |
requirements_content = create_requirements()
|
| 889 |
|
| 890 |
# Create zip file with clean naming
|
| 891 |
-
filename =
|
| 892 |
|
| 893 |
# Create zip in memory and save to disk
|
| 894 |
zip_buffer = io.BytesIO()
|
| 895 |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
| 896 |
zip_file.writestr('app.py', app_content)
|
| 897 |
zip_file.writestr('requirements.txt', requirements_content)
|
| 898 |
-
zip_file.writestr('README.md', readme_content)
|
| 899 |
zip_file.writestr('config.json', json.dumps(config, indent=2))
|
| 900 |
|
| 901 |
# Write zip to file
|
|
@@ -951,20 +821,9 @@ def update_sandbox_preview(config_data):
|
|
| 951 |
|
| 952 |
return preview_text, preview_html
|
| 953 |
|
| 954 |
-
def on_preview_combined(
|
| 955 |
"""Generate configuration and return preview updates"""
|
| 956 |
-
|
| 957 |
-
return (
|
| 958 |
-
{},
|
| 959 |
-
gr.update(value="**Error:** Please provide a Space Title to preview", visible=True),
|
| 960 |
-
gr.update(visible=False),
|
| 961 |
-
gr.update(value="Configuration will appear here after preview generation."),
|
| 962 |
-
*[gr.update() for _ in range(10)], # 10 URL updates
|
| 963 |
-
gr.update(), # preview_add_url_btn
|
| 964 |
-
gr.update(), # preview_remove_url_btn
|
| 965 |
-
2, # preview_url_count
|
| 966 |
-
*[gr.update(visible=False) for _ in range(3)] # 3 example button updates
|
| 967 |
-
)
|
| 968 |
|
| 969 |
try:
|
| 970 |
# Use the system prompt directly (template selector already updates it)
|
|
@@ -1303,9 +1162,8 @@ def export_preview_conversation(history, config_data=None):
|
|
| 1303 |
|
| 1304 |
return gr.update(value=temp_file, visible=True)
|
| 1305 |
|
| 1306 |
-
def on_generate(
|
| 1307 |
-
|
| 1308 |
-
return gr.update(value="Error: Please provide a Space Title", visible=True), gr.update(visible=False), {}
|
| 1309 |
|
| 1310 |
|
| 1311 |
try:
|
|
@@ -1315,7 +1173,7 @@ def on_generate(name, description, system_prompt, model, api_key_var, temperatur
|
|
| 1315 |
|
| 1316 |
final_system_prompt = system_prompt.strip()
|
| 1317 |
|
| 1318 |
-
filename = generate_zip(
|
| 1319 |
|
| 1320 |
success_msg = f"""**Deployment package ready!**
|
| 1321 |
|
|
@@ -1324,13 +1182,12 @@ def on_generate(name, description, system_prompt, model, api_key_var, temperatur
|
|
| 1324 |
**What's included:**
|
| 1325 |
- `app.py` - Ready-to-deploy chat interface (Gradio 5.38.0)
|
| 1326 |
- `requirements.txt` - Latest dependencies
|
| 1327 |
-
- `README.md` - HuggingFace Spaces configuration & instructions
|
| 1328 |
- `config.json` - Configuration backup
|
| 1329 |
|
| 1330 |
**Next steps:**
|
| 1331 |
1. Download the zip file below
|
| 1332 |
2. Go to https://huggingface.co/spaces and create a new Space
|
| 1333 |
-
3. Upload ALL files from the zip to your Space
|
| 1334 |
4. Set your `{api_key_var}` secret in Space settings
|
| 1335 |
|
| 1336 |
**Your Space will be live in minutes!**"""
|
|
@@ -1677,20 +1534,6 @@ with gr.Blocks(
|
|
| 1677 |
gr.Markdown("Convert custom assistants from HuggingChat into chat interfaces with HuggingFace Spaces. Configure and download everything needed to deploy a simple HF space using Gradio.")
|
| 1678 |
|
| 1679 |
with gr.Column():
|
| 1680 |
-
name = gr.Textbox(
|
| 1681 |
-
label="Space Title",
|
| 1682 |
-
placeholder="My Course Helper",
|
| 1683 |
-
value="My Custom Space"
|
| 1684 |
-
)
|
| 1685 |
-
|
| 1686 |
-
description = gr.Textbox(
|
| 1687 |
-
label="Space Description",
|
| 1688 |
-
placeholder="A customizable AI chat interface for...",
|
| 1689 |
-
lines=2,
|
| 1690 |
-
value="",
|
| 1691 |
-
max_lines=2,
|
| 1692 |
-
max_length=57
|
| 1693 |
-
)
|
| 1694 |
|
| 1695 |
model = gr.Dropdown(
|
| 1696 |
label="Model",
|
|
@@ -1897,7 +1740,7 @@ with gr.Blocks(
|
|
| 1897 |
# Connect the generate button
|
| 1898 |
generate_btn.click(
|
| 1899 |
on_generate,
|
| 1900 |
-
inputs=[
|
| 1901 |
outputs=[status, download_file, sandbox_state]
|
| 1902 |
)
|
| 1903 |
|
|
@@ -2095,7 +1938,7 @@ with gr.Blocks(
|
|
| 2095 |
# Connect cross-tab functionality after all components are defined
|
| 2096 |
preview_btn.click(
|
| 2097 |
on_preview_combined,
|
| 2098 |
-
inputs=[
|
| 2099 |
outputs=[preview_config_state, preview_status_comp, preview_chat_section_comp, config_display_comp, preview_url1, preview_url2, preview_url3, preview_url4, preview_url5, preview_url6, preview_url7, preview_url8, preview_url9, preview_url10, preview_add_url_btn, preview_remove_url_btn, preview_url_count, preview_example_btn1, preview_example_btn2, preview_example_btn3]
|
| 2100 |
)
|
| 2101 |
|
|
|
|
| 713 |
return "\n\n" + "\n\n".join(context_parts) + "\n\n"
|
| 714 |
return ""
|
| 715 |
|
| 716 |
+
# Removed create_readme function - no longer generating README.md
|
| 717 |
+
pass # Function removed - README.md no longer generated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
|
| 719 |
def create_requirements():
|
| 720 |
"""Generate requirements.txt with latest versions"""
|
| 721 |
return "gradio>=5.38.0\nrequests>=2.32.3\nbeautifulsoup4>=4.12.3\npython-dotenv>=1.0.0"
|
| 722 |
|
| 723 |
+
def generate_zip(system_prompt, model, api_key_var, temperature, max_tokens, examples_text, access_code_field="", enable_dynamic_urls=False, url1="", url2="", url3="", url4="", url5="", url6="", url7="", url8="", url9="", url10=""):
|
| 724 |
"""Generate deployable zip file"""
|
| 725 |
|
| 726 |
# Process examples
|
|
|
|
| 744 |
|
| 745 |
# Create config
|
| 746 |
config = {
|
|
|
|
|
|
|
| 747 |
'system_prompt': system_prompt,
|
| 748 |
'model': model,
|
| 749 |
'api_key_var': api_key_var,
|
|
|
|
| 756 |
|
| 757 |
# Generate files
|
| 758 |
app_content = SPACE_TEMPLATE.format(**config)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
requirements_content = create_requirements()
|
| 760 |
|
| 761 |
# Create zip file with clean naming
|
| 762 |
+
filename = "ai_assistant_space.zip"
|
| 763 |
|
| 764 |
# Create zip in memory and save to disk
|
| 765 |
zip_buffer = io.BytesIO()
|
| 766 |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
| 767 |
zip_file.writestr('app.py', app_content)
|
| 768 |
zip_file.writestr('requirements.txt', requirements_content)
|
|
|
|
| 769 |
zip_file.writestr('config.json', json.dumps(config, indent=2))
|
| 770 |
|
| 771 |
# Write zip to file
|
|
|
|
| 821 |
|
| 822 |
return preview_text, preview_html
|
| 823 |
|
| 824 |
+
def on_preview_combined(system_prompt, model, temperature, max_tokens, examples_text, enable_dynamic_urls, url1="", url2="", url3="", url4="", url5="", url6="", url7="", url8="", url9="", url10=""):
|
| 825 |
"""Generate configuration and return preview updates"""
|
| 826 |
+
# Removed name validation since title field no longer exists
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 827 |
|
| 828 |
try:
|
| 829 |
# Use the system prompt directly (template selector already updates it)
|
|
|
|
| 1162 |
|
| 1163 |
return gr.update(value=temp_file, visible=True)
|
| 1164 |
|
| 1165 |
+
def on_generate(system_prompt, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, url5, url6, url7, url8, url9, url10):
|
| 1166 |
+
# Removed name validation since title field no longer exists
|
|
|
|
| 1167 |
|
| 1168 |
|
| 1169 |
try:
|
|
|
|
| 1173 |
|
| 1174 |
final_system_prompt = system_prompt.strip()
|
| 1175 |
|
| 1176 |
+
filename = generate_zip(final_system_prompt, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, url5, url6, url7, url8, url9, url10)
|
| 1177 |
|
| 1178 |
success_msg = f"""**Deployment package ready!**
|
| 1179 |
|
|
|
|
| 1182 |
**What's included:**
|
| 1183 |
- `app.py` - Ready-to-deploy chat interface (Gradio 5.38.0)
|
| 1184 |
- `requirements.txt` - Latest dependencies
|
|
|
|
| 1185 |
- `config.json` - Configuration backup
|
| 1186 |
|
| 1187 |
**Next steps:**
|
| 1188 |
1. Download the zip file below
|
| 1189 |
2. Go to https://huggingface.co/spaces and create a new Space
|
| 1190 |
+
3. Upload ALL files from the zip to your Space
|
| 1191 |
4. Set your `{api_key_var}` secret in Space settings
|
| 1192 |
|
| 1193 |
**Your Space will be live in minutes!**"""
|
|
|
|
| 1534 |
gr.Markdown("Convert custom assistants from HuggingChat into chat interfaces with HuggingFace Spaces. Configure and download everything needed to deploy a simple HF space using Gradio.")
|
| 1535 |
|
| 1536 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1537 |
|
| 1538 |
model = gr.Dropdown(
|
| 1539 |
label="Model",
|
|
|
|
| 1740 |
# Connect the generate button
|
| 1741 |
generate_btn.click(
|
| 1742 |
on_generate,
|
| 1743 |
+
inputs=[system_prompt, model, api_key_var, temperature, max_tokens, examples_text, access_code, enable_dynamic_urls, url1, url2, url3, url4, url5, url6, url7, url8, url9, url10],
|
| 1744 |
outputs=[status, download_file, sandbox_state]
|
| 1745 |
)
|
| 1746 |
|
|
|
|
| 1938 |
# Connect cross-tab functionality after all components are defined
|
| 1939 |
preview_btn.click(
|
| 1940 |
on_preview_combined,
|
| 1941 |
+
inputs=[system_prompt, model, temperature, max_tokens, examples_text, enable_dynamic_urls, url1, url2, url3, url4, url5, url6, url7, url8, url9, url10],
|
| 1942 |
outputs=[preview_config_state, preview_status_comp, preview_chat_section_comp, config_display_comp, preview_url1, preview_url2, preview_url3, preview_url4, preview_url5, preview_url6, preview_url7, preview_url8, preview_url9, preview_url10, preview_add_url_btn, preview_remove_url_btn, preview_url_count, preview_example_btn1, preview_example_btn2, preview_example_btn3]
|
| 1943 |
)
|
| 1944 |
|