KazuX-1 commited on
Commit
05fdee9
·
1 Parent(s): 3b7e70b

Fix: Use DialoGPT-small model

Browse files
Files changed (1) hide show
  1. app.py +41 -38
app.py CHANGED
@@ -1,49 +1,52 @@
1
- # app.py - HuggingFace Spaces deployment
2
  import gradio as gr
3
- from model import predict
 
4
 
5
- def chat(message, history):
6
- history = history or []
7
- response = predict(message, history)
8
- history.append((message, response))
9
- return history, history
10
 
11
- def clear_history():
12
- return [], ""
 
 
 
 
 
13
 
14
- # Custom CSS
15
- custom_css = """
16
- .gradio-container {
17
- max-width: 1200px !important;
18
- margin: auto !important;
19
- }
20
- """
21
-
22
- with gr.Blocks(css=custom_css, title="Bangdim AI") as demo:
23
- gr.Markdown("""
24
- <div style="text-align: center; margin-bottom: 20px;">
25
- <h1>🤖 Bangdim AI Customer Service</h1>
26
- <p style="font-size: 16px;">AI Assistant untuk Top Up Game</p>
27
- </div>
28
- """)
29
 
30
- chatbot_ui = gr.Chatbot(height=500, type="tuples")
31
 
32
- with gr.Row():
33
- msg = gr.Textbox(scale=9, placeholder="Ketik pesan Anda di sini...", label="Pesan")
34
- send_btn = gr.Button("Kirim", variant="primary", scale=1)
 
 
 
 
 
35
 
36
- clear_btn = gr.Button("Clear Chat", size="sm")
 
37
 
38
- send_btn.click(chat, [msg, chatbot_ui], [chatbot_ui, chatbot_ui]).then(
39
- lambda: "", None, msg
40
- )
41
 
42
- msg.submit(chat, [msg, chatbot_ui], [chatbot_ui, chatbot_ui]).then(
43
- lambda: "", None, msg
44
- )
 
 
 
 
 
 
45
 
46
- clear_btn.click(clear_history, None, [chatbot_ui, msg])
 
47
 
48
- if __name__ == "__main__":
49
- demo.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
 
5
+ # Cek GPU
6
+ device = 0 if torch.cuda.is_available() else -1
 
 
 
7
 
8
+ # Load model (akan otomatis download pertama kali)
9
+ print("Loading AI model...")
10
+ generator = pipeline(
11
+ 'text-generation',
12
+ model='microsoft/DialoGPT-small', # Model kecil, cepat
13
+ device=device
14
+ )
15
 
16
+ def chat(message, history):
17
+ # Build context
18
+ context = ""
19
+ for h in history[-3:]:
20
+ context += f"User: {h[0]}\nAI: {h[1]}\n"
 
 
 
 
 
 
 
 
 
 
21
 
22
+ prompt = f"{context}User: {message}\nAI:"
23
 
24
+ # Generate response
25
+ result = generator(
26
+ prompt,
27
+ max_length=150,
28
+ temperature=0.8,
29
+ do_sample=True,
30
+ pad_token_id=50256
31
+ )
32
 
33
+ response = result[0]['generated_text']
34
+ response = response.replace(prompt, "").strip()
35
 
36
+ if not response:
37
+ response = "Maaf kak, saya kurang paham. Bisa diulang? 😊"
 
38
 
39
+ history.append((message, response))
40
+ return history, history
41
+
42
+ # UI
43
+ with gr.Blocks() as demo:
44
+ gr.Markdown("# 🤖 Bangdim AI")
45
+ chatbot = gr.Chatbot()
46
+ msg = gr.Textbox(label="Pesan")
47
+ clear = gr.Button("Clear")
48
 
49
+ msg.submit(chat, [msg, chatbot], [chatbot, msg])
50
+ clear.click(lambda: None, None, chatbot)
51
 
52
+ demo.launch()