Samrish2009 commited on
Commit
d237d26
·
verified ·
1 Parent(s): 6c2d9aa

Deploy SAM-AI app.py

Browse files
Files changed (1) hide show
  1. app.py +270 -0
app.py ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SAM-AI Cloud Interface (ZeroGPU Space Edition)
3
+ ==============================================
4
+ Dual-mode Gradio application offering SAM-AI Chat and SAM-AI Code, powered
5
+ exclusively by sovereign open-weight model DeepSeek-R1-Distill-Qwen-7B with the
6
+ verified SAM-AI GSM8K QLoRA adapter.
7
+ """
8
+
9
+ import os
10
+ import sys
11
+ import time
12
+ import gradio as gr
13
+ from pathlib import Path
14
+
15
+ # Add current directory to path
16
+ sys.path.insert(0, str(Path(__file__).parent.resolve()))
17
+
18
+ from model_loader import engine, ModelLifecycleStatus
19
+
20
+ # ZeroGPU requires model loading at module level
21
+ print("[*] Starting SAM-AI ZeroGPU Space...")
22
+ try:
23
+ engine.load()
24
+ except Exception as exc:
25
+ print(f"[!] Warning: Initial model loading encountered exception: {exc}")
26
+
27
+
28
+ SYSTEM_PROMPT_CHAT = (
29
+ "You are SAM-AI, a sovereign artificial intelligence assistant developed by "
30
+ "Parallax Intelligence Lab. You provide accurate, honest, mathematically rigorous, "
31
+ "and evidence-backed explanations. When reasoning through complex problems, think "
32
+ "step-by-step and encapsulate your internal rationale within <think> ... </think> tags."
33
+ )
34
+
35
+ SYSTEM_PROMPT_CODE = (
36
+ "You are SAM-AI Code, a specialized sovereign software engineering assistant developed "
37
+ "by Parallax Intelligence Lab. You assist with coding, debugging, refactoring, and code review. "
38
+ "When suggesting changes, present code clearly and use standard unified diff format where appropriate. "
39
+ "Never invent fake library APIs or pretend unexecuted commands ran successfully."
40
+ )
41
+
42
+
43
+ def chat_respond(user_message, chat_history, temperature, top_p, max_tokens):
44
+ """Handles multi-turn conversation streaming in SAM-AI Chat."""
45
+ if not user_message or not user_message.strip():
46
+ yield chat_history, ""
47
+ return
48
+
49
+ # Build history in OpenAI-style format
50
+ messages = []
51
+ for turn in (chat_history or []):
52
+ if turn[0]:
53
+ messages.append({"role": "user", "content": turn[0]})
54
+ if turn[1]:
55
+ messages.append({"role": "assistant", "content": turn[1]})
56
+
57
+ messages.append({"role": "user", "content": user_message})
58
+
59
+ # Append pending message to history
60
+ new_history = list(chat_history or []) + [[user_message, ""]]
61
+
62
+ for partial_response in engine.generate_stream(
63
+ messages=messages,
64
+ temperature=temperature,
65
+ top_p=top_p,
66
+ max_new_tokens=int(max_tokens),
67
+ system_prompt=SYSTEM_PROMPT_CHAT
68
+ ):
69
+ new_history[-1][1] = partial_response
70
+ yield new_history, ""
71
+
72
+
73
+ def code_respond(user_prompt, code_context, task_type, temperature, max_tokens):
74
+ """Handles coding tasks, explanations, and diff generation in SAM-AI Code."""
75
+ if not user_prompt or not user_prompt.strip():
76
+ yield "Please provide a coding prompt or question."
77
+ return
78
+
79
+ full_content = f"### Task: {task_type}\n\n"
80
+ if code_context and code_context.strip():
81
+ full_content += f"```\n{code_context.strip()}\n```\n\n"
82
+ full_content += f"**User Request:**\n{user_prompt.strip()}"
83
+
84
+ messages = [{"role": "user", "content": full_content}]
85
+
86
+ for partial_response in engine.generate_stream(
87
+ messages=messages,
88
+ temperature=temperature,
89
+ top_p=0.95,
90
+ max_new_tokens=int(max_tokens),
91
+ system_prompt=SYSTEM_PROMPT_CODE
92
+ ):
93
+ yield partial_response
94
+
95
+
96
+ def get_status_markdown():
97
+ """Generates real-time status banner for header."""
98
+ diag = engine.get_diagnostics()
99
+ status = diag["status"]
100
+ is_loaded = diag["is_loaded"]
101
+ device = diag["device"].upper()
102
+ adapter_name = Path(diag["adapter_dir"]).name
103
+
104
+ if is_loaded:
105
+ badge = "🟢 **READY (SOVEREIGN ZERO-GPU)**"
106
+ color = "green"
107
+ elif status == ModelLifecycleStatus.LOADING_BASE_MODEL or status == ModelLifecycleStatus.ATTACHING_ADAPTER:
108
+ badge = "🟡 **LOADING WEIGHTS...**"
109
+ color = "orange"
110
+ else:
111
+ badge = f"🔴 **NOT READY: {status}**"
112
+ color = "red"
113
+
114
+ return (
115
+ f"<div style='border: 1px solid #3b82f6; border-radius: 8px; padding: 12px 16px; background-color: #0f172a; color: #f8fafc; margin-bottom: 12px;'>"
116
+ f"<div style='display: flex; justify-content: space-between; align-items: center;'>"
117
+ f"<div><strong>Model:</strong> <code>{diag['base_model']}</code> + <code>{adapter_name}</code></div>"
118
+ f"<div><strong>Status:</strong> {badge} | <strong>Device:</strong> <code>{device}</code></div>"
119
+ f"</div>"
120
+ f"<div style='font-size: 0.85em; color: #94a3b8; margin-top: 4px;'>{diag['status_detail']}</div>"
121
+ f"</div>"
122
+ )
123
+
124
+
125
+ # Build Gradio UI
126
+ custom_theme = gr.themes.Soft(
127
+ primary_hue="indigo",
128
+ secondary_hue="slate",
129
+ ).set(
130
+ body_background_fill="#0b0f19",
131
+ body_text_color="#f3f4f6",
132
+ block_background_fill="#111827",
133
+ block_border_color="#1f2937",
134
+ )
135
+
136
+ with gr.Blocks(theme=custom_theme, title="SAM-AI Sovereign Assistant") as demo:
137
+ gr.Markdown(
138
+ """
139
+ # 🧠 SAM-AI Sovereign Assistant
140
+ **Self-Hosted Open-Weight Intelligence | DeepSeek-R1-Distill-Qwen-7B + SAM-AI QLoRA Adapter**
141
+ *Running on Hugging Face Spaces ZeroGPU (NVIDIA RTX Pro 6000 Blackwell)*
142
+ """
143
+ )
144
+
145
+ status_header = gr.HTML(value=get_status_markdown)
146
+
147
+ with gr.Tabs() as tabs:
148
+ # =====================================================================
149
+ # Tab 1: SAM-AI Chat
150
+ # =====================================================================
151
+ with gr.Tab("💬 SAM-AI Chat", id="tab_chat"):
152
+ chatbot = gr.Chatbot(
153
+ height=520,
154
+ show_copy_button=True,
155
+ bubble_full_width=False,
156
+ render_markdown=True,
157
+ )
158
+
159
+ with gr.Row():
160
+ chat_input = gr.Textbox(
161
+ placeholder="Ask a question, mathematical problem, or reasoning challenge...",
162
+ show_label=False,
163
+ scale=8,
164
+ lines=2,
165
+ )
166
+ with gr.Column(scale=2, min_width=120):
167
+ send_btn = gr.Button("Send", variant="primary")
168
+ clear_btn = gr.Button("Clear Chat")
169
+
170
+ with gr.Accordion("⚙️ Inference Parameters", open=False):
171
+ with gr.Row():
172
+ chat_temp = gr.Slider(
173
+ minimum=0.0, maximum=1.0, value=0.6, step=0.05, label="Temperature (Creativity / Randomness)"
174
+ )
175
+ chat_top_p = gr.Slider(
176
+ minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P (Nucleus Sampling)"
177
+ )
178
+ chat_max_tokens = gr.Slider(
179
+ minimum=256, maximum=4096, value=2048, step=128, label="Max New Tokens"
180
+ )
181
+
182
+ # Event wiring
183
+ chat_msg_event = chat_input.submit(
184
+ chat_respond,
185
+ inputs=[chat_input, chatbot, chat_temp, chat_top_p, chat_max_tokens],
186
+ outputs=[chatbot, chat_input],
187
+ api_name="chat_respond",
188
+ )
189
+ send_btn.click(
190
+ chat_respond,
191
+ inputs=[chat_input, chatbot, chat_temp, chat_top_p, chat_max_tokens],
192
+ outputs=[chatbot, chat_input],
193
+ api_name="chat_respond",
194
+ )
195
+ clear_btn.click(lambda: None, None, chatbot, queue=False)
196
+
197
+ # =====================================================================
198
+ # Tab 2: SAM-AI Code
199
+ # =====================================================================
200
+ with gr.Tab("💻 SAM-AI Code", id="tab_code"):
201
+ with gr.Row():
202
+ with gr.Column(scale=5):
203
+ code_task = gr.Radio(
204
+ choices=["Generate Code", "Debug & Fix", "Refactor & Optimize", "Generate Unified Diff"],
205
+ value="Generate Code",
206
+ label="Coding Task Type"
207
+ )
208
+ code_prompt = gr.Textbox(
209
+ label="Coding Instructions / Problem Description",
210
+ placeholder="e.g. Implement a thread-safe LRU cache with eviction telemetry in Python...",
211
+ lines=4,
212
+ )
213
+ code_context = gr.Code(
214
+ label="Existing Code / Context (Optional)",
215
+ language="python",
216
+ lines=12,
217
+ )
218
+ run_code_btn = gr.Button("Generate Solution", variant="primary")
219
+
220
+ with gr.Column(scale=5):
221
+ code_output = gr.Markdown(
222
+ label="SAM-AI Code Response",
223
+ value="*Click 'Generate Solution' to run SAM-AI Code on the assigned ZeroGPU.*"
224
+ )
225
+
226
+ with gr.Accordion("⚙️ Code Generation Parameters", open=False):
227
+ with gr.Row():
228
+ code_temp = gr.Slider(
229
+ minimum=0.0, maximum=1.0, value=0.2, step=0.05, label="Temperature (Lower = More Deterministic)"
230
+ )
231
+ code_max_tokens = gr.Slider(
232
+ minimum=512, maximum=4096, value=2048, step=128, label="Max Tokens"
233
+ )
234
+
235
+ run_code_btn.click(
236
+ code_respond,
237
+ inputs=[code_prompt, code_context, code_task, code_temp, code_max_tokens],
238
+ outputs=[code_output],
239
+ api_name="code_respond",
240
+ )
241
+
242
+ # =====================================================================
243
+ # Tab 3: Diagnostics & Sovereign Architecture
244
+ # =====================================================================
245
+ with gr.Tab("🛡️ Sovereign Diagnostics", id="tab_diag"):
246
+ refresh_diag_btn = gr.Button("Refresh Telemetry")
247
+ diag_json = gr.JSON(value=engine.get_diagnostics, label="Engine Diagnostic State")
248
+ refresh_diag_btn.click(engine.get_diagnostics, outputs=[diag_json])
249
+
250
+ gr.Markdown(
251
+ """
252
+ ---
253
+ **Parallax Intelligence Lab | SAM-AI Project**
254
+ *Zero commercial third-party inference APIs. Pure open-weight execution.*
255
+ """
256
+ )
257
+
258
+
259
+ # Configure Gradio queue for ZeroGPU concurrency management
260
+ demo.queue(
261
+ max_size=20,
262
+ default_concurrency_limit=1,
263
+ )
264
+
265
+ if __name__ == "__main__":
266
+ demo.launch(
267
+ server_name="0.0.0.0",
268
+ server_port=7860,
269
+ show_error=True
270
+ )