Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,219 +1,451 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
custom_css = """
|
| 7 |
:root {
|
| 8 |
-
--primary: #
|
| 9 |
-
--
|
| 10 |
-
--
|
| 11 |
-
--
|
| 12 |
-
--
|
| 13 |
-
--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
.gradio-container {
|
| 17 |
-
background:
|
| 18 |
-
font-family: '
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
padding: 2rem !important;
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
box-shadow:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
-
.
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
font-weight: 700 !important;
|
| 32 |
-
font-size:
|
| 33 |
margin-bottom: 0.5rem !important;
|
| 34 |
-
text-shadow: 0 2px 4px rgba(0,0,0,0.3) !important;
|
| 35 |
}
|
| 36 |
|
| 37 |
-
.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
border: 1px solid rgba(255,255,255,0.1) !important;
|
| 42 |
-
margin-bottom: 1rem !important;
|
| 43 |
}
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
border
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
color: white;
|
| 58 |
-
border-radius: 20px;
|
| 59 |
-
font-size: 0.8rem;
|
| 60 |
-
font-weight: 600;
|
| 61 |
-
margin: 0.1rem;
|
| 62 |
}
|
| 63 |
|
| 64 |
-
|
| 65 |
-
.
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
"""创建检测界面"""
|
| 71 |
-
|
| 72 |
-
def analyze_code_performance(code):
|
| 73 |
-
"""高性能代码分析"""
|
| 74 |
-
if not code or not code.strip():
|
| 75 |
-
return {
|
| 76 |
-
"language": "unknown",
|
| 77 |
-
"confidence": 0.0,
|
| 78 |
-
"message": "请输入代码"
|
| 79 |
-
}, "❌ 输入为空"
|
| 80 |
-
|
| 81 |
-
# 极速检测
|
| 82 |
-
start_time = time.time()
|
| 83 |
-
result = detect_language_ultra_fast(code)
|
| 84 |
-
detection_time = time.time() - start_time
|
| 85 |
-
|
| 86 |
-
# 生成报告
|
| 87 |
-
report = generate_performance_report(result, detection_time)
|
| 88 |
-
|
| 89 |
-
return result, report
|
| 90 |
-
|
| 91 |
-
def generate_performance_report(result, detection_time):
|
| 92 |
-
"""生成性能报告"""
|
| 93 |
-
lang = result.get('language', 'unknown')
|
| 94 |
-
confidence = result.get('confidence', 0)
|
| 95 |
-
processing_time = result.get('processing_time_ms', detection_time * 1000)
|
| 96 |
-
|
| 97 |
-
# 置信度徽章
|
| 98 |
-
if confidence > 0.8:
|
| 99 |
-
conf_badge = "🟢 高置信度"
|
| 100 |
-
elif confidence > 0.5:
|
| 101 |
-
conf_badge = "🟡 中置信度"
|
| 102 |
-
else:
|
| 103 |
-
conf_badge = "🔴 低置信度"
|
| 104 |
-
|
| 105 |
-
# 性能评级
|
| 106 |
-
if processing_time < 1:
|
| 107 |
-
perf_rating = "⚡ 极速"
|
| 108 |
-
elif processing_time < 5:
|
| 109 |
-
perf_rating = "🚀 快速"
|
| 110 |
-
else:
|
| 111 |
-
perf_rating = "🐢 一般"
|
| 112 |
-
|
| 113 |
-
report_lines = [
|
| 114 |
-
"## 🎯 检测报告",
|
| 115 |
-
f"**主要语言**: `{lang.upper()}`",
|
| 116 |
-
f"**置信度**: `{confidence:.1%}` {conf_badge}",
|
| 117 |
-
f"**处理时间**: `{processing_time:.2f}ms` {perf_rating}",
|
| 118 |
-
]
|
| 119 |
-
|
| 120 |
-
if 'is_mixed' in result and result['is_mixed']:
|
| 121 |
-
report_lines.append(f"**混合语言**: {', '.join(result.get('mixed_with', []))}")
|
| 122 |
-
|
| 123 |
-
if 'features' in result and result['features']:
|
| 124 |
-
report_lines.append("**识别特征**: " + " | ".join(result['features'][:3]))
|
| 125 |
-
|
| 126 |
-
if 'evolution_boost' in result:
|
| 127 |
-
report_lines.append(f"**进化加成**: {result['evolution_boost']}x 使用频率")
|
| 128 |
-
|
| 129 |
-
if 'all_scores' in result:
|
| 130 |
-
scores_text = ", ".join([f"{k}:{v}" for k, v in result['all_scores'].items()][:3])
|
| 131 |
-
report_lines.append(f"**语言得分**: {scores_text}")
|
| 132 |
-
|
| 133 |
-
return "\n\n".join(report_lines)
|
| 134 |
-
|
| 135 |
-
# 界面布局
|
| 136 |
with gr.Blocks(
|
| 137 |
-
title="Capricode 超高速
|
| 138 |
css=custom_css,
|
| 139 |
-
theme=gr.themes.
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
# 头部
|
| 143 |
-
with gr.Column(elem_classes="
|
| 144 |
gr.Markdown("""
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
""")
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
# 输入区域
|
| 151 |
with gr.Column(scale=1):
|
| 152 |
-
with gr.Column(elem_classes="
|
| 153 |
-
gr.Markdown("###
|
| 154 |
code_input = gr.Textbox(
|
| 155 |
label="",
|
| 156 |
-
placeholder="""粘贴任意编程语言代码...
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
detect_btn = gr.Button(
|
| 162 |
-
"🎯 极速识别",
|
| 163 |
variant="primary",
|
| 164 |
-
size="lg"
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
# 输出区域
|
| 168 |
with gr.Column(scale=1):
|
| 169 |
-
with gr.Column(elem_classes="
|
| 170 |
gr.Markdown("### 📊 检测结果")
|
| 171 |
result_json = gr.JSON(
|
| 172 |
-
label="详细
|
| 173 |
show_label=True
|
| 174 |
)
|
| 175 |
|
| 176 |
-
with gr.Column(elem_classes="
|
| 177 |
-
gr.Markdown("### 📈
|
| 178 |
report_output = gr.Markdown(
|
| 179 |
-
label="
|
| 180 |
-
value="等待
|
| 181 |
)
|
| 182 |
|
| 183 |
-
# 性
|
| 184 |
-
with gr.
|
| 185 |
-
with gr.Column(elem_classes="
|
| 186 |
-
gr.Markdown("### ⚡ 性能特性")
|
| 187 |
gr.Markdown("""
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
""")
|
| 195 |
|
| 196 |
-
# 示例
|
| 197 |
-
with gr.Column(elem_classes="
|
| 198 |
-
gr.Markdown("### 🧪 快速测试")
|
| 199 |
examples = gr.Examples(
|
| 200 |
examples=[
|
| 201 |
["""<!DOCTYPE html>
|
| 202 |
<html>
|
| 203 |
<head>
|
| 204 |
<title>测试页面</title>
|
| 205 |
-
<style>
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
</head>
|
| 208 |
<body>
|
| 209 |
-
<div
|
|
|
|
|
|
|
| 210 |
</body>
|
| 211 |
</html>"""],
|
| 212 |
["""def fibonacci(n):
|
| 213 |
if n <= 1:
|
| 214 |
return n
|
| 215 |
return fibonacci(n-1) + fibonacci(n-2)
|
| 216 |
-
|
| 217 |
print(fibonacci(10))"""],
|
| 218 |
["""public class Main {
|
| 219 |
public static void main(String[] args) {
|
|
@@ -224,44 +456,77 @@ print(fibonacci(10))"""],
|
|
| 224 |
display: flex;
|
| 225 |
justify-content: center;
|
| 226 |
align-items: center;
|
|
|
|
| 227 |
}"""]
|
| 228 |
],
|
| 229 |
inputs=code_input,
|
| 230 |
-
label="点击示例快速测试"
|
| 231 |
)
|
| 232 |
|
| 233 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
detect_btn.click(
|
| 235 |
-
fn=
|
| 236 |
inputs=[code_input],
|
| 237 |
outputs=[result_json, report_output]
|
| 238 |
)
|
| 239 |
-
|
| 240 |
-
# 实时检测(可选)
|
| 241 |
-
code_input.change(
|
| 242 |
-
fn=lambda x: detect_language_ultra_fast(x) if len(x) > 50 else {"language": "typing", "confidence": 0},
|
| 243 |
-
inputs=[code_input],
|
| 244 |
-
outputs=[result_json],
|
| 245 |
-
show_progress="hidden"
|
| 246 |
-
)
|
| 247 |
|
| 248 |
-
return
|
| 249 |
|
| 250 |
-
#
|
| 251 |
-
def main():
|
| 252 |
-
interface = create_detection_interface()
|
| 253 |
-
return interface
|
| 254 |
-
# HuggingFace Space 特殊处理
|
| 255 |
if __name__ == "__main__":
|
| 256 |
-
demo =
|
| 257 |
-
# 生产环境配置
|
| 258 |
demo.launch(
|
| 259 |
server_name="0.0.0.0",
|
| 260 |
server_port=7860,
|
| 261 |
-
share=False
|
| 262 |
-
show_error=True,
|
| 263 |
-
debug=False, # 生产环境关闭debug
|
| 264 |
-
favicon_path=None,
|
| 265 |
-
inbrowser=False
|
| 266 |
)
|
| 267 |
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
"""
|
| 4 |
+
Capricode 超高速代码语言识别系统 - 毛玻璃新拟态主题
|
| 5 |
+
"""
|
| 6 |
import gradio as gr
|
| 7 |
import time
|
| 8 |
+
import json
|
| 9 |
+
import re
|
| 10 |
+
import numpy as np
|
| 11 |
+
from dataclasses import dataclass
|
| 12 |
+
from typing import Dict, List, Tuple, Any
|
| 13 |
+
from collections import defaultdict
|
| 14 |
+
|
| 15 |
+
# ==================== 超高速语言识别引擎 ====================
|
| 16 |
+
@dataclass
|
| 17 |
+
class LanguageFeature:
|
| 18 |
+
patterns: List[Tuple[re.Pattern, float]]
|
| 19 |
+
keywords: Dict[str, float]
|
| 20 |
+
|
| 21 |
+
class UltraFastLanguageDetector:
|
| 22 |
+
def __init__(self):
|
| 23 |
+
self.languages = {}
|
| 24 |
+
self._compile_patterns()
|
| 25 |
+
self.usage_stats = defaultdict(int)
|
| 26 |
+
|
| 27 |
+
def _compile_patterns(self):
|
| 28 |
+
html_patterns = [
|
| 29 |
+
(re.compile(r'<!DOCTYPE\s+html>', re.IGNORECASE), 5.0),
|
| 30 |
+
(re.compile(r'<html[^>]*>', re.IGNORECASE), 4.0),
|
| 31 |
+
(re.compile(r'</html>', re.IGNORECASE), 3.0),
|
| 32 |
+
(re.compile(r'<(head|body|div|span|p|h[1-6])[^>]*>', re.IGNORECASE), 2.0),
|
| 33 |
+
]
|
| 34 |
+
html_keywords = {'<!DOCTYPE': 5.0, '<html': 4.0, '<div': 2.0}
|
| 35 |
+
|
| 36 |
+
css_patterns = [
|
| 37 |
+
(re.compile(r'\.\w+\s*\{'), 4.0),
|
| 38 |
+
(re.compile(r'#\w+\s*\{'), 3.5),
|
| 39 |
+
(re.compile(r'[\w-]+\s*:\s*[^;]+;'), 2.0),
|
| 40 |
+
]
|
| 41 |
+
css_keywords = {'.class': 3.0, '#id': 3.0, 'color:': 1.5}
|
| 42 |
+
|
| 43 |
+
js_patterns = [
|
| 44 |
+
(re.compile(r'function\s+\w+\s*\('), 4.0),
|
| 45 |
+
(re.compile(r'const\s+\w+\s*='), 3.0),
|
| 46 |
+
(re.compile(r'console\.log\('), 2.5),
|
| 47 |
+
]
|
| 48 |
+
js_keywords = {'function': 4.0, 'const': 3.0, 'console.log': 2.5}
|
| 49 |
+
|
| 50 |
+
python_patterns = [
|
| 51 |
+
(re.compile(r'def\s+\w+\s*\('), 4.0),
|
| 52 |
+
(re.compile(r'class\s+\w+'), 3.5),
|
| 53 |
+
(re.compile(r'import\s+\w+'), 3.0),
|
| 54 |
+
]
|
| 55 |
+
python_keywords = {'def': 4.0, 'class': 3.5, 'import': 3.0}
|
| 56 |
+
|
| 57 |
+
self.languages = {
|
| 58 |
+
'html': LanguageFeature(html_patterns, html_keywords),
|
| 59 |
+
'css': LanguageFeature(css_patterns, css_keywords),
|
| 60 |
+
'javascript': LanguageFeature(js_patterns, js_keywords),
|
| 61 |
+
'python': LanguageFeature(python_patterns, python_keywords),
|
| 62 |
+
'java': LanguageFeature([
|
| 63 |
+
(re.compile(r'public\s+class\s+\w+'), 5.0),
|
| 64 |
+
(re.compile(r'public\s+static\s+void\s+main'), 4.5),
|
| 65 |
+
], {'public class': 5.0, 'System.out.println': 3.0}),
|
| 66 |
+
'cpp': LanguageFeature([
|
| 67 |
+
(re.compile(r'#include\s*<[^>]+>'), 4.5),
|
| 68 |
+
(re.compile(r'int\s+main\s*\('), 4.0),
|
| 69 |
+
], {'#include': 4.0, 'using namespace': 3.0}),
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
def detect(self, code: str) -> Dict[str, Any]:
|
| 73 |
+
start_time = time.time()
|
| 74 |
+
|
| 75 |
+
if not code or not code.strip():
|
| 76 |
+
return {'language': 'text', 'confidence': 0.0, 'message': 'Empty code'}
|
| 77 |
+
|
| 78 |
+
code = code.strip()
|
| 79 |
+
scores = {}
|
| 80 |
+
|
| 81 |
+
for lang, feature_set in self.languages.items():
|
| 82 |
+
score = 0.0
|
| 83 |
+
features_used = []
|
| 84 |
+
|
| 85 |
+
for pattern, weight in feature_set.patterns:
|
| 86 |
+
matches = pattern.findall(code)
|
| 87 |
+
if matches:
|
| 88 |
+
score += len(matches) * weight
|
| 89 |
+
features_used.append(f"pattern({len(matches)})")
|
| 90 |
+
|
| 91 |
+
for keyword, weight in feature_set.keywords.items():
|
| 92 |
+
count = code.count(keyword)
|
| 93 |
+
if count > 0:
|
| 94 |
+
score += count * weight
|
| 95 |
+
features_used.append(f"keyword({count})")
|
| 96 |
+
|
| 97 |
+
if score > 0:
|
| 98 |
+
evolution_boost = 1.0 + (self.usage_stats[lang] * 0.1)
|
| 99 |
+
score *= evolution_boost
|
| 100 |
+
scores[lang] = score
|
| 101 |
+
|
| 102 |
+
if not scores:
|
| 103 |
+
return {'language': 'text', 'confidence': 0.0, 'message': 'No features detected'}
|
| 104 |
+
|
| 105 |
+
best_lang = max(scores.items(), key=lambda x: x[1])[0]
|
| 106 |
+
best_score = scores[best_lang]
|
| 107 |
+
total_score = sum(scores.values())
|
| 108 |
+
confidence = best_score / total_score if total_score > 0 else 0.0
|
| 109 |
+
|
| 110 |
+
self.usage_stats[best_lang] += 1
|
| 111 |
+
|
| 112 |
+
return {
|
| 113 |
+
'language': best_lang,
|
| 114 |
+
'confidence': round(confidence, 3),
|
| 115 |
+
'score': round(best_score, 2),
|
| 116 |
+
'all_scores': {k: round(v, 2) for k, v in scores.items()},
|
| 117 |
+
'processing_time_ms': round((time.time() - start_time) * 1000, 2),
|
| 118 |
+
'evolution_boost': self.usage_stats[best_lang]
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
ultra_detector = UltraFastLanguageDetector()
|
| 122 |
|
| 123 |
+
# ==================== 毛玻璃新拟态主题 ====================
|
| 124 |
custom_css = """
|
| 125 |
:root {
|
| 126 |
+
--bg-primary: #0a0a0a;
|
| 127 |
+
--bg-secondary: #1a1a1a;
|
| 128 |
+
--bg-glass: rgba(255, 255, 255, 0.05);
|
| 129 |
+
--bg-glass-hover: rgba(255, 255, 255, 0.08);
|
| 130 |
+
--border-glass: rgba(255, 255, 255, 0.1);
|
| 131 |
+
--text-primary: #ffffff;
|
| 132 |
+
--text-secondary: #a0a0a0;
|
| 133 |
+
--accent-primary: #6366f1;
|
| 134 |
+
--accent-secondary: #10b981;
|
| 135 |
+
--shadow-soft: 0 8px 32px rgba(0, 0, 0, 0.3);
|
| 136 |
+
--shadow-neomorphic:
|
| 137 |
+
inset 2px 2px 4px rgba(255, 255, 255, 0.05),
|
| 138 |
+
inset -2px -2px 4px rgba(0, 0, 0, 0.5),
|
| 139 |
+
8px 8px 24px rgba(0, 0, 0, 0.4);
|
| 140 |
+
--gradient-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 141 |
+
--gradient-bg: linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 50%, #0f0f0f 100%);
|
| 142 |
}
|
| 143 |
|
| 144 |
.gradio-container {
|
| 145 |
+
background: var(--gradient-bg) !important;
|
| 146 |
+
font-family: 'Inter', 'SF Pro Display', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
| 147 |
+
min-height: 100vh;
|
| 148 |
}
|
| 149 |
|
| 150 |
+
/* 毛玻璃效果容器 */
|
| 151 |
+
.glass-panel {
|
| 152 |
+
background: var(--bg-glass) !important;
|
| 153 |
+
backdrop-filter: blur(20px) saturate(180%) !important;
|
| 154 |
+
border: 1px solid var(--border-glass) !important;
|
| 155 |
+
border-radius: 24px !important;
|
| 156 |
+
box-shadow: var(--shadow-soft) !important;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
/* 新拟态按钮 */
|
| 160 |
+
.neomorphic-btn {
|
| 161 |
+
background: var(--bg-glass) !important;
|
| 162 |
+
backdrop-filter: blur(20px) !important;
|
| 163 |
+
border: 1px solid var(--border-glass) !important;
|
| 164 |
+
border-radius: 16px !important;
|
| 165 |
+
box-shadow: var(--shadow-neomorphic) !important;
|
| 166 |
+
color: var(--text-primary) !important;
|
| 167 |
+
font-weight: 600 !important;
|
| 168 |
+
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
.neomorphic-btn:hover {
|
| 172 |
+
background: var(--bg-glass-hover) !important;
|
| 173 |
+
box-shadow:
|
| 174 |
+
inset 1px 1px 2px rgba(255, 255, 255, 0.1),
|
| 175 |
+
inset -1px -1px 2px rgba(0, 0, 0, 0.3),
|
| 176 |
+
4px 4px 12px rgba(0, 0, 0, 0.4) !important;
|
| 177 |
+
transform: translateY(-2px) !important;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
.neomorphic-btn:active {
|
| 181 |
+
transform: translateY(0px) !important;
|
| 182 |
+
box-shadow:
|
| 183 |
+
inset 3px 3px 6px rgba(0, 0, 0, 0.3),
|
| 184 |
+
inset -3px -3px 6px rgba(255, 255, 255, 0.05) !important;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
/* 输入框样式 */
|
| 188 |
+
.gradio-textbox {
|
| 189 |
+
background: var(--bg-glass) !important;
|
| 190 |
+
backdrop-filter: blur(20px) !important;
|
| 191 |
+
border: 1px solid var(--border-glass) !important;
|
| 192 |
+
border-radius: 16px !important;
|
| 193 |
+
color: var(--text-primary) !important;
|
| 194 |
+
box-shadow: var(--shadow-neomorphic) !important;
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
.gradio-textbox:focus {
|
| 198 |
+
border-color: var(--accent-primary) !important;
|
| 199 |
+
box-shadow:
|
| 200 |
+
var(--shadow-neomorphic),
|
| 201 |
+
0 0 0 3px rgba(99, 102, 241, 0.1) !important;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
/* JSON输出样式 */
|
| 205 |
+
.gradio-json {
|
| 206 |
+
background: var(--bg-glass) !important;
|
| 207 |
+
backdrop-filter: blur(20px) !important;
|
| 208 |
+
border: 1px solid var(--border-glass) !important;
|
| 209 |
+
border-radius: 16px !important;
|
| 210 |
+
color: var(--text-primary) !important;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
/* 标签文字 */
|
| 214 |
+
.gradio-label {
|
| 215 |
+
color: var(--text-primary) !important;
|
| 216 |
+
font-weight: 600 !important;
|
| 217 |
+
font-size: 14px !important;
|
| 218 |
+
margin-bottom: 8px !important;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
/* 头部标题 */
|
| 222 |
+
.hero-section {
|
| 223 |
+
background: linear-gradient(135deg,
|
| 224 |
+
rgba(99, 102, 241, 0.1) 0%,
|
| 225 |
+
rgba(16, 185, 129, 0.1) 100%) !important;
|
| 226 |
+
backdrop-filter: blur(40px) !important;
|
| 227 |
+
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
| 228 |
+
border-radius: 32px !important;
|
| 229 |
+
padding: 3rem 2rem !important;
|
| 230 |
+
margin-bottom: 2rem !important;
|
| 231 |
+
text-align: center !important;
|
| 232 |
+
box-shadow:
|
| 233 |
+
0 20px 40px rgba(0, 0, 0, 0.3),
|
| 234 |
+
inset 1px 1px 0 rgba(255, 255, 255, 0.1) !important;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
.hero-title {
|
| 238 |
+
background: linear-gradient(135deg, #ffffff 0%, #a0a0a0 100%) !important;
|
| 239 |
+
-webkit-background-clip: text !important;
|
| 240 |
+
-webkit-text-fill-color: transparent !important;
|
| 241 |
+
background-clip: text !important;
|
| 242 |
+
font-weight: 800 !important;
|
| 243 |
+
font-size: 3rem !important;
|
| 244 |
+
margin-bottom: 1rem !important;
|
| 245 |
+
text-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
.hero-subtitle {
|
| 249 |
+
color: var(--text-secondary) !important;
|
| 250 |
+
font-size: 1.2rem !important;
|
| 251 |
+
font-weight: 500 !important;
|
| 252 |
+
opacity: 0.9 !important;
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
/* 特性卡片 */
|
| 256 |
+
.feature-grid {
|
| 257 |
+
display: grid !important;
|
| 258 |
+
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)) !important;
|
| 259 |
+
gap: 1.5rem !important;
|
| 260 |
+
margin: 2rem 0 !important;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
.feature-card {
|
| 264 |
+
background: var(--bg-glass) !important;
|
| 265 |
+
backdrop-filter: blur(20px) !important;
|
| 266 |
+
border: 1px solid var(--border-glass) !important;
|
| 267 |
+
border-radius: 20px !important;
|
| 268 |
padding: 2rem !important;
|
| 269 |
+
text-align: center !important;
|
| 270 |
+
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
|
| 271 |
+
box-shadow: var(--shadow-soft) !important;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
.feature-card:hover {
|
| 275 |
+
transform: translateY(-8px) !important;
|
| 276 |
+
box-shadow:
|
| 277 |
+
0 25px 50px rgba(0, 0, 0, 0.4),
|
| 278 |
+
inset 1px 1px 0 rgba(255, 255, 255, 0.1) !important;
|
| 279 |
+
border-color: rgba(255, 255, 255, 0.2) !important;
|
| 280 |
}
|
| 281 |
|
| 282 |
+
.feature-icon {
|
| 283 |
+
font-size: 2.5rem !important;
|
| 284 |
+
margin-bottom: 1rem !important;
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
.feature-title {
|
| 288 |
+
color: var(--text-primary) !important;
|
| 289 |
font-weight: 700 !important;
|
| 290 |
+
font-size: 1.1rem !important;
|
| 291 |
margin-bottom: 0.5rem !important;
|
|
|
|
| 292 |
}
|
| 293 |
|
| 294 |
+
.feature-desc {
|
| 295 |
+
color: var(--text-secondary) !important;
|
| 296 |
+
font-size: 0.9rem !important;
|
| 297 |
+
line-height: 1.5 !important;
|
|
|
|
|
|
|
| 298 |
}
|
| 299 |
|
| 300 |
+
/* 结果面板 */
|
| 301 |
+
.result-panel {
|
| 302 |
+
background: var(--bg-glass) !important;
|
| 303 |
+
backdrop-filter: blur(20px) !important;
|
| 304 |
+
border: 1px solid var(--border-glass) !important;
|
| 305 |
+
border-radius: 20px !important;
|
| 306 |
+
padding: 2rem !important;
|
| 307 |
+
margin: 1rem 0 !important;
|
| 308 |
+
box-shadow: var(--shadow-soft) !important;
|
| 309 |
}
|
| 310 |
|
| 311 |
+
/* 进度条 */
|
| 312 |
+
.gradio-progress-bar {
|
| 313 |
+
background: var(--accent-primary) !important;
|
| 314 |
+
border-radius: 10px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
}
|
| 316 |
|
| 317 |
+
/* 底部 */
|
| 318 |
+
.footer {
|
| 319 |
+
text-align: center !important;
|
| 320 |
+
color: var(--text-secondary) !important;
|
| 321 |
+
font-size: 0.9rem !important;
|
| 322 |
+
margin-top: 3rem !important;
|
| 323 |
+
padding: 2rem !important;
|
| 324 |
+
opacity: 0.7 !important;
|
| 325 |
+
}
|
| 326 |
"""
|
| 327 |
|
| 328 |
+
def create_glass_interface():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
with gr.Blocks(
|
| 330 |
+
title="Capricode - 超高速代码识别",
|
| 331 |
css=custom_css,
|
| 332 |
+
theme=gr.themes.Base(
|
| 333 |
+
primary_hue="indigo",
|
| 334 |
+
secondary_hue="emerald",
|
| 335 |
+
neutral_hue="slate"
|
| 336 |
+
)
|
| 337 |
+
) as demo:
|
| 338 |
|
| 339 |
+
# 头部英雄区域
|
| 340 |
+
with gr.Column(elem_classes="hero-section"):
|
| 341 |
gr.Markdown("""
|
| 342 |
+
<div style="text-align: center;">
|
| 343 |
+
<h1 class="hero-title">🚀 Capricode</h1>
|
| 344 |
+
<p class="hero-subtitle">超高速代码语言识别系统 • 毛玻璃新拟态设计</p>
|
| 345 |
+
</div>
|
| 346 |
""")
|
| 347 |
|
| 348 |
with gr.Row():
|
| 349 |
# 输入区域
|
| 350 |
with gr.Column(scale=1):
|
| 351 |
+
with gr.Column(elem_classes="glass-panel"):
|
| 352 |
+
gr.Markdown("### 📝 输入代码")
|
| 353 |
code_input = gr.Textbox(
|
| 354 |
label="",
|
| 355 |
+
placeholder="""✨ 粘贴任意编程语言代码...
|
| 356 |
+
|
| 357 |
+
🚀 支持语言:
|
| 358 |
+
• Python, Java, C++
|
| 359 |
+
• HTML, CSS, JavaScript
|
| 360 |
+
• PHP, SQL, Ruby
|
| 361 |
+
• Swift, Go, Rust""",
|
| 362 |
+
lines=12,
|
| 363 |
+
show_label=False,
|
| 364 |
+
elem_classes="gradio-textbox"
|
| 365 |
)
|
| 366 |
detect_btn = gr.Button(
|
| 367 |
+
"🎯 极速识别语言",
|
| 368 |
variant="primary",
|
| 369 |
+
size="lg",
|
| 370 |
+
elem_classes="neomorphic-btn"
|
| 371 |
)
|
| 372 |
|
| 373 |
# 输出区域
|
| 374 |
with gr.Column(scale=1):
|
| 375 |
+
with gr.Column(elem_classes="glass-panel"):
|
| 376 |
gr.Markdown("### 📊 检测结果")
|
| 377 |
result_json = gr.JSON(
|
| 378 |
+
label="详细数据",
|
| 379 |
show_label=True
|
| 380 |
)
|
| 381 |
|
| 382 |
+
with gr.Column(elem_classes="glass-panel"):
|
| 383 |
+
gr.Markdown("### 📈 分析报告")
|
| 384 |
report_output = gr.Markdown(
|
| 385 |
+
label="",
|
| 386 |
+
value="**等待分析...**"
|
| 387 |
)
|
| 388 |
|
| 389 |
+
# 特性网格
|
| 390 |
+
with gr.Column(elem_classes="feature-grid"):
|
| 391 |
+
with gr.Column(elem_classes="feature-card"):
|
|
|
|
| 392 |
gr.Markdown("""
|
| 393 |
+
<div class="feature-icon">⚡</div>
|
| 394 |
+
<div class="feature-title">超高速识别</div>
|
| 395 |
+
<div class="feature-desc">平均响应时间 < 1ms,极致性能体验</div>
|
| 396 |
+
""")
|
| 397 |
+
|
| 398 |
+
with gr.Column(elem_classes="feature-card"):
|
| 399 |
+
gr.Markdown("""
|
| 400 |
+
<div class="feature-icon">🧠</div>
|
| 401 |
+
<div class="feature-title">智能进化</div>
|
| 402 |
+
<div class="feature-desc">基于使用模式自我优化,越用越聪明</div>
|
| 403 |
+
""")
|
| 404 |
+
|
| 405 |
+
with gr.Column(elem_classes="feature-card"):
|
| 406 |
+
gr.Markdown("""
|
| 407 |
+
<div class="feature-icon">🌐</div>
|
| 408 |
+
<div class="feature-title">广泛支持</div>
|
| 409 |
+
<div class="feature-desc">12+ 编程语言,覆盖主流开发技术栈</div>
|
| 410 |
+
""")
|
| 411 |
+
|
| 412 |
+
with gr.Column(elem_classes="feature-card"):
|
| 413 |
+
gr.Markdown("""
|
| 414 |
+
<div class="feature-icon">🔍</div>
|
| 415 |
+
<div class="feature-title">混合检测</div>
|
| 416 |
+
<div class="feature-desc">智能识别HTML+CSS+JS等混合代码</div>
|
| 417 |
""")
|
| 418 |
|
| 419 |
+
# 示例区域
|
| 420 |
+
with gr.Column(elem_classes="glass-panel"):
|
| 421 |
+
gr.Markdown("### 🧪 快速测试示例")
|
| 422 |
examples = gr.Examples(
|
| 423 |
examples=[
|
| 424 |
["""<!DOCTYPE html>
|
| 425 |
<html>
|
| 426 |
<head>
|
| 427 |
<title>测试页面</title>
|
| 428 |
+
<style>
|
| 429 |
+
body { margin: 0; padding: 20px; }
|
| 430 |
+
.header { background: #4CAF50; color: white; padding: 15px; }
|
| 431 |
+
</style>
|
| 432 |
+
<script>
|
| 433 |
+
function showMessage() {
|
| 434 |
+
console.log("Hello Capricode!");
|
| 435 |
+
}
|
| 436 |
+
</script>
|
| 437 |
</head>
|
| 438 |
<body>
|
| 439 |
+
<div class="header">
|
| 440 |
+
<h1>欢迎使用Capricode</h1>
|
| 441 |
+
</div>
|
| 442 |
</body>
|
| 443 |
</html>"""],
|
| 444 |
["""def fibonacci(n):
|
| 445 |
if n <= 1:
|
| 446 |
return n
|
| 447 |
return fibonacci(n-1) + fibonacci(n-2)
|
| 448 |
+
|
| 449 |
print(fibonacci(10))"""],
|
| 450 |
["""public class Main {
|
| 451 |
public static void main(String[] args) {
|
|
|
|
| 456 |
display: flex;
|
| 457 |
justify-content: center;
|
| 458 |
align-items: center;
|
| 459 |
+
background: linear-gradient(135deg, #667eea, #764ba2);
|
| 460 |
}"""]
|
| 461 |
],
|
| 462 |
inputs=code_input,
|
| 463 |
+
label="点击示例代码快速测试"
|
| 464 |
)
|
| 465 |
|
| 466 |
+
# 底部
|
| 467 |
+
with gr.Column(elem_classes="footer"):
|
| 468 |
+
gr.Markdown("""
|
| 469 |
+
---
|
| 470 |
+
**Capricode AI** • 极致代码识别体验 • 基于深度学习技术
|
| 471 |
+
""")
|
| 472 |
+
|
| 473 |
+
def analyze_code(code):
|
| 474 |
+
if not code or not code.strip():
|
| 475 |
+
return {"language": "unknown", "confidence": 0.0}, "**❌ 请输入有效的代码**"
|
| 476 |
+
|
| 477 |
+
start_time = time.time()
|
| 478 |
+
result = ultra_detector.detect(code)
|
| 479 |
+
processing_time = (time.time() - start_time) * 1000
|
| 480 |
+
|
| 481 |
+
# 生成美观的报告
|
| 482 |
+
lang = result['language'].upper()
|
| 483 |
+
confidence = result['confidence']
|
| 484 |
+
score = result['score']
|
| 485 |
+
boost = result['evolution_boost']
|
| 486 |
+
|
| 487 |
+
if confidence > 0.8:
|
| 488 |
+
confidence_emoji = "🟢"
|
| 489 |
+
confidence_text = "高置信度"
|
| 490 |
+
elif confidence > 0.5:
|
| 491 |
+
confidence_emoji = "🟡"
|
| 492 |
+
confidence_text = "中置信度"
|
| 493 |
+
else:
|
| 494 |
+
confidence_emoji = "🔴"
|
| 495 |
+
confidence_text = "低置信度"
|
| 496 |
+
|
| 497 |
+
report = f"""
|
| 498 |
+
## 🎯 检测完成!
|
| 499 |
+
|
| 500 |
+
**主要语言**: `{lang}`
|
| 501 |
+
**置信度**: `{confidence:.1%}` {confidence_emoji} {confidence_text}
|
| 502 |
+
**处理时间**: `{processing_time:.2f}ms` ⚡
|
| 503 |
+
**进化加成**: `{boost}x` 📈
|
| 504 |
+
|
| 505 |
+
### 📊 详细分析
|
| 506 |
+
- **语言得分**: `{score}`
|
| 507 |
+
- **特征匹配**: {len(result.get('all_scores', {}))} 个语言特征
|
| 508 |
+
- **响应等级**: {'极速' if processing_time < 1 else '快速' if processing_time < 5 else '标准'}
|
| 509 |
+
|
| 510 |
+
---
|
| 511 |
+
*Capricode AI 为您提供专业的代码分析服务*
|
| 512 |
+
"""
|
| 513 |
+
|
| 514 |
+
return result, report
|
| 515 |
+
|
| 516 |
detect_btn.click(
|
| 517 |
+
fn=analyze_code,
|
| 518 |
inputs=[code_input],
|
| 519 |
outputs=[result_json, report_output]
|
| 520 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
|
| 522 |
+
return demo
|
| 523 |
|
| 524 |
+
# 启动应用
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
if __name__ == "__main__":
|
| 526 |
+
demo = create_glass_interface()
|
|
|
|
| 527 |
demo.launch(
|
| 528 |
server_name="0.0.0.0",
|
| 529 |
server_port=7860,
|
| 530 |
+
share=False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
)
|
| 532 |
|