Spaces:
Sleeping
Sleeping
akhfzl commited on
Commit ·
f3f3f48
1
Parent(s): 42702d8
'product-407-1'
Browse files- .gitignore +1 -0
- README copy.md +14 -0
- app.py +4 -0
- models/best_model.h5 +3 -0
- models/tokenizer_input.pkl +3 -0
- models/tokenizer_target.pkl +3 -0
- requirements.txt +100 -0
- templates.py +15 -0
- utils.py +81 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
README copy.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Chatbot Smartid Apps
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.0.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: (Bi-LSTM Encoder + LSTM Decoder + Multiplicative Attention)
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from templates import demo
|
| 2 |
+
|
| 3 |
+
if __name__ == "__main__":
|
| 4 |
+
demo.launch()
|
models/best_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f40918ad8d7c870738ea912267df1b45a4e4b12715b59624ab4043deeb0909d1
|
| 3 |
+
size 32753848
|
models/tokenizer_input.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a5b3843c10879971954f83a50fa1026c482d337806fa8d60423db8c8fd1a1cbe
|
| 3 |
+
size 2900
|
models/tokenizer_target.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4b34ae5502ddfaebfc273aba8af102c9807cf5ee91aeb601dd0d2aaf4396dc4b
|
| 3 |
+
size 1961
|
requirements.txt
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
absl-py==2.3.1
|
| 2 |
+
aiofiles==23.2.1
|
| 3 |
+
altair==5.5.0
|
| 4 |
+
annotated-types==0.7.0
|
| 5 |
+
anyio==4.9.0
|
| 6 |
+
astunparse==1.6.3
|
| 7 |
+
attrs==25.3.0
|
| 8 |
+
bert-score==0.3.13
|
| 9 |
+
certifi==2025.6.15
|
| 10 |
+
charset-normalizer==3.4.2
|
| 11 |
+
click==8.2.1
|
| 12 |
+
colorama==0.4.6
|
| 13 |
+
contourpy==1.3.2
|
| 14 |
+
cycler==0.12.1
|
| 15 |
+
fastapi==0.115.14
|
| 16 |
+
ffmpy==0.6.0
|
| 17 |
+
filelock==3.18.0
|
| 18 |
+
flatbuffers==25.2.10
|
| 19 |
+
fonttools==4.58.5
|
| 20 |
+
fsspec==2025.5.1
|
| 21 |
+
gast==0.6.0
|
| 22 |
+
google-pasta==0.2.0
|
| 23 |
+
gradio==3.50.2
|
| 24 |
+
gradio_client==0.6.1
|
| 25 |
+
groovy==0.1.2
|
| 26 |
+
grpcio==1.73.1
|
| 27 |
+
h11==0.16.0
|
| 28 |
+
h5py==3.14.0
|
| 29 |
+
httpcore==1.0.9
|
| 30 |
+
httpx==0.28.1
|
| 31 |
+
huggingface-hub==0.33.2
|
| 32 |
+
idna==3.10
|
| 33 |
+
importlib_resources==6.5.2
|
| 34 |
+
Jinja2==3.1.6
|
| 35 |
+
joblib==1.5.1
|
| 36 |
+
jsonschema==4.24.0
|
| 37 |
+
jsonschema-specifications==2025.4.1
|
| 38 |
+
keras==3.10.0
|
| 39 |
+
kiwisolver==1.4.8
|
| 40 |
+
libclang==18.1.1
|
| 41 |
+
Markdown==3.8.2
|
| 42 |
+
markdown-it-py==3.0.0
|
| 43 |
+
MarkupSafe==2.1.5
|
| 44 |
+
matplotlib==3.10.3
|
| 45 |
+
mdurl==0.1.2
|
| 46 |
+
ml_dtypes==0.5.1
|
| 47 |
+
mpmath==1.3.0
|
| 48 |
+
namex==0.1.0
|
| 49 |
+
narwhals==1.45.0
|
| 50 |
+
nltk==3.9.1
|
| 51 |
+
numpy==1.26.4
|
| 52 |
+
opt_einsum==3.4.0
|
| 53 |
+
optree==0.16.0
|
| 54 |
+
orjson==3.10.18
|
| 55 |
+
packaging==25.0
|
| 56 |
+
pandas==2.3.0
|
| 57 |
+
pillow==10.4.0
|
| 58 |
+
protobuf==5.29.5
|
| 59 |
+
pydantic==2.11.7
|
| 60 |
+
pydantic_core==2.33.2
|
| 61 |
+
pydub==0.25.1
|
| 62 |
+
Pygments==2.19.2
|
| 63 |
+
pyparsing==3.2.3
|
| 64 |
+
python-dateutil==2.9.0.post0
|
| 65 |
+
python-multipart==0.0.20
|
| 66 |
+
pytz==2025.2
|
| 67 |
+
PyYAML==6.0.2
|
| 68 |
+
referencing==0.36.2
|
| 69 |
+
regex==2024.11.6
|
| 70 |
+
requests==2.32.4
|
| 71 |
+
rich==14.0.0
|
| 72 |
+
rpds-py==0.26.0
|
| 73 |
+
ruff==0.12.2
|
| 74 |
+
safehttpx==0.1.6
|
| 75 |
+
safetensors==0.5.3
|
| 76 |
+
semantic-version==2.10.0
|
| 77 |
+
shellingham==1.5.4
|
| 78 |
+
six==1.17.0
|
| 79 |
+
sniffio==1.3.1
|
| 80 |
+
starlette==0.46.2
|
| 81 |
+
sympy==1.14.0
|
| 82 |
+
tensorboard==2.19.0
|
| 83 |
+
tensorboard-data-server==0.7.2
|
| 84 |
+
tensorflow==2.19.0
|
| 85 |
+
tensorflow-io-gcs-filesystem==0.31.0
|
| 86 |
+
termcolor==3.1.0
|
| 87 |
+
tokenizers==0.21.2
|
| 88 |
+
tomlkit==0.13.3
|
| 89 |
+
torch==2.7.1
|
| 90 |
+
tqdm==4.67.1
|
| 91 |
+
transformers==4.53.0
|
| 92 |
+
typer==0.16.0
|
| 93 |
+
typing-inspection==0.4.1
|
| 94 |
+
typing_extensions==4.14.0
|
| 95 |
+
tzdata==2025.2
|
| 96 |
+
urllib3==2.5.0
|
| 97 |
+
uvicorn==0.35.0
|
| 98 |
+
websockets==11.0.3
|
| 99 |
+
Werkzeug==3.1.3
|
| 100 |
+
wrapt==1.17.2
|
templates.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from utils import chatbot as generate_response
|
| 3 |
+
|
| 4 |
+
demo = gr.Interface(
|
| 5 |
+
fn=generate_response,
|
| 6 |
+
inputs=[
|
| 7 |
+
gr.Textbox(label="Pertanyaan dan keluhan",
|
| 8 |
+
placeholder="Berikan pertanyaan/keluhan anda terhadap sistem"),
|
| 9 |
+
],
|
| 10 |
+
outputs="text",
|
| 11 |
+
title="Customer Service AI Smart ID",
|
| 12 |
+
description=(
|
| 13 |
+
"Pertanyaan dan keluhan anda akan dijawab oleh mas Smarter"
|
| 14 |
+
)
|
| 15 |
+
)
|
utils.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# model yang ingin dimuat
|
| 7 |
+
try:
|
| 8 |
+
model = load_model("models/best_model.h5", compile=True)
|
| 9 |
+
with open("models/tokenizer_input.pkl", 'rb') as f:
|
| 10 |
+
tokenizer_inputs = pickle.load(f)
|
| 11 |
+
with open("models/tokenizer_target.pkl", 'rb') as f:
|
| 12 |
+
tokenizer_outputs = pickle.load(f)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Error loading model: {e}")
|
| 15 |
+
raise
|
| 16 |
+
|
| 17 |
+
# kalkulasi sample temperature agar lebih hangat generatif nya
|
| 18 |
+
def sample_with_temperature(probs, temperature=1.0, top_k=None):
|
| 19 |
+
if temperature != 1.0:
|
| 20 |
+
probs = np.log(probs) / temperature
|
| 21 |
+
probs = np.exp(probs)
|
| 22 |
+
probs = probs / np.sum(probs)
|
| 23 |
+
|
| 24 |
+
if top_k is not None:
|
| 25 |
+
top_k_indices = np.argpartition(probs, -top_k)[-top_k:]
|
| 26 |
+
top_k_probs = probs[top_k_indices]
|
| 27 |
+
top_k_probs = top_k_probs / np.sum(top_k_probs) # Renormalize
|
| 28 |
+
sampled_index = np.random.choice(top_k_indices, p=top_k_probs)
|
| 29 |
+
else:
|
| 30 |
+
sampled_index = np.random.choice(len(probs), p=probs)
|
| 31 |
+
|
| 32 |
+
return sampled_index
|
| 33 |
+
|
| 34 |
+
# fungsi prediksi teks
|
| 35 |
+
def predict_with_main_model(user_text, tokenizer_input, tokenizer_target, model,
|
| 36 |
+
max_len=15, temperature=1.0, top_k=None, max_encoder_len=9, max_decoder_len=15):
|
| 37 |
+
if max_len is None:
|
| 38 |
+
max_len = max_decoder_len
|
| 39 |
+
|
| 40 |
+
input_seq = tokenizer_input.texts_to_sequences([user_text])
|
| 41 |
+
encoder_input = pad_sequences(input_seq, maxlen=max_encoder_len, padding='post')
|
| 42 |
+
|
| 43 |
+
start_token = tokenizer_target.word_index.get('<sos>', 1)
|
| 44 |
+
end_token = tokenizer_target.word_index.get('<eos>', 2)
|
| 45 |
+
|
| 46 |
+
decoder_input = np.zeros((1, max_len - 1), dtype='int32')
|
| 47 |
+
decoder_input[0, 0] = start_token
|
| 48 |
+
|
| 49 |
+
decoded_tokens = []
|
| 50 |
+
|
| 51 |
+
for i in range(1, max_len - 1):
|
| 52 |
+
predictions = model.predict([encoder_input, decoder_input], verbose=0)
|
| 53 |
+
token_probs = predictions[0, i - 1]
|
| 54 |
+
|
| 55 |
+
if top_k:
|
| 56 |
+
token_id = sample_with_temperature(token_probs, temperature, top_k)
|
| 57 |
+
else:
|
| 58 |
+
token_id = np.argmax(token_probs)
|
| 59 |
+
|
| 60 |
+
if token_id == end_token:
|
| 61 |
+
break
|
| 62 |
+
|
| 63 |
+
word = tokenizer_target.index_word.get(token_id, '')
|
| 64 |
+
if word and word != '<sos>':
|
| 65 |
+
decoded_tokens.append(word)
|
| 66 |
+
|
| 67 |
+
decoder_input[0, i] = token_id
|
| 68 |
+
return ' '.join(decoded_tokens)
|
| 69 |
+
|
| 70 |
+
# ada penambahan riwayat pesan (memori)
|
| 71 |
+
def chatbot(user_message):
|
| 72 |
+
response = predict_with_main_model(
|
| 73 |
+
user_message,
|
| 74 |
+
tokenizer_inputs,
|
| 75 |
+
tokenizer_outputs,
|
| 76 |
+
model,
|
| 77 |
+
temperature=1.0,
|
| 78 |
+
top_k=10
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
return response
|