vortexa64 commited on
Commit
c1600ea
Β·
verified Β·
1 Parent(s): ca287ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -18
app.py CHANGED
@@ -2,31 +2,48 @@ import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- # Pakai model Indo ya sayang 🀭
6
- model_id = "cahya/gpt2-small-indonesian-522M"
7
  tokenizer = AutoTokenizer.from_pretrained(model_id)
8
  model = AutoModelForCausalLM.from_pretrained(model_id)
9
 
10
- chat_history = []
 
11
 
12
- def chat(user_input):
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=200,
20
  pad_token_id=tokenizer.eos_token_id,
21
  do_sample=True,
22
- top_k=50,
23
  top_p=0.95,
24
- temperature=0.7
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
- iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="Cici Chat Indo πŸ€­πŸ’•")
32
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()