Spaces:
Sleeping
Sleeping
Download app.py from KingROTX/text_classification: direct link, hf CLI and curl.
- Browser
- Download file 5.35 kB
-
https://huggingface.co/spaces/KingROTX/text_classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/KingROTX/text_classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/KingROTX/text_classification/resolve/main/app.py
5.35 kB
| import gradio as gr | |
| from transformers import pipeline | |
| import whisper | |
| import traceback | |
| import os | |
| # 1. 加载分类模型 | |
| try: | |
| classifier = pipeline("text-classification", model="./", device=-1) | |
| except Exception as e: | |
| classifier = None | |
| # 2. 加载语音识别模型 (Whisper base版) | |
| try: | |
| asr_model = whisper.load_model("base") | |
| except Exception as e: | |
| asr_model = None | |
| # 3. 核心预测逻辑 | |
| def predict(text, audio_path): | |
| final_text = text | |
| # 语音转文字处理 | |
| if audio_path is not None: | |
| if asr_model is not None: | |
| result = asr_model.transcribe(audio_path, language="zh") | |
| final_text = result["text"] | |
| else: | |
| return "❌ 语音识别模型未就绪" | |
| # 检查输入 | |
| if not final_text or not final_text.strip() or "[语音输入" in final_text: | |
| return "👋 请输入文字或开始录音分析" | |
| if classifier is None: | |
| return "❌ 分类模型加载失败" | |
| try: | |
| res = classifier(final_text)[0] | |
| label = "🚨 灾难相关" if res['label'] == 'LABEL_1' else "✅ 安全/非灾难" | |
| score = round(res['score'], 2) | |
| return f"【识别内容】:{final_text}\n\n【分析结果】:{label} ({score})" | |
| except: | |
| return "❌ 分析出错" | |
| # ========================================== | |
| # 4. 蓝绿多巴胺配色 CSS (简洁、居中、清爽) | |
| # ========================================== | |
| blue_green_dopamine = """ | |
| /* 全局背景及容器居中 */ | |
| .gradio-container { | |
| background-color: #f0f9ff !important; /* 极淡的蓝底 */ | |
| max-width: 850px !important; | |
| margin: 50px auto !important; | |
| border-radius: 30px !important; | |
| } | |
| /* 标题:蓝绿渐变 */ | |
| #title_text { | |
| text-align: center !important; | |
| background: linear-gradient(90deg, #00d2ff, #3a7bd5, #00ff88); | |
| -webkit-background-clip: text; | |
| -webkit-text-fill-color: transparent; | |
| font-size: 2.5rem !important; | |
| font-weight: 800 !important; | |
| padding: 30px 0 !important; | |
| } | |
| /* 面板通用样式 */ | |
| .group_panel { | |
| background: #ffffff !important; | |
| border-radius: 25px !important; | |
| padding: 35px !important; /* 增加留白,解决靠边问题 */ | |
| box-shadow: 0 15px 35px rgba(0, 210, 255, 0.1) !important; | |
| margin: 10px !important; | |
| border: none !important; | |
| } | |
| /* 输入面板:左侧蓝色系 */ | |
| .input_box { | |
| border-bottom: 8px solid #00d2ff !important; | |
| } | |
| /* 输出面板:右侧绿色系 */ | |
| .output_box { | |
| border-bottom: 8px solid #00ff88 !important; | |
| } | |
| /* 按钮:电光蓝到青色渐变 */ | |
| #submit_btn { | |
| background: linear-gradient(135deg, #00d2ff 0%, #00bcff 100%) !important; | |
| color: white !important; | |
| border: none !important; | |
| border-radius: 18px !important; | |
| height: 55px !important; | |
| font-size: 1.1rem !important; | |
| font-weight: bold !important; | |
| margin-top: 20px !important; | |
| transition: all 0.3s ease !important; | |
| cursor: pointer !important; | |
| } | |
| #submit_btn:hover { | |
| transform: translateY(-3px); | |
| box-shadow: 0 8px 20px rgba(0, 210, 255, 0.4) !important; | |
| filter: brightness(1.1); | |
| } | |
| /* 结果显示框 */ | |
| #output_text { | |
| background: #f0fff4 !important; /* 极淡的绿底 */ | |
| border: 2px dashed #00ff88 !important; | |
| color: #1a5f3d !important; | |
| font-size: 1.1rem !important; | |
| border-radius: 15px !important; | |
| padding: 15px !important; | |
| } | |
| /* Markdown 文字对齐 */ | |
| .gr-markdown h3 { | |
| text-align: center !important; | |
| color: #334155 !important; | |
| margin-bottom: 15px !important; | |
| } | |
| """ | |
| # ========================================== | |
| # 5. 界面布局 | |
| # ========================================== | |
| with gr.Blocks(css=blue_green_dopamine, title="灾难识别系统") as demo: | |
| gr.Markdown("# 💎 灾难识别分析系统", elem_id="title_text") | |
| with gr.Row(): | |
| # --- 输入列 --- | |
| with gr.Column(elem_classes="group_panel input_box"): | |
| gr.Markdown("### 📥 输入源") | |
| input_text = gr.Textbox( | |
| label="", | |
| placeholder="在此键入文字内容...", | |
| lines=4, | |
| container=False | |
| ) | |
| input_audio = gr.Audio( | |
| label="语音识别", | |
| sources=["microphone"], | |
| type="filepath", | |
| container=False | |
| ) | |
| submit_btn = gr.Button("🚀 立即分析", elem_id="submit_btn") | |
| # --- 输出列 --- | |
| with gr.Column(elem_classes="group_panel output_box"): | |
| gr.Markdown("### 📤 分析结论") | |
| output_text = gr.Textbox( | |
| label="", | |
| interactive=False, | |
| elem_id="output_text", | |
| container=False | |
| ) | |
| gr.Markdown( | |
| "<div style='text-align: center; color: #64748b; margin-top: 25px; font-size: 0.9rem;'>" | |
| "系统支持中文文本及语音实时转写分类" | |
| "</div>" | |
| ) | |
| # 逻辑绑定 | |
| submit_btn.click( | |
| fn=predict, | |
| inputs=[input_text, input_audio], | |
| outputs=output_text | |
| ) | |
| # 录音结束提示 | |
| input_audio.stop_recording( | |
| fn=lambda: gr.update(value="[语音已接收,正在等待分析...]"), | |
| outputs=input_text | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |