Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import gc
|
| 4 |
import re
|
|
@@ -6,177 +5,125 @@ import json
|
|
| 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
|
| 18 |
-
os.environ
|
| 19 |
-
os.environ
|
| 20 |
|
| 21 |
-
import spaces
|
| 22 |
import gradio as gr
|
| 23 |
import torch
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 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 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
MAX_UPLOAD_FILES = 100
|
| 53 |
MAX_FILE_SIZE = 10 * 1024 * 1024
|
| 54 |
MAX_PROJECT_SIZE = 100 * 1024 * 1024
|
| 55 |
-
MAX_CONTEXT_SIZE =
|
|
|
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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 |
-
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
-
"
|
| 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 |
-
|
| 128 |
-
"
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 167 |
-
|
| 168 |
-
|
| 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 |
-
|
| 178 |
-
|
| 179 |
-
connection.execute("""
|
| 180 |
CREATE TABLE IF NOT EXISTS conversations (
|
| 181 |
id TEXT PRIMARY KEY,
|
| 182 |
session_id TEXT NOT NULL,
|
|
@@ -186,23 +133,8 @@ def initialize_database():
|
|
| 186 |
updated TEXT NOT NULL
|
| 187 |
)
|
| 188 |
""")
|
| 189 |
-
|
| 190 |
-
|
| 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,2250 +144,725 @@ def now_string():
|
|
| 212 |
return datetime.utcnow().isoformat(timespec="seconds")
|
| 213 |
|
| 214 |
|
| 215 |
-
def
|
|
|
|
|
|
|
|
|
|
| 216 |
path = PROJECTS_DIR / conversation_id
|
| 217 |
path.mkdir(parents=True, exist_ok=True)
|
| 218 |
return path
|
| 219 |
|
| 220 |
|
| 221 |
-
def
|
|
|
|
| 222 |
timestamp = now_string()
|
| 223 |
-
|
| 224 |
with db_lock:
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 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
|
|
|
|
| 249 |
with db_lock:
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
|
|
|
| 265 |
|
| 266 |
|
| 267 |
def get_conversation(session_id, conversation_id):
|
|
|
|
|
|
|
| 268 |
with db_lock:
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 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 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
with db_lock:
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
status, prompt, response, error,
|
| 365 |
-
created, updated
|
| 366 |
)
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 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 |
-
|
| 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
|
| 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("
|
| 714 |
-
|
| 715 |
model_id = CODE_MODELS[model_name]
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 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 |
-
|
| 737 |
-
torch_dtype=torch.float32,
|
| 738 |
low_cpu_mem_usage=True,
|
|
|
|
| 739 |
)
|
| 740 |
-
|
| 741 |
-
model.to(
|
| 742 |
model.eval()
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 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
|
| 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 |
-
|
| 773 |
-
|
| 774 |
-
"dockerfile", "makefile", ".gitignore"
|
| 775 |
-
}
|
| 776 |
-
)
|
| 777 |
|
| 778 |
|
| 779 |
def project_files(project_dir):
|
| 780 |
result = []
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 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 |
-
|
| 796 |
-
size =
|
| 797 |
-
except
|
| 798 |
continue
|
| 799 |
-
|
| 800 |
result.append({
|
| 801 |
-
"path":
|
| 802 |
"size": size,
|
| 803 |
-
"text": is_text_file(
|
| 804 |
})
|
| 805 |
-
|
| 806 |
return result
|
| 807 |
|
| 808 |
|
| 809 |
def build_project_context(project_dir):
|
| 810 |
files = project_files(project_dir)
|
| 811 |
-
|
| 812 |
-
|
| 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 |
-
|
| 836 |
-
|
| 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 |
-
|
| 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
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
relative = safe_relative_path(item.filename)
|
| 872 |
-
|
| 873 |
-
if item.is_dir():
|
| 874 |
continue
|
| 875 |
-
|
| 876 |
-
if
|
| 877 |
-
raise ValueError(
|
| 878 |
-
|
| 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 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
return
|
| 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 |
-
|
| 912 |
-
|
| 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
|
| 926 |
-
|
| 927 |
project_dir = Path(project_dir)
|
| 928 |
project_dir.mkdir(parents=True, exist_ok=True)
|
| 929 |
-
|
| 930 |
messages = []
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 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 |
-
|
|
|
|
| 947 |
try:
|
| 948 |
-
count =
|
| 949 |
-
|
| 950 |
-
|
| 951 |
-
)
|
| 952 |
-
|
| 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 /
|
| 967 |
shutil.copy2(source, target)
|
| 968 |
-
|
| 969 |
-
|
| 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
|
|
|
|
|
|
|
| 982 |
)
|
| 983 |
-
|
| 984 |
else:
|
| 985 |
-
|
| 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"
|
| 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(
|
| 1037 |
except Exception:
|
| 1038 |
continue
|
| 1039 |
-
|
| 1040 |
target = Path(project_dir) / relative
|
|
|
|
|
|
|
| 1041 |
target.parent.mkdir(parents=True, exist_ok=True)
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
|
|
|
|
| 1048 |
changed.append(relative.as_posix())
|
| 1049 |
-
|
| 1050 |
return changed
|
| 1051 |
|
| 1052 |
|
| 1053 |
-
|
| 1054 |
-
|
| 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 |
-
|
| 1396 |
-
|
| 1397 |
-
|
| 1398 |
-
|
| 1399 |
-
|
| 1400 |
-
|
| 1401 |
-
|
| 1402 |
-
|
| 1403 |
-
|
| 1404 |
-
|
| 1405 |
-
|
| 1406 |
-
|
| 1407 |
-
|
| 1408 |
-
|
| 1409 |
-
|
| 1410 |
-
|
| 1411 |
-
|
| 1412 |
-
|
| 1413 |
-
|
| 1414 |
-
|
| 1415 |
-
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
| 1422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1423 |
|
| 1424 |
-
update_job(
|
| 1425 |
-
job_id,
|
| 1426 |
-
status="error",
|
| 1427 |
-
error=str(error),
|
| 1428 |
-
response=f"**Coding error:** {error}",
|
| 1429 |
-
)
|
| 1430 |
|
| 1431 |
-
|
| 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
|
| 1442 |
-
|
|
|
|
| 1443 |
model_name,
|
| 1444 |
max_tokens,
|
| 1445 |
temperature,
|
| 1446 |
-
|
| 1447 |
conversation_id,
|
| 1448 |
request: gr.Request,
|
| 1449 |
):
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
|
| 1453 |
-
|
| 1454 |
-
"Waiting for prompt.",
|
| 1455 |
-
None,
|
| 1456 |
-
None,
|
| 1457 |
-
None,
|
| 1458 |
-
)
|
| 1459 |
return
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
-
|
|
|
|
| 1463 |
if not conversation_id:
|
| 1464 |
-
|
| 1465 |
-
|
| 1466 |
-
prompt.strip().replace("\n", " ")[:55],
|
| 1467 |
-
)
|
| 1468 |
-
|
| 1469 |
-
project_dir = get_project_dir(conversation_id)
|
| 1470 |
|
| 1471 |
-
|
| 1472 |
-
|
| 1473 |
-
if
|
| 1474 |
try:
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
| 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 |
-
|
| 1596 |
-
|
| 1597 |
-
|
| 1598 |
-
|
| 1599 |
-
|
| 1600 |
-
|
| 1601 |
-
|
| 1602 |
-
|
| 1603 |
-
|
| 1604 |
-
|
| 1605 |
-
|
| 1606 |
-
|
| 1607 |
-
|
| 1608 |
-
|
| 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 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
|
| 1629 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 1646 |
-
|
| 1647 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1648 |
|
| 1649 |
-
return "No running job found."
|
| 1650 |
|
|
|
|
|
|
|
| 1651 |
|
| 1652 |
-
# =========================================================
|
| 1653 |
-
# HISTORY FUNCTIONS
|
| 1654 |
-
# =========================================================
|
| 1655 |
|
| 1656 |
def refresh_history(request: gr.Request):
|
| 1657 |
-
|
| 1658 |
-
choices = get_conversations(session_id)
|
| 1659 |
-
|
| 1660 |
-
return gr.Dropdown(
|
| 1661 |
-
choices=choices,
|
| 1662 |
-
value=None,
|
| 1663 |
-
)
|
| 1664 |
|
| 1665 |
|
| 1666 |
-
def
|
| 1667 |
-
session_id = request
|
| 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 |
-
|
| 1694 |
-
|
| 1695 |
-
|
| 1696 |
-
|
| 1697 |
-
|
| 1698 |
-
|
| 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 |
-
|
| 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=
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
|
|
|
|
|
|
| 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:
|
| 1768 |
-
|
| 1769 |
margin: auto !important;
|
| 1770 |
-
|
| 1771 |
-
padding: 12px !important;
|
| 1772 |
}
|
| 1773 |
-
|
| 1774 |
-
|
| 1775 |
-
|
| 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 |
-
|
| 1793 |
-
|
| 1794 |
-
|
| 1795 |
-
|
| 1796 |
-
|
| 1797 |
-
|
| 1798 |
-
|
| 1799 |
-
-
|
| 1800 |
-
animation: textGradient 5s linear infinite;
|
| 1801 |
-
}
|
| 1802 |
-
|
| 1803 |
-
@keyframes textGradient {
|
| 1804 |
-
0% { background-position: 0%; }
|
| 1805 |
-
100% { background-position: 300%; }
|
| 1806 |
}
|
| 1807 |
-
|
| 1808 |
-
|
| 1809 |
-
|
| 1810 |
-
|
| 1811 |
-
|
| 1812 |
-
|
| 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 |
-
|
| 1850 |
-
|
| 1851 |
-
padding:
|
| 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 |
-
#
|
| 1859 |
-
|
| 1860 |
-
|
|
|
|
|
|
|
| 1861 |
}
|
| 1862 |
-
|
| 1863 |
-
#
|
| 1864 |
-
|
| 1865 |
-
|
| 1866 |
-
|
| 1867 |
}
|
| 1868 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1869 |
@media(max-width:650px) {
|
| 1870 |
-
.gradio-container {
|
| 1871 |
-
|
| 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 |
-
|
| 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(
|
| 1949 |
-
|
| 1950 |
-
|
| 1951 |
-
|
| 1952 |
-
|
| 1953 |
-
|
| 1954 |
-
|
| 1955 |
-
|
| 1956 |
-
|
| 1957 |
-
|
| 1958 |
-
|
| 1959 |
-
gr.
|
| 1960 |
-
|
| 1961 |
-
|
| 1962 |
-
|
| 1963 |
-
|
| 1964 |
-
|
| 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 |
-
|
| 1981 |
-
|
| 1982 |
-
|
| 1983 |
-
|
| 1984 |
-
|
| 1985 |
-
|
| 1986 |
-
|
| 1987 |
-
|
| 1988 |
-
|
| 1989 |
-
|
| 1990 |
-
|
| 1991 |
-
|
| 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 |
-
|
| 2210 |
-
|
| 2211 |
-
|
| 2212 |
-
|
| 2213 |
-
|
| 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 |
-
|
| 2245 |
-
|
| 2246 |
-
|
| 2247 |
-
|
| 2248 |
-
with gr.Row():
|
| 2249 |
-
project_zip_download = gr.File(
|
| 2250 |
-
label="Download Full Project ZIP",
|
| 2251 |
-
interactive=False,
|
| 2252 |
)
|
|
|
|
| 2253 |
|
| 2254 |
-
|
| 2255 |
-
|
| 2256 |
-
|
| 2257 |
-
|
| 2258 |
-
|
| 2259 |
-
|
| 2260 |
-
|
| 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 |
-
|
| 2324 |
-
|
| 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 |
-
|
| 2338 |
-
|
| 2339 |
-
resume_button = gr.Button(
|
| 2340 |
-
"▶ Resume Selected Job",
|
| 2341 |
)
|
| 2342 |
|
| 2343 |
-
|
| 2344 |
-
|
| 2345 |
-
|
| 2346 |
-
|
| 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 |
-
|
| 2380 |
-
|
| 2381 |
-
|
| 2382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2383 |
|
| 2384 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2385 |
|
| 2386 |
-
|
| 2387 |
-
gr.Dropdown(choices=[], value=None),
|
| 2388 |
-
"All history deleted.",
|
| 2389 |
-
)
|
| 2390 |
|
| 2391 |
-
|
| 2392 |
-
|
| 2393 |
-
|
| 2394 |
-
|
| 2395 |
-
|
| 2396 |
-
|
| 2397 |
-
|
|
|
|
|
|
|
|
|
|
| 2398 |
|
| 2399 |
-
|
| 2400 |
-
|
| 2401 |
-
|
| 2402 |
-
|
| 2403 |
-
|
| 2404 |
-
history_status,
|
| 2405 |
-
project_zip_download,
|
| 2406 |
-
single_file_download,
|
| 2407 |
-
],
|
| 2408 |
-
)
|
| 2409 |
|
| 2410 |
-
|
| 2411 |
-
|
| 2412 |
-
|
| 2413 |
-
|
| 2414 |
-
|
| 2415 |
-
|
| 2416 |
-
|
| 2417 |
-
|
| 2418 |
-
|
| 2419 |
-
|
| 2420 |
-
|
| 2421 |
-
|
| 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 |
-
|
| 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,
|
| 2460 |
share=False,
|
| 2461 |
)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gc
|
| 3 |
import re
|
|
|
|
| 5 |
import uuid
|
| 6 |
import time
|
| 7 |
import shutil
|
|
|
|
|
|
|
| 8 |
import zipfile
|
| 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.setdefault("HF_HUB_DISABLE_IMPLICIT_TOKEN", "1")
|
| 17 |
+
os.environ.setdefault("HF_HUB_DISABLE_TELEMETRY", "1")
|
| 18 |
+
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
| 19 |
|
|
|
|
| 20 |
import gradio as gr
|
| 21 |
import torch
|
| 22 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 23 |
|
| 24 |
+
# ============================================================
|
| 25 |
+
# ARISE AI STUDIO — unified ChatGPT-style chat + coding workspace
|
| 26 |
+
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
APP_NAME = "ARISE AI Studio"
|
| 29 |
DEVELOPER_TELEGRAM = "https://t.me/hardhackar009"
|
| 30 |
|
| 31 |
+
# Keep this modest for small Hugging Face Spaces. Increase only if
|
| 32 |
+
# the selected model and available RAM/VRAM can handle it.
|
| 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 = 50000
|
| 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 |
+
probe = DATA_DIR / ".write_test"
|
| 46 |
+
probe.write_text("ok", encoding="utf-8")
|
| 47 |
+
probe.unlink(missing_ok=True)
|
| 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 |
+
CODE_MODELS = {
|
| 59 |
+
"Qwen Coder 0.5B": "Qwen/Qwen2.5-Coder-0.5B-Instruct",
|
| 60 |
+
"Qwen Coder 1.5B": "Qwen/Qwen2.5-Coder-1.5B-Instruct",
|
| 61 |
+
"DeepSeek Coder 1.3B": "deepseek-ai/deepseek-coder-1.3b-instruct",
|
| 62 |
+
"Qwen Coder 3B": "Qwen/Qwen2.5-Coder-3B-Instruct",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
+
DEFAULT_MODEL = "Qwen Coder 1.5B"
|
| 66 |
|
| 67 |
+
SUPPORTED_TEXT_EXTENSIONS = {
|
| 68 |
+
".py", ".js", ".jsx", ".ts", ".tsx", ".html", ".htm",
|
| 69 |
+
".css", ".scss", ".json", ".xml", ".yaml", ".yml",
|
| 70 |
+
".md", ".txt", ".java", ".kt", ".gradle", ".properties",
|
| 71 |
+
".sh", ".bat", ".c", ".cpp", ".h", ".hpp", ".php",
|
| 72 |
+
".sql", ".go", ".rs", ".swift", ".dart", ".vue",
|
| 73 |
+
".svelte", ".toml", ".ini", ".cfg", ".env", ".gitignore",
|
| 74 |
}
|
| 75 |
+
EXCLUDED_DIRS = {
|
| 76 |
+
".git", ".gradle", ".idea", ".vscode", "__pycache__",
|
| 77 |
+
"node_modules", "build", "dist", ".venv", "venv", ".next",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# ----------------------------- Database -----------------------------
|
|
|
|
|
|
|
| 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 |
+
conn = sqlite3.connect(str(DB_PATH), timeout=30, check_same_thread=False)
|
| 119 |
+
conn.row_factory = sqlite3.Row
|
| 120 |
+
return conn
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
def initialize_database():
|
| 124 |
with db_lock:
|
| 125 |
+
conn = db_connect()
|
| 126 |
+
conn.execute("""
|
|
|
|
| 127 |
CREATE TABLE IF NOT EXISTS conversations (
|
| 128 |
id TEXT PRIMARY KEY,
|
| 129 |
session_id TEXT NOT NULL,
|
|
|
|
| 133 |
updated TEXT NOT NULL
|
| 134 |
)
|
| 135 |
""")
|
| 136 |
+
conn.commit()
|
| 137 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
initialize_database()
|
|
|
|
| 144 |
return datetime.utcnow().isoformat(timespec="seconds")
|
| 145 |
|
| 146 |
|
| 147 |
+
def project_dir_for(conversation_id: str) -> Path:
|
| 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 create_conversation(session_id: str, title: str = "New Chat") -> str:
|
| 157 |
+
conversation_id = uuid.uuid4().hex
|
| 158 |
timestamp = now_string()
|
|
|
|
| 159 |
with db_lock:
|
| 160 |
+
conn = db_connect()
|
| 161 |
+
conn.execute(
|
| 162 |
+
"""INSERT INTO conversations
|
| 163 |
+
(id, session_id, title, history, created, updated)
|
| 164 |
+
VALUES (?, ?, ?, ?, ?, ?)""",
|
| 165 |
+
(conversation_id, session_id, title[:80], "[]", timestamp, timestamp),
|
| 166 |
+
)
|
| 167 |
+
conn.commit()
|
| 168 |
+
conn.close()
|
| 169 |
+
project_dir_for(conversation_id)
|
| 170 |
+
return conversation_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
|
| 173 |
+
def save_conversation(session_id, conversation_id, title, history):
|
| 174 |
+
timestamp = now_string()
|
| 175 |
with db_lock:
|
| 176 |
+
conn = db_connect()
|
| 177 |
+
conn.execute(
|
| 178 |
+
"""INSERT INTO conversations
|
| 179 |
+
(id, session_id, title, history, created, updated)
|
| 180 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 181 |
+
ON CONFLICT(id) DO UPDATE SET
|
| 182 |
+
title=excluded.title, history=excluded.history,
|
| 183 |
+
updated=excluded.updated
|
| 184 |
+
WHERE conversations.session_id=excluded.session_id""",
|
| 185 |
+
(
|
| 186 |
+
conversation_id, session_id, title[:80],
|
| 187 |
+
json.dumps(history, ensure_ascii=False), timestamp, timestamp,
|
| 188 |
+
),
|
| 189 |
+
)
|
| 190 |
+
conn.commit()
|
| 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 |
+
conn = db_connect()
|
| 199 |
+
row = conn.execute(
|
| 200 |
+
"SELECT * FROM conversations WHERE id=? AND session_id=?",
|
| 201 |
+
(conversation_id, session_id),
|
| 202 |
+
).fetchone()
|
| 203 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
if not row:
|
| 205 |
return None
|
| 206 |
+
try:
|
| 207 |
+
history = json.loads(row["history"])
|
| 208 |
+
except Exception:
|
| 209 |
+
history = []
|
| 210 |
+
return {"id": row["id"], "title": row["title"], "history": history}
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
def get_conversations(session_id):
|
| 214 |
with db_lock:
|
| 215 |
+
conn = db_connect()
|
| 216 |
+
rows = conn.execute(
|
| 217 |
+
"""SELECT id, title, updated FROM conversations
|
| 218 |
+
WHERE session_id=? ORDER BY updated DESC LIMIT 100""",
|
| 219 |
+
(session_id,),
|
| 220 |
+
).fetchall()
|
| 221 |
+
conn.close()
|
| 222 |
+
return [(f"{r['title']} · {r['updated']}", r["id"]) for r in rows]
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
def delete_conversations(session_id, ids):
|
| 226 |
+
safe_ids = [x for x in (ids or []) if re.fullmatch(r"[a-f0-9]{32}", str(x))]
|
|
|
|
| 227 |
with db_lock:
|
| 228 |
+
conn = db_connect()
|
| 229 |
+
for cid in safe_ids:
|
| 230 |
+
conn.execute(
|
| 231 |
+
"DELETE FROM conversations WHERE id=? AND session_id=?",
|
| 232 |
+
(cid, session_id),
|
|
|
|
|
|
|
| 233 |
)
|
| 234 |
+
shutil.rmtree(PROJECTS_DIR / cid, ignore_errors=True)
|
| 235 |
+
conn.commit()
|
| 236 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
|
| 239 |
+
# ----------------------------- Model loading -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 load_model(model_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
if model_name not in CODE_MODELS:
|
| 253 |
+
raise ValueError("Please select a valid model.")
|
|
|
|
| 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 |
+
clear_model_cache()
|
| 260 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 261 |
+
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
model = AutoModelForCausalLM.from_pretrained(
|
| 263 |
model_id,
|
| 264 |
+
torch_dtype=dtype,
|
|
|
|
| 265 |
low_cpu_mem_usage=True,
|
| 266 |
+
trust_remote_code=True,
|
| 267 |
)
|
| 268 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 269 |
+
model.to(device)
|
| 270 |
model.eval()
|
| 271 |
+
if tokenizer.pad_token_id is None:
|
| 272 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 273 |
+
model_cache.update({
|
| 274 |
+
"name": model_id, "tokenizer": tokenizer, "model": model,
|
| 275 |
+
})
|
| 276 |
return tokenizer, model
|
| 277 |
|
| 278 |
|
| 279 |
+
# ----------------------------- Safe project files -----------------------------
|
|
|
|
|
|
|
| 280 |
|
| 281 |
def safe_relative_path(value):
|
| 282 |
value = str(value).replace("\\", "/").strip()
|
| 283 |
path = Path(value)
|
|
|
|
| 284 |
if (
|
| 285 |
+
not value or path.is_absolute() or ".." in path.parts
|
| 286 |
+
or "\x00" in value or re.match(r"^[A-Za-z]:", value)
|
|
|
|
|
|
|
| 287 |
):
|
| 288 |
raise ValueError("Unsafe project file path.")
|
|
|
|
| 289 |
return path
|
| 290 |
|
| 291 |
|
| 292 |
def is_text_file(path):
|
| 293 |
+
return path.suffix.lower() in SUPPORTED_TEXT_EXTENSIONS or path.name.lower() in {
|
| 294 |
+
"dockerfile", "makefile", ".gitignore",
|
| 295 |
+
}
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
def project_files(project_dir):
|
| 299 |
result = []
|
| 300 |
+
for root, dirs, filenames in os.walk(project_dir):
|
| 301 |
+
dirs[:] = [d for d in dirs if d not in EXCLUDED_DIRS]
|
| 302 |
+
for filename in filenames:
|
| 303 |
+
full = Path(root) / filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
try:
|
| 305 |
+
rel = full.relative_to(project_dir)
|
| 306 |
+
size = full.stat().st_size
|
| 307 |
+
except OSError:
|
| 308 |
continue
|
|
|
|
| 309 |
result.append({
|
| 310 |
+
"path": rel.as_posix(),
|
| 311 |
"size": size,
|
| 312 |
+
"text": is_text_file(full),
|
| 313 |
})
|
|
|
|
| 314 |
return result
|
| 315 |
|
| 316 |
|
| 317 |
def build_project_context(project_dir):
|
| 318 |
files = project_files(project_dir)
|
| 319 |
+
sections = [
|
| 320 |
+
"PROJECT FILE INVENTORY:",
|
|
|
|
| 321 |
json.dumps(files, ensure_ascii=False, indent=2),
|
| 322 |
+
"\nPROJECT TEXT FILE CONTENTS:",
|
|
|
|
| 323 |
]
|
| 324 |
+
used = 0
|
|
|
|
|
|
|
| 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(encoding="utf-8", errors="replace")
|
|
|
|
|
|
|
|
|
|
| 331 |
except Exception:
|
| 332 |
continue
|
| 333 |
+
block = f"\n\n===== FILE: {item['path']} =====\n{content}\n===== END FILE ====="
|
| 334 |
+
if used + len(block) > MAX_CONTEXT_SIZE:
|
| 335 |
+
sections.append("\n[Project context truncated at configured size limit.]")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
break
|
| 337 |
+
sections.append(block)
|
| 338 |
+
used += len(block)
|
| 339 |
+
return "\n".join(sections), files
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
+
def safe_extract_zip(zip_path, destination):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
destination = Path(destination)
|
| 344 |
destination.mkdir(parents=True, exist_ok=True)
|
| 345 |
+
total = 0
|
| 346 |
+
count = 0
|
|
|
|
|
|
|
| 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 entries.")
|
| 351 |
+
for entry in entries:
|
| 352 |
+
if entry.is_dir():
|
|
|
|
|
|
|
|
|
|
| 353 |
continue
|
| 354 |
+
relative = safe_relative_path(entry.filename)
|
| 355 |
+
if entry.file_size > MAX_FILE_SIZE:
|
| 356 |
+
raise ValueError(f"File too large in ZIP: {entry.filename}")
|
| 357 |
+
total += entry.file_size
|
| 358 |
+
if total > MAX_PROJECT_SIZE:
|
| 359 |
+
raise ValueError("ZIP exceeds the project size limit.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
def extract_image_ocr(image_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
try:
|
| 374 |
import pytesseract
|
| 375 |
+
from PIL import Image
|
| 376 |
image = Image.open(image_path).convert("RGB")
|
| 377 |
return pytesseract.image_to_string(image).strip()
|
|
|
|
| 378 |
except ImportError:
|
| 379 |
+
return "OCR unavailable: install pytesseract and the Tesseract system package."
|
| 380 |
+
except Exception as exc:
|
| 381 |
+
return f"OCR could not process this image: {exc}"
|
|
|
|
| 382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
def process_uploads(uploaded_files, project_dir):
|
| 385 |
if not uploaded_files:
|
| 386 |
+
return "No attachments."
|
|
|
|
| 387 |
project_dir = Path(project_dir)
|
| 388 |
project_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 389 |
messages = []
|
| 390 |
+
for uploaded in uploaded_files[:MAX_UPLOAD_FILES]:
|
| 391 |
+
source = Path(str(getattr(uploaded, "name", uploaded)))
|
| 392 |
+
if not source.is_file():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
continue
|
|
|
|
| 394 |
if source.stat().st_size > MAX_FILE_SIZE:
|
| 395 |
+
messages.append(f"Skipped {source.name}: file exceeds 10 MB.")
|
|
|
|
|
|
|
| 396 |
continue
|
| 397 |
|
| 398 |
+
suffix = source.suffix.lower()
|
| 399 |
+
if suffix == ".zip":
|
| 400 |
try:
|
| 401 |
+
count = safe_extract_zip(source, project_dir)
|
| 402 |
+
messages.append(f"Extracted {source.name}: {count} files.")
|
| 403 |
+
except Exception as exc:
|
| 404 |
+
messages.append(f"Could not extract {source.name}: {exc}")
|
| 405 |
+
elif suffix in {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"}:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
image_dir = project_dir / "uploaded_images"
|
| 407 |
image_dir.mkdir(exist_ok=True)
|
| 408 |
+
safe_name = re.sub(r"[^A-Za-z0-9._-]", "_", source.name)[:120]
|
| 409 |
+
target = image_dir / f"{uuid.uuid4().hex[:8]}_{safe_name}"
|
| 410 |
shutil.copy2(source, target)
|
| 411 |
+
ocr = extract_image_ocr(target)
|
| 412 |
+
(target.with_suffix(target.suffix + ".ocr.txt")).write_text(
|
| 413 |
+
ocr, encoding="utf-8",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
)
|
|
|
|
| 415 |
messages.append(
|
| 416 |
+
f"Image attached: {source.name}. "
|
| 417 |
+
"OCR text was extracted when Tesseract was available. "
|
| 418 |
+
"This text model does not directly see image pixels."
|
| 419 |
)
|
|
|
|
| 420 |
else:
|
| 421 |
+
safe_name = re.sub(r"[^A-Za-z0-9._-]", "_", source.name)[:120]
|
| 422 |
+
target = project_dir / f"{uuid.uuid4().hex[:8]}_{safe_name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
shutil.copy2(source, target)
|
| 424 |
+
messages.append(f"Attached: {source.name}")
|
| 425 |
+
return "\n".join(messages) if messages else "No supported files were added."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
|
|
|
| 427 |
|
| 428 |
+
# ----------------------------- Model response and file edits -----------------------------
|
|
|
|
|
|
|
|
|
|
| 429 |
|
| 430 |
FILE_BLOCK_PATTERN = re.compile(
|
| 431 |
+
r"<<<FILE:\s*(.*?)\s*>>>\s*(.*?)\s*<<<END FILE>>>",
|
| 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(match.group(1))
|
| 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 |
+
resolved_parent = target.parent.resolve()
|
| 448 |
+
try:
|
| 449 |
+
resolved_parent.relative_to(root)
|
| 450 |
+
except ValueError:
|
| 451 |
+
continue
|
| 452 |
+
content = match.group(2)
|
| 453 |
+
target.write_text(content.rstrip() + "\n", encoding="utf-8")
|
| 454 |
changed.append(relative.as_posix())
|
|
|
|
| 455 |
return changed
|
| 456 |
|
| 457 |
|
| 458 |
+
def make_chat_prompt(tokenizer, messages):
|
| 459 |
+
if getattr(tokenizer, "chat_template", None):
|
| 460 |
+
return tokenizer.apply_chat_template(
|
| 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 |
+
def generate_reply(prompt, model_name, history, project_context, max_tokens, temperature):
|
| 469 |
+
tokenizer, model = load_model(model_name)
|
| 470 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 471 |
+
for item in (history or [])[-MAX_HISTORY_MESSAGES:]:
|
| 472 |
+
if (
|
| 473 |
+
isinstance(item, dict)
|
| 474 |
+
and item.get("role") in {"user", "assistant"}
|
| 475 |
+
and isinstance(item.get("content"), str)
|
| 476 |
+
):
|
| 477 |
+
messages.append({"role": item["role"], "content": item["content"]})
|
| 478 |
+
|
| 479 |
+
user_content = prompt
|
| 480 |
+
if project_context:
|
| 481 |
+
user_content = (
|
| 482 |
+
"The user has attached files. Use the project context below if relevant.\n\n"
|
| 483 |
+
+ project_context
|
| 484 |
+
+ "\n\nUSER MESSAGE:\n"
|
| 485 |
+
+ prompt
|
| 486 |
+
+ "\n\nIf the user requests project edits, return complete changed/new files "
|
| 487 |
+
"using the required <<<FILE: path>>> format."
|
| 488 |
+
)
|
| 489 |
+
messages.append({"role": "user", "content": user_content})
|
| 490 |
+
formatted = make_chat_prompt(tokenizer, messages)
|
| 491 |
+
|
| 492 |
+
inputs = tokenizer(
|
| 493 |
+
formatted,
|
| 494 |
+
return_tensors="pt",
|
| 495 |
+
truncation=True,
|
| 496 |
+
max_length=MAX_INPUT_TOKENS,
|
| 497 |
+
)
|
| 498 |
+
device = next(model.parameters()).device
|
| 499 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 500 |
+
max_new = max(64, min(int(max_tokens), MAX_OUTPUT_TOKENS))
|
| 501 |
+
temp = max(0.1, min(float(temperature), 1.5))
|
| 502 |
+
kwargs = {
|
| 503 |
+
**inputs,
|
| 504 |
+
"max_new_tokens": max_new,
|
| 505 |
+
"do_sample": temp > 0.15,
|
| 506 |
+
"temperature": temp,
|
| 507 |
+
"pad_token_id": tokenizer.pad_token_id,
|
| 508 |
+
"eos_token_id": tokenizer.eos_token_id,
|
| 509 |
+
}
|
| 510 |
+
with torch.inference_mode():
|
| 511 |
+
output = model.generate(**kwargs)
|
| 512 |
+
new_tokens = output[0][inputs["input_ids"].shape[-1]:]
|
| 513 |
+
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
+
# ----------------------------- Gradio chat callbacks -----------------------------
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
+
def get_session_id(request):
|
| 519 |
+
return request.session_hash if request is not None else "local-session"
|
| 520 |
|
|
|
|
|
|
|
|
|
|
| 521 |
|
| 522 |
+
def chat_submit(
|
| 523 |
+
message,
|
| 524 |
+
attachments,
|
| 525 |
model_name,
|
| 526 |
max_tokens,
|
| 527 |
temperature,
|
| 528 |
+
history,
|
| 529 |
conversation_id,
|
| 530 |
request: gr.Request,
|
| 531 |
):
|
| 532 |
+
history = list(history or [])
|
| 533 |
+
text = (message or "").strip()
|
| 534 |
+
if not text and not attachments:
|
| 535 |
+
yield history, conversation_id, "Write a message or attach a file.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
return
|
| 537 |
+
if len(text) > MAX_CHAT_MESSAGE_LENGTH:
|
| 538 |
+
yield history, conversation_id, "Message is too long. Please shorten it.", None
|
| 539 |
+
return
|
| 540 |
+
session_id = get_session_id(request)
|
| 541 |
if not conversation_id:
|
| 542 |
+
title = (text.replace("\n", " ")[:55] or "New Chat")
|
| 543 |
+
conversation_id = create_conversation(session_id, title)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
|
| 545 |
+
project_dir = project_dir_for(conversation_id)
|
| 546 |
+
attachment_status = ""
|
| 547 |
+
if attachments:
|
| 548 |
try:
|
| 549 |
+
attachment_status = process_uploads(attachments, project_dir)
|
| 550 |
+
except Exception as exc:
|
| 551 |
+
yield history, conversation_id, f"Attachment error: {exc}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
return
|
| 553 |
|
| 554 |
+
project_context = ""
|
| 555 |
+
files = project_files(project_dir)
|
| 556 |
+
if files:
|
| 557 |
+
project_context, _ = build_project_context(project_dir)
|
| 558 |
+
if attachment_status:
|
| 559 |
+
project_context = attachment_status + "\n\n" + project_context
|
| 560 |
+
elif attachment_status:
|
| 561 |
+
project_context = attachment_status
|
| 562 |
+
|
| 563 |
+
user_display = text or "Please inspect the attached files."
|
| 564 |
+
if attachment_status and text:
|
| 565 |
+
user_display += f"\n\n_Attachments: {attachment_status}_"
|
| 566 |
+
history.append({"role": "user", "content": user_display})
|
| 567 |
+
yield history, conversation_id, "ARISE AI is thinking…", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
|
| 569 |
+
try:
|
| 570 |
+
answer = generate_reply(
|
| 571 |
+
text or "Please inspect the attached files and summarize what is available.",
|
| 572 |
+
model_name,
|
| 573 |
+
history[:-1],
|
| 574 |
+
project_context,
|
| 575 |
+
max_tokens,
|
| 576 |
+
temperature,
|
| 577 |
+
)
|
| 578 |
+
if not answer:
|
| 579 |
+
answer = "I couldn't generate a response. Please try again."
|
| 580 |
+
changed = apply_file_changes(answer, project_dir)
|
| 581 |
+
if changed:
|
| 582 |
+
answer += "\n\n---\n**Project files updated:**\n" + "\n".join(
|
| 583 |
+
f"- `{name}`" for name in changed
|
| 584 |
+
)
|
| 585 |
+
history.append({"role": "assistant", "content": answer})
|
| 586 |
+
save_conversation(
|
| 587 |
+
session_id, conversation_id,
|
| 588 |
+
(text.replace("\n", " ")[:55] or "ARISE Project"),
|
| 589 |
+
history,
|
| 590 |
)
|
| 591 |
+
status = "Completed" + (f" · Updated {len(changed)} file(s)" if changed else "")
|
| 592 |
+
yield history, conversation_id, status, create_project_zip(project_dir) if changed else None
|
| 593 |
+
except Exception as exc:
|
| 594 |
+
traceback.print_exc()
|
| 595 |
+
error_text = f"**Generation error:** {type(exc).__name__}: {exc}"
|
| 596 |
+
history.append({"role": "assistant", "content": error_text})
|
| 597 |
+
yield history, conversation_id, "Generation failed. Check Space logs.", None
|
| 598 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
|
| 600 |
+
def create_project_zip(project_dir):
|
| 601 |
+
zip_path = DATA_DIR / f"ARISE_Project_{uuid.uuid4().hex[:10]}.zip"
|
| 602 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as archive:
|
| 603 |
+
for root, dirs, files in os.walk(project_dir):
|
| 604 |
+
dirs[:] = [d for d in dirs if d not in EXCLUDED_DIRS]
|
| 605 |
+
for filename in files:
|
| 606 |
+
full = Path(root) / filename
|
| 607 |
+
archive.write(full, full.relative_to(project_dir))
|
| 608 |
+
return str(zip_path)
|
| 609 |
|
|
|
|
| 610 |
|
| 611 |
+
def new_chat():
|
| 612 |
+
return [], None, "New chat started.", None, None
|
| 613 |
|
|
|
|
|
|
|
|
|
|
| 614 |
|
| 615 |
def refresh_history(request: gr.Request):
|
| 616 |
+
return gr.Dropdown(choices=get_conversations(get_session_id(request)), value=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
|
| 618 |
|
| 619 |
+
def load_history(conversation_id, request: gr.Request):
|
| 620 |
+
session_id = get_session_id(request)
|
| 621 |
+
conversation = get_conversation(session_id, conversation_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
if not conversation:
|
| 623 |
+
return [], None, "Select a valid conversation."
|
| 624 |
+
return conversation["history"], conversation_id, f"Loaded: {conversation['title']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
|
|
|
|
|
|
| 626 |
|
| 627 |
+
def delete_history(conversation_id, request: gr.Request):
|
| 628 |
+
session_id = get_session_id(request)
|
| 629 |
+
if conversation_id:
|
| 630 |
+
delete_conversations(session_id, [conversation_id])
|
| 631 |
+
return [], None, "Conversation deleted.", gr.Dropdown(
|
| 632 |
+
choices=get_conversations(session_id), value=None,
|
|
|
|
|
|
|
| 633 |
)
|
| 634 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
+
def stop_note():
|
| 637 |
+
# This synchronous generation implementation cannot interrupt an already
|
| 638 |
+
# running model.generate call. A real cancellation callback needs a streamer
|
| 639 |
+
# and stopping criteria wired to the generation loop.
|
| 640 |
+
return "Stop is not available during this model's current generation call."
|
| 641 |
|
|
|
|
|
|
|
| 642 |
|
| 643 |
+
# ----------------------------- Premium responsive ChatGPT-like UI -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
|
| 645 |
CSS = r"""
|
| 646 |
+
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700;800&display=swap');
|
| 647 |
+
:root { color-scheme: dark; }
|
| 648 |
+
body, .gradio-container {
|
| 649 |
+
background: #101114 !important;
|
| 650 |
+
color: #ececf1 !important;
|
| 651 |
+
font-family: 'DM Sans', sans-serif !important;
|
| 652 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
.gradio-container {
|
| 654 |
+
max-width: 1100px !important;
|
| 655 |
+
min-height: 100vh !important;
|
| 656 |
margin: auto !important;
|
| 657 |
+
padding: 12px 16px 24px !important;
|
|
|
|
| 658 |
}
|
| 659 |
+
#arise-header {
|
| 660 |
+
display:flex; align-items:center; justify-content:space-between;
|
| 661 |
+
padding: 10px 4px 18px; border-bottom:1px solid #2b2c31;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
}
|
| 663 |
+
#brand { font-size:20px; font-weight:800; letter-spacing:-.5px; }
|
| 664 |
+
#brand span { color:#a78bfa; }
|
| 665 |
+
#subbrand { color:#92939b; font-size:11px; margin-top:2px; }
|
| 666 |
+
#chat-shell { max-width:850px; margin:0 auto; }
|
| 667 |
+
#welcome { text-align:center; padding: 11vh 12px 28px; }
|
| 668 |
+
#welcome h1 { font-size:clamp(27px,5vw,40px); font-weight:700; letter-spacing:-1.5px; color:#f7f7fa; }
|
| 669 |
+
#chatbot {
|
| 670 |
+
border:0 !important; background:transparent !important;
|
| 671 |
+
min-height: 48vh !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
}
|
| 673 |
+
#chatbot .message { border-radius:18px !important; }
|
| 674 |
+
#composer {
|
| 675 |
+
position:sticky; bottom:8px; z-index:10;
|
| 676 |
+
background:#202127 !important; border:1px solid #3a3b43 !important;
|
| 677 |
+
border-radius:25px !important; padding:10px 12px !important;
|
| 678 |
+
box-shadow:0 8px 35px rgba(0,0,0,.28);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
}
|
| 680 |
+
#composer textarea {
|
| 681 |
+
background:transparent !important; border:0 !important;
|
| 682 |
+
box-shadow:none !important; color:#f4f4f5 !important;
|
| 683 |
+
font-size:15px !important; padding:8px 5px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
}
|
| 685 |
+
#composer textarea:focus { box-shadow:none !important; }
|
| 686 |
+
#send-btn {
|
| 687 |
+
background:#f4f4f5 !important; color:#151515 !important;
|
| 688 |
+
border:0 !important; border-radius:50% !important;
|
| 689 |
+
min-width:42px !important; height:42px !important;
|
| 690 |
+
font-size:19px !important; font-weight:800 !important;
|
| 691 |
}
|
| 692 |
+
#plus-btn {
|
| 693 |
+
background:#34353c !important; color:#f4f4f5 !important;
|
| 694 |
+
border:0 !important; border-radius:50% !important;
|
| 695 |
+
width:40px !important; min-width:40px !important; height:40px !important;
|
| 696 |
+
font-size:22px !important;
|
| 697 |
}
|
| 698 |
+
#utility-row { max-width:850px; margin:10px auto; }
|
| 699 |
+
#status { color:#9a9ba4 !important; font-size:12px !important; }
|
| 700 |
+
#side-tools { border:1px solid #303139; background:#191a20; border-radius:16px; padding:12px; }
|
| 701 |
+
#footer { text-align:center; color:#777983; font-size:11px; padding:22px 0 8px; }
|
| 702 |
+
button { transition:background .15s ease, transform .15s ease !important; }
|
| 703 |
+
button:hover { transform:translateY(-1px); }
|
| 704 |
@media(max-width:650px) {
|
| 705 |
+
.gradio-container { padding:8px 8px 18px !important; }
|
| 706 |
+
#welcome { padding-top:8vh; }
|
| 707 |
+
#chatbot { min-height:42vh !important; }
|
| 708 |
+
#composer { border-radius:21px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
}
|
| 710 |
"""
|
| 711 |
|
| 712 |
+
with gr.Blocks(title=APP_NAME, theme=gr.themes.Base(
|
| 713 |
+
primary_hue="violet", secondary_hue="slate", neutral_hue="zinc",
|
| 714 |
+
), css=CSS) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
conversation_state = gr.State(None)
|
|
|
|
| 716 |
|
| 717 |
+
gr.HTML("""
|
| 718 |
+
<div id="arise-header">
|
| 719 |
+
<div>
|
| 720 |
+
<div id="brand">⚡ ARISE <span>AI</span></div>
|
| 721 |
+
<div id="subbrand">Your unified AI workspace</div>
|
| 722 |
+
</div>
|
| 723 |
+
<div style="color:#a7a8b0;font-size:12px">Open Models · Chat · Coding</div>
|
| 724 |
+
</div>
|
| 725 |
+
""")
|
| 726 |
+
|
| 727 |
+
with gr.Column(elem_id="chat-shell"):
|
| 728 |
+
gr.HTML("""
|
| 729 |
+
<div id="welcome">
|
| 730 |
+
<h1>What can I help with?</h1>
|
| 731 |
+
<div style="color:#9697a1;font-size:14px">
|
| 732 |
+
Ask anything, write code, or attach a project to get started.
|
| 733 |
+
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
</div>
|
| 735 |
+
""")
|
| 736 |
+
chatbot = gr.Chatbot(
|
| 737 |
+
value=[],
|
| 738 |
+
type="messages",
|
| 739 |
+
elem_id="chatbot",
|
| 740 |
+
height=470,
|
| 741 |
+
show_label=False,
|
| 742 |
+
avatar_images=(None, None),
|
| 743 |
+
layout="bubble",
|
| 744 |
+
)
|
| 745 |
+
|
| 746 |
+
with gr.Group(elem_id="composer"):
|
| 747 |
+
message_box = gr.Textbox(
|
| 748 |
+
placeholder="Message ARISE AI…",
|
| 749 |
+
show_label=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
lines=2,
|
| 751 |
+
max_lines=8,
|
| 752 |
+
container=False,
|
| 753 |
+
elem_id="message-box",
|
| 754 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
with gr.Row():
|
| 756 |
+
attach_button = gr.UploadButton(
|
| 757 |
+
"+",
|
| 758 |
+
file_types=[".zip", ".png", ".jpg", ".jpeg", ".webp",
|
| 759 |
+
".bmp", ".gif", ".py", ".js", ".ts", ".tsx",
|
| 760 |
+
".html", ".css", ".json", ".java", ".kt",
|
| 761 |
+
".gradle", ".txt", ".md", ".xml", ".pdf"],
|
| 762 |
+
file_count="multiple",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
variant="secondary",
|
| 764 |
+
elem_id="plus-btn",
|
| 765 |
+
scale=0,
|
| 766 |
)
|
| 767 |
+
attachment_state = gr.State([])
|
| 768 |
+
attachment_label = gr.Markdown(
|
| 769 |
+
"<span style='color:#8f9099;font-size:12px'>+ Attach images, ZIPs or project files (optional)</span>",
|
| 770 |
+
elem_id="status",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
)
|
| 772 |
+
send_button = gr.Button("↑", variant="primary", elem_id="send-btn", scale=0)
|
| 773 |
|
| 774 |
+
with gr.Row(elem_id="utility-row"):
|
| 775 |
+
model_dropdown = gr.Dropdown(
|
| 776 |
+
choices=list(CODE_MODELS.keys()),
|
| 777 |
+
value=DEFAULT_MODEL,
|
| 778 |
+
label="Model",
|
| 779 |
+
scale=2,
|
| 780 |
+
min_width=150,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 781 |
)
|
| 782 |
+
max_tokens_slider = gr.Slider(
|
| 783 |
+
128, MAX_OUTPUT_TOKENS, value=768, step=128,
|
| 784 |
+
label="Response length", scale=2,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
)
|
| 786 |
+
temperature_slider = gr.Slider(
|
| 787 |
+
0.1, 1.0, value=0.3, step=0.1,
|
| 788 |
+
label="Creativity", scale=2,
|
|
|
|
|
|
|
| 789 |
)
|
| 790 |
|
| 791 |
+
status_box = gr.Markdown("Ready.", elem_id="status")
|
| 792 |
+
project_download = gr.File(
|
| 793 |
+
label="Updated project ZIP",
|
| 794 |
+
interactive=False,
|
| 795 |
+
visible=True,
|
| 796 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 797 |
|
| 798 |
+
with gr.Accordion("☰ Chat history and settings", open=False):
|
| 799 |
+
history_dropdown = gr.Dropdown(
|
| 800 |
+
choices=[], label="Saved conversations", interactive=True,
|
| 801 |
+
)
|
| 802 |
+
with gr.Row():
|
| 803 |
+
refresh_button = gr.Button("Refresh history")
|
| 804 |
+
open_button = gr.Button("Open selected")
|
| 805 |
+
delete_button = gr.Button("Delete selected", variant="stop")
|
| 806 |
+
new_button = gr.Button("+ New chat", variant="secondary")
|
| 807 |
|
| 808 |
+
gr.Markdown(
|
| 809 |
+
f"Developer: [@hardhackar009]({DEVELOPER_TELEGRAM}) · "
|
| 810 |
+
"Image note: this text-only model can process extracted OCR text, "
|
| 811 |
+
"but it does not directly understand image pixels."
|
| 812 |
+
)
|
| 813 |
|
| 814 |
+
gr.HTML('<div id="footer">ARISE AI STUDIO · Chat, coding and project assistance</div>')
|
|
|
|
|
|
|
|
|
|
| 815 |
|
| 816 |
+
attach_button.upload(
|
| 817 |
+
fn=lambda files: (
|
| 818 |
+
files or [],
|
| 819 |
+
f"<span style='color:#c4b5fd;font-size:12px'>{len(files or [])} attachment(s) selected</span>"
|
| 820 |
+
if files else
|
| 821 |
+
"<span style='color:#8f9099;font-size:12px'>No attachments selected</span>"
|
| 822 |
+
),
|
| 823 |
+
inputs=attach_button,
|
| 824 |
+
outputs=[attachment_state, attachment_label],
|
| 825 |
+
)
|
| 826 |
|
| 827 |
+
submit_inputs = [
|
| 828 |
+
message_box, attachment_state, model_dropdown,
|
| 829 |
+
max_tokens_slider, temperature_slider, chatbot, conversation_state,
|
| 830 |
+
]
|
| 831 |
+
submit_outputs = [chatbot, conversation_state, status_box, project_download]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 832 |
|
| 833 |
+
send_event = send_button.click(
|
| 834 |
+
fn=chat_submit, inputs=submit_inputs, outputs=submit_outputs,
|
| 835 |
+
)
|
| 836 |
+
message_event = message_box.submit(
|
| 837 |
+
fn=chat_submit, inputs=submit_inputs, outputs=submit_outputs,
|
| 838 |
+
)
|
| 839 |
+
send_event.then(
|
| 840 |
+
fn=lambda: ("", [], "<span style='color:#8f9099;font-size:12px'>+ Attach images, ZIPs or project files (optional)</span>"),
|
| 841 |
+
outputs=[message_box, attachment_state, attachment_label],
|
| 842 |
+
)
|
| 843 |
+
message_event.then(
|
| 844 |
+
fn=lambda: ("", [], "<span style='color:#8f9099;font-size:12px'>+ Attach images, ZIPs or project files (optional)</span>"),
|
| 845 |
+
outputs=[message_box, attachment_state, attachment_label],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
)
|
| 847 |
|
| 848 |
+
refresh_button.click(fn=refresh_history, outputs=history_dropdown)
|
| 849 |
+
open_button.click(
|
| 850 |
+
fn=load_history,
|
| 851 |
+
inputs=history_dropdown,
|
| 852 |
+
outputs=[chatbot, conversation_state, status_box],
|
| 853 |
+
)
|
| 854 |
+
delete_button.click(
|
| 855 |
+
fn=delete_history,
|
| 856 |
+
inputs=history_dropdown,
|
| 857 |
+
outputs=[chatbot, conversation_state, status_box, history_dropdown],
|
| 858 |
+
)
|
| 859 |
+
new_button.click(
|
| 860 |
+
fn=new_chat,
|
| 861 |
+
outputs=[chatbot, conversation_state, status_box, project_download, attachment_state],
|
| 862 |
+
)
|
| 863 |
|
| 864 |
if __name__ == "__main__":
|
| 865 |
+
demo.queue(default_concurrency_limit=1, max_size=20).launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 866 |
show_error=True,
|
| 867 |
share=False,
|
| 868 |
)
|