CoolFace
Apppublic

LoveMinji/Hyperboy

sourceHugging Facemitupdated 10mo agoView on Hugging Face
0likes
app.py223 linesDownload Raw Back to root
1import streamlit as st2from openai import OpenAI3import os4 5# --- 1. 页面配置 (Page Config) ---6st.set_page_config(7    page_title="PaperPolisher Pro",8    page_icon="⚡",9    layout="wide",10    initial_sidebar_state="collapsed" # 强制收起侧边栏,打造沉浸式体验11)12 13# --- 2. 深度定制 CSS (Framer 级美学) ---14custom_css = """15<style>16    /* 引入 Inter 字体,更有科技感 */17    @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap');18    19    /* 全局重置 */20    html, body, [class*="css"] {21        font-family: 'Inter', sans-serif;22        color: #ffffff; 23    }24 25    /* 动态极光背景 (Aurora Background) */26    .stApp {27        background: linear-gradient(125deg, #000428, #004e92, #2b32b2, #000000);28        background-size: 400% 400%;29        animation: gradientBG 15s ease infinite;30    }31    32    @keyframes gradientBG {33        0% {background-position: 0% 50%;}34        50% {background-position: 100% 50%;}35        100% {background-position: 0% 50%;}36    }37 38    /* 隐藏 Streamlit 默认元素 */39    header, footer, #MainMenu {visibility: hidden;}40    41    /* 核心容器:玻璃拟态 (Glassmorphism) */42    /* 这里我们用 CSS 劫持 Streamlit 的 block 容器 */43    div[data-testid="stVerticalBlockBorderWrapper"] > div > div {44        background: rgba(255, 255, 255, 0.03);45        backdrop-filter: blur(16px);46        -webkit-backdrop-filter: blur(16px);47        border-radius: 20px;48        border: 1px solid rgba(255, 255, 255, 0.08);49        box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);50        padding: 2rem;51    }52 53    /* 标题样式:流光文字 */54    .hero-title {55        font-size: 3.5rem;56        font-weight: 800;57        background: linear-gradient(to right, #a8c0ff, #3f2b96);58        -webkit-background-clip: text;59        -webkit-text-fill-color: transparent;60        text-align: center;61        margin-bottom: 0.5rem;62        letter-spacing: -2px;63    }64    65    .hero-desc {66        text-align: center;67        color: rgba(255,255,255,0.6);68        font-size: 1.1rem;69        margin-bottom: 2rem;70    }71 72    /* 输入框美化:深邃黑洞风格 */73    .stTextArea textarea {74        background-color: rgba(0, 0, 0, 0.4) !important;75        border: 1px solid rgba(255, 255, 255, 0.1) !important;76        border-radius: 12px;77        color: #e0e0e0 !important;78        padding: 15px;79        font-size: 16px;80        transition: all 0.3s ease;81    }82    83    .stTextArea textarea:focus {84        border-color: #8E2DE2 !important;85        box-shadow: 0 0 15px rgba(142, 45, 226, 0.3);86        background-color: rgba(0, 0, 0, 0.6) !important;87    }88 89    /* 胶囊按钮:霓虹光效 */90    .stButton > button {91        width: 100%;92        background: linear-gradient(90deg, #4A00E0 0%, #8E2DE2 100%);93        border: none;94        color: white;95        padding: 0.8rem 2rem;96        border-radius: 50px;97        font-weight: 600;98        font-size: 1.1rem;99        box-shadow: 0 4px 15px rgba(74, 0, 224, 0.4);100        transition: transform 0.2s, box-shadow 0.2s;101    }102    103    .stButton > button:hover {104        transform: translateY(-2px);105        box-shadow: 0 8px 25px rgba(74, 0, 224, 0.6);106        color: white;107    }108 109    /* 结果框样式:代码块风格 */110    .result-card {111        background: rgba(16, 185, 129, 0.05);112        border: 1px solid rgba(16, 185, 129, 0.2);113        border-radius: 12px;114        padding: 20px;115        margin-top: 20px;116        color: #d1fae5;117        font-family: 'Inter', sans-serif;118        line-height: 1.6;119    }120    121    /* 单选按钮美化 */122    .stRadio > div {123        display: flex;124        justify-content: center;125        gap: 2rem;126    }127</style>128"""129st.markdown(custom_css, unsafe_allow_html=True)130 131# --- 3. 初始化 API ---132try:133    client = OpenAI(134        api_key=os.environ.get("DEEPSEEK_API_KEY"),135        base_url="https://api.deepseek.com"136    )137except:138    st.error("⚠️ 请在 Hugging Face Settings 中配置 DEEPSEEK_API_KEY")139 140# --- 4. 界面布局 (Layout) ---141 142# 顶部标题区143st.markdown('<div class="hero-title">PaperPolisher</div>', unsafe_allow_html=True)144st.markdown('<div class="hero-desc">DeepSeek 驱动 · 沉浸式论文重构引擎</div>', unsafe_allow_html=True)145 146# 主容器 (模拟卡片布局)147# 我们使用 columns 来控制宽度,让它在宽屏上看起来像个手机界面,更聚焦148spacer_left, main_content, spacer_right = st.columns([1, 2, 1])149 150with main_content:151    # A. 模式选择 (胶囊式)152    mode = st.radio(153        "Processing Mode",154        ["🔥 暴力降重", "🎓 SCI 润色"],155        horizontal=True,156        label_visibility="collapsed"157    )158    159    st.markdown("<div style='height: 20px'></div>", unsafe_allow_html=True) # 间距160 161    # B. 输入区域162    user_input = st.text_area(163        "Input Text",164        height=300,165        placeholder="在此粘贴您的论文段落...\n\n(建议每次处理 500-800 字)",166        label_visibility="collapsed"167    )168    169    st.markdown("<div style='height: 20px'></div>", unsafe_allow_html=True) # 间距170 171    # C. 提交按钮172    submit = st.button("⚡ 立即激活重构 (ACTIVATE)")173 174    # --- 5. 逻辑处理与输出 ---175    if submit:176        if not user_input:177            st.toast("⚠️ 请先输入内容", icon="👻")178        else:179            # 提示词逻辑180            if "暴力降重" in mode:181                sys_prompt = "你是专业的学术论文降重专家。任务:将用户输入的文本进行彻底改写,以大幅降低查重率。要求:彻底改变句式结构(主动变被动,倒装等);替换高频词为低频学术词汇;增加形容词/副词稀释关键词;保持原意,逻辑通顺;直接输出结果,不要废话。"182            else:183                sys_prompt = "You are a professional academic editor for top-tier journals (Nature/Science). Polish the text to meet SCI standards. Requirements: Fix grammar/syntax errors; Use formal, authoritative academic vocabulary; Improve flow; Output ONLY the polished text."184 185            # 输出区域186            output_placeholder = st.empty()187            188            with output_placeholder.container():189                # 加载动画190                with st.spinner("🔮 AI 正在重构神经元..."):191                    try:192                        response = client.chat.completions.create(193                            model="deepseek-chat",194                            messages=[195                                {"role": "system", "content": sys_prompt},196                                {"role": "user", "content": user_input},197                            ],198                            stream=False199                        )200                        result = response.choices[0].message.content201                        202                        # 成功结果展示203                        st.markdown(f"""204                        <div class="result-card">205                            <strong>✅ 处理结果:</strong><br><br>206                            {result.replace(chr(10), '<br>')}207                        </div>208                        """, unsafe_allow_html=True)209                        210                        st.balloons()211                        212                    except Exception as e:213                        st.error(f"服务连接失败: {e}")214 215    # 初始状态提示216    elif not submit:217        st.markdown("""218        <div style="text-align: center; color: rgba(255,255,255,0.3); font-size: 0.9rem; margin-top: 20px;">219            Power by DeepSeek V2.5 · Secure & Private220        </div>221        """, unsafe_allow_html=True)222 223