Spaces:
Running
Running
# Config Editor Component for Deployed Spaces | |
# This can be integrated into the deployed app.py | |
import json | |
import gradio as gr | |
import os | |
def load_config(): | |
"""Load configuration from config.json""" | |
try: | |
with open('config.json', 'r') as f: | |
return json.load(f) | |
except Exception as e: | |
return {"error": f"Failed to load config: {str(e)}"} | |
def save_config(system_prompt, temperature, max_tokens, examples_text, grounding_urls_text): | |
"""Save configuration back to config.json""" | |
try: | |
# Load existing config | |
config = load_config() | |
# Update modifiable fields | |
config['system_prompt'] = system_prompt | |
config['temperature'] = temperature | |
config['max_tokens'] = int(max_tokens) | |
# Parse examples (one per line) | |
if examples_text: | |
examples = [ex.strip() for ex in examples_text.split('\n') if ex.strip()] | |
config['examples'] = str(examples) | |
# Parse grounding URLs | |
if grounding_urls_text: | |
urls = [url.strip() for url in grounding_urls_text.split('\n') if url.strip()] | |
config['grounding_urls'] = json.dumps(urls) | |
# Save config | |
with open('config.json', 'w') as f: | |
json.dump(config, f, indent=2) | |
return "β Configuration saved successfully! Refresh the page to apply changes." | |
except Exception as e: | |
return f"β Error saving config: {str(e)}" | |
def create_config_editor(): | |
"""Create the configuration editor interface""" | |
config = load_config() | |
with gr.Group(): | |
gr.Markdown("### Configuration Editor") | |
gr.Markdown("Edit your assistant's configuration below. Changes require a page refresh to take effect.") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
system_prompt = gr.TextArea( | |
label="System Prompt", | |
value=config.get('system_prompt', ''), | |
lines=10, | |
placeholder="Define your assistant's role and behavior..." | |
) | |
with gr.Column(scale=1): | |
temperature = gr.Slider( | |
label="Temperature", | |
minimum=0.0, | |
maximum=2.0, | |
step=0.1, | |
value=config.get('temperature', 0.7) | |
) | |
max_tokens = gr.Number( | |
label="Max Response Tokens", | |
value=config.get('max_tokens', 500), | |
minimum=50, | |
maximum=8000 | |
) | |
examples_text = gr.TextArea( | |
label="Example Prompts (one per line)", | |
value='\n'.join(eval(config.get('examples', '[]'))), | |
lines=5, | |
placeholder="What is machine learning?\nExplain quantum computing\nHow do neural networks work?" | |
) | |
grounding_urls_text = gr.TextArea( | |
label="Grounding URLs (one per line)", | |
value='\n'.join(json.loads(config.get('grounding_urls', '[]'))), | |
lines=5, | |
placeholder="https://example.com/docs\nhttps://wiki.example.com" | |
) | |
save_btn = gr.Button("πΎ Save Configuration", variant="primary") | |
status = gr.Markdown("") | |
save_btn.click( | |
save_config, | |
inputs=[system_prompt, temperature, max_tokens, examples_text, grounding_urls_text], | |
outputs=status | |
) | |
gr.Markdown(""" | |
### Configuration Tips: | |
- **System Prompt**: Define your assistant's personality, knowledge, and behavior | |
- **Temperature**: Lower values (0.0-0.5) for focused responses, higher (0.7-1.5) for creativity | |
- **Max Tokens**: Limit response length (1 token β 0.75 words) | |
- **Examples**: Provide sample questions to guide users | |
- **Grounding URLs**: Add reference websites for context | |
β οΈ **Note**: Model selection and API key cannot be changed here for security reasons. | |
""") | |
return config |