Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
|
| 4 |
from transformers import (
|
| 5 |
AutoTokenizer,
|
|
@@ -10,7 +11,7 @@ from peft import PeftModel
|
|
| 10 |
|
| 11 |
|
| 12 |
# ==========================================
|
| 13 |
-
# MODEL
|
| 14 |
# ==========================================
|
| 15 |
|
| 16 |
BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
|
|
@@ -19,22 +20,7 @@ ADAPTER_MODEL = "dd253B/DhanushAI-0.5B"
|
|
| 19 |
|
| 20 |
|
| 21 |
# ==========================================
|
| 22 |
-
#
|
| 23 |
-
# ==========================================
|
| 24 |
-
|
| 25 |
-
if torch.cuda.is_available():
|
| 26 |
-
DEVICE = "cuda"
|
| 27 |
-
DTYPE = torch.float16
|
| 28 |
-
else:
|
| 29 |
-
DEVICE = "cpu"
|
| 30 |
-
DTYPE = torch.float32
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
print("Device:", DEVICE)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# ==========================================
|
| 37 |
-
# TOKENIZER
|
| 38 |
# ==========================================
|
| 39 |
|
| 40 |
print("Loading tokenizer...")
|
|
@@ -48,41 +34,34 @@ if tokenizer.pad_token is None:
|
|
| 48 |
|
| 49 |
|
| 50 |
# ==========================================
|
| 51 |
-
#
|
| 52 |
# ==========================================
|
| 53 |
|
| 54 |
print("Loading base model...")
|
| 55 |
|
| 56 |
-
|
| 57 |
BASE_MODEL,
|
| 58 |
-
torch_dtype=
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# ==========================================
|
| 65 |
-
# YOUR LORA MODEL
|
| 66 |
-
# ==========================================
|
| 67 |
-
|
| 68 |
-
print("Loading DhanushAI...")
|
| 69 |
|
| 70 |
model = PeftModel.from_pretrained(
|
| 71 |
-
|
| 72 |
ADAPTER_MODEL
|
| 73 |
)
|
| 74 |
|
| 75 |
-
model.to(DEVICE)
|
| 76 |
-
|
| 77 |
model.eval()
|
| 78 |
|
| 79 |
print("DhanushAI loaded!")
|
| 80 |
|
| 81 |
|
| 82 |
# ==========================================
|
| 83 |
-
#
|
| 84 |
# ==========================================
|
| 85 |
|
|
|
|
| 86 |
def chat(message):
|
| 87 |
|
| 88 |
if not message or not message.strip():
|
|
@@ -99,8 +78,9 @@ Assistant:"""
|
|
| 99 |
return_tensors="pt"
|
| 100 |
)
|
| 101 |
|
|
|
|
| 102 |
inputs = {
|
| 103 |
-
key: value.to(
|
| 104 |
for key, value in inputs.items()
|
| 105 |
}
|
| 106 |
|
|
@@ -140,16 +120,15 @@ Assistant:"""
|
|
| 140 |
|
| 141 |
|
| 142 |
# ==========================================
|
| 143 |
-
# GRADIO
|
| 144 |
# ==========================================
|
| 145 |
|
| 146 |
demo = gr.Interface(
|
| 147 |
-
|
| 148 |
fn=chat,
|
| 149 |
|
| 150 |
inputs=gr.Textbox(
|
| 151 |
label="Message",
|
| 152 |
-
placeholder="Ask DhanushAI
|
| 153 |
),
|
| 154 |
|
| 155 |
outputs=gr.Textbox(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
import spaces
|
| 4 |
|
| 5 |
from transformers import (
|
| 6 |
AutoTokenizer,
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
# ==========================================
|
| 14 |
+
# MODEL
|
| 15 |
# ==========================================
|
| 16 |
|
| 17 |
BASE_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# ==========================================
|
| 23 |
+
# LOAD TOKENIZER
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# ==========================================
|
| 25 |
|
| 26 |
print("Loading tokenizer...")
|
|
|
|
| 34 |
|
| 35 |
|
| 36 |
# ==========================================
|
| 37 |
+
# LOAD MODEL
|
| 38 |
# ==========================================
|
| 39 |
|
| 40 |
print("Loading base model...")
|
| 41 |
|
| 42 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 43 |
BASE_MODEL,
|
| 44 |
+
torch_dtype=torch.float16,
|
| 45 |
+
device_map="auto"
|
| 46 |
)
|
| 47 |
|
| 48 |
+
print("Loading DhanushAI adapter...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
model = PeftModel.from_pretrained(
|
| 51 |
+
model,
|
| 52 |
ADAPTER_MODEL
|
| 53 |
)
|
| 54 |
|
|
|
|
|
|
|
| 55 |
model.eval()
|
| 56 |
|
| 57 |
print("DhanushAI loaded!")
|
| 58 |
|
| 59 |
|
| 60 |
# ==========================================
|
| 61 |
+
# GPU FUNCTION
|
| 62 |
# ==========================================
|
| 63 |
|
| 64 |
+
@spaces.GPU
|
| 65 |
def chat(message):
|
| 66 |
|
| 67 |
if not message or not message.strip():
|
|
|
|
| 78 |
return_tensors="pt"
|
| 79 |
)
|
| 80 |
|
| 81 |
+
# Move inputs to the same device as model
|
| 82 |
inputs = {
|
| 83 |
+
key: value.to(model.device)
|
| 84 |
for key, value in inputs.items()
|
| 85 |
}
|
| 86 |
|
|
|
|
| 120 |
|
| 121 |
|
| 122 |
# ==========================================
|
| 123 |
+
# GRADIO
|
| 124 |
# ==========================================
|
| 125 |
|
| 126 |
demo = gr.Interface(
|
|
|
|
| 127 |
fn=chat,
|
| 128 |
|
| 129 |
inputs=gr.Textbox(
|
| 130 |
label="Message",
|
| 131 |
+
placeholder="Ask DhanushAI..."
|
| 132 |
),
|
| 133 |
|
| 134 |
outputs=gr.Textbox(
|