Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,83 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 4 |
|
| 5 |
-
|
| 6 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 12 |
)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 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=
|
| 35 |
-
inputs=gr.
|
| 36 |
-
outputs=
|
| 37 |
-
title="
|
| 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()
|