File size: 1,260 Bytes
5218754
 
 
 
605a67e
5218754
 
 
 
1bbc887
5218754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bf8a4
5218754
 
 
 
 
 
 
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
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import gradio as gr
import torch

scam_model_name = "Tlezz324/thai-scam-detector-v1.69"

tokenizer = AutoTokenizer.from_pretrained(scam_model_name)
scam_model = AutoModelForSequenceClassification.from_pretrained(scam_model_name)

asr = pipeline("automatic-speech-recognition", model="airesearch/wav2vec2-large-xlsr-53-th")

def transcribe_and_predict(audio):
    if audio is None:
        return "No audio input detected", ""
    
    text = asr(audio)["text"]
    
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        logits = scam_model(**inputs).logits
        pred = torch.argmax(logits, dim=1).item()
    label = "Scam" if pred == 1 else "Not Scam"
    
    return text, label

iface = gr.Interface(
    fn=transcribe_and_predict,
    inputs=gr.Audio(type="filepath"),
    outputs=["text", "text"],
    title="Thai Scam Detector with Speech-to-Text",
    description="อัปโหลดไฟล์เสียงเพื่อแปลงเป็นข้อความและตรวจสอบว่าหลอกลวงหรือไม่"
)

if __name__ == "__main__":
    iface.launch()