yuudev commited on
Commit
a60aa1a
·
verified ·
1 Parent(s): 757b718

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -4,14 +4,14 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
  import torch
5
 
6
  # =====================================================
7
- # 🔹 Load model Gemma 2B (cocok buat CPU di HF Space)
8
  # =====================================================
9
  model_name = "google/gemma-2b-it"
10
 
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
  model = AutoModelForCausalLM.from_pretrained(
13
  model_name,
14
- torch_dtype=torch.float32, # biar kompatibel di CPU Space
15
  device_map="auto",
16
  )
17
 
@@ -22,7 +22,7 @@ pipe = pipeline(
22
  max_new_tokens=512,
23
  temperature=0.7,
24
  top_p=0.9,
25
- repetition_penalty=1.1
26
  )
27
 
28
  # =====================================================
@@ -34,23 +34,30 @@ def chat(user_input, history):
34
 
35
  history = history or []
36
 
37
- # prompt dengan identitas pembuatnya
38
  prompt = (
39
  "Kamu adalah YuuVx, asisten AI yang dibuat oleh Yusuf Ramadhani. "
40
- "Kamu cerdas, tenang, dan selalu berbicara dalam Bahasa Indonesia. "
41
- "Gunakan gaya santai tapi sopan, jangan terlalu panjang, dan responsif.\n\n"
42
  )
43
 
44
- for role, text in history:
45
- prompt += f"{role}: {text}\n"
46
- prompt += f"User: {user_input}\nAssistant:"
 
 
 
 
 
 
47
 
48
  # generate jawaban
49
  output = pipe(prompt, max_new_tokens=256, do_sample=True)
50
- response = output[0]["generated_text"].split("Assistant:")[-1].strip()
 
 
 
51
 
52
- history.append(("User", user_input))
53
- history.append(("Assistant", response))
54
  return history, history
55
 
56
  # =====================================================
@@ -60,9 +67,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
60
  gr.Markdown("<h1 style='text-align:center;'>🤖 YuuVx Chat - Gemma 2B</h1>")
61
  gr.Markdown("<p style='text-align:center;'>Dibuat oleh <b>Yusuf Ramadhani</b></p>")
62
 
63
- chatbot = gr.Chatbot(label="💬 Chat", height=520)
64
- msg = gr.Textbox(label="Ketik pesanmu di sini...")
65
- state = gr.State()
66
 
67
  def respond(message, chat_history):
68
  result, chat_history = chat(message, chat_history)
@@ -71,7 +78,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
71
  msg.submit(respond, [msg, state], [msg, chatbot, state])
72
 
73
  # =====================================================
74
- # 🔹 Run app
75
  # =====================================================
76
  if __name__ == "__main__":
77
  demo.launch()
 
4
  import torch
5
 
6
  # =====================================================
7
+ # 🔹 Load model Gemma 2B
8
  # =====================================================
9
  model_name = "google/gemma-2b-it"
10
 
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
  model = AutoModelForCausalLM.from_pretrained(
13
  model_name,
14
+ torch_dtype=torch.float32, # cocok buat CPU Space
15
  device_map="auto",
16
  )
17
 
 
22
  max_new_tokens=512,
23
  temperature=0.7,
24
  top_p=0.9,
25
+ repetition_penalty=1.1,
26
  )
27
 
28
  # =====================================================
 
34
 
35
  history = history or []
36
 
37
+ # build prompt dengan konteks
38
  prompt = (
39
  "Kamu adalah YuuVx, asisten AI yang dibuat oleh Yusuf Ramadhani. "
40
+ "Kamu cerdas, tenang, dan selalu berbicara dalam Bahasa Indonesia dengan nada santai tapi sopan. "
41
+ "Gunakan kalimat yang alami dan tidak terlalu panjang.\n\n"
42
  )
43
 
44
+ for msg in history:
45
+ role = msg["role"]
46
+ content = msg["content"]
47
+ if role == "user":
48
+ prompt += f"User: {content}\n"
49
+ else:
50
+ prompt += f"YuuVx: {content}\n"
51
+
52
+ prompt += f"User: {user_input}\nYuuVx:"
53
 
54
  # generate jawaban
55
  output = pipe(prompt, max_new_tokens=256, do_sample=True)
56
+ response = output[0]["generated_text"].split("YuuVx:")[-1].strip()
57
+
58
+ history.append({"role": "user", "content": user_input})
59
+ history.append({"role": "assistant", "content": response})
60
 
 
 
61
  return history, history
62
 
63
  # =====================================================
 
67
  gr.Markdown("<h1 style='text-align:center;'>🤖 YuuVx Chat - Gemma 2B</h1>")
68
  gr.Markdown("<p style='text-align:center;'>Dibuat oleh <b>Yusuf Ramadhani</b></p>")
69
 
70
+ chatbot = gr.Chatbot(label="💬 Chat dengan YuuVx", height=520, type="messages")
71
+ msg = gr.Textbox(label="Ketik pesanmu di sini...", placeholder="Tulis sesuatu...")
72
+ state = gr.State([])
73
 
74
  def respond(message, chat_history):
75
  result, chat_history = chat(message, chat_history)
 
78
  msg.submit(respond, [msg, state], [msg, chatbot, state])
79
 
80
  # =====================================================
81
+ # 🔹 Run App
82
  # =====================================================
83
  if __name__ == "__main__":
84
  demo.launch()