File size: 4,608 Bytes
f8cd41a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Enhanced Space Template that loads from config.json
# This shows how the deployed app.py should be modified

import json
import os

# Load configuration from config.json at startup
def load_configuration():
    """Load configuration from config.json with fallbacks"""
    try:
        with open('config.json', 'r') as f:
            config = json.load(f)
            print("βœ… Configuration loaded from config.json")
            return config
    except FileNotFoundError:
        print("⚠️ config.json not found, using embedded defaults")
        # Fallback to embedded configuration
        return {
            'name': 'AI Assistant',
            'description': 'A customizable AI assistant',
            'system_prompt': 'You are a helpful AI assistant.',
            'model': 'gemini/gemini-2.0-flash-thinking-exp-1219',
            'temperature': 0.7,
            'max_tokens': 500,
            'examples': '[]',
            'grounding_urls': '[]',
            'enable_dynamic_urls': False,
            'api_key_var': 'OPENROUTER_API_KEY'
        }
    except Exception as e:
        print(f"❌ Error loading config.json: {str(e)}")
        return None

# Initialize configuration
CONFIG = load_configuration()

# Extract configuration values
SPACE_NAME = CONFIG.get('name', 'AI Assistant')
SPACE_DESCRIPTION = CONFIG.get('description', 'A customizable AI assistant')
SYSTEM_PROMPT = CONFIG.get('system_prompt', 'You are a helpful AI assistant.')
MODEL = CONFIG.get('model', 'gemini/gemini-2.0-flash-thinking-exp-1219')
TEMPERATURE = CONFIG.get('temperature', 0.7)
MAX_TOKENS = CONFIG.get('max_tokens', 500)
GROUNDING_URLS = json.loads(CONFIG.get('grounding_urls', '[]'))
ENABLE_DYNAMIC_URLS = CONFIG.get('enable_dynamic_urls', False)
API_KEY_VAR = CONFIG.get('api_key_var', 'OPENROUTER_API_KEY')

# Get API key from environment
API_KEY = os.environ.get(API_KEY_VAR)

# Example of how to integrate config editor into the main interface
def create_interface_with_config():
    """Create the main interface with configuration tab"""
    import gradio as gr
    
    with gr.Blocks(title=SPACE_NAME) as demo:
        gr.Markdown(f"# {SPACE_NAME}")
        gr.Markdown(f"{SPACE_DESCRIPTION}")
        
        with gr.Tabs():
            # Main chat tab
            with gr.Tab("πŸ’¬ Chat"):
                # ... existing chat interface code ...
                pass
            
            # Configuration tab (only show if no access code or user is authenticated)
            with gr.Tab("βš™οΈ Configuration"):
                # Import and use the config editor
                from config_editor_template import create_config_editor
                create_config_editor()
            
            # Help tab
            with gr.Tab("❓ Help"):
                gr.Markdown("""
                ### How to Customize Your Assistant
                
                1. **Configuration Tab**: Edit settings directly in the browser
                2. **Files Tab**: For advanced users - edit config.json directly
                3. **Changes**: Refresh the page after saving to apply changes
                
                ### Configuration File Structure
                
                The `config.json` file contains:
                - `system_prompt`: Your assistant's instructions
                - `temperature`: Response randomness (0-2)
                - `max_tokens`: Maximum response length
                - `examples`: Sample prompts for users
                - `grounding_urls`: Reference websites
                
                ### Best Practices
                
                1. **Test Changes**: Try different prompts after each change
                2. **Iterative Refinement**: Make small changes and test
                3. **Backup**: Download config.json before major changes
                4. **Share**: Export your config to share with colleagues
                """)
    
    return demo

# Reload configuration endpoint for dynamic updates
def reload_configuration():
    """Reload configuration without restart - for advanced usage"""
    global CONFIG, SYSTEM_PROMPT, TEMPERATURE, MAX_TOKENS, GROUNDING_URLS
    
    new_config = load_configuration()
    if new_config:
        CONFIG = new_config
        SYSTEM_PROMPT = CONFIG.get('system_prompt', SYSTEM_PROMPT)
        TEMPERATURE = CONFIG.get('temperature', TEMPERATURE)
        MAX_TOKENS = CONFIG.get('max_tokens', MAX_TOKENS)
        GROUNDING_URLS = json.loads(CONFIG.get('grounding_urls', '[]'))
        return "βœ… Configuration reloaded successfully"
    return "❌ Failed to reload configuration"