Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345567889106 unmodified lines1718192120212235 unmodified lines58596063616263643 unmodified lines6869707374717273747775767778798081828382838485868788888990919293944 unmodified lines9910010110110210310410561 unmodified lines1671681691691701711701711721731741751751761771771781791801821811821832 unmodified lines18618718819119219318919019119219319419519619719819920020120220320420520620720818 unmodified lines2272282292182302312321 unmodified linefrom code_name_suggester import get_suggesterimport gradio as gr
# 1. 加载模型# 加载模型suggester = get_suggester("KyrieChao/code-method-namer", device="cpu")
# 2. 预测函数def suggest(code: str): if not code.strip(): return {"error": "请输入代码"}6 unmodified lines except Exception as e: return {"error": str(e)}
# 3. 渲染建议(带编号卡片 + 复制)def render_suggestions(names): if isinstance(names, dict) and "error" in names: return f"""35 unmodified lines items.append(item) return f"""<div style="display:flex; flex-direction:column; gap:4px;">{"".join(items)}</div>"""
# 4. 自定义 CSS(已包含字体)# 自定义 CSS(扩大布局、加大标题)custom_css = """@import url('https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:wght@400;500;600&display=swap');
3 unmodified lines}
.container { max-width: 1000px; margin: 2rem auto; max-width: 100%; /* 铺满 */ width: 100%; padding: 2rem; background: white; border-radius: 32px; border-radius: 24px; box-shadow: 0 20px 40px -15px rgba(0,0,0,0.08); margin: 2rem auto; min-height: 80vh;}
.title { font-size: 2.5rem !important; font-weight: 600 !important; font-size: 3rem !important; font-weight: 700 !important; letter-spacing: -0.02em; background: linear-gradient(135deg, #4f46e5, #7c3aed); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin-bottom: 0.25rem !important; margin-bottom: 0.5rem !important; text-align: center;}
.subtitle { color: #64748b; font-size: 1.1rem;4 unmodified lines
.gr-code, pre, code { font-family: 'JetBrains Mono', 'Fira Code', monospace !important; font-size: 14px; font-size: 15px; border-radius: 16px !important; background: #f1f5f9 !important; border: 1px solid #e2e8f0 !important;61 unmodified linesfooter { display: none !important; }
@media (max-width: 640px) { .container { padding: 1.5rem; margin: 1rem; } .title { font-size: 2rem !important; } .gr-button[size="sm"] { font-size: 0.75rem !important; padding: 0.2rem 0.8rem !important; } .container { padding: 1.5rem; } .title { font-size: 2.2rem !important; }}"""
# 5. 构建界面 —— 修正点:不再用 with gr.HTML 包裹# 构建界面with gr.Blocks(title="Code Method Namer") as demo: # 字体已通过 @import 加载,此处可省略,但保留也无妨 gr.HTML(""" <link href="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet"> """) # 使用 Column 模拟 container with gr.Column(elem_classes="container"): gr.Markdown("<div class='title'>🤖 Code Method Namer</div>") gr.Markdown("<div class='subtitle'>基于 CodeT5 的智能命名助手 · 用 <code>todo</code> 或 <code>???</code> 标记待命名位置</div>")2 unmodified lines with gr.Column(scale=1): input_code = gr.Code( label="📝 输入代码片段", lines=8, language="python", value="def todo(a,b): return a+b" lines=10, language="java", value="""public static <T> T ???(int maxAttempts, long initialDelay, Callable<T> operation) throws Exception { long delay = initialDelay; for (int i = 1; i <= maxAttempts; i++) { try { return operation.call(); } catch (Exception e) { if (i == maxAttempts) throw e; Thread.sleep(delay); delay *= 2; } } throw new IllegalStateException("Unreachable");}""", # ← 复杂 Java 示例 ) with gr.Row(): clear_btn = gr.Button("🧹 清空", variant="secondary", size="sm")18 unmodified lines ) clear_btn.click(lambda: "", inputs=None, outputs=input_code)
# 6. 启动 —— theme 和 css 放这里!if __name__ == "__main__": demo.launch( server_name="0.0.0.0",app.py+32-21