Spaces:
Sleeping
Sleeping
File size: 5,348 Bytes
0819742 0351bfb ad40006 e67c5aa 0819742 e67c5aa ad40006 e67c5aa ad40006 e67c5aa 0819742 0351bfb e67c5aa 0351bfb e67c5aa 0351bfb b74f998 0351bfb e67c5aa 0351bfb e67c5aa b74f998 ad40006 e67c5aa e7cb288 0351bfb 1463148 0351bfb ad40006 0351bfb ad40006 0351bfb ad40006 0351bfb ad40006 0351bfb 1463148 0351bfb ad40006 0351bfb 1463148 0351bfb ad40006 0351bfb e67c5aa ad40006 0351bfb ad40006 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 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() |