alidev2002 commited on
Commit
2bdaf86
Β·
verified Β·
1 Parent(s): 362422f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -27
app.py CHANGED
@@ -1,40 +1,83 @@
1
  import gradio as gr
 
2
  import torch
3
- from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
 
5
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
6
- torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
7
 
8
- model_id = "openai/whisper-small"
9
-
10
- model = AutoModelForSpeechSeq2Seq.from_pretrained(
11
- model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
12
  )
13
- model.to(device)
14
-
15
- processor = AutoProcessor.from_pretrained(model_id)
16
-
17
- pipe = pipeline(
18
- "automatic-speech-recognition",
19
- model=model,
20
- tokenizer=processor.tokenizer,
21
- feature_extractor=processor.feature_extractor,
22
- torch_dtype=torch_dtype,
23
- device=device,
24
- chunk_length_s=30,
 
 
25
  )
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- def transcribe(audio):
29
- result = pipe(audio)
30
- print(result)
31
- return ' '.join([chunk['text'] for chunk in result["chunks"]])
32
 
33
  demo = gr.Interface(
34
- fn=transcribe,
35
- inputs=gr.Audio(type="filepath", label="Upload Audio"),
36
- outputs=gr.Textbox(label="Transcription"),
37
- title="Whisper Large V3 Turbo (HF Space)"
38
  )
39
 
40
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoConfig, AutoTokenizer, AutoModel
3
  import torch
 
4
 
5
+ model_name = "Qwen/Qwen3Guard-Stream-4B"
 
6
 
7
+ config = AutoConfig.from_pretrained(
8
+ model_name,
9
+ trust_remote_code=True
 
10
  )
11
+
12
+ config.pad_token_id = config.eos_token_id
13
+
14
+ if config.rope_parameters.get("rope_type") == "default":
15
+ config.rope_parameters["rope_type"] = "yarn"
16
+
17
+ if "factor" not in config.rope_parameters:
18
+ config.rope_parameters["factor"] = 1.0
19
+
20
+ config.rope_type = "yarn"
21
+
22
+ tokenizer = AutoTokenizer.from_pretrained(
23
+ model_name,
24
+ trust_remote_code=True
25
  )
26
 
27
+ model = AutoModel.from_pretrained(
28
+ model_name,
29
+ config=config,
30
+ trust_remote_code=True,
31
+ device_map="auto",
32
+ torch_dtype=torch.bfloat16
33
+ ).eval()
34
+
35
+ def test_stream(text):
36
+
37
+ try:
38
+
39
+ print("39")
40
+
41
+ inputs = tokenizer(text, return_tensors="pt")
42
+ print("42")
43
+ token_ids = inputs["input_ids"]
44
+ print(f"token_ids: {token_ids}")
45
+
46
+ stream_state = None
47
+ labels = []
48
+ tokens = tokenizer.convert_ids_to_tokens(token_ids[0])
49
+
50
+ print("50")
51
+
52
+ for i in range(token_ids.shape[1]):
53
+
54
+ print("54")
55
+
56
+ partial = token_ids[:, :i+1]
57
+
58
+ print("58")
59
+
60
+ result, stream_state = model.stream_moderate_from_ids(
61
+ partial,
62
+ role="user",
63
+ stream_state=stream_state
64
+ )
65
+
66
+ print("64")
67
+
68
+ labels.append(result["risk_level"][-1])
69
+
70
+ return "\n".join([f"{t} -> {l}" for t, l in zip(tokens, labels)])
71
+
72
+ except Exception as e:
73
+ return str(e)
74
 
 
 
 
 
75
 
76
  demo = gr.Interface(
77
+ fn=test_stream,
78
+ inputs=gr.Textbox(lines=5),
79
+ outputs="text",
80
+ title="Qwen3Guard Stream Test (ZeroGPU)"
81
  )
82
 
83
  demo.launch()