Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
|
| 6 |
-
# 1.
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
print("Loading tokenizer...")
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
|
| 12 |
-
print("Loading model on CPU
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
MODEL_ID,
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
device_map="cpu"
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def chat_function(message, history):
|
| 21 |
-
# Construct formatting conversation list matrices
|
| 22 |
messages = [
|
| 23 |
-
{"role": "system", "content": "You are an expert full-stack developer assistant."}
|
| 24 |
]
|
| 25 |
|
| 26 |
-
# Re-insert existing browser chat history logs
|
| 27 |
for user_msg, bot_msg in history:
|
| 28 |
messages.append({"role": "user", "content": user_msg})
|
| 29 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 30 |
|
| 31 |
messages.append({"role": "user", "content": message})
|
| 32 |
|
| 33 |
-
# Process tokens safely
|
| 34 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 35 |
-
|
| 36 |
-
# Set up a dynamic background streamer so answers appear word-by-word in browser
|
| 37 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 38 |
|
| 39 |
generation_kwargs = dict(
|
|
@@ -43,7 +43,6 @@ def chat_function(message, history):
|
|
| 43 |
temperature=0.6,
|
| 44 |
)
|
| 45 |
|
| 46 |
-
# Run text generation in a separate background processor thread
|
| 47 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 48 |
thread.start()
|
| 49 |
|
|
@@ -52,7 +51,6 @@ def chat_function(message, history):
|
|
| 52 |
partial_text += new_text
|
| 53 |
yield partial_text
|
| 54 |
|
| 55 |
-
# 4. Initialize the native Gradio browser interface
|
| 56 |
demo = gr.ChatInterface(
|
| 57 |
fn=chat_function,
|
| 58 |
title="🤖 Cydercoder Qwen 3B AI Chatbot",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from peft import PeftModel
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
+
# 1. Map both coordinates: The base model engine and your custom adapter layer
|
| 8 |
+
BASE_MODEL = "Qwen/Qwen2.5-Coder-3B-Instruct"
|
| 9 |
+
ADAPTER_MODEL = "Cydercoder/qwen2.5-coder-3b"
|
| 10 |
|
| 11 |
+
print("Loading official base tokenizer...")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 13 |
|
| 14 |
+
print("Loading public base model on CPU...")
|
| 15 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
BASE_MODEL,
|
|
|
|
| 17 |
torch_dtype=torch.float32,
|
| 18 |
device_map="cpu"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
print("Merging your custom fine-tuned engineering weights...")
|
| 22 |
+
# This layers your specialized tasks right over the active model architecture
|
| 23 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL)
|
| 24 |
+
|
| 25 |
def chat_function(message, history):
|
|
|
|
| 26 |
messages = [
|
| 27 |
+
{"role": "system", "content": "You are an expert full-stack developer assistant fine-tuned for frontend, backend, animations, and debugging."}
|
| 28 |
]
|
| 29 |
|
|
|
|
| 30 |
for user_msg, bot_msg in history:
|
| 31 |
messages.append({"role": "user", "content": user_msg})
|
| 32 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 33 |
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
|
|
|
|
| 36 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
|
|
|
|
|
|
| 37 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 38 |
|
| 39 |
generation_kwargs = dict(
|
|
|
|
| 43 |
temperature=0.6,
|
| 44 |
)
|
| 45 |
|
|
|
|
| 46 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 47 |
thread.start()
|
| 48 |
|
|
|
|
| 51 |
partial_text += new_text
|
| 52 |
yield partial_text
|
| 53 |
|
|
|
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
fn=chat_function,
|
| 56 |
title="🤖 Cydercoder Qwen 3B AI Chatbot",
|