mgbam commited on
Commit
517f963
·
verified ·
1 Parent(s): dad8300

Update constants.py

Browse files
Files changed (1) hide show
  1. constants.py +141 -110
constants.py CHANGED
@@ -1,121 +1,152 @@
 
 
 
 
1
  import os
2
- import re
3
- from http import HTTPStatus
4
- from typing import Dict, List, Optional, Tuple
5
- import base64
6
- import mimetypes
7
- import PyPDF2
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 environment variable is not set. Please set it to your Hugging Face API token.")
28
-
29
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
30
- if not OPENAI_API_KEY:
31
- print("Warning: OPENAI_API_KEY not set; OpenAI provider will be unavailable.")
32
-
33
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
34
- if not GEMINI_API_KEY:
35
- print("Warning: GEMINI_API_KEY not set; Gemini provider will be unavailable.")
36
-
37
- # Gradio supported languages for syntax highlighting
38
- GRADIO_SUPPORTED_LANGUAGES = [
39
- "python", "c", "cpp", "markdown", "latex", "json", "html", "css", "javascript",
40
- "jinja2", "typescript", "yaml", "dockerfile", "shell", "r", "sql", "sql-msSQL",
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(language):
47
- return language if language in GRADIO_SUPPORTED_LANGUAGES else None
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
- SYSTEM_PROMPTS = {
85
- "html": HTML_SYSTEM_PROMPT,
86
- "transformers_js": TRANSFORMERS_JS_SYSTEM_PROMPT,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  }
88
 
89
- # Available HF models for code generation
90
- AVAILABLE_MODELS = [
91
- {"name": "Moonshot Kimi-K2", "id": "moonshotai/Kimi-K2-Instruct", "description": "Moonshot AI Kimi-K2-Instruct model", "category": "Hugging Face"},
92
- {"name": "DeepSeek V3", "id": "deepseek-ai/DeepSeek-V3-0324", "description": "DeepSeek V3 model", "category": "Hugging Face"},
93
- {"name": "DeepSeek R1", "id": "deepseek-ai/DeepSeek-R1-0528", "description": "DeepSeek R1 model", "category": "Hugging Face"},
94
- {"name": "ERNIE-4.5-VL", "id": "baidu/ERNIE-4.5-VL-424B-A47B-Base-PT", "description": "ERNIE-4.5-VL model", "category": "Hugging Face"},
95
- {"name": "MiniMax M1", "id": "MiniMaxAI/MiniMax-M1-80k", "description": "MiniMax M1 model", "category": "Hugging Face"},
96
- {"name": "Qwen3-235B-A22B", "id": "Qwen/Qwen3-235B-A22B", "description": "Qwen3-235B-A22B model", "category": "Hugging Face"},
97
- {"name": "SmolLM3-3B", "id": "HuggingFaceTB/SmolLM3-3B", "description": "SmolLM3-3B model", "category": "Hugging Face"},
98
- {"name": "GLM-4.1V-9B-Thinking", "id": "THUDM/GLM-4.1V-9B-Thinking", "description": "GLM-4.1V-9B-Thinking model", "category": "Hugging Face"},
99
- {"name": "OpenAI GPT-4", "id": "openai/gpt-4", "description": "OpenAI GPT-4 model", "category": "OpenAI"},
100
- {"name": "Gemini Pro", "id": "gemini/pro", "description": "Google Gemini Pro model", "category": "Gemini"},
101
- {"name": "Fireworks AI", "id": "fireworks-ai/fireworks-v1", "description": "Fireworks AI model", "category": "Fireworks AI"},
 
 
 
 
 
 
 
 
102
  ]
103
 
104
- # Quick‑start demo list
105
- # Quick‑start demo list for the sidebar
106
- DEMO_LIST = [
107
- {"title": "Todo App", "description": "Create a simple todo application with add, delete, and mark as complete functionality"},
108
- {"title": "Calculator", "description": "Build a basic calculator with addition, subtraction, multiplication, and division"},
109
- {"title": "Chat Interface", "description": "Build a chat interface with message history and user input"},
110
- {"title": "E-commerce Product Card", "description": "Create a product card component for an e-commerce website"},
111
- {"title": "Login Form", "description": "Build a responsive login form with validation"},
112
- {"title": "Dashboard Layout", "description": "Create a dashboard layout with sidebar navigation and main content area"},
113
- {"title": "Data Table", "description": "Build a data table with sorting and filtering capabilities"},
114
- {"title": "Image Gallery", "description": "Create an image gallery with lightbox functionality and responsive grid layout"},
115
- {"title": "UI from Image", "description": "Upload an image of a UI design and I'll generate the HTML/CSS code for it"},
116
- {"title": "Extract Text from Image", "description": "Upload an image containing text and I'll extract and process the text content"},
117
- {"title": "Website Redesign", "description": "Enter a website URL to extract its content and redesign it with a modern, responsive layout"},
118
- {"title": "Modify HTML", "description": "After generating HTML, ask me to modify it with specific changes using search/replace format"},
119
- {"title": "Search/Replace Example", "description": "Generate HTML first, then ask: 'Change the title to My New Title' or 'Add a blue background to the body'"},
120
- {"title": "Transformers.js App", "description": "Create a transformers.js application with AI/ML functionality using the transformers.js library"}
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
+ ]