akhfzl commited on
Commit
5844049
·
1 Parent(s): c406418

main-model-407

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
app.py CHANGED
@@ -1,64 +1,4 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
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 CHANGED
@@ -1 +1,93 @@
1
- huggingface_hub==0.25.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.3.1
2
+ aiofiles==24.1.0
3
+ annotated-types==0.7.0
4
+ anyio==4.9.0
5
+ astunparse==1.6.3
6
+ bert-score==0.3.13
7
+ certifi==2025.6.15
8
+ charset-normalizer==3.4.2
9
+ click==8.2.1
10
+ colorama==0.4.6
11
+ contourpy==1.3.2
12
+ cycler==0.12.1
13
+ fastapi==0.115.14
14
+ ffmpy==0.6.0
15
+ filelock==3.18.0
16
+ flatbuffers==25.2.10
17
+ fonttools==4.58.5
18
+ fsspec==2025.5.1
19
+ gast==0.6.0
20
+ google-pasta==0.2.0
21
+ gradio==5.35.0
22
+ gradio_client==1.10.4
23
+ groovy==0.1.2
24
+ grpcio==1.73.1
25
+ h11==0.16.0
26
+ h5py==3.14.0
27
+ httpcore==1.0.9
28
+ httpx==0.28.1
29
+ huggingface-hub==0.33.2
30
+ idna==3.10
31
+ Jinja2==3.1.6
32
+ joblib==1.5.1
33
+ keras==3.10.0
34
+ kiwisolver==1.4.8
35
+ libclang==18.1.1
36
+ Markdown==3.8.2
37
+ markdown-it-py==3.0.0
38
+ MarkupSafe==3.0.2
39
+ matplotlib==3.10.3
40
+ mdurl==0.1.2
41
+ ml_dtypes==0.5.1
42
+ mpmath==1.3.0
43
+ namex==0.1.0
44
+ networkx==3.5
45
+ nltk==3.9.1
46
+ numpy==2.1.3
47
+ opt_einsum==3.4.0
48
+ optree==0.16.0
49
+ orjson==3.10.18
50
+ packaging==25.0
51
+ pandas==2.3.0
52
+ pillow==11.3.0
53
+ protobuf==5.29.5
54
+ pydantic==2.11.7
55
+ pydantic_core==2.33.2
56
+ pydub==0.25.1
57
+ Pygments==2.19.2
58
+ pyparsing==3.2.3
59
+ python-dateutil==2.9.0.post0
60
+ python-multipart==0.0.20
61
+ pytz==2025.2
62
+ PyYAML==6.0.2
63
+ regex==2024.11.6
64
+ requests==2.32.4
65
+ rich==14.0.0
66
+ ruff==0.12.2
67
+ safehttpx==0.1.6
68
+ safetensors==0.5.3
69
+ semantic-version==2.10.0
70
+ shellingham==1.5.4
71
+ six==1.17.0
72
+ sniffio==1.3.1
73
+ starlette==0.46.2
74
+ sympy==1.14.0
75
+ tensorboard==2.19.0
76
+ tensorboard-data-server==0.7.2
77
+ tensorflow==2.19.0
78
+ tensorflow-io-gcs-filesystem==0.31.0
79
+ termcolor==3.1.0
80
+ tokenizers==0.21.2
81
+ tomlkit==0.13.3
82
+ torch==2.7.1
83
+ tqdm==4.67.1
84
+ transformers==4.53.0
85
+ typer==0.16.0
86
+ typing-inspection==0.4.1
87
+ typing_extensions==4.14.0
88
+ tzdata==2025.2
89
+ urllib3==2.5.0
90
+ uvicorn==0.35.0
91
+ websockets==15.0.1
92
+ Werkzeug==3.1.3
93
+ wrapt==1.17.2
templates.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from utils import chatbot
3
+
4
+ with gr.Blocks() as demo:
5
+ gr.Markdown("## 🤖 Chatbot LSTM dengan Gradio")
6
+ chatbot_output = gr.Chatbot()
7
+ msg = gr.Textbox(label="Ketik pesan Anda dan tekan Enter")
8
+ clear = gr.Button("🔄 Bersihkan Riwayat")
9
+
10
+ state = gr.State([])
11
+
12
+ msg.submit(chatbot, [msg, state], [msg, chatbot_output, state])
13
+ clear.click(lambda: ([], []), None, [chatbot_output, state])
utils.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def load_models(path_model, path_tokenizer_input, path_tokenizer_output):
8
+ model = load_model(path_model, compile=True)
9
+
10
+ with open(path_tokenizer_input, 'rb') as f:
11
+ tokenizer_inputs = pickle.load(f)
12
+
13
+ with open(path_tokenizer_output, 'rb') as f:
14
+ tokenizer_outputs = pickle.load(f)
15
+
16
+ return model, tokenizer_inputs, tokenizer_outputs
17
+
18
+ # kalkulasi sample temperature agar lebih hangat generatif nya
19
+ def sample_with_temperature(probs, temperature=1.0, top_k=None):
20
+ if temperature != 1.0:
21
+ probs = np.log(probs) / temperature
22
+ probs = np.exp(probs)
23
+ probs = probs / np.sum(probs)
24
+
25
+ if top_k is not None:
26
+ top_k_indices = np.argpartition(probs, -top_k)[-top_k:]
27
+ top_k_probs = probs[top_k_indices]
28
+ top_k_probs = top_k_probs / np.sum(top_k_probs) # Renormalize
29
+ sampled_index = np.random.choice(top_k_indices, p=top_k_probs)
30
+ else:
31
+ sampled_index = np.random.choice(len(probs), p=probs)
32
+
33
+ return sampled_index
34
+
35
+ # fungsi prediksi teks
36
+ def predict_with_main_model(user_text, tokenizer_input, tokenizer_target, model,
37
+ max_len=15, temperature=1.0, top_k=None, max_encoder_len=9, max_decoder_len=15):
38
+ if max_len is None:
39
+ max_len = max_decoder_len
40
+
41
+ input_seq = tokenizer_input.texts_to_sequences([user_text])
42
+ encoder_input = pad_sequences(input_seq, maxlen=max_encoder_len, padding='post')
43
+
44
+ start_token = tokenizer_target.word_index.get('<sos>', 1)
45
+ end_token = tokenizer_target.word_index.get('<eos>', 2)
46
+
47
+ decoder_input = np.zeros((1, max_len - 1), dtype='int32')
48
+ decoder_input[0, 0] = start_token
49
+
50
+ decoded_tokens = []
51
+
52
+ for i in range(1, max_len - 1):
53
+ predictions = model.predict([encoder_input, decoder_input], verbose=0)
54
+ token_probs = predictions[0, i - 1]
55
+
56
+ if top_k:
57
+ token_id = sample_with_temperature(token_probs, temperature, top_k)
58
+ else:
59
+ token_id = np.argmax(token_probs)
60
+
61
+ if token_id == end_token:
62
+ break
63
+
64
+ word = tokenizer_target.index_word.get(token_id, '')
65
+ if word and word != '<sos>':
66
+ decoded_tokens.append(word)
67
+
68
+ decoder_input[0, i] = token_id
69
+ return ' '.join(decoded_tokens)
70
+
71
+ # ada penambahan riwayat pesan (memori)
72
+ def chatbot(user_message, histories):
73
+ histories = histories or []
74
+
75
+ model, tokenizer_inputs, tokenizer_outputs = load_models(
76
+ "models/best_model.h5",
77
+ "models/tokenizer_input.pkl",
78
+ "models/tokenizer_target.pkl"
79
+ )
80
+
81
+ response = predict_with_main_model(
82
+ user_message,
83
+ tokenizer_inputs,
84
+ tokenizer_outputs,
85
+ model,
86
+ temperature=1.0,
87
+ top_k=10
88
+ )
89
+
90
+ histories.append(("👤: " + user_message, "🤖: " + response))
91
+
92
+ return "", histories, histories