Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gc
|
| 3 |
import re
|
|
@@ -5,125 +6,177 @@ import json
|
|
| 5 |
import uuid
|
| 6 |
import time
|
| 7 |
import shutil
|
| 8 |
-
import
|
| 9 |
import sqlite3
|
|
|
|
| 10 |
import threading
|
| 11 |
import traceback
|
| 12 |
from pathlib import Path
|
| 13 |
from datetime import datetime
|
| 14 |
-
from typing import Optional, List, Tuple
|
| 15 |
|
| 16 |
-
os.environ
|
| 17 |
-
os.environ
|
| 18 |
-
os.environ
|
| 19 |
|
|
|
|
| 20 |
import gradio as gr
|
| 21 |
import torch
|
| 22 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
APP_NAME = "ARISE AI Studio"
|
| 29 |
DEVELOPER_TELEGRAM = "https://t.me/hardhackar009"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
MAX_INPUT_TOKENS = 4096
|
| 34 |
-
MAX_OUTPUT_TOKENS = 2048
|
| 35 |
-
MAX_HISTORY_MESSAGES = 12
|
| 36 |
MAX_UPLOAD_FILES = 100
|
| 37 |
MAX_FILE_SIZE = 10 * 1024 * 1024
|
| 38 |
MAX_PROJECT_SIZE = 100 * 1024 * 1024
|
| 39 |
-
MAX_CONTEXT_SIZE =
|
| 40 |
-
MAX_CHAT_MESSAGE_LENGTH = 12000
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
DATA_DIR = Path(os.getenv("ARISE_DATA_DIR", "/data/arise_ai_studio"))
|
|
|
|
| 43 |
try:
|
| 44 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
except Exception:
|
| 49 |
DATA_DIR = Path("./arise_ai_studio_data")
|
| 50 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 51 |
|
| 52 |
DB_PATH = DATA_DIR / "arise_history.db"
|
| 53 |
PROJECTS_DIR = DATA_DIR / "projects"
|
| 54 |
-
UPLOADS_DIR = DATA_DIR / "uploads"
|
| 55 |
PROJECTS_DIR.mkdir(parents=True, exist_ok=True)
|
| 56 |
-
UPLOADS_DIR.mkdir(parents=True, exist_ok=True)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
"
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
-
DEFAULT_MODEL = "Qwen Coder 1.5B"
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
-
SYSTEM_PROMPT = r"""
|
| 81 |
-
You are ARISE AI, a helpful general assistant and professional software
|
| 82 |
-
engineering assistant. You can answer normal questions and help with
|
| 83 |
-
Python, Kotlin, Java, Android, Gradle, AAPT2, JavaScript, TypeScript,
|
| 84 |
-
HTML, CSS, APIs, databases, Hugging Face, Gradio, PyTorch and debugging.
|
| 85 |
-
|
| 86 |
-
GENERAL CHAT:
|
| 87 |
-
- If the user asks a normal question, answer normally and clearly.
|
| 88 |
-
- Do not demand an upload. Attachments are optional.
|
| 89 |
-
- Be honest about uncertainty and never claim code was tested unless it was.
|
| 90 |
-
- Use Markdown and avoid unnecessary verbosity.
|
| 91 |
-
|
| 92 |
-
PROJECT WORK:
|
| 93 |
-
- When project files are supplied, inspect the supplied project context first.
|
| 94 |
-
- Preserve existing features and change only what the user requested.
|
| 95 |
-
- Do not invent unseen files or pretend you inspected files not supplied.
|
| 96 |
-
- If asked to modify/create project files, return every changed/new file
|
| 97 |
-
completely using exactly:
|
| 98 |
-
<<<FILE: relative/path/to/file.ext>>>
|
| 99 |
-
complete file content
|
| 100 |
-
<<<END FILE>>>
|
| 101 |
-
- Do not use absolute paths or ../ path traversal.
|
| 102 |
-
- Do not put explanations inside file blocks.
|
| 103 |
-
- Do not output file blocks for unchanged files.
|
| 104 |
-
- If no file changes are required, answer normally.
|
| 105 |
-
- For a project upload, explain any important limitations.
|
| 106 |
-
- For images, only describe content that is actually available through
|
| 107 |
-
extracted OCR text; do not claim visual understanding from OCR alone.
|
| 108 |
-
"""
|
| 109 |
|
| 110 |
-
#
|
|
|
|
|
|
|
| 111 |
|
| 112 |
db_lock = threading.RLock()
|
| 113 |
-
model_lock = threading.RLock()
|
| 114 |
-
model_cache = {"name": None, "tokenizer": None, "model": None}
|
| 115 |
|
| 116 |
|
| 117 |
def db_connect():
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
def initialize_database():
|
| 124 |
with db_lock:
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 127 |
CREATE TABLE IF NOT EXISTS conversations (
|
| 128 |
id TEXT PRIMARY KEY,
|
| 129 |
session_id TEXT NOT NULL,
|
|
@@ -133,8 +186,23 @@ def initialize_database():
|
|
| 133 |
updated TEXT NOT NULL
|
| 134 |
)
|
| 135 |
""")
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
initialize_database()
|
|
@@ -144,728 +212,2248 @@ def now_string():
|
|
| 144 |
return datetime.utcnow().isoformat(timespec="seconds")
|
| 145 |
|
| 146 |
|
| 147 |
-
def
|
| 148 |
-
# conversation_id is generated by this app; validate before path use.
|
| 149 |
-
if not re.fullmatch(r"[a-f0-9]{32}", str(conversation_id or "")):
|
| 150 |
-
raise ValueError("Invalid conversation ID.")
|
| 151 |
path = PROJECTS_DIR / conversation_id
|
| 152 |
path.mkdir(parents=True, exist_ok=True)
|
| 153 |
return path
|
| 154 |
|
| 155 |
|
| 156 |
-
def
|
| 157 |
-
conversation_id = uuid.uuid4().hex
|
| 158 |
timestamp = now_string()
|
|
|
|
| 159 |
with db_lock:
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
(
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
|
| 173 |
-
def
|
| 174 |
-
timestamp = now_string()
|
| 175 |
with db_lock:
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
conn.close()
|
| 192 |
|
| 193 |
|
| 194 |
def get_conversation(session_id, conversation_id):
|
| 195 |
-
if not conversation_id:
|
| 196 |
-
return None
|
| 197 |
with db_lock:
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
if not row:
|
| 205 |
return None
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
|
| 212 |
|
| 213 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
with db_lock:
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
"
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
| 227 |
with db_lock:
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
| 233 |
)
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
-
def clear_model_cache():
|
| 242 |
-
old_model = model_cache.get("model")
|
| 243 |
-
old_tokenizer = model_cache.get("tokenizer")
|
| 244 |
-
model_cache.update({"name": None, "tokenizer": None, "model": None})
|
| 245 |
-
del old_model, old_tokenizer
|
| 246 |
gc.collect()
|
|
|
|
| 247 |
if torch.cuda.is_available():
|
| 248 |
torch.cuda.empty_cache()
|
| 249 |
|
| 250 |
|
| 251 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
if model_name not in CODE_MODELS:
|
| 253 |
-
raise ValueError("
|
|
|
|
| 254 |
model_id = CODE_MODELS[model_name]
|
| 255 |
-
with model_lock:
|
| 256 |
-
if model_cache["name"] == model_id and model_cache["model"] is not None:
|
| 257 |
-
return model_cache["tokenizer"], model_cache["model"]
|
| 258 |
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
model = AutoModelForCausalLM.from_pretrained(
|
| 263 |
model_id,
|
| 264 |
-
|
|
|
|
| 265 |
low_cpu_mem_usage=True,
|
| 266 |
-
trust_remote_code=True,
|
| 267 |
)
|
| 268 |
-
|
| 269 |
-
model.to(
|
| 270 |
model.eval()
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
return tokenizer, model
|
| 277 |
|
| 278 |
|
| 279 |
-
#
|
|
|
|
|
|
|
| 280 |
|
| 281 |
def safe_relative_path(value):
|
| 282 |
value = str(value).replace("\\", "/").strip()
|
| 283 |
path = Path(value)
|
|
|
|
| 284 |
if (
|
| 285 |
-
not value
|
| 286 |
-
or
|
|
|
|
|
|
|
| 287 |
):
|
| 288 |
raise ValueError("Unsafe project file path.")
|
|
|
|
| 289 |
return path
|
| 290 |
|
| 291 |
|
| 292 |
def is_text_file(path):
|
| 293 |
-
return
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
def project_files(project_dir):
|
| 299 |
result = []
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
try:
|
| 305 |
-
|
| 306 |
-
size =
|
| 307 |
-
except
|
| 308 |
continue
|
|
|
|
| 309 |
result.append({
|
| 310 |
-
"path":
|
| 311 |
"size": size,
|
| 312 |
-
"text": is_text_file(
|
| 313 |
})
|
|
|
|
| 314 |
return result
|
| 315 |
|
| 316 |
|
| 317 |
def build_project_context(project_dir):
|
| 318 |
files = project_files(project_dir)
|
| 319 |
-
|
| 320 |
-
|
|
|
|
| 321 |
json.dumps(files, ensure_ascii=False, indent=2),
|
| 322 |
-
"
|
|
|
|
| 323 |
]
|
| 324 |
-
|
|
|
|
|
|
|
| 325 |
for item in files:
|
| 326 |
if not item["text"]:
|
| 327 |
continue
|
|
|
|
| 328 |
path = project_dir / item["path"]
|
|
|
|
| 329 |
try:
|
| 330 |
-
content = path.read_text(
|
|
|
|
|
|
|
|
|
|
| 331 |
except Exception:
|
| 332 |
continue
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
break
|
| 337 |
-
sections.append(block)
|
| 338 |
-
used += len(block)
|
| 339 |
-
return "\n".join(sections), files
|
| 340 |
|
|
|
|
|
|
|
| 341 |
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
destination = Path(destination)
|
| 344 |
destination.mkdir(parents=True, exist_ok=True)
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
| 347 |
with zipfile.ZipFile(zip_path, "r") as archive:
|
| 348 |
entries = archive.infolist()
|
|
|
|
| 349 |
if len(entries) > MAX_UPLOAD_FILES:
|
| 350 |
-
raise ValueError("ZIP contains too many
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
| 353 |
continue
|
| 354 |
-
|
| 355 |
-
if
|
| 356 |
-
raise ValueError(
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
target = destination / relative
|
| 361 |
target.parent.mkdir(parents=True, exist_ok=True)
|
| 362 |
-
# Reject symlinks in ZIP archives.
|
| 363 |
-
mode = (entry.external_attr >> 16) & 0o170000
|
| 364 |
-
if mode == 0o120000:
|
| 365 |
-
raise ValueError("ZIP symlinks are not accepted.")
|
| 366 |
-
with archive.open(entry, "r") as src, target.open("wb") as dst:
|
| 367 |
-
shutil.copyfileobj(src, dst)
|
| 368 |
-
count += 1
|
| 369 |
-
return count
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
try:
|
| 374 |
import pytesseract
|
| 375 |
-
|
| 376 |
image = Image.open(image_path).convert("RGB")
|
| 377 |
return pytesseract.image_to_string(image).strip()
|
|
|
|
| 378 |
except ImportError:
|
| 379 |
-
return
|
| 380 |
-
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
def process_uploads(uploaded_files, project_dir):
|
| 385 |
if not uploaded_files:
|
| 386 |
-
return "No
|
|
|
|
| 387 |
project_dir = Path(project_dir)
|
| 388 |
project_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 389 |
messages = []
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
continue
|
|
|
|
| 394 |
if source.stat().st_size > MAX_FILE_SIZE:
|
| 395 |
-
messages.append(
|
|
|
|
|
|
|
| 396 |
continue
|
| 397 |
|
| 398 |
-
|
| 399 |
-
if suffix == ".zip":
|
| 400 |
try:
|
| 401 |
-
count =
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
image_dir = project_dir / "uploaded_images"
|
| 407 |
image_dir.mkdir(exist_ok=True)
|
| 408 |
-
|
| 409 |
-
target = image_dir /
|
| 410 |
shutil.copy2(source, target)
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
)
|
|
|
|
| 415 |
messages.append(
|
| 416 |
-
f"Image
|
| 417 |
-
"OCR text was extracted when Tesseract was available. "
|
| 418 |
-
"This text model does not directly see image pixels."
|
| 419 |
)
|
|
|
|
| 420 |
else:
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
shutil.copy2(source, target)
|
| 424 |
-
messages.append(f"
|
| 425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
|
| 428 |
-
#
|
|
|
|
|
|
|
| 429 |
|
| 430 |
FILE_BLOCK_PATTERN = re.compile(
|
| 431 |
-
r"<<<FILE:\s*(.*?)
|
| 432 |
re.DOTALL | re.IGNORECASE,
|
| 433 |
)
|
| 434 |
|
| 435 |
|
| 436 |
def apply_file_changes(response, project_dir):
|
| 437 |
changed = []
|
|
|
|
| 438 |
for match in FILE_BLOCK_PATTERN.finditer(response):
|
|
|
|
|
|
|
|
|
|
| 439 |
try:
|
| 440 |
-
relative = safe_relative_path(
|
| 441 |
except Exception:
|
| 442 |
continue
|
|
|
|
| 443 |
target = Path(project_dir) / relative
|
| 444 |
-
# Resolve and ensure the destination remains inside the project.
|
| 445 |
-
root = Path(project_dir).resolve()
|
| 446 |
target.parent.mkdir(parents=True, exist_ok=True)
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
target.write_text(content.rstrip() + "\n", encoding="utf-8")
|
| 454 |
changed.append(relative.as_posix())
|
|
|
|
| 455 |
return changed
|
| 456 |
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
messages, tokenize=False, add_generation_prompt=True,
|
| 462 |
-
)
|
| 463 |
-
return "\n\n".join(
|
| 464 |
-
f"{m['role'].upper()}:\n{m['content']}" for m in messages
|
| 465 |
-
) + "\n\nASSISTANT:\n"
|
| 466 |
|
|
|
|
|
|
|
| 467 |
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
tokenizer, model = load_model(model_name)
|
| 471 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 472 |
-
for item in (history or [])[-MAX_HISTORY_MESSAGES:]:
|
| 473 |
-
if (
|
| 474 |
-
isinstance(item, dict)
|
| 475 |
-
and item.get("role") in {"user", "assistant"}
|
| 476 |
-
and isinstance(item.get("content"), str)
|
| 477 |
-
):
|
| 478 |
-
messages.append({"role": item["role"], "content": item["content"]})
|
| 479 |
-
|
| 480 |
-
user_content = prompt
|
| 481 |
-
if project_context:
|
| 482 |
-
user_content = (
|
| 483 |
-
"The user has attached files. Use the project context below if relevant.\n\n"
|
| 484 |
-
+ project_context
|
| 485 |
-
+ "\n\nUSER MESSAGE:\n"
|
| 486 |
-
+ prompt
|
| 487 |
-
+ "\n\nIf the user requests project edits, return complete changed/new files "
|
| 488 |
-
"using the required <<<FILE: path>>> format."
|
| 489 |
-
)
|
| 490 |
-
messages.append({"role": "user", "content": user_content})
|
| 491 |
-
formatted = make_chat_prompt(tokenizer, messages)
|
| 492 |
-
inputs = tokenizer(
|
| 493 |
-
formatted, return_tensors="pt", truncation=True,
|
| 494 |
-
max_length=MAX_INPUT_TOKENS,
|
| 495 |
)
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
)
|
| 503 |
-
kwargs = {
|
| 504 |
-
**inputs,
|
| 505 |
-
"max_new_tokens": max_new,
|
| 506 |
-
"do_sample": temp > 0.15,
|
| 507 |
-
"temperature": temp,
|
| 508 |
-
"pad_token_id": tokenizer.pad_token_id,
|
| 509 |
-
"eos_token_id": tokenizer.eos_token_id,
|
| 510 |
-
"streamer": streamer,
|
| 511 |
-
}
|
| 512 |
-
failures = []
|
| 513 |
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
try:
|
| 522 |
-
|
| 523 |
-
except
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
| 526 |
-
|
| 527 |
-
worker.start()
|
| 528 |
-
for piece in streamer:
|
| 529 |
-
if piece:
|
| 530 |
-
yield piece
|
| 531 |
-
worker.join(timeout=10)
|
| 532 |
-
if failures:
|
| 533 |
-
raise failures[0]
|
| 534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
|
| 536 |
-
|
| 537 |
|
| 538 |
-
|
| 539 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
-
def
|
| 543 |
-
|
| 544 |
-
attachments,
|
| 545 |
model_name,
|
| 546 |
max_tokens,
|
| 547 |
temperature,
|
| 548 |
-
|
| 549 |
conversation_id,
|
| 550 |
request: gr.Request,
|
| 551 |
):
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
|
|
|
|
|
|
| 559 |
return
|
| 560 |
-
|
|
|
|
|
|
|
| 561 |
if not conversation_id:
|
| 562 |
-
|
| 563 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
-
|
| 566 |
-
attachment_status = ""
|
| 567 |
-
if attachments:
|
| 568 |
try:
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
return
|
| 573 |
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
|
|
|
|
|
|
| 600 |
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
)
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
)
|
| 612 |
-
|
| 613 |
-
yield history, conversation_id, status, create_project_zip(project_dir) if changed else None
|
| 614 |
-
except Exception as exc:
|
| 615 |
-
traceback.print_exc()
|
| 616 |
-
error_text = f"**Generation error:** {type(exc).__name__}: {exc}"
|
| 617 |
-
history[-1] = {"role": "assistant", "content": error_text}
|
| 618 |
-
yield history, conversation_id, "Generation failed. Check Space logs.", None
|
| 619 |
|
|
|
|
|
|
|
| 620 |
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
|
|
|
|
| 631 |
|
| 632 |
-
def new_chat():
|
| 633 |
-
return [], None, "New chat started.", None, None
|
| 634 |
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
def refresh_history(request: gr.Request):
|
| 637 |
-
|
|
|
|
| 638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
-
def load_history(conversation_id, request: gr.Request):
|
| 641 |
-
session_id = get_session_id(request)
|
| 642 |
-
conversation = get_conversation(session_id, conversation_id)
|
| 643 |
if not conversation:
|
| 644 |
-
return
|
| 645 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
|
|
|
|
|
|
|
| 647 |
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
| 654 |
)
|
| 655 |
|
|
|
|
| 656 |
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
|
|
|
| 662 |
|
| 663 |
|
| 664 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 665 |
|
| 666 |
CSS = r"""
|
| 667 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
| 668 |
-
|
| 669 |
-
|
|
|
|
|
|
|
|
|
|
| 670 |
body {
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 675 |
}
|
| 676 |
-
.gradio-container { max-width:1180px !important; min-height:100vh !important; margin:auto !important; padding:14px 16px 28px !important; background:transparent !important; }
|
| 677 |
-
#arise-header { position:relative; isolation:isolate; display:flex; align-items:center; justify-content:space-between; gap:14px; padding:17px 20px; border:1px solid rgba(160,128,255,.34); border-radius:22px; background:linear-gradient(115deg,rgba(38,23,75,.92),rgba(10,34,55,.88),rgba(28,19,61,.92)); box-shadow:0 10px 38px rgba(0,0,0,.28), inset 0 0 26px rgba(139,92,246,.08); overflow:hidden; }
|
| 678 |
-
#arise-header:before { content:""; position:absolute; z-index:-1; inset:-80%; background:conic-gradient(from 0deg,transparent 0 28%,rgba(89,225,255,.18) 35%,transparent 43%,rgba(166,117,255,.2) 52%,transparent 60%); animation:headerOrbit 12s linear infinite; }
|
| 679 |
-
#arise-header:after { content:""; position:absolute; inset:0; border-radius:inherit; padding:1px; background:linear-gradient(110deg,rgba(165,130,255,.8),transparent 35%,rgba(66,232,255,.65)); -webkit-mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0); -webkit-mask-composite:xor; mask-composite:exclude; pointer-events:none; }
|
| 680 |
-
@keyframes headerOrbit { to { transform:rotate(360deg); } }
|
| 681 |
-
#brand { font-size:clamp(20px,3vw,27px); font-weight:900; letter-spacing:-1px; text-shadow:0 0 18px rgba(160,128,255,.45); }
|
| 682 |
-
#brand span { background:linear-gradient(90deg,#c6a9ff,#55eaff); color:transparent; -webkit-background-clip:text; background-clip:text; }
|
| 683 |
-
#subbrand { color:#b7bed5; font-size:11px; letter-spacing:1.6px; margin-top:4px; text-transform:uppercase; }
|
| 684 |
-
#chat-shell { max-width:900px; margin:0 auto; }
|
| 685 |
-
#welcome { text-align:center; padding:8vh 12px 30px; animation:welcomeIn .8s ease both; }
|
| 686 |
-
#welcome h1 { font-size:clamp(29px,5vw,46px); font-weight:900; letter-spacing:-1.8px; background:linear-gradient(90deg,#fff,#c4aaff,#64eaff,#fff); background-size:220% auto; color:transparent; -webkit-background-clip:text; background-clip:text; animation:shimmerText 6s linear infinite; }
|
| 687 |
-
#welcome div { color:#aab2cc !important; }
|
| 688 |
-
@keyframes shimmerText { to { background-position:220% center; } }
|
| 689 |
-
@keyframes welcomeIn { from { opacity:0; transform:translateY(16px); } to { opacity:1; transform:translateY(0); } }
|
| 690 |
-
#chatbot { border:1px solid rgba(139,125,190,.18) !important; border-radius:22px !important; background:rgba(9,12,23,.42) !important; min-height:50vh !important; box-shadow:inset 0 0 30px rgba(88,70,150,.05); }
|
| 691 |
-
#chatbot .message { border-radius:17px !important; animation:messageIn .28s ease both; }
|
| 692 |
-
@keyframes messageIn { from { opacity:0; transform:translateY(7px); } to { opacity:1; transform:translateY(0); } }
|
| 693 |
-
#composer { position:sticky; bottom:8px; z-index:10; background:linear-gradient(135deg,rgba(28,30,51,.96),rgba(17,27,43,.96)) !important; border:1px solid rgba(155,123,255,.48) !important; border-radius:25px !important; padding:10px 12px !important; box-shadow:0 0 0 1px rgba(66,232,255,.06),0 8px 35px rgba(0,0,0,.35),0 0 28px rgba(122,85,255,.13); transition:box-shadow .25s,border-color .25s; }
|
| 694 |
-
#composer:focus-within { border-color:rgba(66,232,255,.78) !important; box-shadow:0 0 0 3px rgba(66,232,255,.08),0 0 32px rgba(66,232,255,.16),0 12px 40px rgba(0,0,0,.35); }
|
| 695 |
-
#composer textarea { background:transparent !important; border:0 !important; box-shadow:none !important; color:#f4f4ff !important; font-size:15px !important; padding:8px 5px !important; }
|
| 696 |
-
#composer textarea:focus { box-shadow:none !important; }
|
| 697 |
-
#send-btn { background:linear-gradient(135deg,#a27aff,#4cbbf5) !important; color:#fff !important; border:1px solid rgba(255,255,255,.28) !important; border-radius:50% !important; min-width:44px !important; height:44px !important; font-size:21px !important; font-weight:900 !important; box-shadow:0 0 18px rgba(130,100,255,.42); }
|
| 698 |
-
#send-btn:hover { box-shadow:0 0 28px rgba(66,232,255,.5) !important; transform:translateY(-2px) scale(1.04) !important; }
|
| 699 |
-
#plus-btn { background:linear-gradient(145deg,#333550,#20283b) !important; color:#e7e2ff !important; border:1px solid rgba(160,140,255,.28) !important; border-radius:50% !important; width:42px !important; min-width:42px !important; height:42px !important; font-size:22px !important; box-shadow:inset 0 0 12px rgba(139,92,246,.12); }
|
| 700 |
-
#utility-row { max-width:900px; margin:12px auto; padding:12px 14px; border:1px solid rgba(139,125,190,.2); border-radius:17px; background:rgba(16,19,33,.7); backdrop-filter:blur(14px); }
|
| 701 |
-
#status { color:#aab2ca !important; font-size:12px !important; }
|
| 702 |
-
.live-status { display:inline-flex; align-items:center; gap:9px; color:#c8c2ff; }
|
| 703 |
-
.live-status i { width:8px; height:8px; border-radius:50%; background:#5de8ff; box-shadow:0 0 12px #5de8ff; animation:statusPulse 1s ease-in-out infinite; }
|
| 704 |
-
@keyframes statusPulse { 50% { opacity:.35; transform:scale(.72); } }
|
| 705 |
-
#side-tools { border:1px solid rgba(139,125,190,.25); background:var(--panel); border-radius:18px; padding:14px; }
|
| 706 |
-
#footer { text-align:center; color:#818ba8; font-size:11px; padding:24px 0 8px; letter-spacing:.5px; }
|
| 707 |
-
.gradio-container button { border-radius:12px !important; transition:all .22s ease !important; }
|
| 708 |
-
.gradio-container button:hover { transform:translateY(-2px); box-shadow:0 0 20px rgba(139,92,246,.28); }
|
| 709 |
-
.gradio-container input,.gradio-container textarea,.gradio-container .wrap { border-color:rgba(147,130,204,.25) !important; }
|
| 710 |
-
.gradio-container .accordion, .gradio-container [data-testid="accordion"] { border:1px solid rgba(139,125,190,.22) !important; border-radius:17px !important; background:rgba(16,19,33,.68) !important; }
|
| 711 |
-
@media(max-width:650px) { .gradio-container { padding:8px 7px 18px !important; } #arise-header { padding:14px 12px; border-radius:17px; } #welcome { padding-top:6vh; } #chatbot { min-height:43vh !important; } #composer { border-radius:21px !important; } #utility-row { padding:9px; } }
|
| 712 |
"""
|
| 713 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
with gr.Blocks(title=APP_NAME) as demo:
|
|
|
|
| 715 |
conversation_state = gr.State(None)
|
|
|
|
| 716 |
|
| 717 |
-
gr.HTML(
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
""
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
gr.
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
</div>
|
| 735 |
-
"""
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 749 |
lines=2,
|
| 750 |
-
max_lines=8,
|
| 751 |
-
container=False,
|
| 752 |
-
elem_id="message-box",
|
| 753 |
)
|
|
|
|
| 754 |
with gr.Row():
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 765 |
)
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
)
|
| 771 |
-
send_button = gr.Button("β", variant="primary", elem_id="send-btn", scale=0)
|
| 772 |
|
| 773 |
-
|
| 774 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 775 |
choices=list(CODE_MODELS.keys()),
|
| 776 |
-
value=
|
| 777 |
-
label="Model",
|
| 778 |
-
scale=2,
|
| 779 |
-
min_width=150,
|
| 780 |
)
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
label="
|
|
|
|
|
|
|
| 784 |
)
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
label="
|
|
|
|
|
|
|
| 788 |
)
|
| 789 |
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
|
|
|
| 796 |
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
open_button = gr.Button("Open selected")
|
| 804 |
-
delete_button = gr.Button("Delete selected", variant="stop")
|
| 805 |
-
new_button = gr.Button("οΌ New chat", variant="secondary")
|
| 806 |
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
)
|
| 812 |
|
| 813 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 814 |
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
if files else
|
| 820 |
-
"<span style='color:#8f9099;font-size:12px'>No attachments selected</span>"
|
| 821 |
-
),
|
| 822 |
-
inputs=attach_button,
|
| 823 |
-
outputs=[attachment_state, attachment_label],
|
| 824 |
-
)
|
| 825 |
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
submit_outputs = [chatbot, conversation_state, status_box, project_download]
|
| 831 |
|
| 832 |
-
|
| 833 |
-
fn=chat_submit, inputs=submit_inputs, outputs=submit_outputs,
|
| 834 |
-
)
|
| 835 |
-
message_event = message_box.submit(
|
| 836 |
-
fn=chat_submit, inputs=submit_inputs, outputs=submit_outputs,
|
| 837 |
-
)
|
| 838 |
-
send_event.then(
|
| 839 |
-
fn=lambda: ("", [], "<span style='color:#8f9099;font-size:12px'>οΌ Attach images, ZIPs or project files (optional)</span>"),
|
| 840 |
-
outputs=[message_box, attachment_state, attachment_label],
|
| 841 |
-
)
|
| 842 |
-
message_event.then(
|
| 843 |
-
fn=lambda: ("", [], "<span style='color:#8f9099;font-size:12px'>οΌ Attach images, ZIPs or project files (optional)</span>"),
|
| 844 |
-
outputs=[message_box, attachment_state, attachment_label],
|
| 845 |
-
)
|
| 846 |
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
)
|
| 862 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 863 |
if __name__ == "__main__":
|
| 864 |
-
|
| 865 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 866 |
primary_hue="violet",
|
| 867 |
-
secondary_hue="
|
| 868 |
-
neutral_hue="
|
| 869 |
),
|
| 870 |
css=CSS,
|
| 871 |
show_error=True,
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
import gc
|
| 4 |
import re
|
|
|
|
| 6 |
import uuid
|
| 7 |
import time
|
| 8 |
import shutil
|
| 9 |
+
import random
|
| 10 |
import sqlite3
|
| 11 |
+
import zipfile
|
| 12 |
import threading
|
| 13 |
import traceback
|
| 14 |
from pathlib import Path
|
| 15 |
from datetime import datetime
|
|
|
|
| 16 |
|
| 17 |
+
os.environ["HF_HUB_DISABLE_IMPLICIT_TOKEN"] = "1"
|
| 18 |
+
os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
|
| 19 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 20 |
|
| 21 |
+
import spaces
|
| 22 |
import gradio as gr
|
| 23 |
import torch
|
|
|
|
| 24 |
|
| 25 |
+
from PIL import Image
|
| 26 |
+
|
| 27 |
+
from diffusers import (
|
| 28 |
+
DiffusionPipeline,
|
| 29 |
+
AutoPipelineForImage2Image,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
from transformers import (
|
| 33 |
+
AutoTokenizer,
|
| 34 |
+
AutoModelForCausalLM,
|
| 35 |
+
TextIteratorStreamer,
|
| 36 |
+
StoppingCriteria,
|
| 37 |
+
StoppingCriteriaList,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
from threading import Thread, Event
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# =========================================================
|
| 44 |
+
# CONFIGURATION
|
| 45 |
+
# =========================================================
|
| 46 |
|
| 47 |
APP_NAME = "ARISE AI Studio"
|
| 48 |
DEVELOPER_TELEGRAM = "https://t.me/hardhackar009"
|
| 49 |
|
| 50 |
+
MAX_INPUT_LENGTH = 4096
|
| 51 |
+
MAX_HISTORY_MESSAGES = 10
|
|
|
|
|
|
|
|
|
|
| 52 |
MAX_UPLOAD_FILES = 100
|
| 53 |
MAX_FILE_SIZE = 10 * 1024 * 1024
|
| 54 |
MAX_PROJECT_SIZE = 100 * 1024 * 1024
|
| 55 |
+
MAX_CONTEXT_SIZE = 80000
|
|
|
|
| 56 |
|
| 57 |
+
IMAGE_DTYPE = torch.float16
|
| 58 |
+
|
| 59 |
+
# For persistent Hugging Face Storage, mount storage at /data.
|
| 60 |
DATA_DIR = Path(os.getenv("ARISE_DATA_DIR", "/data/arise_ai_studio"))
|
| 61 |
+
|
| 62 |
try:
|
| 63 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 64 |
+
test_file = DATA_DIR / ".write_test"
|
| 65 |
+
test_file.write_text("ok")
|
| 66 |
+
test_file.unlink()
|
| 67 |
except Exception:
|
| 68 |
DATA_DIR = Path("./arise_ai_studio_data")
|
| 69 |
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 70 |
|
| 71 |
DB_PATH = DATA_DIR / "arise_history.db"
|
| 72 |
PROJECTS_DIR = DATA_DIR / "projects"
|
|
|
|
| 73 |
PROJECTS_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 74 |
|
| 75 |
+
SUPPORTED_TEXT_EXTENSIONS = {
|
| 76 |
+
".py", ".js", ".jsx", ".ts", ".tsx",
|
| 77 |
+
".html", ".htm", ".css", ".scss",
|
| 78 |
+
".json", ".xml", ".yaml", ".yml",
|
| 79 |
+
".md", ".txt", ".java", ".kt",
|
| 80 |
+
".gradle", ".properties", ".sh",
|
| 81 |
+
".bat", ".c", ".cpp", ".h", ".hpp",
|
| 82 |
+
".php", ".sql", ".go", ".rs",
|
| 83 |
+
".swift", ".dart", ".vue", ".svelte",
|
| 84 |
+
".toml", ".ini", ".cfg", ".env.example",
|
| 85 |
+
".gitignore", ".dockerfile",
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
EXCLUDED_DIRS = {
|
| 89 |
+
".git", ".gradle", ".idea", ".vscode",
|
| 90 |
+
"__pycache__", "node_modules", "build",
|
| 91 |
+
"dist", ".venv", "venv", ".next",
|
| 92 |
}
|
| 93 |
|
|
|
|
| 94 |
|
| 95 |
+
# =========================================================
|
| 96 |
+
# IMAGE MODELS
|
| 97 |
+
# =========================================================
|
| 98 |
+
|
| 99 |
+
IMAGE_MODELS = {
|
| 100 |
+
"SDXL Turbo": {
|
| 101 |
+
"id": "stabilityai/sdxl-turbo",
|
| 102 |
+
"steps": 4,
|
| 103 |
+
"guidance": 0.0,
|
| 104 |
+
"max_steps": 4,
|
| 105 |
+
},
|
| 106 |
+
"Stable Diffusion 1.5": {
|
| 107 |
+
"id": "stable-diffusion-v1-5/stable-diffusion-v1-5",
|
| 108 |
+
"steps": 25,
|
| 109 |
+
"guidance": 7.5,
|
| 110 |
+
"max_steps": 30,
|
| 111 |
+
},
|
| 112 |
+
"SD Turbo": {
|
| 113 |
+
"id": "stabilityai/sd-turbo",
|
| 114 |
+
"steps": 4,
|
| 115 |
+
"guidance": 0.0,
|
| 116 |
+
"max_steps": 4,
|
| 117 |
+
},
|
| 118 |
+
"Tiny SD": {
|
| 119 |
+
"id": "segmind/tiny-sd",
|
| 120 |
+
"steps": 20,
|
| 121 |
+
"guidance": 7.5,
|
| 122 |
+
"max_steps": 30,
|
| 123 |
+
},
|
| 124 |
}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
IMAGE_TO_IMAGE_MODELS = {
|
| 128 |
+
"Stable Diffusion 1.5 Img2Img":
|
| 129 |
+
"stable-diffusion-v1-5/stable-diffusion-v1-5",
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# =========================================================
|
| 134 |
+
# CODING MODELS
|
| 135 |
+
# =========================================================
|
| 136 |
+
|
| 137 |
+
CODE_MODELS = {
|
| 138 |
+
"Qwen Coder 0.5B":
|
| 139 |
+
"Qwen/Qwen2.5-Coder-0.5B-Instruct",
|
| 140 |
+
|
| 141 |
+
"Qwen Coder 1.5B":
|
| 142 |
+
"Qwen/Qwen2.5-Coder-1.5B-Instruct",
|
| 143 |
+
|
| 144 |
+
"DeepSeek Coder 1.3B":
|
| 145 |
+
"deepseek-ai/deepseek-coder-1.3b-instruct",
|
| 146 |
+
|
| 147 |
+
"Qwen Coder 3B":
|
| 148 |
+
"Qwen/Qwen2.5-Coder-3B-Instruct",
|
| 149 |
+
|
| 150 |
+
"StarCoder2 3B":
|
| 151 |
+
"bigcode/starcoder2-3b",
|
| 152 |
+
|
| 153 |
+
"CodeGen 350M":
|
| 154 |
+
"Salesforce/codegen-350M-mono",
|
| 155 |
}
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
+
# =========================================================
|
| 159 |
+
# DATABASE
|
| 160 |
+
# =========================================================
|
| 161 |
|
| 162 |
db_lock = threading.RLock()
|
|
|
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
def db_connect():
|
| 166 |
+
connection = sqlite3.connect(
|
| 167 |
+
str(DB_PATH),
|
| 168 |
+
timeout=30,
|
| 169 |
+
check_same_thread=False,
|
| 170 |
+
)
|
| 171 |
+
connection.row_factory = sqlite3.Row
|
| 172 |
+
return connection
|
| 173 |
|
| 174 |
|
| 175 |
def initialize_database():
|
| 176 |
with db_lock:
|
| 177 |
+
connection = db_connect()
|
| 178 |
+
|
| 179 |
+
connection.execute("""
|
| 180 |
CREATE TABLE IF NOT EXISTS conversations (
|
| 181 |
id TEXT PRIMARY KEY,
|
| 182 |
session_id TEXT NOT NULL,
|
|
|
|
| 186 |
updated TEXT NOT NULL
|
| 187 |
)
|
| 188 |
""")
|
| 189 |
+
|
| 190 |
+
connection.execute("""
|
| 191 |
+
CREATE TABLE IF NOT EXISTS jobs (
|
| 192 |
+
id TEXT PRIMARY KEY,
|
| 193 |
+
session_id TEXT NOT NULL,
|
| 194 |
+
conversation_id TEXT NOT NULL,
|
| 195 |
+
status TEXT NOT NULL,
|
| 196 |
+
prompt TEXT NOT NULL,
|
| 197 |
+
response TEXT NOT NULL,
|
| 198 |
+
error TEXT,
|
| 199 |
+
created TEXT NOT NULL,
|
| 200 |
+
updated TEXT NOT NULL
|
| 201 |
+
)
|
| 202 |
+
""")
|
| 203 |
+
|
| 204 |
+
connection.commit()
|
| 205 |
+
connection.close()
|
| 206 |
|
| 207 |
|
| 208 |
initialize_database()
|
|
|
|
| 212 |
return datetime.utcnow().isoformat(timespec="seconds")
|
| 213 |
|
| 214 |
|
| 215 |
+
def get_project_dir(conversation_id):
|
|
|
|
|
|
|
|
|
|
| 216 |
path = PROJECTS_DIR / conversation_id
|
| 217 |
path.mkdir(parents=True, exist_ok=True)
|
| 218 |
return path
|
| 219 |
|
| 220 |
|
| 221 |
+
def save_conversation(session_id, conversation_id, title, history):
|
|
|
|
| 222 |
timestamp = now_string()
|
| 223 |
+
|
| 224 |
with db_lock:
|
| 225 |
+
connection = db_connect()
|
| 226 |
+
|
| 227 |
+
connection.execute("""
|
| 228 |
+
INSERT INTO conversations
|
| 229 |
+
(id, session_id, title, history, created, updated)
|
| 230 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 231 |
+
ON CONFLICT(id) DO UPDATE SET
|
| 232 |
+
title=excluded.title,
|
| 233 |
+
history=excluded.history,
|
| 234 |
+
updated=excluded.updated
|
| 235 |
+
""", (
|
| 236 |
+
conversation_id,
|
| 237 |
+
session_id,
|
| 238 |
+
title,
|
| 239 |
+
json.dumps(history, ensure_ascii=False),
|
| 240 |
+
timestamp,
|
| 241 |
+
timestamp,
|
| 242 |
+
))
|
| 243 |
+
|
| 244 |
+
connection.commit()
|
| 245 |
+
connection.close()
|
| 246 |
|
| 247 |
|
| 248 |
+
def get_conversations(session_id):
|
|
|
|
| 249 |
with db_lock:
|
| 250 |
+
connection = db_connect()
|
| 251 |
+
|
| 252 |
+
rows = connection.execute("""
|
| 253 |
+
SELECT id, title, updated
|
| 254 |
+
FROM conversations
|
| 255 |
+
WHERE session_id=?
|
| 256 |
+
ORDER BY updated DESC
|
| 257 |
+
""", (session_id,)).fetchall()
|
| 258 |
+
|
| 259 |
+
connection.close()
|
| 260 |
+
|
| 261 |
+
return [
|
| 262 |
+
(f"{row['title']} Β· {row['updated']}", row["id"])
|
| 263 |
+
for row in rows
|
| 264 |
+
]
|
|
|
|
| 265 |
|
| 266 |
|
| 267 |
def get_conversation(session_id, conversation_id):
|
|
|
|
|
|
|
| 268 |
with db_lock:
|
| 269 |
+
connection = db_connect()
|
| 270 |
+
|
| 271 |
+
row = connection.execute("""
|
| 272 |
+
SELECT *
|
| 273 |
+
FROM conversations
|
| 274 |
+
WHERE id=? AND session_id=?
|
| 275 |
+
""", (conversation_id, session_id)).fetchone()
|
| 276 |
+
|
| 277 |
+
connection.close()
|
| 278 |
+
|
| 279 |
if not row:
|
| 280 |
return None
|
| 281 |
+
|
| 282 |
+
return {
|
| 283 |
+
"id": row["id"],
|
| 284 |
+
"title": row["title"],
|
| 285 |
+
"history": json.loads(row["history"]),
|
| 286 |
+
}
|
| 287 |
|
| 288 |
|
| 289 |
+
def delete_conversations(session_id, conversation_ids):
|
| 290 |
+
if not conversation_ids:
|
| 291 |
+
return
|
| 292 |
+
|
| 293 |
+
with db_lock:
|
| 294 |
+
connection = db_connect()
|
| 295 |
+
|
| 296 |
+
for conversation_id in conversation_ids:
|
| 297 |
+
connection.execute("""
|
| 298 |
+
DELETE FROM conversations
|
| 299 |
+
WHERE id=? AND session_id=?
|
| 300 |
+
""", (conversation_id, session_id))
|
| 301 |
+
|
| 302 |
+
connection.execute("""
|
| 303 |
+
DELETE FROM jobs
|
| 304 |
+
WHERE conversation_id=? AND session_id=?
|
| 305 |
+
""", (conversation_id, session_id))
|
| 306 |
+
|
| 307 |
+
project_dir = PROJECTS_DIR / conversation_id
|
| 308 |
+
|
| 309 |
+
if project_dir.exists():
|
| 310 |
+
shutil.rmtree(project_dir, ignore_errors=True)
|
| 311 |
+
|
| 312 |
+
connection.commit()
|
| 313 |
+
connection.close()
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def create_conversation(session_id, title="New Project"):
|
| 317 |
+
conversation_id = uuid.uuid4().hex
|
| 318 |
+
save_conversation(
|
| 319 |
+
session_id,
|
| 320 |
+
conversation_id,
|
| 321 |
+
title,
|
| 322 |
+
[],
|
| 323 |
+
)
|
| 324 |
+
get_project_dir(conversation_id)
|
| 325 |
+
return conversation_id
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
# =========================================================
|
| 329 |
+
# JOB DATABASE
|
| 330 |
+
# =========================================================
|
| 331 |
+
|
| 332 |
+
def update_job(job_id, **values):
|
| 333 |
+
if not values:
|
| 334 |
+
return
|
| 335 |
+
|
| 336 |
+
values["updated"] = now_string()
|
| 337 |
+
|
| 338 |
+
assignments = ", ".join(
|
| 339 |
+
f"{key}=?" for key in values
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
parameters = list(values.values()) + [job_id]
|
| 343 |
+
|
| 344 |
with db_lock:
|
| 345 |
+
connection = db_connect()
|
| 346 |
+
connection.execute(
|
| 347 |
+
f"UPDATE jobs SET {assignments} WHERE id=?",
|
| 348 |
+
parameters,
|
| 349 |
+
)
|
| 350 |
+
connection.commit()
|
| 351 |
+
connection.close()
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
def create_job(session_id, conversation_id, prompt):
|
| 355 |
+
job_id = uuid.uuid4().hex
|
| 356 |
+
timestamp = now_string()
|
| 357 |
+
|
| 358 |
with db_lock:
|
| 359 |
+
connection = db_connect()
|
| 360 |
+
|
| 361 |
+
connection.execute("""
|
| 362 |
+
INSERT INTO jobs (
|
| 363 |
+
id, session_id, conversation_id,
|
| 364 |
+
status, prompt, response, error,
|
| 365 |
+
created, updated
|
| 366 |
)
|
| 367 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 368 |
+
""", (
|
| 369 |
+
job_id,
|
| 370 |
+
session_id,
|
| 371 |
+
conversation_id,
|
| 372 |
+
"queued",
|
| 373 |
+
prompt,
|
| 374 |
+
"",
|
| 375 |
+
None,
|
| 376 |
+
timestamp,
|
| 377 |
+
timestamp,
|
| 378 |
+
))
|
| 379 |
+
|
| 380 |
+
connection.commit()
|
| 381 |
+
connection.close()
|
| 382 |
+
|
| 383 |
+
return job_id
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
def get_job(job_id, session_id):
|
| 387 |
+
with db_lock:
|
| 388 |
+
connection = db_connect()
|
| 389 |
+
|
| 390 |
+
row = connection.execute("""
|
| 391 |
+
SELECT *
|
| 392 |
+
FROM jobs
|
| 393 |
+
WHERE id=? AND session_id=?
|
| 394 |
+
""", (job_id, session_id)).fetchone()
|
| 395 |
+
|
| 396 |
+
connection.close()
|
| 397 |
+
|
| 398 |
+
return dict(row) if row else None
|
| 399 |
|
| 400 |
|
| 401 |
+
def get_active_job(session_id, conversation_id):
|
| 402 |
+
with db_lock:
|
| 403 |
+
connection = db_connect()
|
| 404 |
+
|
| 405 |
+
row = connection.execute("""
|
| 406 |
+
SELECT *
|
| 407 |
+
FROM jobs
|
| 408 |
+
WHERE session_id=?
|
| 409 |
+
AND conversation_id=?
|
| 410 |
+
AND status IN ('queued', 'running')
|
| 411 |
+
ORDER BY created DESC
|
| 412 |
+
LIMIT 1
|
| 413 |
+
""", (session_id, conversation_id)).fetchone()
|
| 414 |
+
|
| 415 |
+
connection.close()
|
| 416 |
+
|
| 417 |
+
return dict(row) if row else None
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
# =========================================================
|
| 421 |
+
# MODEL CACHE
|
| 422 |
+
# =========================================================
|
| 423 |
+
|
| 424 |
+
image_cache = {
|
| 425 |
+
"name": None,
|
| 426 |
+
"pipe": None,
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
code_cache = {
|
| 430 |
+
"name": None,
|
| 431 |
+
"tokenizer": None,
|
| 432 |
+
"model": None,
|
| 433 |
+
}
|
| 434 |
+
|
| 435 |
+
image_lock = threading.RLock()
|
| 436 |
+
code_lock = threading.RLock()
|
| 437 |
+
|
| 438 |
+
job_threads = {}
|
| 439 |
+
job_threads_lock = threading.RLock()
|
| 440 |
+
job_stop_events = {}
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
def clear_image_cache():
|
| 444 |
+
old_pipe = image_cache["pipe"]
|
| 445 |
+
|
| 446 |
+
image_cache["pipe"] = None
|
| 447 |
+
image_cache["name"] = None
|
| 448 |
+
|
| 449 |
+
if old_pipe is not None:
|
| 450 |
+
del old_pipe
|
| 451 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
gc.collect()
|
| 453 |
+
|
| 454 |
if torch.cuda.is_available():
|
| 455 |
torch.cuda.empty_cache()
|
| 456 |
|
| 457 |
|
| 458 |
+
def clear_code_cache():
|
| 459 |
+
old_model = code_cache["model"]
|
| 460 |
+
old_tokenizer = code_cache["tokenizer"]
|
| 461 |
+
|
| 462 |
+
code_cache["model"] = None
|
| 463 |
+
code_cache["tokenizer"] = None
|
| 464 |
+
code_cache["name"] = None
|
| 465 |
+
|
| 466 |
+
if old_model is not None:
|
| 467 |
+
del old_model
|
| 468 |
+
|
| 469 |
+
if old_tokenizer is not None:
|
| 470 |
+
del old_tokenizer
|
| 471 |
+
|
| 472 |
+
gc.collect()
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
# =========================================================
|
| 476 |
+
# IMAGE PIPELINE
|
| 477 |
+
# =========================================================
|
| 478 |
+
|
| 479 |
+
def load_image_pipeline(model_id, mode):
|
| 480 |
+
cache_key = f"{mode}:{model_id}"
|
| 481 |
+
|
| 482 |
+
with image_lock:
|
| 483 |
+
if (
|
| 484 |
+
image_cache["name"] == cache_key
|
| 485 |
+
and image_cache["pipe"] is not None
|
| 486 |
+
):
|
| 487 |
+
return image_cache["pipe"]
|
| 488 |
+
|
| 489 |
+
clear_image_cache()
|
| 490 |
+
|
| 491 |
+
if mode == "text":
|
| 492 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 493 |
+
model_id,
|
| 494 |
+
torch_dtype=IMAGE_DTYPE,
|
| 495 |
+
token=False,
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
elif mode == "image":
|
| 499 |
+
pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 500 |
+
model_id,
|
| 501 |
+
torch_dtype=IMAGE_DTYPE,
|
| 502 |
+
token=False,
|
| 503 |
+
)
|
| 504 |
+
|
| 505 |
+
else:
|
| 506 |
+
raise ValueError("Invalid image mode.")
|
| 507 |
+
|
| 508 |
+
pipe = pipe.to("cuda")
|
| 509 |
+
|
| 510 |
+
if hasattr(pipe, "enable_attention_slicing"):
|
| 511 |
+
pipe.enable_attention_slicing()
|
| 512 |
+
|
| 513 |
+
image_cache["pipe"] = pipe
|
| 514 |
+
image_cache["name"] = cache_key
|
| 515 |
+
|
| 516 |
+
return pipe
|
| 517 |
+
|
| 518 |
+
|
| 519 |
+
# =========================================================
|
| 520 |
+
# TEXT TO IMAGE
|
| 521 |
+
# =========================================================
|
| 522 |
+
|
| 523 |
+
@spaces.GPU(duration=120)
|
| 524 |
+
def generate_image(
|
| 525 |
+
prompt,
|
| 526 |
+
negative_prompt,
|
| 527 |
+
model_name,
|
| 528 |
+
width,
|
| 529 |
+
height,
|
| 530 |
+
steps,
|
| 531 |
+
seed,
|
| 532 |
+
random_seed,
|
| 533 |
+
):
|
| 534 |
+
if not prompt or not prompt.strip():
|
| 535 |
+
raise gr.Error("Enter an image prompt.")
|
| 536 |
+
|
| 537 |
+
if model_name not in IMAGE_MODELS:
|
| 538 |
+
raise gr.Error("Invalid image model.")
|
| 539 |
+
|
| 540 |
+
config = IMAGE_MODELS[model_name]
|
| 541 |
+
|
| 542 |
+
if random_seed:
|
| 543 |
+
seed = random.randint(0, 2147483647)
|
| 544 |
+
|
| 545 |
+
seed = int(seed or 0)
|
| 546 |
+
|
| 547 |
+
width = max(256, min(int(width), 768))
|
| 548 |
+
height = max(256, min(int(height), 768))
|
| 549 |
+
|
| 550 |
+
width = (width // 8) * 8
|
| 551 |
+
height = (height // 8) * 8
|
| 552 |
+
|
| 553 |
+
actual_steps = min(
|
| 554 |
+
max(int(steps), 1),
|
| 555 |
+
config["max_steps"],
|
| 556 |
+
)
|
| 557 |
+
|
| 558 |
+
if model_name in ["SDXL Turbo", "SD Turbo"]:
|
| 559 |
+
actual_steps = min(actual_steps, 4)
|
| 560 |
+
|
| 561 |
+
try:
|
| 562 |
+
with image_lock:
|
| 563 |
+
pipe = load_image_pipeline(
|
| 564 |
+
config["id"],
|
| 565 |
+
"text",
|
| 566 |
+
)
|
| 567 |
+
|
| 568 |
+
generator = torch.Generator(
|
| 569 |
+
device="cuda"
|
| 570 |
+
).manual_seed(seed)
|
| 571 |
+
|
| 572 |
+
result = pipe(
|
| 573 |
+
prompt=prompt.strip(),
|
| 574 |
+
negative_prompt=negative_prompt or None,
|
| 575 |
+
width=width,
|
| 576 |
+
height=height,
|
| 577 |
+
num_inference_steps=actual_steps,
|
| 578 |
+
guidance_scale=config["guidance"],
|
| 579 |
+
generator=generator,
|
| 580 |
+
)
|
| 581 |
+
|
| 582 |
+
return result.images[0], seed
|
| 583 |
+
|
| 584 |
+
except Exception as error:
|
| 585 |
+
traceback.print_exc()
|
| 586 |
+
raise gr.Error(f"Image generation failed: {error}")
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
# =========================================================
|
| 590 |
+
# IMAGE TO IMAGE
|
| 591 |
+
# =========================================================
|
| 592 |
+
|
| 593 |
+
@spaces.GPU(duration=120)
|
| 594 |
+
def generate_image_to_image(
|
| 595 |
+
input_image,
|
| 596 |
+
prompt,
|
| 597 |
+
negative_prompt,
|
| 598 |
+
model_name,
|
| 599 |
+
strength,
|
| 600 |
+
steps,
|
| 601 |
+
guidance,
|
| 602 |
+
seed,
|
| 603 |
+
random_seed,
|
| 604 |
+
):
|
| 605 |
+
if input_image is None:
|
| 606 |
+
raise gr.Error("Upload an image.")
|
| 607 |
+
|
| 608 |
+
if not prompt or not prompt.strip():
|
| 609 |
+
raise gr.Error("Enter transformation prompt.")
|
| 610 |
+
|
| 611 |
+
if model_name not in IMAGE_TO_IMAGE_MODELS:
|
| 612 |
+
raise gr.Error("Invalid image model.")
|
| 613 |
+
|
| 614 |
+
if random_seed:
|
| 615 |
+
seed = random.randint(0, 2147483647)
|
| 616 |
+
|
| 617 |
+
seed = int(seed or 0)
|
| 618 |
+
|
| 619 |
+
strength = max(0.05, min(float(strength), 0.95))
|
| 620 |
+
steps = max(1, min(int(steps), 30))
|
| 621 |
+
|
| 622 |
+
try:
|
| 623 |
+
image = input_image.convert("RGB")
|
| 624 |
+
image.thumbnail((768, 768), Image.Resampling.LANCZOS)
|
| 625 |
+
|
| 626 |
+
width = max(256, (image.width // 8) * 8)
|
| 627 |
+
height = max(256, (image.height // 8) * 8)
|
| 628 |
+
|
| 629 |
+
image = image.resize(
|
| 630 |
+
(width, height),
|
| 631 |
+
Image.Resampling.LANCZOS,
|
| 632 |
+
)
|
| 633 |
+
|
| 634 |
+
model_id = IMAGE_TO_IMAGE_MODELS[model_name]
|
| 635 |
+
|
| 636 |
+
with image_lock:
|
| 637 |
+
pipe = load_image_pipeline(model_id, "image")
|
| 638 |
+
|
| 639 |
+
generator = torch.Generator(
|
| 640 |
+
device="cuda"
|
| 641 |
+
).manual_seed(seed)
|
| 642 |
+
|
| 643 |
+
result = pipe(
|
| 644 |
+
prompt=prompt.strip(),
|
| 645 |
+
image=image,
|
| 646 |
+
negative_prompt=negative_prompt or None,
|
| 647 |
+
strength=strength,
|
| 648 |
+
num_inference_steps=steps,
|
| 649 |
+
guidance_scale=float(guidance),
|
| 650 |
+
generator=generator,
|
| 651 |
+
)
|
| 652 |
+
|
| 653 |
+
return result.images[0], seed
|
| 654 |
+
|
| 655 |
+
except Exception as error:
|
| 656 |
+
traceback.print_exc()
|
| 657 |
+
raise gr.Error(f"Image-to-image failed: {error}")
|
| 658 |
+
|
| 659 |
+
|
| 660 |
+
# =========================================================
|
| 661 |
+
# CODING SYSTEM PROMPT
|
| 662 |
+
# =========================================================
|
| 663 |
+
|
| 664 |
+
SYSTEM_PROMPT = """
|
| 665 |
+
You are ARISE AI, a professional software engineering assistant.
|
| 666 |
+
|
| 667 |
+
You specialize in:
|
| 668 |
+
Python, Kotlin, Java, Android, Gradle, AAPT2,
|
| 669 |
+
JavaScript, TypeScript, HTML, CSS, React,
|
| 670 |
+
APIs, databases, Hugging Face, Gradio,
|
| 671 |
+
PyTorch, Diffusers, Transformers, debugging,
|
| 672 |
+
software architecture and code optimization.
|
| 673 |
+
|
| 674 |
+
RULES:
|
| 675 |
+
1. Understand the complete user request.
|
| 676 |
+
2. Generate practical working code.
|
| 677 |
+
3. Preserve existing features.
|
| 678 |
+
4. Never intentionally omit important code.
|
| 679 |
+
5. Analyze errors before proposing fixes.
|
| 680 |
+
6. Return complete files when requested.
|
| 681 |
+
7. Explain dependencies and limitations.
|
| 682 |
+
8. Never claim code was tested unless tested.
|
| 683 |
+
9. Use Markdown.
|
| 684 |
+
10. Avoid unnecessary changes.
|
| 685 |
+
11. Maintain environment compatibility.
|
| 686 |
+
12. Do not invent file contents that were not provided.
|
| 687 |
+
|
| 688 |
+
PROJECT FILE EDITING FORMAT:
|
| 689 |
+
|
| 690 |
+
When modifying project files, return every complete modified or new file
|
| 691 |
+
using exactly this format:
|
| 692 |
+
|
| 693 |
+
<<<FILE: relative/path/to/file.ext>>>
|
| 694 |
+
complete file content here
|
| 695 |
+
<<<END FILE>>>
|
| 696 |
+
|
| 697 |
+
Do not include unchanged files in this format.
|
| 698 |
+
Do not use absolute paths.
|
| 699 |
+
Do not use ../ path traversal.
|
| 700 |
+
Do not omit required imports or code.
|
| 701 |
+
Do not put explanatory text inside a file block.
|
| 702 |
+
|
| 703 |
+
When no file changes are needed, explain the answer normally.
|
| 704 |
+
"""
|
| 705 |
+
|
| 706 |
+
|
| 707 |
+
# =========================================================
|
| 708 |
+
# LOAD CODING MODEL
|
| 709 |
+
# =========================================================
|
| 710 |
+
|
| 711 |
+
def load_code_model(model_name):
|
| 712 |
if model_name not in CODE_MODELS:
|
| 713 |
+
raise ValueError("Invalid coding model.")
|
| 714 |
+
|
| 715 |
model_id = CODE_MODELS[model_name]
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
+
with code_lock:
|
| 718 |
+
if (
|
| 719 |
+
code_cache["name"] == model_id
|
| 720 |
+
and code_cache["model"] is not None
|
| 721 |
+
):
|
| 722 |
+
return (
|
| 723 |
+
code_cache["tokenizer"],
|
| 724 |
+
code_cache["model"],
|
| 725 |
+
)
|
| 726 |
+
|
| 727 |
+
clear_code_cache()
|
| 728 |
+
|
| 729 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 730 |
+
model_id,
|
| 731 |
+
token=False,
|
| 732 |
+
)
|
| 733 |
+
|
| 734 |
model = AutoModelForCausalLM.from_pretrained(
|
| 735 |
model_id,
|
| 736 |
+
token=False,
|
| 737 |
+
torch_dtype=torch.float32,
|
| 738 |
low_cpu_mem_usage=True,
|
|
|
|
| 739 |
)
|
| 740 |
+
|
| 741 |
+
model.to("cpu")
|
| 742 |
model.eval()
|
| 743 |
+
|
| 744 |
+
code_cache["name"] = model_id
|
| 745 |
+
code_cache["tokenizer"] = tokenizer
|
| 746 |
+
code_cache["model"] = model
|
| 747 |
+
|
| 748 |
return tokenizer, model
|
| 749 |
|
| 750 |
|
| 751 |
+
# =========================================================
|
| 752 |
+
# SAFE FILE HELPERS
|
| 753 |
+
# =========================================================
|
| 754 |
|
| 755 |
def safe_relative_path(value):
|
| 756 |
value = str(value).replace("\\", "/").strip()
|
| 757 |
path = Path(value)
|
| 758 |
+
|
| 759 |
if (
|
| 760 |
+
not value
|
| 761 |
+
or path.is_absolute()
|
| 762 |
+
or ".." in path.parts
|
| 763 |
+
or "\x00" in value
|
| 764 |
):
|
| 765 |
raise ValueError("Unsafe project file path.")
|
| 766 |
+
|
| 767 |
return path
|
| 768 |
|
| 769 |
|
| 770 |
def is_text_file(path):
|
| 771 |
+
return (
|
| 772 |
+
path.suffix.lower() in SUPPORTED_TEXT_EXTENSIONS
|
| 773 |
+
or path.name.lower() in {
|
| 774 |
+
"dockerfile", "makefile", ".gitignore"
|
| 775 |
+
}
|
| 776 |
+
)
|
| 777 |
|
| 778 |
|
| 779 |
def project_files(project_dir):
|
| 780 |
result = []
|
| 781 |
+
|
| 782 |
+
for root, dirs, files in os.walk(project_dir):
|
| 783 |
+
dirs[:] = [
|
| 784 |
+
name for name in dirs
|
| 785 |
+
if name not in EXCLUDED_DIRS
|
| 786 |
+
]
|
| 787 |
+
|
| 788 |
+
for filename in files:
|
| 789 |
+
full_path = Path(root) / filename
|
| 790 |
+
|
| 791 |
+
if full_path.name == "project_index.json":
|
| 792 |
+
continue
|
| 793 |
+
|
| 794 |
try:
|
| 795 |
+
relative = full_path.relative_to(project_dir)
|
| 796 |
+
size = full_path.stat().st_size
|
| 797 |
+
except Exception:
|
| 798 |
continue
|
| 799 |
+
|
| 800 |
result.append({
|
| 801 |
+
"path": relative.as_posix(),
|
| 802 |
"size": size,
|
| 803 |
+
"text": is_text_file(full_path),
|
| 804 |
})
|
| 805 |
+
|
| 806 |
return result
|
| 807 |
|
| 808 |
|
| 809 |
def build_project_context(project_dir):
|
| 810 |
files = project_files(project_dir)
|
| 811 |
+
|
| 812 |
+
output = [
|
| 813 |
+
"PROJECT STRUCTURE:",
|
| 814 |
json.dumps(files, ensure_ascii=False, indent=2),
|
| 815 |
+
"",
|
| 816 |
+
"PROJECT FILE CONTENTS:",
|
| 817 |
]
|
| 818 |
+
|
| 819 |
+
total = 0
|
| 820 |
+
|
| 821 |
for item in files:
|
| 822 |
if not item["text"]:
|
| 823 |
continue
|
| 824 |
+
|
| 825 |
path = project_dir / item["path"]
|
| 826 |
+
|
| 827 |
try:
|
| 828 |
+
content = path.read_text(
|
| 829 |
+
encoding="utf-8",
|
| 830 |
+
errors="replace",
|
| 831 |
+
)
|
| 832 |
except Exception:
|
| 833 |
continue
|
| 834 |
+
|
| 835 |
+
section = (
|
| 836 |
+
f"\n\n===== FILE: {item['path']} =====\n"
|
| 837 |
+
f"{content}\n"
|
| 838 |
+
f"===== END FILE =====\n"
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
if total + len(section) > MAX_CONTEXT_SIZE:
|
| 842 |
+
output.append(
|
| 843 |
+
"\n[Project context truncated due to configured context limit.]"
|
| 844 |
+
)
|
| 845 |
break
|
|
|
|
|
|
|
|
|
|
| 846 |
|
| 847 |
+
output.append(section)
|
| 848 |
+
total += len(section)
|
| 849 |
|
| 850 |
+
return "\n".join(output), files
|
| 851 |
+
|
| 852 |
+
|
| 853 |
+
# =========================================================
|
| 854 |
+
# ZIP EXTRACTION
|
| 855 |
+
# =========================================================
|
| 856 |
+
|
| 857 |
+
def extract_zip_safely(zip_path, destination):
|
| 858 |
destination = Path(destination)
|
| 859 |
destination.mkdir(parents=True, exist_ok=True)
|
| 860 |
+
|
| 861 |
+
total_size = 0
|
| 862 |
+
extracted = 0
|
| 863 |
+
|
| 864 |
with zipfile.ZipFile(zip_path, "r") as archive:
|
| 865 |
entries = archive.infolist()
|
| 866 |
+
|
| 867 |
if len(entries) > MAX_UPLOAD_FILES:
|
| 868 |
+
raise ValueError("ZIP contains too many files.")
|
| 869 |
+
|
| 870 |
+
for item in entries:
|
| 871 |
+
relative = safe_relative_path(item.filename)
|
| 872 |
+
|
| 873 |
+
if item.is_dir():
|
| 874 |
continue
|
| 875 |
+
|
| 876 |
+
if item.file_size > MAX_FILE_SIZE:
|
| 877 |
+
raise ValueError(
|
| 878 |
+
f"File too large in ZIP: {item.filename}"
|
| 879 |
+
)
|
| 880 |
+
|
| 881 |
+
total_size += item.file_size
|
| 882 |
+
|
| 883 |
+
if total_size > MAX_PROJECT_SIZE:
|
| 884 |
+
raise ValueError("ZIP project exceeds size limit.")
|
| 885 |
+
|
| 886 |
target = destination / relative
|
| 887 |
target.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 888 |
|
| 889 |
+
with archive.open(item, "r") as source:
|
| 890 |
+
with target.open("wb") as output:
|
| 891 |
+
shutil.copyfileobj(source, output)
|
| 892 |
+
|
| 893 |
+
extracted += 1
|
| 894 |
+
|
| 895 |
+
return extracted
|
| 896 |
|
| 897 |
+
|
| 898 |
+
# =========================================================
|
| 899 |
+
# IMAGE OCR
|
| 900 |
+
# =========================================================
|
| 901 |
+
|
| 902 |
+
def extract_image_text(image_path):
|
| 903 |
try:
|
| 904 |
import pytesseract
|
| 905 |
+
|
| 906 |
image = Image.open(image_path).convert("RGB")
|
| 907 |
return pytesseract.image_to_string(image).strip()
|
| 908 |
+
|
| 909 |
except ImportError:
|
| 910 |
+
return (
|
| 911 |
+
"OCR is not configured. Install pytesseract and the "
|
| 912 |
+
"Tesseract system package to extract image text."
|
| 913 |
+
)
|
| 914 |
+
|
| 915 |
+
except Exception as error:
|
| 916 |
+
return f"Image OCR could not run: {error}"
|
| 917 |
|
| 918 |
|
| 919 |
+
# =========================================================
|
| 920 |
+
# UPLOAD PROCESSING
|
| 921 |
+
# =========================================================
|
| 922 |
+
|
| 923 |
def process_uploads(uploaded_files, project_dir):
|
| 924 |
if not uploaded_files:
|
| 925 |
+
return "No new files uploaded."
|
| 926 |
+
|
| 927 |
project_dir = Path(project_dir)
|
| 928 |
project_dir.mkdir(parents=True, exist_ok=True)
|
| 929 |
+
|
| 930 |
messages = []
|
| 931 |
+
|
| 932 |
+
for uploaded in uploaded_files:
|
| 933 |
+
source = Path(
|
| 934 |
+
getattr(uploaded, "name", uploaded)
|
| 935 |
+
)
|
| 936 |
+
|
| 937 |
+
if not source.exists():
|
| 938 |
continue
|
| 939 |
+
|
| 940 |
if source.stat().st_size > MAX_FILE_SIZE:
|
| 941 |
+
messages.append(
|
| 942 |
+
f"Skipped {source.name}: file exceeds upload limit."
|
| 943 |
+
)
|
| 944 |
continue
|
| 945 |
|
| 946 |
+
if source.suffix.lower() == ".zip":
|
|
|
|
| 947 |
try:
|
| 948 |
+
count = extract_zip_safely(
|
| 949 |
+
source,
|
| 950 |
+
project_dir,
|
| 951 |
+
)
|
| 952 |
+
messages.append(
|
| 953 |
+
f"Extracted {source.name}: {count} files."
|
| 954 |
+
)
|
| 955 |
+
except Exception as error:
|
| 956 |
+
messages.append(
|
| 957 |
+
f"ZIP extraction failed for {source.name}: {error}"
|
| 958 |
+
)
|
| 959 |
+
|
| 960 |
+
elif source.suffix.lower() in {
|
| 961 |
+
".png", ".jpg", ".jpeg", ".webp", ".bmp"
|
| 962 |
+
}:
|
| 963 |
image_dir = project_dir / "uploaded_images"
|
| 964 |
image_dir.mkdir(exist_ok=True)
|
| 965 |
+
|
| 966 |
+
target = image_dir / source.name
|
| 967 |
shutil.copy2(source, target)
|
| 968 |
+
|
| 969 |
+
extracted_text = extract_image_text(target)
|
| 970 |
+
|
| 971 |
+
ocr_path = image_dir / (
|
| 972 |
+
source.stem + "_ocr.txt"
|
| 973 |
+
)
|
| 974 |
+
|
| 975 |
+
ocr_path.write_text(
|
| 976 |
+
extracted_text,
|
| 977 |
+
encoding="utf-8",
|
| 978 |
)
|
| 979 |
+
|
| 980 |
messages.append(
|
| 981 |
+
f"Image added: {source.name}. OCR text saved."
|
|
|
|
|
|
|
| 982 |
)
|
| 983 |
+
|
| 984 |
else:
|
| 985 |
+
target = project_dir / source.name
|
| 986 |
+
|
| 987 |
+
if target.exists():
|
| 988 |
+
target = project_dir / (
|
| 989 |
+
source.stem + "_" + uuid.uuid4().hex[:6]
|
| 990 |
+
+ source.suffix
|
| 991 |
+
)
|
| 992 |
+
|
| 993 |
shutil.copy2(source, target)
|
| 994 |
+
messages.append(f"File added: {target.name}")
|
| 995 |
+
|
| 996 |
+
# Save a first-analysis project index.
|
| 997 |
+
index_path = project_dir / "project_index.json"
|
| 998 |
+
|
| 999 |
+
if not index_path.exists():
|
| 1000 |
+
context, files = build_project_context(project_dir)
|
| 1001 |
+
|
| 1002 |
+
index_path.write_text(
|
| 1003 |
+
json.dumps({
|
| 1004 |
+
"first_analysis": now_string(),
|
| 1005 |
+
"files": files,
|
| 1006 |
+
"analysis_note": (
|
| 1007 |
+
"Initial file inventory created. "
|
| 1008 |
+
"Detailed code understanding is performed "
|
| 1009 |
+
"when the coding model receives the project."
|
| 1010 |
+
),
|
| 1011 |
+
}, ensure_ascii=False, indent=2),
|
| 1012 |
+
encoding="utf-8",
|
| 1013 |
+
)
|
| 1014 |
+
|
| 1015 |
+
return "\n".join(messages)
|
| 1016 |
|
| 1017 |
|
| 1018 |
+
# =========================================================
|
| 1019 |
+
# APPLY MODEL FILE CHANGES
|
| 1020 |
+
# =========================================================
|
| 1021 |
|
| 1022 |
FILE_BLOCK_PATTERN = re.compile(
|
| 1023 |
+
r"<<<FILE:\s*(.*?)>>>\s*(.*?)\s*<<<END FILE>>>",
|
| 1024 |
re.DOTALL | re.IGNORECASE,
|
| 1025 |
)
|
| 1026 |
|
| 1027 |
|
| 1028 |
def apply_file_changes(response, project_dir):
|
| 1029 |
changed = []
|
| 1030 |
+
|
| 1031 |
for match in FILE_BLOCK_PATTERN.finditer(response):
|
| 1032 |
+
relative_name = match.group(1).strip()
|
| 1033 |
+
content = match.group(2)
|
| 1034 |
+
|
| 1035 |
try:
|
| 1036 |
+
relative = safe_relative_path(relative_name)
|
| 1037 |
except Exception:
|
| 1038 |
continue
|
| 1039 |
+
|
| 1040 |
target = Path(project_dir) / relative
|
|
|
|
|
|
|
| 1041 |
target.parent.mkdir(parents=True, exist_ok=True)
|
| 1042 |
+
|
| 1043 |
+
target.write_text(
|
| 1044 |
+
content.rstrip() + "\n",
|
| 1045 |
+
encoding="utf-8",
|
| 1046 |
+
)
|
| 1047 |
+
|
|
|
|
| 1048 |
changed.append(relative.as_posix())
|
| 1049 |
+
|
| 1050 |
return changed
|
| 1051 |
|
| 1052 |
|
| 1053 |
+
# =========================================================
|
| 1054 |
+
# PROJECT DOWNLOADS
|
| 1055 |
+
# =========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1056 |
|
| 1057 |
+
def create_project_zip(project_dir):
|
| 1058 |
+
project_dir = Path(project_dir)
|
| 1059 |
|
| 1060 |
+
zip_path = DATA_DIR / (
|
| 1061 |
+
"ARISE_Project_" + uuid.uuid4().hex[:10] + ".zip"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1062 |
)
|
| 1063 |
+
|
| 1064 |
+
with zipfile.ZipFile(
|
| 1065 |
+
zip_path,
|
| 1066 |
+
"w",
|
| 1067 |
+
zipfile.ZIP_DEFLATED,
|
| 1068 |
+
) as archive:
|
| 1069 |
+
|
| 1070 |
+
for root, dirs, files in os.walk(project_dir):
|
| 1071 |
+
dirs[:] = [
|
| 1072 |
+
name for name in dirs
|
| 1073 |
+
if name not in EXCLUDED_DIRS
|
| 1074 |
+
]
|
| 1075 |
+
|
| 1076 |
+
for filename in files:
|
| 1077 |
+
full_path = Path(root) / filename
|
| 1078 |
+
|
| 1079 |
+
if full_path == zip_path:
|
| 1080 |
+
continue
|
| 1081 |
+
|
| 1082 |
+
archive.write(
|
| 1083 |
+
full_path,
|
| 1084 |
+
full_path.relative_to(project_dir),
|
| 1085 |
+
)
|
| 1086 |
+
|
| 1087 |
+
return str(zip_path)
|
| 1088 |
+
|
| 1089 |
+
|
| 1090 |
+
def create_single_file_download(project_dir, relative_name):
|
| 1091 |
+
relative = safe_relative_path(relative_name)
|
| 1092 |
+
source = Path(project_dir) / relative
|
| 1093 |
+
|
| 1094 |
+
if not source.exists() or not source.is_file():
|
| 1095 |
+
return None
|
| 1096 |
+
|
| 1097 |
+
download_dir = DATA_DIR / "downloads"
|
| 1098 |
+
download_dir.mkdir(exist_ok=True)
|
| 1099 |
+
|
| 1100 |
+
destination = download_dir / (
|
| 1101 |
+
uuid.uuid4().hex[:8] + "_" + source.name
|
| 1102 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1103 |
|
| 1104 |
+
shutil.copy2(source, destination)
|
| 1105 |
+
return str(destination)
|
| 1106 |
+
|
| 1107 |
+
|
| 1108 |
+
# =========================================================
|
| 1109 |
+
# HTML PREVIEW
|
| 1110 |
+
# =========================================================
|
| 1111 |
+
|
| 1112 |
+
def make_html_preview(project_dir):
|
| 1113 |
+
project_dir = Path(project_dir)
|
| 1114 |
+
|
| 1115 |
+
html_files = list(project_dir.rglob("index.html"))
|
| 1116 |
+
|
| 1117 |
+
if not html_files:
|
| 1118 |
+
html_files = list(project_dir.rglob("*.html"))
|
| 1119 |
+
|
| 1120 |
+
if not html_files:
|
| 1121 |
+
return (
|
| 1122 |
+
"<div style='padding:25px;color:#ddd'>"
|
| 1123 |
+
"No HTML file found in this project."
|
| 1124 |
+
"</div>"
|
| 1125 |
+
)
|
| 1126 |
+
|
| 1127 |
+
html_file = html_files[0]
|
| 1128 |
+
|
| 1129 |
+
try:
|
| 1130 |
+
html = html_file.read_text(
|
| 1131 |
+
encoding="utf-8",
|
| 1132 |
+
errors="replace",
|
| 1133 |
+
)
|
| 1134 |
+
|
| 1135 |
+
# Inline local CSS and JavaScript for a basic preview.
|
| 1136 |
+
def replace_asset(match):
|
| 1137 |
+
asset_name = match.group(1).split("?")[0].split("#")[0]
|
| 1138 |
+
asset_path = (
|
| 1139 |
+
html_file.parent / asset_name
|
| 1140 |
+
).resolve()
|
| 1141 |
+
|
| 1142 |
try:
|
| 1143 |
+
asset_path.relative_to(project_dir.resolve())
|
| 1144 |
+
except ValueError:
|
| 1145 |
+
return match.group(0)
|
| 1146 |
+
|
| 1147 |
+
if not asset_path.exists():
|
| 1148 |
+
return match.group(0)
|
| 1149 |
+
|
| 1150 |
+
content = asset_path.read_text(
|
| 1151 |
+
encoding="utf-8",
|
| 1152 |
+
errors="replace",
|
| 1153 |
+
)
|
| 1154 |
+
|
| 1155 |
+
if match.group(0).lower().startswith("<link"):
|
| 1156 |
+
return "<style>\n" + content + "\n</style>"
|
| 1157 |
+
|
| 1158 |
+
return "<script>\n" + content + "\n</script>"
|
| 1159 |
+
|
| 1160 |
+
html = re.sub(
|
| 1161 |
+
r'<link[^>]+href=["\']([^"\']+\.css)["\'][^>]*>',
|
| 1162 |
+
replace_asset,
|
| 1163 |
+
html,
|
| 1164 |
+
flags=re.IGNORECASE,
|
| 1165 |
+
)
|
| 1166 |
+
|
| 1167 |
+
html = re.sub(
|
| 1168 |
+
r'<script[^>]+src=["\']([^"\']+\.js)["\'][^>]*>\s*</script>',
|
| 1169 |
+
replace_asset,
|
| 1170 |
+
html,
|
| 1171 |
+
flags=re.IGNORECASE,
|
| 1172 |
+
)
|
| 1173 |
+
|
| 1174 |
+
return f"""
|
| 1175 |
+
<iframe
|
| 1176 |
+
sandbox="allow-scripts allow-forms"
|
| 1177 |
+
style="
|
| 1178 |
+
width:100%;
|
| 1179 |
+
height:650px;
|
| 1180 |
+
border:1px solid #34415e;
|
| 1181 |
+
border-radius:14px;
|
| 1182 |
+
background:white;
|
| 1183 |
+
"
|
| 1184 |
+
srcdoc="{html.replace('&', '&').replace('"', '"').replace('<', '<').replace('>', '>')}"
|
| 1185 |
+
></iframe>
|
| 1186 |
+
"""
|
| 1187 |
+
|
| 1188 |
+
except Exception as error:
|
| 1189 |
+
return (
|
| 1190 |
+
"<div style='padding:20px;color:#ff9999'>"
|
| 1191 |
+
f"Preview error: {error}"
|
| 1192 |
+
"</div>"
|
| 1193 |
+
)
|
| 1194 |
+
|
| 1195 |
+
|
| 1196 |
+
# =========================================================
|
| 1197 |
+
# STOP CRITERIA
|
| 1198 |
+
# =========================================================
|
| 1199 |
+
|
| 1200 |
+
class StopCriteria(StoppingCriteria):
|
| 1201 |
+
def __init__(self, event):
|
| 1202 |
+
self.event = event
|
| 1203 |
+
|
| 1204 |
+
def __call__(self, input_ids, scores, **kwargs):
|
| 1205 |
+
return self.event.is_set()
|
| 1206 |
+
|
| 1207 |
+
|
| 1208 |
+
# =========================================================
|
| 1209 |
+
# BACKGROUND CODING WORKER
|
| 1210 |
+
# =========================================================
|
| 1211 |
+
|
| 1212 |
+
def run_coding_worker(
|
| 1213 |
+
job_id,
|
| 1214 |
+
session_id,
|
| 1215 |
+
conversation_id,
|
| 1216 |
+
prompt,
|
| 1217 |
+
model_name,
|
| 1218 |
+
max_tokens,
|
| 1219 |
+
temperature,
|
| 1220 |
+
stop_event,
|
| 1221 |
+
):
|
| 1222 |
+
try:
|
| 1223 |
+
update_job(job_id, status="running", response="")
|
| 1224 |
+
|
| 1225 |
+
project_dir = get_project_dir(conversation_id)
|
| 1226 |
+
project_context, files = build_project_context(project_dir)
|
| 1227 |
+
|
| 1228 |
+
tokenizer, model = load_code_model(model_name)
|
| 1229 |
+
|
| 1230 |
+
messages = [
|
| 1231 |
+
{
|
| 1232 |
+
"role": "system",
|
| 1233 |
+
"content": SYSTEM_PROMPT,
|
| 1234 |
+
},
|
| 1235 |
+
]
|
| 1236 |
+
|
| 1237 |
+
conversation = get_conversation(
|
| 1238 |
+
session_id,
|
| 1239 |
+
conversation_id,
|
| 1240 |
+
)
|
| 1241 |
+
|
| 1242 |
+
history = (
|
| 1243 |
+
conversation["history"]
|
| 1244 |
+
if conversation
|
| 1245 |
+
else []
|
| 1246 |
+
)
|
| 1247 |
+
|
| 1248 |
+
for item in history[-MAX_HISTORY_MESSAGES:]:
|
| 1249 |
+
if (
|
| 1250 |
+
isinstance(item, dict)
|
| 1251 |
+
and item.get("role") in ["user", "assistant"]
|
| 1252 |
+
and isinstance(item.get("content"), str)
|
| 1253 |
+
):
|
| 1254 |
+
messages.append({
|
| 1255 |
+
"role": item["role"],
|
| 1256 |
+
"content": item["content"],
|
| 1257 |
+
})
|
| 1258 |
+
|
| 1259 |
+
project_prompt = prompt
|
| 1260 |
+
|
| 1261 |
+
if files:
|
| 1262 |
+
project_prompt = (
|
| 1263 |
+
"PROJECT CONTEXT:\n"
|
| 1264 |
+
+ project_context
|
| 1265 |
+
+ "\n\nUSER REQUEST:\n"
|
| 1266 |
+
+ prompt
|
| 1267 |
+
+ "\n\n"
|
| 1268 |
+
+ "Analyze the supplied project before editing. "
|
| 1269 |
+
+ "Make only the requested changes. "
|
| 1270 |
+
+ "Return every changed/new file using the required "
|
| 1271 |
+
+ "<<<FILE: path>>> format."
|
| 1272 |
+
)
|
| 1273 |
+
|
| 1274 |
+
messages.append({
|
| 1275 |
+
"role": "user",
|
| 1276 |
+
"content": project_prompt,
|
| 1277 |
+
})
|
| 1278 |
+
|
| 1279 |
+
if getattr(tokenizer, "chat_template", None):
|
| 1280 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
| 1281 |
+
messages,
|
| 1282 |
+
tokenize=False,
|
| 1283 |
+
add_generation_prompt=True,
|
| 1284 |
+
)
|
| 1285 |
+
else:
|
| 1286 |
+
formatted_prompt = "\n\n".join(
|
| 1287 |
+
f"{item['role'].upper()}:\n{item['content']}"
|
| 1288 |
+
for item in messages
|
| 1289 |
+
) + "\n\nASSISTANT:\n"
|
| 1290 |
+
|
| 1291 |
+
inputs = tokenizer(
|
| 1292 |
+
formatted_prompt,
|
| 1293 |
+
return_tensors="pt",
|
| 1294 |
+
truncation=True,
|
| 1295 |
+
max_length=MAX_INPUT_LENGTH,
|
| 1296 |
+
)
|
| 1297 |
+
|
| 1298 |
+
streamer = TextIteratorStreamer(
|
| 1299 |
+
tokenizer,
|
| 1300 |
+
skip_prompt=True,
|
| 1301 |
+
skip_special_tokens=True,
|
| 1302 |
+
timeout=120,
|
| 1303 |
+
)
|
| 1304 |
+
|
| 1305 |
+
generation_kwargs = {
|
| 1306 |
+
**inputs,
|
| 1307 |
+
"streamer": streamer,
|
| 1308 |
+
"max_new_tokens": max(128, min(int(max_tokens), 2048)),
|
| 1309 |
+
"do_sample": float(temperature) > 0.05,
|
| 1310 |
+
"temperature": max(float(temperature), 0.05),
|
| 1311 |
+
"pad_token_id": (
|
| 1312 |
+
tokenizer.pad_token_id
|
| 1313 |
+
if tokenizer.pad_token_id is not None
|
| 1314 |
+
else tokenizer.eos_token_id
|
| 1315 |
+
),
|
| 1316 |
+
"stopping_criteria": StoppingCriteriaList([
|
| 1317 |
+
StopCriteria(stop_event)
|
| 1318 |
+
]),
|
| 1319 |
+
}
|
| 1320 |
+
|
| 1321 |
+
errors = []
|
| 1322 |
+
|
| 1323 |
+
def generate():
|
| 1324 |
+
try:
|
| 1325 |
+
with torch.inference_mode():
|
| 1326 |
+
model.generate(**generation_kwargs)
|
| 1327 |
+
except Exception as error:
|
| 1328 |
+
errors.append(str(error))
|
| 1329 |
+
traceback.print_exc()
|
| 1330 |
+
|
| 1331 |
+
try:
|
| 1332 |
+
streamer.end()
|
| 1333 |
+
except Exception:
|
| 1334 |
+
pass
|
| 1335 |
+
|
| 1336 |
+
generation_thread = Thread(
|
| 1337 |
+
target=generate,
|
| 1338 |
+
daemon=True,
|
| 1339 |
+
)
|
| 1340 |
+
generation_thread.start()
|
| 1341 |
+
|
| 1342 |
+
response = ""
|
| 1343 |
+
|
| 1344 |
+
for token in streamer:
|
| 1345 |
+
if stop_event.is_set():
|
| 1346 |
+
break
|
| 1347 |
|
| 1348 |
+
response += token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1349 |
|
| 1350 |
+
update_job(
|
| 1351 |
+
job_id,
|
| 1352 |
+
response=response,
|
| 1353 |
+
status="running",
|
| 1354 |
+
)
|
| 1355 |
+
|
| 1356 |
+
generation_thread.join(timeout=15)
|
| 1357 |
+
|
| 1358 |
+
if generation_thread.is_alive():
|
| 1359 |
+
stop_event.set()
|
| 1360 |
+
|
| 1361 |
+
if errors:
|
| 1362 |
+
raise RuntimeError(errors[0])
|
| 1363 |
+
|
| 1364 |
+
if stop_event.is_set():
|
| 1365 |
+
update_job(
|
| 1366 |
+
job_id,
|
| 1367 |
+
status="stopped",
|
| 1368 |
+
response=response + "\n\nGeneration stopped.",
|
| 1369 |
+
)
|
| 1370 |
+
return
|
| 1371 |
+
|
| 1372 |
+
if not response.strip():
|
| 1373 |
+
response = "No response generated."
|
| 1374 |
+
|
| 1375 |
+
changed_files = apply_file_changes(
|
| 1376 |
+
response,
|
| 1377 |
+
project_dir,
|
| 1378 |
+
)
|
| 1379 |
+
|
| 1380 |
+
history = history + [
|
| 1381 |
+
{
|
| 1382 |
+
"role": "user",
|
| 1383 |
+
"content": prompt,
|
| 1384 |
+
},
|
| 1385 |
+
{
|
| 1386 |
+
"role": "assistant",
|
| 1387 |
+
"content": response,
|
| 1388 |
+
},
|
| 1389 |
+
]
|
| 1390 |
+
|
| 1391 |
+
title = prompt.strip().replace("\n", " ")[:55]
|
| 1392 |
+
if not title:
|
| 1393 |
+
title = "ARISE Project"
|
| 1394 |
+
|
| 1395 |
+
save_conversation(
|
| 1396 |
+
session_id,
|
| 1397 |
+
conversation_id,
|
| 1398 |
+
title,
|
| 1399 |
+
history,
|
| 1400 |
+
)
|
| 1401 |
|
| 1402 |
+
final_response = response
|
| 1403 |
|
| 1404 |
+
if changed_files:
|
| 1405 |
+
file_status = "\n".join(
|
| 1406 |
+
f"- <span style='color:#35d58a'>β</span> `{name}` β Edited"
|
| 1407 |
+
for name in changed_files
|
| 1408 |
+
)
|
| 1409 |
+
|
| 1410 |
+
final_response += (
|
| 1411 |
+
"\n\n---\n### Project File Updates\n"
|
| 1412 |
+
+ file_status
|
| 1413 |
+
)
|
| 1414 |
+
|
| 1415 |
+
update_job(
|
| 1416 |
+
job_id,
|
| 1417 |
+
status="completed",
|
| 1418 |
+
response=final_response,
|
| 1419 |
+
)
|
| 1420 |
+
|
| 1421 |
+
except Exception as error:
|
| 1422 |
+
traceback.print_exc()
|
| 1423 |
|
| 1424 |
+
update_job(
|
| 1425 |
+
job_id,
|
| 1426 |
+
status="error",
|
| 1427 |
+
error=str(error),
|
| 1428 |
+
response=f"**Coding error:** {error}",
|
| 1429 |
+
)
|
| 1430 |
+
|
| 1431 |
+
finally:
|
| 1432 |
+
with job_threads_lock:
|
| 1433 |
+
job_threads.pop(job_id, None)
|
| 1434 |
+
job_stop_events.pop(job_id, None)
|
| 1435 |
+
|
| 1436 |
+
|
| 1437 |
+
# =========================================================
|
| 1438 |
+
# START / MONITOR CODING JOB
|
| 1439 |
+
# =========================================================
|
| 1440 |
|
| 1441 |
+
def coding_assistant(
|
| 1442 |
+
prompt,
|
|
|
|
| 1443 |
model_name,
|
| 1444 |
max_tokens,
|
| 1445 |
temperature,
|
| 1446 |
+
uploaded_files,
|
| 1447 |
conversation_id,
|
| 1448 |
request: gr.Request,
|
| 1449 |
):
|
| 1450 |
+
if not prompt or not prompt.strip():
|
| 1451 |
+
yield (
|
| 1452 |
+
"Please enter your coding request.",
|
| 1453 |
+
conversation_id,
|
| 1454 |
+
"Waiting for prompt.",
|
| 1455 |
+
None,
|
| 1456 |
+
None,
|
| 1457 |
+
None,
|
| 1458 |
+
)
|
| 1459 |
return
|
| 1460 |
+
|
| 1461 |
+
session_id = request.session_hash if request else "default"
|
| 1462 |
+
|
| 1463 |
if not conversation_id:
|
| 1464 |
+
conversation_id = create_conversation(
|
| 1465 |
+
session_id,
|
| 1466 |
+
prompt.strip().replace("\n", " ")[:55],
|
| 1467 |
+
)
|
| 1468 |
+
|
| 1469 |
+
project_dir = get_project_dir(conversation_id)
|
| 1470 |
+
|
| 1471 |
+
upload_status = ""
|
| 1472 |
|
| 1473 |
+
if uploaded_files:
|
|
|
|
|
|
|
| 1474 |
try:
|
| 1475 |
+
upload_status = process_uploads(
|
| 1476 |
+
uploaded_files,
|
| 1477 |
+
project_dir,
|
| 1478 |
+
)
|
| 1479 |
+
except Exception as error:
|
| 1480 |
+
yield (
|
| 1481 |
+
f"Upload error: {error}",
|
| 1482 |
+
conversation_id,
|
| 1483 |
+
"Upload failed.",
|
| 1484 |
+
None,
|
| 1485 |
+
None,
|
| 1486 |
+
None,
|
| 1487 |
+
)
|
| 1488 |
return
|
| 1489 |
|
| 1490 |
+
job_id = create_job(
|
| 1491 |
+
session_id,
|
| 1492 |
+
conversation_id,
|
| 1493 |
+
prompt.strip(),
|
| 1494 |
+
)
|
| 1495 |
+
|
| 1496 |
+
stop_event = Event()
|
| 1497 |
+
|
| 1498 |
+
with job_threads_lock:
|
| 1499 |
+
job_stop_events[job_id] = stop_event
|
| 1500 |
+
|
| 1501 |
+
worker = Thread(
|
| 1502 |
+
target=run_coding_worker,
|
| 1503 |
+
args=(
|
| 1504 |
+
job_id,
|
| 1505 |
+
session_id,
|
| 1506 |
+
conversation_id,
|
| 1507 |
+
prompt.strip(),
|
| 1508 |
+
model_name,
|
| 1509 |
+
max_tokens,
|
| 1510 |
+
temperature,
|
| 1511 |
+
stop_event,
|
| 1512 |
+
),
|
| 1513 |
+
daemon=True,
|
| 1514 |
+
)
|
| 1515 |
+
|
| 1516 |
+
job_threads[job_id] = worker
|
| 1517 |
+
worker.start()
|
| 1518 |
|
| 1519 |
+
yield (
|
| 1520 |
+
"β³ ARISE AI is analyzing your request...",
|
| 1521 |
+
conversation_id,
|
| 1522 |
+
upload_status or "Job started.",
|
| 1523 |
+
None,
|
| 1524 |
+
None,
|
| 1525 |
+
job_id,
|
| 1526 |
+
)
|
| 1527 |
+
|
| 1528 |
+
previous_response = ""
|
| 1529 |
+
|
| 1530 |
+
while True:
|
| 1531 |
+
job = get_job(job_id, session_id)
|
| 1532 |
+
|
| 1533 |
+
if not job:
|
| 1534 |
+
yield (
|
| 1535 |
+
"Job not found.",
|
| 1536 |
+
conversation_id,
|
| 1537 |
+
"Job unavailable.",
|
| 1538 |
+
None,
|
| 1539 |
+
None,
|
| 1540 |
+
job_id,
|
| 1541 |
)
|
| 1542 |
+
return
|
| 1543 |
+
|
| 1544 |
+
response = job["response"] or ""
|
| 1545 |
+
status = job["status"]
|
| 1546 |
+
|
| 1547 |
+
status_text = {
|
| 1548 |
+
"queued": "β³ Waiting in queue",
|
| 1549 |
+
"running": "βοΈ ARISE AI is working",
|
| 1550 |
+
"completed": "β
Coding completed",
|
| 1551 |
+
"stopped": "βΉ Generation stopped",
|
| 1552 |
+
"error": "β Job failed",
|
| 1553 |
+
}.get(status, status)
|
| 1554 |
+
|
| 1555 |
+
if upload_status:
|
| 1556 |
+
status_text += "\n\n" + upload_status
|
| 1557 |
+
|
| 1558 |
+
if response != previous_response or status not in [
|
| 1559 |
+
"queued", "running"
|
| 1560 |
+
]:
|
| 1561 |
+
previous_response = response
|
| 1562 |
+
|
| 1563 |
+
zip_file = None
|
| 1564 |
+
single_file = None
|
| 1565 |
+
preview = None
|
| 1566 |
+
|
| 1567 |
+
if status == "completed":
|
| 1568 |
+
zip_file = create_project_zip(project_dir)
|
| 1569 |
+
|
| 1570 |
+
changed = apply_file_changes(
|
| 1571 |
+
response,
|
| 1572 |
+
project_dir,
|
| 1573 |
+
)
|
| 1574 |
+
|
| 1575 |
+
if changed:
|
| 1576 |
+
single_file = create_single_file_download(
|
| 1577 |
+
project_dir,
|
| 1578 |
+
changed[-1],
|
| 1579 |
+
)
|
| 1580 |
+
|
| 1581 |
+
preview = make_html_preview(project_dir)
|
| 1582 |
+
|
| 1583 |
+
yield (
|
| 1584 |
+
response or "No response yet.",
|
| 1585 |
+
conversation_id,
|
| 1586 |
+
status_text,
|
| 1587 |
+
zip_file,
|
| 1588 |
+
single_file,
|
| 1589 |
+
job_id,
|
| 1590 |
+
)
|
| 1591 |
+
|
| 1592 |
+
if status not in ["queued", "running"]:
|
| 1593 |
+
return
|
| 1594 |
+
|
| 1595 |
+
time.sleep(1)
|
| 1596 |
+
|
| 1597 |
+
|
| 1598 |
+
def resume_job(job_id, request: gr.Request):
|
| 1599 |
+
session_id = request.session_hash if request else "default"
|
| 1600 |
+
|
| 1601 |
+
if not job_id:
|
| 1602 |
+
yield (
|
| 1603 |
+
"No active job selected.",
|
| 1604 |
+
"Nothing to resume.",
|
| 1605 |
+
None,
|
| 1606 |
+
None,
|
| 1607 |
)
|
| 1608 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1609 |
|
| 1610 |
+
while True:
|
| 1611 |
+
job = get_job(job_id, session_id)
|
| 1612 |
|
| 1613 |
+
if not job:
|
| 1614 |
+
yield (
|
| 1615 |
+
"Job not found for this session.",
|
| 1616 |
+
"Job unavailable.",
|
| 1617 |
+
None,
|
| 1618 |
+
None,
|
| 1619 |
+
)
|
| 1620 |
+
return
|
| 1621 |
+
|
| 1622 |
+
status = job["status"]
|
| 1623 |
+
response = job["response"] or ""
|
| 1624 |
+
|
| 1625 |
+
yield (
|
| 1626 |
+
response,
|
| 1627 |
+
f"Job status: {status}",
|
| 1628 |
+
None,
|
| 1629 |
+
None,
|
| 1630 |
+
)
|
| 1631 |
+
|
| 1632 |
+
if status not in ["queued", "running"]:
|
| 1633 |
+
return
|
| 1634 |
+
|
| 1635 |
+
time.sleep(2)
|
| 1636 |
+
|
| 1637 |
+
|
| 1638 |
+
def stop_job(job_id):
|
| 1639 |
+
if not job_id:
|
| 1640 |
+
return "No active job."
|
| 1641 |
+
|
| 1642 |
+
with job_threads_lock:
|
| 1643 |
+
event = job_stop_events.get(job_id)
|
| 1644 |
+
|
| 1645 |
+
if event:
|
| 1646 |
+
event.set()
|
| 1647 |
+
return "βΉ Stop requested."
|
| 1648 |
|
| 1649 |
+
return "No running job found."
|
| 1650 |
|
|
|
|
|
|
|
| 1651 |
|
| 1652 |
+
# =========================================================
|
| 1653 |
+
# HISTORY FUNCTIONS
|
| 1654 |
+
# =========================================================
|
| 1655 |
|
| 1656 |
def refresh_history(request: gr.Request):
|
| 1657 |
+
session_id = request.session_hash if request else "default"
|
| 1658 |
+
choices = get_conversations(session_id)
|
| 1659 |
|
| 1660 |
+
return gr.Dropdown(
|
| 1661 |
+
choices=choices,
|
| 1662 |
+
value=None,
|
| 1663 |
+
)
|
| 1664 |
+
|
| 1665 |
+
|
| 1666 |
+
def load_selected_history(conversation_id, request: gr.Request):
|
| 1667 |
+
session_id = request.session_hash if request else "default"
|
| 1668 |
+
|
| 1669 |
+
if not conversation_id:
|
| 1670 |
+
return (
|
| 1671 |
+
"",
|
| 1672 |
+
None,
|
| 1673 |
+
"Select a history item.",
|
| 1674 |
+
None,
|
| 1675 |
+
)
|
| 1676 |
+
|
| 1677 |
+
conversation = get_conversation(
|
| 1678 |
+
session_id,
|
| 1679 |
+
conversation_id,
|
| 1680 |
+
)
|
| 1681 |
|
|
|
|
|
|
|
|
|
|
| 1682 |
if not conversation:
|
| 1683 |
+
return (
|
| 1684 |
+
"",
|
| 1685 |
+
None,
|
| 1686 |
+
"History not found.",
|
| 1687 |
+
None,
|
| 1688 |
+
)
|
| 1689 |
|
| 1690 |
+
history = conversation["history"]
|
| 1691 |
+
display = ""
|
| 1692 |
|
| 1693 |
+
for item in history:
|
| 1694 |
+
role = item.get("role", "assistant").title()
|
| 1695 |
+
content = item.get("content", "")
|
| 1696 |
+
display += f"\n\n### {role}\n{content}\n"
|
| 1697 |
+
|
| 1698 |
+
active_job = get_active_job(
|
| 1699 |
+
session_id,
|
| 1700 |
+
conversation_id,
|
| 1701 |
)
|
| 1702 |
|
| 1703 |
+
job_id = active_job["id"] if active_job else None
|
| 1704 |
|
| 1705 |
+
return (
|
| 1706 |
+
display or "This conversation is empty.",
|
| 1707 |
+
conversation_id,
|
| 1708 |
+
f"Loaded: {conversation['title']}",
|
| 1709 |
+
job_id,
|
| 1710 |
+
)
|
| 1711 |
|
| 1712 |
|
| 1713 |
+
def delete_selected_history(conversation_ids, request: gr.Request):
|
| 1714 |
+
session_id = request.session_hash if request else "default"
|
| 1715 |
+
|
| 1716 |
+
ids = conversation_ids or []
|
| 1717 |
+
delete_conversations(session_id, ids)
|
| 1718 |
+
|
| 1719 |
+
return (
|
| 1720 |
+
gr.Dropdown(choices=get_conversations(session_id), value=None),
|
| 1721 |
+
"Selected history deleted.",
|
| 1722 |
+
)
|
| 1723 |
+
|
| 1724 |
+
|
| 1725 |
+
def clear_current_conversation():
|
| 1726 |
+
return (
|
| 1727 |
+
"",
|
| 1728 |
+
None,
|
| 1729 |
+
"Ready for a new conversation.",
|
| 1730 |
+
None,
|
| 1731 |
+
)
|
| 1732 |
+
|
| 1733 |
+
|
| 1734 |
+
# =========================================================
|
| 1735 |
+
# HTML PREVIEW REFRESH
|
| 1736 |
+
# =========================================================
|
| 1737 |
+
|
| 1738 |
+
def preview_project(conversation_id):
|
| 1739 |
+
if not conversation_id:
|
| 1740 |
+
return "<p>No project selected.</p>"
|
| 1741 |
+
|
| 1742 |
+
return make_html_preview(
|
| 1743 |
+
get_project_dir(conversation_id)
|
| 1744 |
+
)
|
| 1745 |
+
|
| 1746 |
+
|
| 1747 |
+
# =========================================================
|
| 1748 |
+
# PREMIUM CSS
|
| 1749 |
+
# =========================================================
|
| 1750 |
|
| 1751 |
CSS = r"""
|
| 1752 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap');
|
| 1753 |
+
|
| 1754 |
+
:root {
|
| 1755 |
+
color-scheme: dark;
|
| 1756 |
+
}
|
| 1757 |
+
|
| 1758 |
body {
|
| 1759 |
+
background:
|
| 1760 |
+
radial-gradient(circle at 15% 10%, rgba(105,64,255,.15), transparent 35%),
|
| 1761 |
+
radial-gradient(circle at 90% 80%, rgba(0,205,255,.10), transparent 35%),
|
| 1762 |
+
#080b14 !important;
|
| 1763 |
+
font-family: Inter, sans-serif !important;
|
| 1764 |
+
}
|
| 1765 |
+
|
| 1766 |
+
.gradio-container {
|
| 1767 |
+
width: 100% !important;
|
| 1768 |
+
max-width: 1250px !important;
|
| 1769 |
+
margin: auto !important;
|
| 1770 |
+
background: transparent !important;
|
| 1771 |
+
padding: 12px !important;
|
| 1772 |
+
}
|
| 1773 |
+
|
| 1774 |
+
#main-header {
|
| 1775 |
+
position: relative;
|
| 1776 |
+
overflow: hidden;
|
| 1777 |
+
text-align: center;
|
| 1778 |
+
padding: 38px 16px;
|
| 1779 |
+
border: 1px solid rgba(139,92,246,.35);
|
| 1780 |
+
border-radius: 22px;
|
| 1781 |
+
background: linear-gradient(120deg, rgba(45,26,90,.95), rgba(9,35,57,.95), rgba(30,17,70,.95));
|
| 1782 |
+
animation: bannerGlow 5s ease-in-out infinite alternate;
|
| 1783 |
+
}
|
| 1784 |
+
|
| 1785 |
+
@keyframes bannerGlow {
|
| 1786 |
+
from { box-shadow: 0 0 15px rgba(139,92,246,.15); }
|
| 1787 |
+
to {
|
| 1788 |
+
box-shadow: 0 0 38px rgba(6,214,233,.24), 0 0 65px rgba(139,92,246,.17);
|
| 1789 |
+
}
|
| 1790 |
+
}
|
| 1791 |
+
|
| 1792 |
+
.hero-title {
|
| 1793 |
+
font-size: clamp(29px, 6vw, 56px);
|
| 1794 |
+
font-weight: 900;
|
| 1795 |
+
letter-spacing: -2px;
|
| 1796 |
+
background: linear-gradient(90deg,#fff,#b8a5ff,#56e8ff,#fff);
|
| 1797 |
+
background-size: 300%;
|
| 1798 |
+
-webkit-background-clip: text;
|
| 1799 |
+
-webkit-text-fill-color: transparent;
|
| 1800 |
+
animation: textGradient 5s linear infinite;
|
| 1801 |
+
}
|
| 1802 |
+
|
| 1803 |
+
@keyframes textGradient {
|
| 1804 |
+
0% { background-position: 0%; }
|
| 1805 |
+
100% { background-position: 300%; }
|
| 1806 |
+
}
|
| 1807 |
+
|
| 1808 |
+
.hero-subtitle {
|
| 1809 |
+
color: #bdc7df;
|
| 1810 |
+
font-size: 13px;
|
| 1811 |
+
letter-spacing: 2px;
|
| 1812 |
+
margin-top: 12px;
|
| 1813 |
+
}
|
| 1814 |
+
|
| 1815 |
+
#menu-panel {
|
| 1816 |
+
border: 1px solid rgba(139,92,246,.35);
|
| 1817 |
+
background: rgba(16,22,39,.95);
|
| 1818 |
+
border-radius: 14px;
|
| 1819 |
+
padding: 12px;
|
| 1820 |
+
}
|
| 1821 |
+
|
| 1822 |
+
.gradio-container .tab-nav {
|
| 1823 |
+
border: 1px solid rgba(139,92,246,.25);
|
| 1824 |
+
border-radius: 15px;
|
| 1825 |
+
background: rgba(18,24,40,.8);
|
| 1826 |
+
padding: 7px;
|
| 1827 |
+
}
|
| 1828 |
+
|
| 1829 |
+
.gradio-container button {
|
| 1830 |
+
border-radius: 12px !important;
|
| 1831 |
+
transition: all .25s ease !important;
|
| 1832 |
+
}
|
| 1833 |
+
|
| 1834 |
+
.gradio-container button:hover {
|
| 1835 |
+
transform: translateY(-2px);
|
| 1836 |
+
box-shadow: 0 0 20px rgba(139,92,246,.35);
|
| 1837 |
+
}
|
| 1838 |
+
|
| 1839 |
+
.primary {
|
| 1840 |
+
background: linear-gradient(100deg,#7045ed,#168edb) !important;
|
| 1841 |
+
color: white !important;
|
| 1842 |
+
font-weight: 800 !important;
|
| 1843 |
+
}
|
| 1844 |
+
|
| 1845 |
+
textarea, input {
|
| 1846 |
+
border-radius: 12px !important;
|
| 1847 |
+
}
|
| 1848 |
+
|
| 1849 |
+
#coding-output {
|
| 1850 |
+
min-height: 300px;
|
| 1851 |
+
padding: 15px;
|
| 1852 |
+
border: 1px solid rgba(6,214,233,.22);
|
| 1853 |
+
border-radius: 15px;
|
| 1854 |
+
background: rgba(9,15,28,.8);
|
| 1855 |
+
overflow-wrap: anywhere;
|
| 1856 |
+
}
|
| 1857 |
+
|
| 1858 |
+
#coding-output pre {
|
| 1859 |
+
max-width: 100%;
|
| 1860 |
+
overflow-x: auto;
|
| 1861 |
+
}
|
| 1862 |
+
|
| 1863 |
+
#footer {
|
| 1864 |
+
text-align: center;
|
| 1865 |
+
color: #8390ad;
|
| 1866 |
+
padding: 20px;
|
| 1867 |
+
}
|
| 1868 |
+
|
| 1869 |
+
@media(max-width:650px) {
|
| 1870 |
+
.gradio-container {
|
| 1871 |
+
padding: 6px !important;
|
| 1872 |
+
}
|
| 1873 |
+
|
| 1874 |
+
#main-header {
|
| 1875 |
+
padding: 28px 10px;
|
| 1876 |
+
border-radius: 16px;
|
| 1877 |
+
}
|
| 1878 |
+
|
| 1879 |
+
.hero-title {
|
| 1880 |
+
font-size: 30px;
|
| 1881 |
+
letter-spacing: -1px;
|
| 1882 |
+
}
|
| 1883 |
+
|
| 1884 |
+
.hero-subtitle {
|
| 1885 |
+
font-size: 10px;
|
| 1886 |
+
letter-spacing: 1px;
|
| 1887 |
+
}
|
| 1888 |
+
|
| 1889 |
+
.gradio-container .tab-nav button {
|
| 1890 |
+
font-size: 11px !important;
|
| 1891 |
+
padding: 8px 5px !important;
|
| 1892 |
+
}
|
| 1893 |
+
|
| 1894 |
+
#coding-output {
|
| 1895 |
+
min-height: 230px;
|
| 1896 |
+
padding: 10px;
|
| 1897 |
+
}
|
| 1898 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1899 |
"""
|
| 1900 |
|
| 1901 |
+
|
| 1902 |
+
# =========================================================
|
| 1903 |
+
# HEADER
|
| 1904 |
+
# =========================================================
|
| 1905 |
+
|
| 1906 |
+
HEADER = f"""
|
| 1907 |
+
<div id="main-header">
|
| 1908 |
+
<div style="
|
| 1909 |
+
font-size:12px;
|
| 1910 |
+
color:#83eaff;
|
| 1911 |
+
letter-spacing:3px;
|
| 1912 |
+
font-weight:800;
|
| 1913 |
+
margin-bottom:12px;
|
| 1914 |
+
">
|
| 1915 |
+
NEXT GENERATION ARTIFICIAL INTELLIGENCE
|
| 1916 |
+
</div>
|
| 1917 |
+
|
| 1918 |
+
<div class="hero-title">β‘ ARISE AI STUDIO</div>
|
| 1919 |
+
|
| 1920 |
+
<div class="hero-subtitle">
|
| 1921 |
+
CREATE Β· TRANSFORM Β· CODE Β· INNOVATE
|
| 1922 |
+
</div>
|
| 1923 |
+
|
| 1924 |
+
<div style="
|
| 1925 |
+
display:flex;
|
| 1926 |
+
justify-content:center;
|
| 1927 |
+
flex-wrap:wrap;
|
| 1928 |
+
gap:8px;
|
| 1929 |
+
margin-top:22px;
|
| 1930 |
+
">
|
| 1931 |
+
<span style="background:#ffffff15;padding:8px 13px;border-radius:30px;color:#d8d5ff;font-size:12px">π¨ AI Art</span>
|
| 1932 |
+
<span style="background:#ffffff15;padding:8px 13px;border-radius:30px;color:#d8d5ff;font-size:12px">πΌ Image Transform</span>
|
| 1933 |
+
<span style="background:#ffffff15;padding:8px 13px;border-radius:30px;color:#d8d5ff;font-size:12px">π» AI Coding</span>
|
| 1934 |
+
</div>
|
| 1935 |
+
</div>
|
| 1936 |
+
"""
|
| 1937 |
+
|
| 1938 |
+
|
| 1939 |
+
# =========================================================
|
| 1940 |
+
# GRADIO APPLICATION
|
| 1941 |
+
# =========================================================
|
| 1942 |
+
|
| 1943 |
with gr.Blocks(title=APP_NAME) as demo:
|
| 1944 |
+
|
| 1945 |
conversation_state = gr.State(None)
|
| 1946 |
+
job_state = gr.State(None)
|
| 1947 |
|
| 1948 |
+
gr.HTML(HEADER)
|
| 1949 |
+
|
| 1950 |
+
with gr.Row():
|
| 1951 |
+
menu_button = gr.Button("β° Menu", size="sm")
|
| 1952 |
+
gr.Markdown(
|
| 1953 |
+
f"[Developer Contact β]({DEVELOPER_TELEGRAM})"
|
| 1954 |
+
)
|
| 1955 |
+
|
| 1956 |
+
with gr.Column(visible=False, elem_id="menu-panel") as menu_panel:
|
| 1957 |
+
gr.Markdown("### ARISE AI Studio Menu")
|
| 1958 |
+
|
| 1959 |
+
gr.Markdown(
|
| 1960 |
+
f"**Developer Contact:** [Telegram @hardhackar009]({DEVELOPER_TELEGRAM})"
|
| 1961 |
+
)
|
| 1962 |
+
|
| 1963 |
+
gr.Markdown(
|
| 1964 |
+
"Open the History tab to load, select or delete saved conversations."
|
| 1965 |
+
)
|
| 1966 |
+
|
| 1967 |
+
menu_button.click(
|
| 1968 |
+
lambda: gr.update(visible=True),
|
| 1969 |
+
outputs=menu_panel,
|
| 1970 |
+
)
|
| 1971 |
+
|
| 1972 |
+
gr.Markdown(
|
| 1973 |
+
"""
|
| 1974 |
+
<div style="text-align:center;color:#9ba9c5;padding:10px">
|
| 1975 |
+
Premium AI Workspace Β· Open Models Β· Live Generation
|
| 1976 |
</div>
|
| 1977 |
+
"""
|
| 1978 |
+
)
|
| 1979 |
+
|
| 1980 |
+
with gr.Tabs():
|
| 1981 |
+
|
| 1982 |
+
# =================================================
|
| 1983 |
+
# TEXT TO IMAGE
|
| 1984 |
+
# =================================================
|
| 1985 |
+
|
| 1986 |
+
with gr.Tab("π¨ Text to Image"):
|
| 1987 |
+
|
| 1988 |
+
gr.Markdown("## Create Images with AI")
|
| 1989 |
+
|
| 1990 |
+
image_model = gr.Dropdown(
|
| 1991 |
+
choices=list(IMAGE_MODELS.keys()),
|
| 1992 |
+
value="SDXL Turbo",
|
| 1993 |
+
label="Select Image Model",
|
| 1994 |
+
)
|
| 1995 |
+
|
| 1996 |
+
image_prompt = gr.Textbox(
|
| 1997 |
+
label="Image Prompt",
|
| 1998 |
+
placeholder="Describe your image...",
|
| 1999 |
+
lines=4,
|
| 2000 |
+
)
|
| 2001 |
+
|
| 2002 |
+
negative_prompt = gr.Textbox(
|
| 2003 |
+
label="Negative Prompt",
|
| 2004 |
lines=2,
|
|
|
|
|
|
|
|
|
|
| 2005 |
)
|
| 2006 |
+
|
| 2007 |
with gr.Row():
|
| 2008 |
+
width = gr.Slider(
|
| 2009 |
+
256, 768,
|
| 2010 |
+
value=512,
|
| 2011 |
+
step=64,
|
| 2012 |
+
label="Image Width",
|
| 2013 |
+
)
|
| 2014 |
+
|
| 2015 |
+
height = gr.Slider(
|
| 2016 |
+
256, 768,
|
| 2017 |
+
value=512,
|
| 2018 |
+
step=64,
|
| 2019 |
+
label="Image Height",
|
| 2020 |
+
)
|
| 2021 |
+
|
| 2022 |
+
steps = gr.Slider(
|
| 2023 |
+
1, 30,
|
| 2024 |
+
value=4,
|
| 2025 |
+
step=1,
|
| 2026 |
+
label="Inference Steps",
|
| 2027 |
+
)
|
| 2028 |
+
|
| 2029 |
+
with gr.Row():
|
| 2030 |
+
seed = gr.Number(
|
| 2031 |
+
value=0,
|
| 2032 |
+
precision=0,
|
| 2033 |
+
label="Seed",
|
| 2034 |
)
|
| 2035 |
+
|
| 2036 |
+
random_seed = gr.Checkbox(
|
| 2037 |
+
value=True,
|
| 2038 |
+
label="Random Seed",
|
| 2039 |
)
|
|
|
|
| 2040 |
|
| 2041 |
+
generate_button = gr.Button(
|
| 2042 |
+
"β¨ Generate Image",
|
| 2043 |
+
variant="primary",
|
| 2044 |
+
)
|
| 2045 |
+
|
| 2046 |
+
image_output = gr.Image(
|
| 2047 |
+
label="Generated Artwork",
|
| 2048 |
+
type="pil",
|
| 2049 |
+
height=400,
|
| 2050 |
+
)
|
| 2051 |
+
|
| 2052 |
+
seed_output = gr.Number(
|
| 2053 |
+
label="Used Seed",
|
| 2054 |
+
precision=0,
|
| 2055 |
+
)
|
| 2056 |
+
|
| 2057 |
+
generate_button.click(
|
| 2058 |
+
fn=generate_image,
|
| 2059 |
+
inputs=[
|
| 2060 |
+
image_prompt,
|
| 2061 |
+
negative_prompt,
|
| 2062 |
+
image_model,
|
| 2063 |
+
width,
|
| 2064 |
+
height,
|
| 2065 |
+
steps,
|
| 2066 |
+
seed,
|
| 2067 |
+
random_seed,
|
| 2068 |
+
],
|
| 2069 |
+
outputs=[
|
| 2070 |
+
image_output,
|
| 2071 |
+
seed_output,
|
| 2072 |
+
],
|
| 2073 |
+
)
|
| 2074 |
+
|
| 2075 |
+
# =================================================
|
| 2076 |
+
# IMAGE TO IMAGE
|
| 2077 |
+
# =================================================
|
| 2078 |
+
|
| 2079 |
+
with gr.Tab("πΌ Image to Image"):
|
| 2080 |
+
|
| 2081 |
+
gr.Markdown("## Transform Your Images")
|
| 2082 |
+
|
| 2083 |
+
input_image = gr.Image(
|
| 2084 |
+
label="Upload Original Image",
|
| 2085 |
+
type="pil",
|
| 2086 |
+
height=300,
|
| 2087 |
+
)
|
| 2088 |
+
|
| 2089 |
+
img2img_model = gr.Dropdown(
|
| 2090 |
+
choices=list(IMAGE_TO_IMAGE_MODELS.keys()),
|
| 2091 |
+
value="Stable Diffusion 1.5 Img2Img",
|
| 2092 |
+
label="Image-to-Image Model",
|
| 2093 |
+
)
|
| 2094 |
+
|
| 2095 |
+
img2img_prompt = gr.Textbox(
|
| 2096 |
+
label="Transformation Prompt",
|
| 2097 |
+
placeholder="Describe the new image style...",
|
| 2098 |
+
lines=4,
|
| 2099 |
+
)
|
| 2100 |
+
|
| 2101 |
+
img2img_negative = gr.Textbox(
|
| 2102 |
+
label="Negative Prompt",
|
| 2103 |
+
lines=2,
|
| 2104 |
+
)
|
| 2105 |
+
|
| 2106 |
+
strength = gr.Slider(
|
| 2107 |
+
0.05, 0.95,
|
| 2108 |
+
value=0.70,
|
| 2109 |
+
step=0.05,
|
| 2110 |
+
label="Transformation Strength",
|
| 2111 |
+
)
|
| 2112 |
+
|
| 2113 |
+
with gr.Row():
|
| 2114 |
+
img2img_steps = gr.Slider(
|
| 2115 |
+
1, 30,
|
| 2116 |
+
value=25,
|
| 2117 |
+
step=1,
|
| 2118 |
+
label="Inference Steps",
|
| 2119 |
+
)
|
| 2120 |
+
|
| 2121 |
+
img2img_guidance = gr.Slider(
|
| 2122 |
+
1, 15,
|
| 2123 |
+
value=7.5,
|
| 2124 |
+
step=0.5,
|
| 2125 |
+
label="Guidance Scale",
|
| 2126 |
+
)
|
| 2127 |
+
|
| 2128 |
+
with gr.Row():
|
| 2129 |
+
img2img_seed = gr.Number(
|
| 2130 |
+
value=0,
|
| 2131 |
+
precision=0,
|
| 2132 |
+
label="Seed",
|
| 2133 |
+
)
|
| 2134 |
+
|
| 2135 |
+
img2img_random = gr.Checkbox(
|
| 2136 |
+
value=True,
|
| 2137 |
+
label="Random Seed",
|
| 2138 |
+
)
|
| 2139 |
+
|
| 2140 |
+
img2img_button = gr.Button(
|
| 2141 |
+
"πͺ Transform Image",
|
| 2142 |
+
variant="primary",
|
| 2143 |
+
)
|
| 2144 |
+
|
| 2145 |
+
img2img_output = gr.Image(
|
| 2146 |
+
label="Transformed Image",
|
| 2147 |
+
type="pil",
|
| 2148 |
+
height=400,
|
| 2149 |
+
)
|
| 2150 |
+
|
| 2151 |
+
img2img_seed_output = gr.Number(
|
| 2152 |
+
label="Used Seed",
|
| 2153 |
+
precision=0,
|
| 2154 |
+
)
|
| 2155 |
+
|
| 2156 |
+
img2img_button.click(
|
| 2157 |
+
fn=generate_image_to_image,
|
| 2158 |
+
inputs=[
|
| 2159 |
+
input_image,
|
| 2160 |
+
img2img_prompt,
|
| 2161 |
+
img2img_negative,
|
| 2162 |
+
img2img_model,
|
| 2163 |
+
strength,
|
| 2164 |
+
img2img_steps,
|
| 2165 |
+
img2img_guidance,
|
| 2166 |
+
img2img_seed,
|
| 2167 |
+
img2img_random,
|
| 2168 |
+
],
|
| 2169 |
+
outputs=[
|
| 2170 |
+
img2img_output,
|
| 2171 |
+
img2img_seed_output,
|
| 2172 |
+
],
|
| 2173 |
+
)
|
| 2174 |
+
|
| 2175 |
+
# =================================================
|
| 2176 |
+
# AI CODING
|
| 2177 |
+
# =================================================
|
| 2178 |
+
|
| 2179 |
+
with gr.Tab("π» AI Coding"):
|
| 2180 |
+
|
| 2181 |
+
gr.Markdown(
|
| 2182 |
+
"""
|
| 2183 |
+
## ARISE AI Coding Workspace
|
| 2184 |
+
|
| 2185 |
+
Upload an existing project, describe your changes,
|
| 2186 |
+
and download the updated project.
|
| 2187 |
+
"""
|
| 2188 |
+
)
|
| 2189 |
+
|
| 2190 |
+
code_model = gr.Dropdown(
|
| 2191 |
choices=list(CODE_MODELS.keys()),
|
| 2192 |
+
value="Qwen Coder 0.5B",
|
| 2193 |
+
label="Select Coding Model",
|
|
|
|
|
|
|
| 2194 |
)
|
| 2195 |
+
|
| 2196 |
+
project_upload = gr.File(
|
| 2197 |
+
label="οΌ Upload ZIP, Images or Project Files",
|
| 2198 |
+
file_count="multiple",
|
| 2199 |
+
type="filepath",
|
| 2200 |
)
|
| 2201 |
+
|
| 2202 |
+
code_prompt = gr.Textbox(
|
| 2203 |
+
label="Your Coding Request",
|
| 2204 |
+
placeholder="Example: Change the app theme and update the home screen...",
|
| 2205 |
+
lines=5,
|
| 2206 |
)
|
| 2207 |
|
| 2208 |
+
with gr.Row():
|
| 2209 |
+
max_tokens = gr.Slider(
|
| 2210 |
+
128, 2048,
|
| 2211 |
+
value=768,
|
| 2212 |
+
step=128,
|
| 2213 |
+
label="Maximum Output Tokens",
|
| 2214 |
+
)
|
| 2215 |
|
| 2216 |
+
temperature = gr.Slider(
|
| 2217 |
+
0.1, 1.0,
|
| 2218 |
+
value=0.3,
|
| 2219 |
+
step=0.1,
|
| 2220 |
+
label="Temperature",
|
| 2221 |
+
)
|
|
|
|
|
|
|
|
|
|
| 2222 |
|
| 2223 |
+
coding_output = gr.Markdown(
|
| 2224 |
+
value="Ready to work on your project.",
|
| 2225 |
+
elem_id="coding-output",
|
| 2226 |
+
)
|
|
|
|
| 2227 |
|
| 2228 |
+
with gr.Row():
|
| 2229 |
+
generate_code = gr.Button(
|
| 2230 |
+
"β‘ Start Coding",
|
| 2231 |
+
variant="primary",
|
| 2232 |
+
)
|
| 2233 |
|
| 2234 |
+
stop_button = gr.Button(
|
| 2235 |
+
"βΉ Stop",
|
| 2236 |
+
variant="stop",
|
| 2237 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2238 |
|
| 2239 |
+
new_project_button = gr.Button(
|
| 2240 |
+
"οΌ New Project",
|
| 2241 |
+
variant="secondary",
|
| 2242 |
+
)
|
|
|
|
| 2243 |
|
| 2244 |
+
coding_status = gr.Markdown("Ready.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2245 |
|
| 2246 |
+
gr.Markdown("### Project Downloads")
|
| 2247 |
+
|
| 2248 |
+
with gr.Row():
|
| 2249 |
+
project_zip_download = gr.File(
|
| 2250 |
+
label="Download Full Project ZIP",
|
| 2251 |
+
interactive=False,
|
| 2252 |
+
)
|
| 2253 |
+
|
| 2254 |
+
single_file_download = gr.File(
|
| 2255 |
+
label="Download Last Edited File",
|
| 2256 |
+
interactive=False,
|
| 2257 |
+
)
|
| 2258 |
+
|
| 2259 |
+
preview_button = gr.Button(
|
| 2260 |
+
"π Preview HTML Project",
|
| 2261 |
+
)
|
| 2262 |
+
|
| 2263 |
+
html_preview = gr.HTML(
|
| 2264 |
+
value="<div style='padding:15px;color:#aaa'>HTML preview will appear here.</div>"
|
| 2265 |
+
)
|
| 2266 |
+
|
| 2267 |
+
generate_code.click(
|
| 2268 |
+
fn=coding_assistant,
|
| 2269 |
+
inputs=[
|
| 2270 |
+
code_prompt,
|
| 2271 |
+
code_model,
|
| 2272 |
+
max_tokens,
|
| 2273 |
+
temperature,
|
| 2274 |
+
project_upload,
|
| 2275 |
+
conversation_state,
|
| 2276 |
+
],
|
| 2277 |
+
outputs=[
|
| 2278 |
+
coding_output,
|
| 2279 |
+
conversation_state,
|
| 2280 |
+
coding_status,
|
| 2281 |
+
project_zip_download,
|
| 2282 |
+
single_file_download,
|
| 2283 |
+
job_state,
|
| 2284 |
+
],
|
| 2285 |
+
)
|
| 2286 |
+
|
| 2287 |
+
stop_button.click(
|
| 2288 |
+
fn=stop_job,
|
| 2289 |
+
inputs=job_state,
|
| 2290 |
+
outputs=coding_status,
|
| 2291 |
+
)
|
| 2292 |
+
|
| 2293 |
+
new_project_button.click(
|
| 2294 |
+
fn=clear_current_conversation,
|
| 2295 |
+
outputs=[
|
| 2296 |
+
coding_output,
|
| 2297 |
+
conversation_state,
|
| 2298 |
+
coding_status,
|
| 2299 |
+
job_state,
|
| 2300 |
+
],
|
| 2301 |
+
)
|
| 2302 |
+
|
| 2303 |
+
preview_button.click(
|
| 2304 |
+
fn=preview_project,
|
| 2305 |
+
inputs=conversation_state,
|
| 2306 |
+
outputs=html_preview,
|
| 2307 |
+
)
|
| 2308 |
+
|
| 2309 |
+
# =================================================
|
| 2310 |
+
# HISTORY
|
| 2311 |
+
# =================================================
|
| 2312 |
+
|
| 2313 |
+
with gr.Tab("π History"):
|
| 2314 |
+
|
| 2315 |
+
gr.Markdown("## Saved Conversations and Projects")
|
| 2316 |
+
|
| 2317 |
+
history_dropdown = gr.Dropdown(
|
| 2318 |
+
choices=[],
|
| 2319 |
+
label="Saved History",
|
| 2320 |
+
interactive=True,
|
| 2321 |
+
)
|
| 2322 |
+
|
| 2323 |
+
with gr.Row():
|
| 2324 |
+
refresh_history_button = gr.Button(
|
| 2325 |
+
"π Refresh History",
|
| 2326 |
+
)
|
| 2327 |
+
|
| 2328 |
+
load_history_button = gr.Button(
|
| 2329 |
+
"π Open Selected",
|
| 2330 |
+
variant="primary",
|
| 2331 |
+
)
|
| 2332 |
+
|
| 2333 |
+
history_output = gr.Markdown(
|
| 2334 |
+
"Select a saved conversation."
|
| 2335 |
+
)
|
| 2336 |
+
|
| 2337 |
+
history_status = gr.Markdown("")
|
| 2338 |
+
|
| 2339 |
+
resume_button = gr.Button(
|
| 2340 |
+
"βΆ Resume Selected Job",
|
| 2341 |
+
)
|
| 2342 |
+
|
| 2343 |
+
with gr.Row():
|
| 2344 |
+
delete_history_button = gr.Button(
|
| 2345 |
+
"π Delete Selected",
|
| 2346 |
+
variant="stop",
|
| 2347 |
+
)
|
| 2348 |
+
|
| 2349 |
+
delete_all_history_button = gr.Button(
|
| 2350 |
+
"π Delete All",
|
| 2351 |
+
variant="stop",
|
| 2352 |
+
)
|
| 2353 |
+
|
| 2354 |
+
refresh_history_button.click(
|
| 2355 |
+
fn=refresh_history,
|
| 2356 |
+
outputs=history_dropdown,
|
| 2357 |
+
)
|
| 2358 |
+
|
| 2359 |
+
load_history_button.click(
|
| 2360 |
+
fn=load_selected_history,
|
| 2361 |
+
inputs=history_dropdown,
|
| 2362 |
+
outputs=[
|
| 2363 |
+
history_output,
|
| 2364 |
+
conversation_state,
|
| 2365 |
+
history_status,
|
| 2366 |
+
job_state,
|
| 2367 |
+
],
|
| 2368 |
+
)
|
| 2369 |
+
|
| 2370 |
+
delete_history_button.click(
|
| 2371 |
+
fn=delete_selected_history,
|
| 2372 |
+
inputs=history_dropdown,
|
| 2373 |
+
outputs=[
|
| 2374 |
+
history_dropdown,
|
| 2375 |
+
history_status,
|
| 2376 |
+
],
|
| 2377 |
+
)
|
| 2378 |
+
|
| 2379 |
+
def delete_all(request: gr.Request):
|
| 2380 |
+
session_id = request.session_hash if request else "default"
|
| 2381 |
+
choices = get_conversations(session_id)
|
| 2382 |
+
ids = [value for _, value in choices]
|
| 2383 |
+
|
| 2384 |
+
delete_conversations(session_id, ids)
|
| 2385 |
+
|
| 2386 |
+
return (
|
| 2387 |
+
gr.Dropdown(choices=[], value=None),
|
| 2388 |
+
"All history deleted.",
|
| 2389 |
+
)
|
| 2390 |
+
|
| 2391 |
+
delete_all_history_button.click(
|
| 2392 |
+
fn=delete_all,
|
| 2393 |
+
outputs=[
|
| 2394 |
+
history_dropdown,
|
| 2395 |
+
history_status,
|
| 2396 |
+
],
|
| 2397 |
+
)
|
| 2398 |
+
|
| 2399 |
+
resume_button.click(
|
| 2400 |
+
fn=resume_job,
|
| 2401 |
+
inputs=job_state,
|
| 2402 |
+
outputs=[
|
| 2403 |
+
history_output,
|
| 2404 |
+
history_status,
|
| 2405 |
+
project_zip_download,
|
| 2406 |
+
single_file_download,
|
| 2407 |
+
],
|
| 2408 |
+
)
|
| 2409 |
+
|
| 2410 |
+
# =====================================================
|
| 2411 |
+
# FOOTER
|
| 2412 |
+
# =====================================================
|
| 2413 |
+
|
| 2414 |
+
gr.HTML(
|
| 2415 |
+
f"""
|
| 2416 |
+
<div id="footer">
|
| 2417 |
+
<div style="
|
| 2418 |
+
font-size:19px;
|
| 2419 |
+
font-weight:800;
|
| 2420 |
+
color:#c7b7ff;
|
| 2421 |
+
margin-bottom:8px;
|
| 2422 |
+
">
|
| 2423 |
+
β‘ ARISE AI STUDIO
|
| 2424 |
+
</div>
|
| 2425 |
+
|
| 2426 |
+
<div>Premium AI Workspace</div>
|
| 2427 |
+
|
| 2428 |
+
<div style="margin-top:8px;font-size:12px">
|
| 2429 |
+
Developer:
|
| 2430 |
+
<a href="{DEVELOPER_TELEGRAM}" target="_blank">
|
| 2431 |
+
@hardhackar009
|
| 2432 |
+
</a>
|
| 2433 |
+
</div>
|
| 2434 |
+
|
| 2435 |
+
<div style="margin-top:8px;font-size:11px">
|
| 2436 |
+
Powered by Open Source Models and Hugging Face
|
| 2437 |
+
</div>
|
| 2438 |
+
</div>
|
| 2439 |
+
"""
|
| 2440 |
)
|
| 2441 |
|
| 2442 |
+
|
| 2443 |
+
# =========================================================
|
| 2444 |
+
# LAUNCH
|
| 2445 |
+
# =========================================================
|
| 2446 |
+
|
| 2447 |
if __name__ == "__main__":
|
| 2448 |
+
|
| 2449 |
+
demo.queue(
|
| 2450 |
+
default_concurrency_limit=1,
|
| 2451 |
+
max_size=20,
|
| 2452 |
+
).launch(
|
| 2453 |
+
theme=gr.themes.Soft(
|
| 2454 |
primary_hue="violet",
|
| 2455 |
+
secondary_hue="cyan",
|
| 2456 |
+
neutral_hue="slate",
|
| 2457 |
),
|
| 2458 |
css=CSS,
|
| 2459 |
show_error=True,
|