Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
import spaces
|
|
|
|
| 4 |
|
| 5 |
from transformers import (
|
| 6 |
AutoTokenizer,
|
|
@@ -10,60 +10,74 @@ from transformers import (
|
|
| 10 |
from peft import PeftModel
|
| 11 |
|
| 12 |
|
| 13 |
-
# ==========================================
|
| 14 |
# MODEL
|
| 15 |
-
# ==========================================
|
| 16 |
|
| 17 |
BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 18 |
|
| 19 |
ADAPTER_MODEL = "dd253B/DhanushAI-0.5B"
|
| 20 |
|
| 21 |
|
| 22 |
-
# ==========================================
|
| 23 |
-
#
|
| 24 |
-
# ==========================================
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 29 |
-
BASE_MODEL
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# ==========================================
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
BASE_MODEL,
|
| 44 |
-
torch_dtype=torch.float16,
|
| 45 |
-
device_map="auto"
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
ADAPTER_MODEL
|
| 53 |
-
)
|
| 54 |
|
| 55 |
-
model.
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
@spaces.GPU
|
| 65 |
def chat(message):
|
| 66 |
|
|
|
|
|
|
|
| 67 |
if not message or not message.strip():
|
| 68 |
return "Please enter a message."
|
| 69 |
|
|
@@ -78,9 +92,8 @@ Assistant:"""
|
|
| 78 |
return_tensors="pt"
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Move inputs to the same device as model
|
| 82 |
inputs = {
|
| 83 |
-
key: value.to(
|
| 84 |
for key, value in inputs.items()
|
| 85 |
}
|
| 86 |
|
|
@@ -119,11 +132,12 @@ Assistant:"""
|
|
| 119 |
return answer
|
| 120 |
|
| 121 |
|
| 122 |
-
# ==========================================
|
| 123 |
# GRADIO
|
| 124 |
-
# ==========================================
|
| 125 |
|
| 126 |
demo = gr.Interface(
|
|
|
|
| 127 |
fn=chat,
|
| 128 |
|
| 129 |
inputs=gr.Textbox(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import spaces
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
from transformers import (
|
| 6 |
AutoTokenizer,
|
|
|
|
| 10 |
from peft import PeftModel
|
| 11 |
|
| 12 |
|
| 13 |
+
# ============================================================
|
| 14 |
# MODEL
|
| 15 |
+
# ============================================================
|
| 16 |
|
| 17 |
BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 18 |
|
| 19 |
ADAPTER_MODEL = "dd253B/DhanushAI-0.5B"
|
| 20 |
|
| 21 |
|
| 22 |
+
# ============================================================
|
| 23 |
+
# GLOBAL VARIABLES
|
| 24 |
+
# ============================================================
|
| 25 |
|
| 26 |
+
tokenizer = None
|
| 27 |
+
model = None
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# ============================================================
|
| 31 |
+
# LOAD MODEL
|
| 32 |
+
# ============================================================
|
| 33 |
|
| 34 |
+
def load_model():
|
| 35 |
|
| 36 |
+
global tokenizer
|
| 37 |
+
global model
|
|
|
|
| 38 |
|
| 39 |
+
if model is not None:
|
| 40 |
+
return
|
| 41 |
|
| 42 |
+
print("Loading tokenizer...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 45 |
+
BASE_MODEL
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
if tokenizer.pad_token is None:
|
| 49 |
+
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
print("Loading base model...")
|
| 52 |
|
| 53 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
BASE_MODEL,
|
| 55 |
+
torch_dtype=torch.float16
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
print("Loading DhanushAI adapter...")
|
| 59 |
|
| 60 |
+
model = PeftModel.from_pretrained(
|
| 61 |
+
base_model,
|
| 62 |
+
ADAPTER_MODEL
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
model = model.to("cuda")
|
| 66 |
+
|
| 67 |
+
model.eval()
|
| 68 |
+
|
| 69 |
+
print("DhanushAI loaded successfully!")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# ============================================================
|
| 73 |
+
# CHAT
|
| 74 |
+
# ============================================================
|
| 75 |
|
| 76 |
@spaces.GPU
|
| 77 |
def chat(message):
|
| 78 |
|
| 79 |
+
load_model()
|
| 80 |
+
|
| 81 |
if not message or not message.strip():
|
| 82 |
return "Please enter a message."
|
| 83 |
|
|
|
|
| 92 |
return_tensors="pt"
|
| 93 |
)
|
| 94 |
|
|
|
|
| 95 |
inputs = {
|
| 96 |
+
key: value.to("cuda")
|
| 97 |
for key, value in inputs.items()
|
| 98 |
}
|
| 99 |
|
|
|
|
| 132 |
return answer
|
| 133 |
|
| 134 |
|
| 135 |
+
# ============================================================
|
| 136 |
# GRADIO
|
| 137 |
+
# ============================================================
|
| 138 |
|
| 139 |
demo = gr.Interface(
|
| 140 |
+
|
| 141 |
fn=chat,
|
| 142 |
|
| 143 |
inputs=gr.Textbox(
|