Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 11 |
-
|
| 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 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 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 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from transformers import Trainer, TrainingArguments
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
+
MODEL_NAME = "cahya/indogpt2-base"
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load tokenizer & model
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 12 |
|
| 13 |
+
# Load dataset dari file lokal
|
| 14 |
+
dataset = load_dataset("json", data_files={"train": "data.json"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def tokenize_function(examples):
|
| 17 |
+
return tokenizer(examples["text"], truncation=True, max_length=512)
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
tokenized_dataset = dataset.map(tokenize_function, batched=True)
|
| 20 |
|
| 21 |
+
training_args = TrainingArguments(
|
| 22 |
+
output_dir="./indoGPT-finetuned",
|
| 23 |
+
per_device_train_batch_size=1,
|
| 24 |
+
gradient_accumulation_steps=8,
|
| 25 |
+
num_train_epochs=1,
|
| 26 |
+
logging_steps=10,
|
| 27 |
+
evaluation_strategy="no",
|
| 28 |
+
save_strategy="no",
|
| 29 |
+
learning_rate=5e-5,
|
| 30 |
+
fp16=True,
|
| 31 |
+
)
|
| 32 |
|
| 33 |
+
trainer = Trainer(
|
| 34 |
+
model=model,
|
| 35 |
+
args=training_args,
|
| 36 |
+
train_dataset=tokenized_dataset["train"],
|
| 37 |
+
)
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def fine_tune():
|
| 40 |
+
trainer.train()
|
| 41 |
+
return "Fine-tuning selesai!"
|
| 42 |
|
| 43 |
+
def chat(input_text):
|
| 44 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
| 45 |
+
outputs = model.generate(**inputs, max_length=100, do_sample=True, top_p=0.9, temperature=0.7)
|
| 46 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# Chatbot IndoGPT Simple")
|
| 50 |
+
user_input = gr.Textbox(label="Ketik pesan kamu")
|
| 51 |
+
fine_tune_btn = gr.Button("Fine-tune model")
|
| 52 |
+
output = gr.Textbox(label="Balasan chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
fine_tune_btn.click(fine_tune, outputs=output)
|
| 55 |
+
user_input.submit(chat, inputs=user_input, outputs=output)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|