import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch
# --- Load tokenizer and model for CPU ---
tokenizer = AutoTokenizer.from_pretrained("unsloth/Qwen2.5-0.5B-Instruct")
base_model = AutoModelForCausalLM.from_pretrained(
"unsloth/Qwen2.5-0.5B-Instruct",
dtype=torch.float32,
device_map={"": "cpu"},
)
model = PeftModel.from_pretrained(base_model, "Rustamshry/Plantinga-RL").to("cpu")
system = """
Respond in the following format:
Explain your reasoning here step-by-step.
Give the final answer here.
"""
# --- Chatbot logic ---
def generate_response(user_input, chat_history):
if not chat_history:
# Add the system message only once, at the beginning
chat_history = [{"role": "system", "content": system}]
if not user_input.strip():
return chat_history, chat_history
chat_history.append({"role": "user", "content": user_input})
text = tokenizer.apply_chat_template(
chat_history,
tokenize=False,
add_generation_prompt=True,
)
inputs = tokenizer(text, return_tensors="pt").to("cpu")
output_tokens = model.generate(
**inputs,
max_new_tokens=1200,
)
response = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
response = response.split(user_input)[-1].strip()
chat_history.append({"role": "assistant", "content": response})
gr_chat_history = [
(m["content"], chat_history[i + 1]["content"])
for i, m in enumerate(chat_history[:-1])
if m["role"] == "user"
]
return gr_chat_history, chat_history
# --- UI Design ---
with gr.Blocks(theme=gr.themes.Soft(primary_hue="yellow", secondary_hue="slate")) as demo:
gr.HTML("""
๐ Plantinga-RL
Philosophical reasoning on complex concepts, arguments, and debates
""")
with gr.Row():
with gr.Column(scale=6):
chatbot = gr.Chatbot(
label="Philosophy Chat",
height=600,
bubble_full_width=True,
show_copy_button=True,
avatar_images=(
"https://cdn-icons-png.flaticon.com/512/1077/1077012.png", # user icon
"https://cdn-icons-png.flaticon.com/512/4140/4140048.png", # bot icon
),
)
user_input = gr.Textbox(
placeholder="Ask me...",
label="๐ฌ Your question",
lines=3,
autofocus=True,
)
with gr.Row():
send_btn = gr.Button("๐ Send", variant="primary")
clear_btn = gr.Button("๐งน Clear Chat")
state = gr.State([])
send_btn.click(generate_response, [user_input, state], [chatbot, state])
user_input.submit(generate_response, [user_input, state], [chatbot, state])
clear_btn.click(lambda: ([], []), None, [chatbot, state])
gr.HTML("""
Powered by Qwen3-1.7B + Nizami-1.7B | Built with โค๏ธ using Gradio
""")
demo.launch(share=True)