Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# Buat pipeline text-generation
|
| 15 |
-
chatbot = pipeline(
|
| 16 |
-
"text-generation",
|
| 17 |
-
model=model,
|
| 18 |
-
tokenizer=tokenizer,
|
| 19 |
-
max_new_tokens=512
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
# Fungsi chatbot
|
| 23 |
def respond(message, history):
|
| 24 |
prompt = ""
|
| 25 |
for user, bot in history:
|
| 26 |
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 27 |
prompt += f"User: {message}\nAssistant:"
|
| 28 |
|
| 29 |
-
output =
|
| 30 |
-
|
| 31 |
-
# Ambil jawaban setelah "Assistant:"
|
| 32 |
-
response = output.split("Assistant:")[-1].strip()
|
| 33 |
return response
|
| 34 |
|
| 35 |
-
# Gradio UI
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown("# 🤖 DeepSeek V3.1 Chatbot")
|
| 38 |
-
|
| 39 |
-
msg = gr.Textbox(placeholder="Tulis
|
| 40 |
clear = gr.Button("Clear")
|
| 41 |
|
| 42 |
def user_input(message, history):
|
|
@@ -44,7 +32,7 @@ with gr.Blocks() as demo:
|
|
| 44 |
history.append((message, response))
|
| 45 |
return "", history
|
| 46 |
|
| 47 |
-
msg.submit(user_input, [msg,
|
| 48 |
-
clear.click(lambda: None, None,
|
| 49 |
|
| 50 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
|
| 4 |
+
# Ganti dengan path model GGUF (download dulu ke Space atau pakai hf:// link)
|
| 5 |
+
MODEL_PATH = "DeepSeek-V3.1-Chat-Q4_K_M.gguf"
|
| 6 |
|
| 7 |
+
# Load model quantized (ringan untuk CPU 16GB)
|
| 8 |
+
llm = Llama(
|
| 9 |
+
model_path=MODEL_PATH,
|
| 10 |
+
n_ctx=2048,
|
| 11 |
+
n_threads=4
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def respond(message, history):
|
| 15 |
prompt = ""
|
| 16 |
for user, bot in history:
|
| 17 |
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 18 |
prompt += f"User: {message}\nAssistant:"
|
| 19 |
|
| 20 |
+
output = llm(prompt, max_tokens=512, temperature=0.7, top_p=0.9)
|
| 21 |
+
response = output["choices"][0]["text"].strip()
|
|
|
|
|
|
|
| 22 |
return response
|
| 23 |
|
|
|
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("# 🤖 DeepSeek V3.1 Chatbot (Quantized, CPU)")
|
| 26 |
+
chatbot = gr.Chatbot()
|
| 27 |
+
msg = gr.Textbox(placeholder="Tulis pesan di sini...")
|
| 28 |
clear = gr.Button("Clear")
|
| 29 |
|
| 30 |
def user_input(message, history):
|
|
|
|
| 32 |
history.append((message, response))
|
| 33 |
return "", history
|
| 34 |
|
| 35 |
+
msg.submit(user_input, [msg, chatbot], [msg, chatbot])
|
| 36 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 37 |
|
| 38 |
demo.launch()
|