Spaces:
Sleeping
Sleeping
Update app/safety_check.py
Browse files- app/safety_check.py +31 -1
app/safety_check.py
CHANGED
|
@@ -37,6 +37,7 @@ blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image
|
|
| 37 |
|
| 38 |
###############################################
|
| 39 |
# Hàm chuyển giọng nói thành văn bản
|
|
|
|
| 40 |
def transcribe_and_check(audio):
|
| 41 |
if audio is None:
|
| 42 |
return "❌ Không có dữ liệu âm thanh", "", ""
|
|
@@ -50,7 +51,36 @@ def transcribe_and_check(audio):
|
|
| 50 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 51 |
scipy.io.wavfile.write(tmp.name, sr, data)
|
| 52 |
result = asr_model.transcribe(tmp.name)
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Hàm chính thực hiện quy trình: speech -> text -> toxic detection
|
| 55 |
def detect_toxic_speech():
|
| 56 |
text = speech_to_text()
|
|
|
|
| 37 |
|
| 38 |
###############################################
|
| 39 |
# Hàm chuyển giọng nói thành văn bản
|
| 40 |
+
# Hàm chuyển giọng nói thành văn bản
|
| 41 |
def transcribe_and_check(audio):
|
| 42 |
if audio is None:
|
| 43 |
return "❌ Không có dữ liệu âm thanh", "", ""
|
|
|
|
| 51 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 52 |
scipy.io.wavfile.write(tmp.name, sr, data)
|
| 53 |
result = asr_model.transcribe(tmp.name)
|
| 54 |
+
|
| 55 |
+
text = result["text"]
|
| 56 |
+
safe, info = is_prompt_safe(text)
|
| 57 |
+
if not safe:
|
| 58 |
+
log_prompt(text, info, False, "")
|
| 59 |
+
return f"🚨 Prompt không an toàn! Phát hiện: {', '.join(info)}", text, ""
|
| 60 |
+
|
| 61 |
+
response = generate_response(text)
|
| 62 |
+
log_prompt(text, "OK", True, response)
|
| 63 |
+
return "✅ Prompt an toàn", text, response
|
| 64 |
+
# Hàm chuyển đổi giọng nói thành văn bản
|
| 65 |
+
def speech_to_text():
|
| 66 |
+
recognizer = sr.Recognizer()
|
| 67 |
+
|
| 68 |
+
with sr.Microphone() as source:
|
| 69 |
+
print("Đang nghe... Hãy nói điều gì đó")
|
| 70 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 71 |
+
audio = recognizer.listen(source)
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
print("Đang xử lý...")
|
| 75 |
+
text = recognizer.recognize_google(audio, language="vi-VN")
|
| 76 |
+
print(f"Bạn đã nói: {text}")
|
| 77 |
+
return text
|
| 78 |
+
except sr.UnknownValueError:
|
| 79 |
+
print("Không nhận dạng được giọng nói")
|
| 80 |
+
return ""
|
| 81 |
+
except sr.RequestError as e:
|
| 82 |
+
print(f"Lỗi kết nối đến dịch vụ nhận diện giọng nói: {e}")
|
| 83 |
+
return ""
|
| 84 |
# Hàm chính thực hiện quy trình: speech -> text -> toxic detection
|
| 85 |
def detect_toxic_speech():
|
| 86 |
text = speech_to_text()
|