Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import onnxruntime as ort
|
| 5 |
+
|
| 6 |
+
# Load vocab
|
| 7 |
+
with open("vocab.json") as f:
|
| 8 |
+
vocab = json.load(f)
|
| 9 |
+
token2id = vocab
|
| 10 |
+
id2token = {v:k for k,v in vocab.items()}
|
| 11 |
+
|
| 12 |
+
# Load ONNX model
|
| 13 |
+
session = ort.InferenceSession("chat_model.onnx")
|
| 14 |
+
|
| 15 |
+
def tokenize(text):
|
| 16 |
+
return [token2id.get(ch, 0) for ch in text] # contoh simpel
|
| 17 |
+
|
| 18 |
+
def detokenize(ids):
|
| 19 |
+
return "".join([id2token.get(i, "?") for i in ids])
|
| 20 |
+
|
| 21 |
+
def predict(text):
|
| 22 |
+
ids = tokenize(text)
|
| 23 |
+
x = np.array([ids], dtype=np.int64) # biasanya int64
|
| 24 |
+
|
| 25 |
+
# Input sesuai nama model
|
| 26 |
+
input_name = session.get_inputs()[0].name
|
| 27 |
+
output_name = session.get_outputs()[0].name
|
| 28 |
+
|
| 29 |
+
# Run inference
|
| 30 |
+
output = session.run([output_name], {input_name: x})[0][0]
|
| 31 |
+
|
| 32 |
+
# Convert ke teks
|
| 33 |
+
return detokenize(output.tolist())
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
demo = gr.Interface(fn=predict, inputs="text", outputs="text")
|
| 37 |
+
demo.launch()
|