KazuX-1 commited on
Commit ·
05fdee9
1
Parent(s): 3b7e70b
Fix: Use DialoGPT-small model
Browse files
app.py
CHANGED
|
@@ -1,49 +1,52 @@
|
|
| 1 |
-
# app.py - HuggingFace Spaces deployment
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
response = predict(message, history)
|
| 8 |
-
history.append((message, response))
|
| 9 |
-
return history, history
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 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()
|
|
|