ylliprifti commited on
Commit
016e3e3
·
1 Parent(s): 10a0600

Deploy AnySecret Chat Assistant

Browse files

- Gradio-based chat interface
- Llama 3.2 3B Instruct with LoRA fine-tuning
- Specialized for AnySecret configuration management
- Professional UI with examples and advanced settings
- Ready for production deployment

Files changed (5) hide show
  1. .gitignore +51 -0
  2. Dockerfile +41 -0
  3. README.md +90 -6
  4. app.py +268 -0
  5. requirements.txt +14 -0
.gitignore ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+ MANIFEST
23
+
24
+ # Virtual environments
25
+ env/
26
+ venv/
27
+ ENV/
28
+ env.bak/
29
+ venv.bak/
30
+
31
+ # IDEs
32
+ .vscode/
33
+ .idea/
34
+ *.swp
35
+ *.swo
36
+ *~
37
+
38
+ # OS
39
+ .DS_Store
40
+ Thumbs.db
41
+
42
+ # Gradio
43
+ gradio_cached_examples/
44
+ flagged/
45
+
46
+ # Model cache
47
+ .cache/
48
+ models/
49
+
50
+ # Logs
51
+ *.log
Dockerfile ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile for AnySecret Chat Assistant
2
+ # Optimized for HuggingFace Spaces deployment
3
+
4
+ FROM python:3.10-slim
5
+
6
+ # Set working directory
7
+ WORKDIR /app
8
+
9
+ # Install system dependencies
10
+ RUN apt-get update && apt-get install -y \
11
+ git \
12
+ curl \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # Copy requirements first for better caching
16
+ COPY requirements.txt .
17
+
18
+ # Install Python dependencies
19
+ RUN pip install --no-cache-dir -r requirements.txt
20
+
21
+ # Copy application files
22
+ COPY . .
23
+
24
+ # Create non-root user for security
25
+ RUN useradd -m -u 1000 anysecret
26
+ RUN chown -R anysecret:anysecret /app
27
+ USER anysecret
28
+
29
+ # Expose port
30
+ EXPOSE 7860
31
+
32
+ # Health check
33
+ HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
34
+ CMD curl -f http://localhost:7860/ || exit 1
35
+
36
+ # Set environment variables
37
+ ENV GRADIO_SERVER_NAME=0.0.0.0
38
+ ENV GRADIO_SERVER_PORT=7860
39
+
40
+ # Run the application
41
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,12 +1,96 @@
1
  ---
2
- title: Anysecret Chat
3
- emoji: 📉
4
  colorFrom: indigo
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.44.1
8
  app_file: app.py
9
- pinned: false
 
 
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: AnySecret Chat Assistant
3
+ emoji: 🔐
4
  colorFrom: indigo
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
+ pinned: true
10
+ license: mit
11
+ short_description: AI assistant for AnySecret configuration management
12
+ hardware: cpu-upgrade
13
  ---
14
 
