Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ from transformers import pipeline
|
|
3 |
import random
|
4 |
import string
|
5 |
import datetime
|
|
|
|
|
|
|
6 |
|
7 |
# Daftar produk dan kategori
|
8 |
categories = {
|
@@ -15,12 +18,32 @@ categories = {
|
|
15 |
# Inisialisasi model pengenalan suara
|
16 |
speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Fungsi untuk mengonversi suara ke teks
|
19 |
def speech_to_text(audio):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Fungsi untuk membuat dokumen LaTeX
|
26 |
def create_latex(data, filename):
|
@@ -88,12 +111,16 @@ PT. Contoh Perusahaan
|
|
88 |
def buat_penawaran(nama_prospek, alamat_prospek, jenis_prospek, produk_dipilih, jumlah_produk, harga_produk, diskon, tanggal, syarat, audio_input):
|
89 |
# Proses input suara jika ada
|
90 |
if audio_input:
|
91 |
-
audio_text = speech_to_text(audio_input)
|
92 |
-
if
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
97 |
|
98 |
data = {
|
99 |
'nama_prospek': nama_prospek if nama_prospek else "Prospek Tanpa Nama",
|
|
|
3 |
import random
|
4 |
import string
|
5 |
import datetime
|
6 |
+
import librosa
|
7 |
+
import soundfile as sf
|
8 |
+
import numpy as np
|
9 |
|
10 |
# Daftar produk dan kategori
|
11 |
categories = {
|
|
|
18 |
# Inisialisasi model pengenalan suara
|
19 |
speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
|
20 |
|
21 |
+
# Fungsi untuk memproses audio
|
22 |
+
def preprocess_audio(audio):
|
23 |
+
if audio is None:
|
24 |
+
return None
|
25 |
+
try:
|
26 |
+
# audio[0] adalah sampling rate, audio[1] adalah data audio
|
27 |
+
sr, y = audio
|
28 |
+
# Resample ke 16kHz
|
29 |
+
y = librosa.resample(y.astype(np.float32), orig_sr=sr, target_sr=16000)
|
30 |
+
# Simpan sementara ke file WAV
|
31 |
+
temp_file = "temp_audio.wav"
|
32 |
+
sf.write(temp_file, y, 16000)
|
33 |
+
return temp_file
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error memproses audio: {str(e)}"
|
36 |
+
|
37 |
# Fungsi untuk mengonversi suara ke teks
|
38 |
def speech_to_text(audio):
|
39 |
+
processed_audio = preprocess_audio(audio)
|
40 |
+
if not isinstance(processed_audio, str) or processed_audio.startswith("Error"):
|
41 |
+
return processed_audio if processed_audio else ""
|
42 |
+
try:
|
43 |
+
result = speech_recognizer(processed_audio)
|
44 |
+
return result["text"]
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error pengenalan suara: {str(e)}"
|
47 |
|
48 |
# Fungsi untuk membuat dokumen LaTeX
|
49 |
def create_latex(data, filename):
|
|
|
111 |
def buat_penawaran(nama_prospek, alamat_prospek, jenis_prospek, produk_dipilih, jumlah_produk, harga_produk, diskon, tanggal, syarat, audio_input):
|
112 |
# Proses input suara jika ada
|
113 |
if audio_input:
|
114 |
+
audio_text = speech_to_text(audio_input)
|
115 |
+
if isinstance(audio_text, str) and not audio_text.startswith("Error"):
|
116 |
+
audio_text = audio_text.lower()
|
117 |
+
if "nama" in audio_text and not nama_prospek:
|
118 |
+
nama_prospek = audio_text.split("nama")[-1].strip()
|
119 |
+
if "alamat" in audio_text and not alamat_prospek;
|
120 |
+
|
121 |
+
alamat_prospek = audio_text.split("alamat")[-1].strip()
|
122 |
+
else:
|
123 |
+
return None, f"Peringatan: Gagal memproses input suara - {audio_text}"
|
124 |
|
125 |
data = {
|
126 |
'nama_prospek': nama_prospek if nama_prospek else "Prospek Tanpa Nama",
|