Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,16 +11,13 @@ MODEL_ID = "google/medasr"
|
|
| 11 |
TITLE = "🏥 MedASR - Medical Speech Recognition"
|
| 12 |
DESCRIPTION = """
|
| 13 |
Demo for **MedASR** (Medical Automated Speech Recognition).
|
| 14 |
-
|
| 15 |
⚠️ **Note**: This is a gated model. You need to provide your HuggingFace Token to access it.
|
| 16 |
-
|
| 17 |
### How to get a Token:
|
| 18 |
1. Visit https://huggingface.co/google/medasr
|
| 19 |
2. Click "Access this model" and agree to the terms
|
| 20 |
3. Go to https://huggingface.co/settings/tokens
|
| 21 |
4. Create a new token with "Read" permission
|
| 22 |
5. Copy the token (starts with `hf_`)
|
| 23 |
-
|
| 24 |
### Instructions:
|
| 25 |
1. Enter your HF Token below
|
| 26 |
2. Click "🔑 Load Model"
|
|
@@ -32,22 +29,12 @@ Demo for **MedASR** (Medical Automated Speech Recognition).
|
|
| 32 |
pipeline_instance = None
|
| 33 |
|
| 34 |
def load_model_with_token(hf_token):
|
| 35 |
-
"""使用用户提供的 token 加载模型"""
|
| 36 |
global pipeline_instance
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return "❌ Please enter your HuggingFace Token first.", "❌ Model not loaded"
|
| 40 |
-
|
| 41 |
-
if not hf_token.startswith("hf_"):
|
| 42 |
-
return "❌ Invalid token format. Token should start with 'hf_'", "❌ Model not loaded"
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
print(f"Loading model with token: {hf_token[:10]}...")
|
| 46 |
-
|
| 47 |
-
# 设备配置
|
| 48 |
device = 0 if torch.cuda.is_available() else -1
|
| 49 |
-
|
| 50 |
-
# 创建 pipeline
|
| 51 |
pipeline_instance = pipeline(
|
| 52 |
"automatic-speech-recognition",
|
| 53 |
model=MODEL_ID,
|
|
@@ -55,14 +42,9 @@ def load_model_with_token(hf_token):
|
|
| 55 |
device=device,
|
| 56 |
torch_dtype=torch.float16 if device == 0 else torch.float32
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
device_name = "GPU (CUDA)" if device == 0 else "CPU"
|
| 60 |
-
return f"✅ Model loaded successfully on {device_name}!", "✅ Model Ready"
|
| 61 |
-
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
-
print(error_msg)
|
| 65 |
-
return error_msg, "❌ Model Failed"
|
| 66 |
|
| 67 |
def transcribe_audio(audio_file):
|
| 68 |
"""转录音频"""
|
|
@@ -172,7 +154,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 172 |
|
| 173 |
# 事件绑定
|
| 174 |
load_model_btn.click(
|
| 175 |
-
fn=lambda
|
| 176 |
inputs=[hf_token],
|
| 177 |
outputs=[transcribe_btn, model_status]
|
| 178 |
)
|
|
|
|
| 11 |
TITLE = "🏥 MedASR - Medical Speech Recognition"
|
| 12 |
DESCRIPTION = """
|
| 13 |
Demo for **MedASR** (Medical Automated Speech Recognition).
|
|
|
|
| 14 |
⚠️ **Note**: This is a gated model. You need to provide your HuggingFace Token to access it.
|
|
|
|
| 15 |
### How to get a Token:
|
| 16 |
1. Visit https://huggingface.co/google/medasr
|
| 17 |
2. Click "Access this model" and agree to the terms
|
| 18 |
3. Go to https://huggingface.co/settings/tokens
|
| 19 |
4. Create a new token with "Read" permission
|
| 20 |
5. Copy the token (starts with `hf_`)
|
|
|
|
| 21 |
### Instructions:
|
| 22 |
1. Enter your HF Token below
|
| 23 |
2. Click "🔑 Load Model"
|
|
|
|
| 29 |
pipeline_instance = None
|
| 30 |
|
| 31 |
def load_model_with_token(hf_token):
|
|
|
|
| 32 |
global pipeline_instance
|
| 33 |
+
if not hf_token or not hf_token.startswith("hf_"):
|
| 34 |
+
return gr.update(interactive=False), "❌ Invalid Token"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
try:
|
|
|
|
|
|
|
|
|
|
| 37 |
device = 0 if torch.cuda.is_available() else -1
|
|
|
|
|
|
|
| 38 |
pipeline_instance = pipeline(
|
| 39 |
"automatic-speech-recognition",
|
| 40 |
model=MODEL_ID,
|
|
|
|
| 42 |
device=device,
|
| 43 |
torch_dtype=torch.float16 if device == 0 else torch.float32
|
| 44 |
)
|
| 45 |
+
return gr.update(interactive=True), "✅ Model loaded successfully!"
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
+
return gr.update(interactive=False), f"❌ Error: {str(e)}"
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def transcribe_audio(audio_file):
|
| 50 |
"""转录音频"""
|
|
|
|
| 154 |
|
| 155 |
# 事件绑定
|
| 156 |
load_model_btn.click(
|
| 157 |
+
fn=load_model_with_token, # 直接调用,不要用 lambda 嵌套 update_model_status
|
| 158 |
inputs=[hf_token],
|
| 159 |
outputs=[transcribe_btn, model_status]
|
| 160 |
)
|