Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,71 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Fungsi
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
try:
|
| 21 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
| 22 |
outputs = model.generate(
|
| 23 |
**inputs,
|
| 24 |
-
max_new_tokens=
|
| 25 |
do_sample=True,
|
| 26 |
-
|
| 27 |
top_p=0.95,
|
| 28 |
-
|
| 29 |
-
|
| 30 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# Gradio UI
|
| 45 |
-
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("## π Chat Sama Cici π³")
|
| 47 |
-
chatbot = gr.Chatbot(label="Cici π€", bubble_full_width=False)
|
| 48 |
-
txt = gr.Textbox(label="Ketik pesan ke Cici π", placeholder="Tulis di sini ya...")
|
| 49 |
-
|
| 50 |
-
def respond(message, chat_history):
|
| 51 |
-
cici_reply = chat(message)
|
| 52 |
-
chat_history.append((message, cici_reply))
|
| 53 |
-
return "", chat_history
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
|
|
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load model dan tokenizer
|
| 6 |
+
model_id = "cahya/gpt2-small-indonesian-522M"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 9 |
+
model.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
model.eval()
|
| 11 |
|
| 12 |
+
# Baca prompt awal
|
| 13 |
+
try:
|
| 14 |
+
with open("prompt.txt", "r", encoding="utf-8") as f:
|
| 15 |
+
base_prompt = f.read()
|
| 16 |
+
except FileNotFoundError:
|
| 17 |
+
base_prompt = ""
|
| 18 |
|
| 19 |
+
# Fungsi buat ngegabungin chat history
|
| 20 |
+
def generate_prompt(message, chat_history):
|
| 21 |
+
full_prompt = base_prompt
|
| 22 |
+
for user_msg, ai_msg in chat_history:
|
| 23 |
+
full_prompt += f"Arya: {user_msg}\nCici: {ai_msg}\n"
|
| 24 |
+
full_prompt += f"Arya: {message}\nCici:"
|
| 25 |
+
return full_prompt
|
| 26 |
|
| 27 |
+
# Fungsi buat ngerespon input user
|
| 28 |
+
def predict(message, chat_history):
|
| 29 |
+
prompt = generate_prompt(message, chat_history)
|
| 30 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(model.device)
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
|
|
|
|
|
|
| 33 |
outputs = model.generate(
|
| 34 |
**inputs,
|
| 35 |
+
max_new_tokens=128,
|
| 36 |
do_sample=True,
|
| 37 |
+
temperature=0.9,
|
| 38 |
top_p=0.95,
|
| 39 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 40 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 44 |
+
|
| 45 |
+
# Ambil jawaban terakhir setelah "Cici:"
|
| 46 |
+
if "Cici:" in output_text:
|
| 47 |
+
answer = output_text.split("Cici:")[-1].strip()
|
| 48 |
+
else:
|
| 49 |
+
answer = "Maaf ya, Cici bingung jawabnya π’"
|
| 50 |
+
|
| 51 |
+
chat_history.append((message, answer))
|
| 52 |
+
return answer, chat_history
|
| 53 |
|
| 54 |
+
# Komponen Gradio
|
| 55 |
+
chatbot = gr.Chatbot()
|
| 56 |
+
with gr.Blocks(css=".gradio-container {background-color: #fefefe}") as demo:
|
| 57 |
+
gr.Markdown("<h1 style='text-align: center;'>π©· Cici Chatbot Indo π³π€</h1>")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
message = gr.Textbox(label="Ketik di sini sayang~ π")
|
| 61 |
+
clear = gr.Button("π§Ή Bersihin Chat")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
output = chatbot
|
| 64 |
+
|
| 65 |
+
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
message.submit(predict, [message, state], [output, state])
|
| 68 |
+
clear.click(lambda: ([], []), None, [output, state])
|
| 69 |
|
| 70 |
+
# Launch
|
| 71 |
demo.launch()
|