Spaces:
Sleeping
Sleeping
File size: 3,638 Bytes
c361120 46b4ec1 3079076 c361120 3079076 9696916 3079076 223064e 3079076 9696916 3079076 5a6dbf1 c361120 3079076 9696916 9c3f222 3079076 46b4ec1 3079076 c361120 46b4ec1 3079076 9696916 3079076 c361120 9696916 c361120 3079076 9696916 5636348 3079076 5636348 3079076 6518aba 5636348 c361120 5636348 3079076 5636348 3079076 5636348 c361120 5636348 3079076 5636348 3079076 5636348 c361120 9696916 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
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/Qwen3-1.7B")
base_model = AutoModelForCausalLM.from_pretrained(
"unsloth/Qwen3-1.7B",
dtype=torch.float32,
device_map={"": "cpu"},
)
model = PeftModel.from_pretrained(base_model, "khazarai/BioGenesis-ToT").to("cpu")
# --- Chatbot logic ---
def generate_response(user_input, chat_history):
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,
enable_thinking=True,
)
inputs = tokenizer(text, return_tensors="pt").to("cpu")
output_tokens = model.generate(
**inputs,
max_new_tokens=2200,
temperature=0.6,
top_p=0.95,
top_k=20,
do_sample=True
)
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="emerald", secondary_hue="slate")) as demo:
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px;">
<h1 style="font-family: 'Inter', sans-serif; font-weight: 800; color: #047857; font-size: 2.2em;">
🧬 BioGenesis-ToT Chatbot
</h1>
<p style="color: #4B5563; font-size: 1.05em; margin-top: -10px;">
Your AI companion for biology, biochemistry, and life sciences.
</p>
<p style="color: #4B5563; font-size: 1.05em; margin-top: -10px;">
NOTE: The model's answers may be shorter or slightly different because this instance runs on CPU using float32, unlike the GPU float16 setup used during fine-tuning.
</p>
</div>
""")
with gr.Row():
with gr.Column(scale=6):
chatbot = gr.Chatbot(
label="BioGenesis 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 about cell biology, molecular structure, or biochemistry...",
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("""
<div style="text-align: center; margin-top: 25px; color: #6B7280; font-size: 0.9em;">
Powered by <b>Qwen3-1.7B + BioGenesis-ToT</b> | Built with ❤️ using Gradio
</div>
""")
demo.launch(share=True) |