15
+ # 🔐 AnySecret Chat Assistant
16
+
17
+ An AI-powered assistant specialized in AnySecret configuration management, trained to help with:
18
+
19
+ - **Multi-cloud configuration** (AWS, GCP, Azure, Kubernetes)
20
+ - **CLI commands** and usage patterns
21
+ - **CI/CD integration** (GitHub Actions, Jenkins, GitLab)
22
+ - **Python SDK** implementation
23
+ - **Security best practices** for secrets management
24
+ - **Migration guidance** from other tools
25
+
26
+ ## 🚀 Features
27
+
28
+ - **Specialized Knowledge**: Trained specifically on AnySecret documentation and patterns
29
+ - **Interactive Chat**: Real-time conversation interface
30
+ - **Code Examples**: Provides practical, copy-pasteable code snippets
31
+ - **Multi-cloud Expertise**: Understands differences between cloud providers
32
+ - **Production Ready**: Includes enterprise deployment guidance
33
+
34
+ ## 💬 Example Questions
35
+
36
+ Try asking:
37
+ - "How do I configure AnySecret for AWS?"
38
+ - "Show me a GitHub Actions workflow with AnySecret"
39
+ - "What's the difference between secrets and parameters?"
40
+ - "How do I migrate from AWS Parameter Store?"
41
+ - "Can you show me Python SDK examples?"
42
+
43
+ ## 🛠️ Technical Details
44
+
45
+ - **Base Model**: Meta Llama 3.2 3B Instruct
46
+ - **Fine-tuning**: LoRA (Low-Rank Adaptation) on AnySecret-specific data
47
+ - **Training Data**: 43 curated examples across 7 categories
48
+ - **Framework**: Transformers + PEFT + Gradio
49
+
50
+ ## 📚 Related Links
51
+
52
+ - **Website**: [anysecret.io](https://anysecret.io)
53
+ - **Documentation**: [docs.anysecret.io](https://docs.anysecret.io)
54
+ - **GitHub**: [anysecret-io/anysecret-lib](https://github.com/anysecret-io/anysecret-lib)
55
+ - **Commercial License**: [License Terms](https://github.com/anysecret-io/anysecret-lib/blob/main/LICENSE-COMMERCIAL)
56
+
57
+ ## 🔧 Local Development
58
+
59
+ ```bash
60
+ # Clone this space
61
+ git clone https://huggingface.co/spaces/anysecret-io/anysecret-chat
62
+ cd anysecret-chat
63
+
64
+ # Install dependencies
65
+ pip install -r requirements.txt
66
+
67
+ # Run locally
68
+ python app.py
69
+ ```
70
+
71
+ ## 📖 Model Information
72
+
73
+ This assistant uses a fine-tuned version of Llama 3.2 3B Instruct, specifically trained on AnySecret patterns and best practices. The model can:
74
+
75
+ - Generate CLI commands with proper syntax
76
+ - Explain configuration concepts clearly
77
+ - Provide code examples in multiple languages
78
+ - Suggest architectural patterns for different scales
79
+ - Help troubleshoot common issues
80
+
81
+ ## ⚠️ Limitations
82
+
83
+ - **Training Data**: Based on AnySecret v1.x documentation
84
+ - **Not Official Support**: For production issues, use official support channels
85
+ - **General Purpose**: May not have latest feature updates
86
+ - **Experimental**: This is a demonstration of AI-assisted documentation
87
+
88
+ ## 📄 License
89
+
90
+ This chat interface is MIT licensed. The underlying AnySecret software is dual-licensed:
91
+ - **AGPL-3.0** for open source use
92
+ - **Commercial License** for business use
93
+
94
+ ---
95
+
96
+ Built with ❤️ by the AnySecret team • [Get Commercial License](https://anysecret.io/#license)
app.py ADDED
@@ -0,0 +1,268 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ AnySecret Chat Assistant - HuggingFace Spaces Gradio Interface
4
+ A specialized AI assistant for AnySecret configuration management
5
+ """
6
+
7
+ import os
8
+ import gradio as gr
9
+ import torch
10
+ from transformers import AutoTokenizer, AutoModelForCausalLM
11
+ from peft import PeftModel
12
+ import logging
13
+
14
+ # Configure logging
15
+ logging.basicConfig(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
+ # Model configuration
19
+ BASE_MODEL = "meta-llama/Llama-3.2-3B-Instruct"
20
+ PEFT_MODEL = "anysecret-io/anysecret-assistant"
21
+
22
+ # Global variables for model and tokenizer
23
+ model = None
24
+ tokenizer = None
25
+ device = None
26
+
27
+ def load_model():
28
+ """Load the model and tokenizer"""
29
+ global model, tokenizer, device
30
+
31
+ try:
32
+ # Determine device
33
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
34
+ logger.info(f"Using device: {device}")
35
+
36
+ # Load tokenizer
37
+ logger.info("Loading tokenizer...")
38
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True)
39
+ if tokenizer.pad_token is None:
40
+ tokenizer.pad_token = tokenizer.eos_token
41
+ tokenizer.padding_side = "left" # Better for chat
42
+
43
+ # Load base model
44
+ logger.info("Loading base model...")
45
+ base_model = AutoModelForCausalLM.from_pretrained(
46
+ BASE_MODEL,
47
+ torch_dtype=torch.float16 if device.type == "cuda" else torch.float32,
48
+ device_map="auto" if device.type == "cuda" else None,
49
+ trust_remote_code=True,
50
+ low_cpu_mem_usage=True
51
+ )
52
+
53
+ # Load LoRA adapter
54
+ logger.info("Loading LoRA adapter...")
55
+ model = PeftModel.from_pretrained(
56
+ base_model,
57
+ PEFT_MODEL,
58
+ torch_dtype=torch.float16 if device.type == "cuda" else torch.float32
59
+ )
60
+
61
+ # Move to device if not using device_map
62
+ if device.type != "cuda":
63
+ model = model.to(device)
64
+
65
+ model.eval()
66
+ logger.info("Model loaded successfully!")
67
+ return True
68
+
69
+ except Exception as e:
70
+ logger.error(f"Error loading model: {e}")
71
+ return False
72
+
73
+ def generate_response(message, history, max_new_tokens=512, temperature=0.1, top_p=0.9):
74
+ """Generate response from the model"""
75
+ if model is None or tokenizer is None:
76
+ return "Model not loaded. Please try again."
77
+
78
+ try:
79
+ # Format the conversation with proper prompt structure
80
+ conversation = ""
81
+
82
+ # Add conversation history
83
+ for user_msg, assistant_msg in history:
84
+ conversation += f"### Instruction:\n{user_msg}\n\n### Response:\n{assistant_msg}\n\n"
85
+
86
+ # Add current message
87
+ conversation += f"### Instruction:\n{message}\n\n### Response:\n"
88
+
89
+ # Tokenize
90
+ inputs = tokenizer(
91
+ conversation,
92
+ return_tensors="pt",
93
+ truncation=True,
94
+ max_length=1024, # Leave room for generation
95
+ padding=True
96
+ ).to(device)
97
+
98
+ # Generate
99
+ with torch.no_grad():
100
+ outputs = model.generate(
101
+ **inputs,
102
+ max_new_tokens=max_new_tokens,
103
+ temperature=temperature,
104
+ top_p=top_p,
105
+ do_sample=True,
106
+ pad_token_id=tokenizer.pad_token_id,
107
+ eos_token_id=tokenizer.eos_token_id,
108
+ repetition_penalty=1.1
109
+ )
110
+
111
+ # Decode response
112
+ full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
113
+
114
+ # Extract just the new response
115
+ if "### Response:\n" in full_response:
116
+ response = full_response.split("### Response:\n")[-1].strip()
117
+ else:
118
+ response = full_response[len(conversation):].strip()
119
+
120
+ # Clean up response
121
+ response = response.replace("### Instruction:", "").strip()
122
+
123
+ return response
124
+
125
+ except Exception as e:
126
+ logger.error(f"Error generating response: {e}")
127
+ return f"Sorry, I encountered an error: {str(e)}"
128
+
129
+ def chat_interface(message, history):
130
+ """Main chat interface function for Gradio"""
131
+ response = generate_response(message, history)
132
+ return response
133
+
134
+ # Custom CSS for AnySecret branding
135
+ css = """
136
+ .gradio-container {
137
+ max-width: 1000px !important;
138
+ }
139
+
140
+ .header {
141
+ text-align: center;
142
+ padding: 20px 0;
143
+ background: linear-gradient(135deg, #6366f1 0%, #818cf8 100%);
144
+ color: white;
145
+ margin-bottom: 20px;
146
+ border-radius: 10px;
147
+ }
148
+
149
+ .header h1 {
150
+ margin: 0;
151
+ font-size: 2.5em;
152
+ font-weight: bold;
153
+ }
154
+
155
+ .header p {
156
+ margin: 10px 0 0 0;
157
+ font-size: 1.1em;
158
+ opacity: 0.9;
159
+ }
160
+
161
+ .footer {
162
+ text-align: center;
163
+ padding: 20px 0;
164
+ color: #666;
165
+ font-size: 0.9em;
166
+ }
167
+
168
+ .examples-container {
169
+ margin: 20px 0;
170
+ }
171
+
172
+ .examples-container h3 {
173
+ color: #374151;
174
+ margin-bottom: 10px;
175
+ }
176
+ """
177
+
178
+ # Load model on startup
179
+ logger.info("Initializing AnySecret Chat Assistant...")
180
+ model_loaded = load_model()
181
+
182
+ if not model_loaded:
183
+ logger.error("Failed to load model!")
184
+
185
+ # Create Gradio interface
186
+ with gr.Blocks(css=css, title="AnySecret Chat Assistant") as demo:
187
+ # Header
188
+ gr.HTML("""
189
+ <div class="header">
190
+ <h1>🔐 AnySecret Chat Assistant</h1>
191
+ <p>Your AI assistant for configuration management across any cloud provider</p>
192
+ </div>
193
+ """)
194
+
195
+ if model_loaded:
196
+ # Main chat interface
197
+ chatbot = gr.ChatInterface(
198
+ fn=chat_interface,
199
+ title="",
200
+ description="Ask me anything about AnySecret configuration management, CLI commands, cloud integrations, or best practices!",
201
+ examples=[
202
+ "How do I configure AnySecret for AWS?",
203
+ "What's the difference between secrets and parameters?",
204
+ "Show me how to use anysecret in a GitHub Actions workflow",
205
+ "How do I set up AnySecret with Kubernetes?",
206
+ "What are the best practices for managing secrets in production?",
207
+ "How do I migrate from AWS Parameter Store to AnySecret?",
208
+ "Can you show me a Python example using the AnySecret SDK?"
209
+ ],
210
+ retry_btn="🔄 Retry",
211
+ undo_btn="↩️ Undo",
212
+ clear_btn="🗑️ Clear Chat",
213
+ submit_btn="Send",
214
+ stop_btn="⏹️ Stop",
215
+ theme="default"
216
+ )
217
+
218
+ # Advanced settings
219
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
220
+ with gr.Row():
221
+ max_tokens = gr.Slider(
222
+ minimum=50,
223
+ maximum=1024,
224
+ value=512,
225
+ label="Max Response Length",
226
+ info="Maximum number of tokens to generate"
227
+ )
228
+ temperature = gr.Slider(
229
+ minimum=0.1,
230
+ maximum=1.0,
231
+ value=0.1,
232
+ label="Temperature",
233
+ info="Higher values make responses more creative"
234
+ )
235
+ else:
236
+ gr.HTML("""
237
+ <div style="text-align: center; padding: 40px; color: #dc2626;">
238
+ <h2>⚠️ Model Loading Failed</h2>
239
+ <p>The AnySecret assistant model could not be loaded. Please try refreshing the page or contact support.</p>
240
+ </div>
241
+ """)
242
+
243
+ # Footer
244
+ gr.HTML("""
245
+ <div class="footer">
246
+ <p>
247
+ Powered by <strong>AnySecret.io</strong> •
248
+ <a href="https://anysecret.io" target="_blank">Website</a> •
249
+ <a href="https://docs.anysecret.io" target="_blank">Documentation</a> •
250
+ <a href="https://github.com/anysecret-io/anysecret-lib" target="_blank">GitHub</a>
251
+ </p>
252
+ <p style="font-size: 0.8em; margin-top: 10px; opacity: 0.7;">
253
+ This assistant is trained on AnySecret documentation and best practices.
254
+ For production support, please visit our official channels.
255
+ </p>
256
+ </div>
257
+ """)
258
+
259
+ # Launch configuration
260
+ if __name__ == "__main__":
261
+ demo.launch(
262
+ server_name="0.0.0.0",
263
+ server_port=7860,
264
+ share=False,
265
+ debug=False,
266
+ show_error=True,
267
+ quiet=False
268
+ )
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Core dependencies for AnySecret Chat Assistant
2
+ torch>=2.0.0
3
+ transformers>=4.35.0
4
+ peft>=0.6.0
5
+ accelerate>=0.24.0
6
+ gradio>=4.0.0
7
+
8
+ # Optional: Better performance on GPU
9
+ # bitsandbytes>=0.41.0
10
+
11
+ # Utilities
12
+ numpy
13
+ requests
14
+ huggingface_hub>=0.17.0