Update app.py
Browse files
app.py
CHANGED
|
@@ -2,31 +2,48 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
model_id = "cahya/gpt2-small-indonesian-522M"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
global chat_history
|
| 14 |
-
input_text = " ".join(chat_history + [user_input])
|
| 15 |
-
|
| 16 |
-
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 17 |
-
output_ids = model.generate(
|
| 18 |
input_ids,
|
| 19 |
-
max_length=
|
| 20 |
pad_token_id=tokenizer.eos_token_id,
|
| 21 |
do_sample=True,
|
| 22 |
-
top_k=
|
| 23 |
top_p=0.95,
|
| 24 |
-
temperature=0.
|
| 25 |
)
|
| 26 |
-
response = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 27 |
-
chat_history.append(user_input)
|
| 28 |
-
chat_history.append(response)
|
| 29 |
-
return response
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
model_id = "flax-community/gpt2-medium-indonesian"
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 8 |
|
| 9 |
+
def chat_cici(prompt):
|
| 10 |
+
input_ids = tokenizer.encode(prompt + tokenizer.eos_token, return_tensors='pt')
|
| 11 |
|
| 12 |
+
output = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
input_ids,
|
| 14 |
+
max_length=250,
|
| 15 |
pad_token_id=tokenizer.eos_token_id,
|
| 16 |
do_sample=True,
|
| 17 |
+
top_k=40,
|
| 18 |
top_p=0.95,
|
| 19 |
+
temperature=0.8
|
| 20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
reply = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 23 |
+
return reply.strip()
|
| 24 |
+
|
| 25 |
+
# Gaya cantik buat Cici π€π
|
| 26 |
+
with gr.Blocks(title="Chat Cici AI π€π") as demo:
|
| 27 |
+
gr.Markdown(
|
| 28 |
+
"""
|
| 29 |
+
<div style="text-align: center; font-size: 26px; color: hotpink;">
|
| 30 |
+
π Chat bareng AI Pintar versi Cici π<br>
|
| 31 |
+
<span style="font-size: 16px; color: gray;">Cici manja, pintar, dan selalu jawab pakai hati π€</span>
|
| 32 |
+
</div>
|
| 33 |
+
""")
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
chatbot = gr.Chatbot(label="Cici AI π", bubble_full_width=False, show_copy_button=True)
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
msg = gr.Textbox(placeholder="Tulis di sini ya sayang π³π", label="Ketik pesan kamu~")
|
| 40 |
+
send = gr.Button("Kirim π")
|
| 41 |
+
|
| 42 |
+
def respond(message, history):
|
| 43 |
+
response = chat_cici(message)
|
| 44 |
+
history = history + [[message, response]]
|
| 45 |
+
return history, ""
|
| 46 |
+
|
| 47 |
+
send.click(fn=respond, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 48 |
+
|
| 49 |
+
demo.launch()
|