Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345567889101 unmodified line1213141615161819201718192220212223262728292425262728312930313432333437353940363738433940414243444945515246475548495758596061505152535455565765666768695859606162637273747576777879808164656667688687886970909171727394957475769877781001011021031041051061071081091101117980818283848586116878811889901201219192931249412612712895961301319798991001341351361371011391021031041051431441451061071471081491501511091101111121531541551561571581591601611621131141151641161171181191201211711221231 unmodified linefrom transformers import pipelineimport traceback
# 1. 加载模型 (保持不变)# 1. 加载模型try: print("正在加载模型...") # 强制在 CPU 上运行 pipe = pipeline("text-classification", model="./", device=-1) print("模型加载成功!")except Exception as e:1 unmodified line traceback.print_exc() pipe = None
# 2. 核心预测逻辑# 2. 预测逻辑 (保持原始文字逻辑)def predict(text): # 处理语音转文字组件可能的特殊输出 (比如 None 或占位符) if not text or not text.strip() or text == "[语音输入已接收,请点击开始检测]": return "👋 嘿!快输入点什么,或者录个音,我等着呢!" if not text or not text.strip(): return "⚠️ 请输入文本或进行语音输入。" if pipe is None: return "😭 哎呀,模型开小差了,没法预测。" return "❌ 模型加载失败,无法预测。" try: res = pipe(text)[0] # 标签映射 label = "🚨 有危险!灾难相关" if res['label'] == 'LABEL_1' else "✅ 呼... 安全的" score = round(res['score'], 2) return f"🎉 检测结果: {label} (确信度: {score})" # 保持你原来的标签文字 label = "🚨 灾难相关" if res['label'] == 'LABEL_1' else "✅ 安全/非灾难" score = round(res['score'], 2) return f"检测结果: {label} (置信度: {score})" except Exception as e: return f"🤯 糟糕,出错了: {str(e)}" return f"❌ 预测出错: {str(e)}"
# ==========================================# 4. 多巴胺炫彩设计 (自定义 CSS)# 3. 多巴胺配色 CSS (仅修改视觉,不改内容)# ==========================================dopamine_css = """/* 整体背景:明亮轻盈 */.gradio-container { background-color: #fdfcf5; /* 极淡的奶油黄 */ font-family: 'Comic Sans MS', 'Curisve', sans-serif; /* 换个活泼点的字体 */ background-color: #fdfcf5; }
/* 标题样式:糖果色渐变 *//* 标题:彩色渐变 */#title_text { text-align: center; background: linear-gradient(90deg, #ff007f, #ffaa00, #00e676, #00e5ff); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-size: 2.5em; font-weight: bold; margin-bottom: 20px; padding: 10px;}
/* 组件面板样式:多彩边框、白色背景 *//* 输入区:亮粉色硬核边框 */.group_panel_input { background: #ffffff; border: 4px solid #ff007f; /* 亮粉色边框 */ border-radius: 20px; padding: 20px; box-shadow: 8px 8px 0px #ffaa00; /* 亮黄色硬阴影 */ background: #ffffff !important; border: 4px solid #ff007f !important; border-radius: 20px !important; box-shadow: 8px 8px 0px #ffaa00 !important;}
/* 输出区:亮青色硬核边框 */.group_panel_output { background: #ffffff; border: 4px solid #00e676; /* 亮绿色边框 */ border-radius: 20px; padding: 20px; box-shadow: 8px 8px 0px #00e5ff; /* 亮蓝色硬阴影 */ background: #ffffff !important; border: 4px solid #00e676 !important; border-radius: 20px !important; box-shadow: 8px 8px 0px #00e5ff !important;}
/* 输入框和文本域样式:干净、带彩色边框focus */.gr-textbox, .gr-audio { border: 2px solid #e0e0e0 !important; border-radius: 10px !important;}.gr-textbox:focus, .gr-audio:focus { border-color: #ff007f !important; /*聚焦时变粉 */}
/* 按钮样式:超级橙色,悬停会变大 *//* 按钮:多巴胺橙色 */#submit_btn { background: linear-gradient(135deg, #ff9100 0%, #ff3d00 100%) !important; border: none !important; color: white !important; font-size: 1.2em !important; font-weight: bold !important; border-radius: 50px !important; /* 圆角按钮 */ border-radius: 50px !important; box-shadow: 0 4px 15px rgba(255, 61, 0, 0.4) !important; transition: all 0.2s ease-in-out !important; margin-top: 15px; transition: all 0.2s ease !important;}#submit_btn:hover { box-shadow: 0 6px 20px rgba(255, 61, 0, 0.6) !important; transform: scale(1.05); /* 稍微放大 */ transform: scale(1.05);}
/* 结果输出框样式:绿色背景,白色文字 *//* 结果框:亮绿色背景 */#output_text { font-size: 1.3em; font-weight: bold; color: #ffffff !important; border: none; background: #00e676 !important; /* 亮绿色 */ border-radius: 10px; padding: 15px;}
/* 调整Markdown文本颜色 */.gr-markdown h3 { color: #333 !important; background: #00e676 !important; color: white !important; font-weight: bold !important; border-radius: 10px !important;}"""
# ==========================================# 5. 布局设计 (Gradio Blocks)# 4. 布局设计# ==========================================with gr.Blocks(css=dopamine_css, title="多巴胺 AI 识别系统") as demo:with gr.Blocks(css=dopamine_css, title="灾难识别系统") as demo: gr.Markdown("# 🌈 多巴胺文本/语音灾难识别系统", elem_id="title_text") gr.Markdown("<br>") gr.Markdown("# 🛸 灾难识别系统", elem_id="title_text") with gr.Row(): # --- 左侧:输入面板 (亮粉边框) --- with gr.Column(scale=1, elem_classes="group_panel_input"): gr.Markdown("### ✍️ 快来说点什么...") # 文字输入框 gr.Markdown("### 📥 输入") input_text = gr.Textbox( label="在下面打字...", placeholder="刚发生了地震,请求救援...", label="手动输入中文文本", placeholder="例如:刚发生了地震,请求救援...", lines=5 ) gr.Markdown("<p style='text-align:center; color:#777;'>—— 或者 ——</p>") # 语音输入组件 input_audio = gr.Audio( label="按住录音,说中文:", label="或者:点击录音(中文语音)", sources=["microphone"], type="filepath" ) # 提交按钮 (亮橙色) submit_btn = gr.Button("🚀 快乐检测", elem_id="submit_btn") submit_btn = gr.Button("🚀 开始检测", elem_id="submit_btn")
# --- 右侧:输出面板 (亮绿边框) --- with gr.Column(scale=1, elem_classes="group_panel_output"): gr.Markdown("### 🎈 检测结果出来啦!") output_text = gr.Textbox(label="AI 的回复:", interactive=False, elem_id="output_text") gr.Markdown("<br><p style='color:#555;'>注意:语音输入后,请确认文字已填入上方框内,再点击检测按钮哦!</p>") gr.Markdown("### 📤 检测结果") output_text = gr.Textbox(label="检测结果", interactive=False, elem_id="output_text") gr.Markdown("*(注意:语音输入可能需要几秒钟进行转录和分析)*")
# ========================================== # 6. 交互逻辑绑定 # ========================================== # 逻辑一:点击按钮检测 submit_btn.click( fn=predict, inputs=input_text, outputs=output_text ) # 交互绑定 submit_btn.click(fn=predict, inputs=input_text, outputs=output_text)
# 逻辑二:语音录制停止时,在文字框显示提示 input_audio.stop_recording( fn=lambda: gr.update(value="[语音输入已接收,请点击开始检测]"), inputs=None, outputs=input_text )
# 7. 启动if __name__ == "__main__": demo.launch()app.py+45-95