Update constants.py
Browse files- constants.py +141 -110
constants.py
CHANGED
@@ -1,121 +1,152 @@
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
import docx
|
9 |
-
import cv2
|
10 |
-
import numpy as np
|
11 |
-
from PIL import Image
|
12 |
-
import pytesseract
|
13 |
-
import requests
|
14 |
-
from urllib.parse import urlparse, urljoin
|
15 |
-
from bs4 import BeautifulSoup
|
16 |
-
import html2text
|
17 |
-
import json
|
18 |
-
import time
|
19 |
-
import webbrowser
|
20 |
-
import urllib.parse
|
21 |
-
|
22 |
-
import gradio as gr
|
23 |
-
|
24 |
-
# Load API keys from environment
|
25 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
26 |
if not HF_TOKEN:
|
27 |
-
raise RuntimeError("HF_TOKEN
|
28 |
-
|
29 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
"
|
40 |
-
"
|
41 |
-
"sql-mySQL", "sql-mariaDB", "sql-sqlite", "sql-cassandra", "sql-plSQL", "sql-hive",
|
42 |
-
"sql-pgSQL", "sql-gql", "sql-gpSQL", "sql-sparkSQL", "sql-esper", None
|
43 |
]
|
44 |
|
45 |
|
46 |
-
def get_gradio_language(
|
47 |
-
return
|
48 |
-
|
49 |
-
# Search/Replace Constants
|
50 |
-
SEARCH_START = "<<<<<<< SEARCH"
|
51 |
-
DIVIDER = "======="
|
52 |
-
REPLACE_END = ">>>>>>> REPLACE"
|
53 |
-
|
54 |
-
HTML_SYSTEM_PROMPT = """
|
55 |
-
You are an expert-level front-end web developer, renowned for creating modern, clean, and fully responsive web pages. Your task is to generate complete, single-file HTML documents based on user requests.
|
56 |
-
|
57 |
-
**Your Guiding Principles:**
|
58 |
-
1. **Structure is Paramount:** Employ semantic HTML5 tags (`<header>`, `<main>`, `<section>`, `<footer>`, `<nav>`, etc.) to create a logical and accessible document structure.
|
59 |
-
2. **Style with Finesse:** All CSS styling MUST be placed within a single `<style>` tag in the `<head>` of the HTML document. Adhere to modern practices; DO NOT use inline styles (e.g., `<div style="...">`).
|
60 |
-
3. **Modern & Responsive:** Utilize modern CSS techniques like Flexbox and Grid for layout. Ensure the page is fully responsive and looks exceptional on all screen sizes, from mobile phones to widescreen desktops.
|
61 |
-
4. **Accessibility First:** All images (`<img>`) must have descriptive `alt` attributes. Interactive elements should be accessible.
|
62 |
-
5. **Complete & Valid:** Always generate a full, valid HTML5 document, including `<!DOCTYPE html>`, `<html>`, `<head>`, and `<body>` tags. Include a relevant `<title>` in the head.
|
63 |
-
6. **JavaScript Best Practices:** Any necessary JavaScript should be placed in a `<script>` tag just before the closing `</body>` tag. The code must be clean, commented, and efficient.
|
64 |
-
|
65 |
-
**Output Format:**
|
66 |
-
You MUST output ONLY the raw HTML code enclosed within a single ```html ... ``` code block. Do not include any conversational text, apologies, or explanations before or after the code block. Your response should be immediately usable.
|
67 |
-
"""
|
68 |
-
|
69 |
-
# --- Transformers.js System Prompt ---
|
70 |
-
# This prompt is specialized for creating demos with the transformers.js library.
|
71 |
-
TRANSFORMERS_JS_SYSTEM_PROMPT = """
|
72 |
-
You are a specialist in machine learning on the web, with deep expertise in the transformers.js library. Your goal is to create compelling, self-contained AI applications that run entirely in the browser.
|
73 |
-
|
74 |
-
**Your Core Directives:**
|
75 |
-
1. **Functionality:** The application must be fully functional and demonstrate a clear use case of the transformers.js library (e.g., text generation, sentiment analysis, image classification).
|
76 |
-
2. **User Experience:** Create a clean, intuitive user interface. Include loading indicators, clear instructions, and well-designed input/output areas.
|
77 |
-
3. **Code Quality:** Write modern, commented ES6+ JavaScript. The code should be modular and easy to understand.
|
78 |
-
4. **Dependency Handling:** Import the transformers.js library directly from the CDN (`https://cdn.jsdelivr.net/npm/@xenova/transformers@2`).
|
79 |
|
80 |
-
**Output Format:**
|
81 |
-
Your entire response MUST consist of exactly three markdown code blocks in the following order, with no other text or explanations:
|
82 |
-
"""
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
}
|
88 |
|
89 |
-
#
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
{"name": "
|
95 |
-
{"name": "
|
96 |
-
{"name": "
|
97 |
-
{"name": "
|
98 |
-
{"name": "
|
99 |
-
{"name": "
|
100 |
-
{"name": "
|
101 |
-
{"name": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
]
|
103 |
|
104 |
-
#
|
105 |
-
#
|
106 |
-
|
107 |
-
|
108 |
-
{"title": "
|
109 |
-
{"title": "
|
110 |
-
{"title": "
|
111 |
-
{"title": "
|
112 |
-
{"title": "
|
113 |
-
{"title": "
|
114 |
-
{"title": "
|
115 |
-
{"title": "
|
116 |
-
{"title": "
|
117 |
-
{"title": "
|
118 |
-
{"title": "
|
119 |
-
{"title": "
|
120 |
-
{"title": "Transformers.js
|
121 |
-
]
|
|
|
1 |
+
"""
|
2 |
+
constants.py ── central config shared by every module
|
3 |
+
"""
|
4 |
+
|
5 |
import os
|
6 |
+
from typing import Dict, List, Optional
|
7 |
+
|
8 |
+
# --------------------------------------------------------------------------- #
|
9 |
+
# 0 · Minimal env‑checks (HF token is required; others optional) #
|
10 |
+
# --------------------------------------------------------------------------- #
|
11 |
+
HF_TOKEN: str | None = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if not HF_TOKEN:
|
13 |
+
raise RuntimeError("HF_TOKEN env‑var missing – please export it!")
|
14 |
+
|
15 |
+
OPENAI_API_KEY: str | None = os.getenv("OPENAI_API_KEY")
|
16 |
+
GEMINI_API_KEY: str | None = os.getenv("GEMINI_API_KEY")
|
17 |
+
|
18 |
+
# --------------------------------------------------------------------------- #
|
19 |
+
# 1 · Gradio syntax‑highlight whitelist #
|
20 |
+
# --------------------------------------------------------------------------- #
|
21 |
+
GRADIO_SUPPORTED_LANGUAGES: List[Optional[str]] = [
|
22 |
+
"python", "c", "cpp", "markdown", "latex", "json", "html", "css",
|
23 |
+
"javascript", "jinja2", "typescript", "yaml", "dockerfile", "shell", "r",
|
24 |
+
"sql", "sql-msSQL", "sql-mySQL", "sql-mariaDB", "sql-sqlite",
|
25 |
+
"sql-cassandra", "sql-plSQL", "sql-hive", "sql-pgSQL", "sql-gql",
|
26 |
+
"sql-gpSQL", "sql-sparkSQL", "sql-esper", None,
|
|
|
|
|
27 |
]
|
28 |
|
29 |
|
30 |
+
def get_gradio_language(lang: str) -> Optional[str]:
|
31 |
+
return lang if lang in GRADIO_SUPPORTED_LANGUAGES else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
|
|
|
|
|
|
33 |
|
34 |
+
# --------------------------------------------------------------------------- #
|
35 |
+
# 2 · Search/replace markers #
|
36 |
+
# --------------------------------------------------------------------------- #
|
37 |
+
SEARCH_START = "<<<<<<< SEARCH"
|
38 |
+
DIVIDER = "======="
|
39 |
+
REPLACE_END = ">>>>>>> REPLACE"
|
40 |
+
|
41 |
+
# --------------------------------------------------------------------------- #
|
42 |
+
# 3 · System prompts (regular + “with search”) #
|
43 |
+
# --------------------------------------------------------------------------- #
|
44 |
+
HTML_SYSTEM_PROMPT = (
|
45 |
+
"ONLY USE HTML, CSS, JS. Produce **one** fully‑responsive `<html>` file; "
|
46 |
+
"put all CSS inside a `<style>` tag in `<head>`, all JS inside one "
|
47 |
+
"`<script>` before `</body>`. No inline styles. Respond solely with a "
|
48 |
+
"```html``` fenced block – no extra commentary."
|
49 |
+
)
|
50 |
+
|
51 |
+
HTML_SYSTEM_PROMPT_WITH_SEARCH = (
|
52 |
+
HTML_SYSTEM_PROMPT
|
53 |
+
+ "\n\nFeel free to integrate insights gleaned from live web‑search."
|
54 |
+
)
|
55 |
+
|
56 |
+
TRANSFORMERS_JS_SYSTEM_PROMPT = (
|
57 |
+
"Create a browser‑only demo using `@xenova/transformers`. Output **exactly "
|
58 |
+
"three** fenced blocks, in order: `index.html`, `index.js`, `style.css`. "
|
59 |
+
"No prose."
|
60 |
+
)
|
61 |
+
|
62 |
+
TRANSFORMERS_JS_SYSTEM_PROMPT_WITH_SEARCH = (
|
63 |
+
TRANSFORMERS_JS_SYSTEM_PROMPT
|
64 |
+
+ "\n\nLeverage real‑time search to adopt the latest transformers.js tricks."
|
65 |
+
)
|
66 |
+
|
67 |
+
SVELTE_SYSTEM_PROMPT = (
|
68 |
+
"Generate the minimum custom files for a SvelteKit (TS) app. Always emit "
|
69 |
+
"`src/App.svelte` and `src/app.css` blocks – extra components only if needed."
|
70 |
+
)
|
71 |
+
|
72 |
+
SVELTE_SYSTEM_PROMPT_WITH_SEARCH = (
|
73 |
+
SVELTE_SYSTEM_PROMPT
|
74 |
+
+ "\n\nUse live search results to stay current with Svelte best‑practices."
|
75 |
+
)
|
76 |
+
|
77 |
+
GENERIC_SYSTEM_PROMPT = (
|
78 |
+
"You are an expert {language} developer. Deliver clean, runnable "
|
79 |
+
"{language} code inside one fenced block and nothing else."
|
80 |
+
)
|
81 |
+
|
82 |
+
GENERIC_SYSTEM_PROMPT_WITH_SEARCH = (
|
83 |
+
GENERIC_SYSTEM_PROMPT
|
84 |
+
+ "\n\nIncorporate any pertinent findings from web‑search."
|
85 |
+
)
|
86 |
+
|
87 |
+
FollowUpSystemPrompt = f"""
|
88 |
+
Modify existing code using SEARCH / REPLACE blocks only:
|
89 |
+
|
90 |
+
{SEARCH_START}
|
91 |
+
old lines
|
92 |
+
{DIVIDER}
|
93 |
+
new lines
|
94 |
+
{REPLACE_END}
|
95 |
+
""".strip()
|
96 |
+
|
97 |
+
TransformersJSFollowUpSystemPrompt = (
|
98 |
+
"You are editing an existing **transformers.js** project (index.html / "
|
99 |
+
"index.js / style.css). Use the same SEARCH / REPLACE format.\n"
|
100 |
+
+ FollowUpSystemPrompt
|
101 |
+
)
|
102 |
+
|
103 |
+
SYSTEM_PROMPTS: Dict[str, str] = {
|
104 |
+
"html": HTML_SYSTEM_PROMPT,
|
105 |
+
"html_search": HTML_SYSTEM_PROMPT_WITH_SEARCH,
|
106 |
+
"transformers.js": TRANSFORMERS_JS_SYSTEM_PROMPT,
|
107 |
+
"transformers.js_search": TRANSFORMERS_JS_SYSTEM_PROMPT_WITH_SEARCH,
|
108 |
+
"svelte": SVELTE_SYSTEM_PROMPT,
|
109 |
+
"svelte_search": SVELTE_SYSTEM_PROMPT_WITH_SEARCH,
|
110 |
}
|
111 |
|
112 |
+
# --------------------------------------------------------------------------- #
|
113 |
+
# 4 · Model catalogue #
|
114 |
+
# --------------------------------------------------------------------------- #
|
115 |
+
AVAILABLE_MODELS: List[Dict[str, str]] = [
|
116 |
+
# ── the 12 models from the user’s screenshot ───────────────────────────
|
117 |
+
{"name": "Moonshot Kimi‑K2", "id": "moonshotai/Kimi-K2-Instruct"},
|
118 |
+
{"name": "DeepSeek V3", "id": "deepseek-ai/DeepSeek-V3-0324"},
|
119 |
+
{"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528"},
|
120 |
+
{"name": "ERNIE‑4.5‑VL", "id": "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT"},
|
121 |
+
{"name": "MiniMax M1", "id": "MiniMaxAI/MiniMax-M1-80k"},
|
122 |
+
{"name": "Qwen3‑235B‑A22B", "id": "Qwen/Qwen3-235B-A22B"},
|
123 |
+
{"name": "SmolLM3‑3B", "id": "HuggingFaceTB/SmolLM3-3B"},
|
124 |
+
{"name": "GLM‑4.1V‑9B‑Thinking", "id": "THUDM/GLM-4.1V-9B-Thinking"},
|
125 |
+
{"name": "Qwen3‑235B‑A22B‑Instruct‑2507", "id": "Qwen/Qwen3-235B-A22B-Instruct-2507"},
|
126 |
+
{"name": "Qwen3‑Coder‑480B‑A35B", "id": "Qwen/Qwen3-Coder-480B-A35B-Instruct"},
|
127 |
+
{"name": "Qwen3‑32B", "id": "Qwen/Qwen3-32B"},
|
128 |
+
{"name": "Qwen3‑235B‑A22B‑Thinking", "id": "Qwen/Qwen3-235B-A22B-Thinking-2507"},
|
129 |
+
# ── optional extra providers (comment‑out if unneeded) ─────────────────
|
130 |
+
{"name": "OpenAI GPT‑4", "id": "openai/gpt-4", "provider": "openai"},
|
131 |
+
{"name": "Gemini Pro", "id": "gemini/pro", "provider": "gemini"},
|
132 |
+
{"name": "Fireworks V1", "id": "fireworks-ai/fireworks-v1", "provider": "fireworks"},
|
133 |
]
|
134 |
|
135 |
+
# --------------------------------------------------------------------------- #
|
136 |
+
# 5 · Sidebar quick‑start demos #
|
137 |
+
# --------------------------------------------------------------------------- #
|
138 |
+
DEMO_LIST: List[Dict[str, str]] = [
|
139 |
+
{"title": "Todo App", "description": "Add / delete / complete todo list."},
|
140 |
+
{"title": "Calculator", "description": "Four‑function calculator."},
|
141 |
+
{"title": "Chat Interface", "description": "Two‑pane chat layout."},
|
142 |
+
{"title": "E‑commerce Product", "description": "Responsive product card."},
|
143 |
+
{"title": "Login Form", "description": "Email + password form."},
|
144 |
+
{"title": "Dashboard Layout", "description": "Sidebar nav + content grid."},
|
145 |
+
{"title": "Data Table", "description": "Sortable, filterable table."},
|
146 |
+
{"title": "Image Gallery", "description": "CSS‑grid lightbox gallery."},
|
147 |
+
{"title": "UI from Image", "description": "Screenshot → HTML/CSS."},
|
148 |
+
{"title": "Extract Text from Img", "description": "OCR image for text."},
|
149 |
+
{"title": "Website Redesign", "description": "Modern redesign of a URL."},
|
150 |
+
{"title": "Modify HTML", "description": "Apply SEARCH / REPLACE edits."},
|
151 |
+
{"title": "Transformers.js Demo", "description": "Pure browser AI demo."},
|
152 |
+
]
|