Spaces:
Running
on
Zero
Running
on
Zero
chingshuai
commited on
Commit
·
2fbe0ed
1
Parent(s):
b3c5ec9
update app
Browse files
app.py
CHANGED
|
@@ -6,6 +6,20 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 6 |
# Model configuration
|
| 7 |
MODEL_NAME = "Text2MotionPrompter/Text2MotionPrompter"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Prompt template
|
| 10 |
TEMPLATE = """
|
| 11 |
# Role
|
|
@@ -46,25 +60,6 @@ Analyze the user-provided [Input Action] and generate a structured JSON response
|
|
| 46 |
{}
|
| 47 |
"""
|
| 48 |
|
| 49 |
-
# Global variables for lazy loading
|
| 50 |
-
tokenizer = None
|
| 51 |
-
model = None
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def load_model():
|
| 55 |
-
"""Load the model and tokenizer (lazy loading)"""
|
| 56 |
-
global tokenizer, model
|
| 57 |
-
if tokenizer is None:
|
| 58 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 59 |
-
if model is None:
|
| 60 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 61 |
-
MODEL_NAME,
|
| 62 |
-
torch_dtype=torch.float16,
|
| 63 |
-
device_map="auto"
|
| 64 |
-
)
|
| 65 |
-
return tokenizer, model
|
| 66 |
-
|
| 67 |
-
|
| 68 |
@spaces.GPU(duration=120)
|
| 69 |
def generate_motion_prompt(action_input: str, max_new_tokens: int = 512) -> str:
|
| 70 |
"""
|
|
@@ -80,8 +75,8 @@ def generate_motion_prompt(action_input: str, max_new_tokens: int = 512) -> str:
|
|
| 80 |
if not action_input.strip():
|
| 81 |
return "Please enter an action description."
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
|
| 86 |
# Prepare the prompt
|
| 87 |
messages = [
|
|
@@ -94,7 +89,7 @@ def generate_motion_prompt(action_input: str, max_new_tokens: int = 512) -> str:
|
|
| 94 |
add_generation_prompt=True,
|
| 95 |
)
|
| 96 |
|
| 97 |
-
model_inputs = tokenizer([text], return_tensors="pt").to(
|
| 98 |
|
| 99 |
# Generate response
|
| 100 |
with torch.no_grad():
|
|
|
|
| 6 |
# Model configuration
|
| 7 |
MODEL_NAME = "Text2MotionPrompter/Text2MotionPrompter"
|
| 8 |
|
| 9 |
+
# Pre-load tokenizer at startup
|
| 10 |
+
print("📥 Loading tokenizer...")
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
print("✅ Tokenizer loaded successfully!")
|
| 13 |
+
|
| 14 |
+
# Pre-load model to CPU at startup
|
| 15 |
+
print(f"📥 Loading model to CPU: {MODEL_NAME}...")
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
MODEL_NAME,
|
| 18 |
+
torch_dtype="auto",
|
| 19 |
+
device_map="cpu"
|
| 20 |
+
)
|
| 21 |
+
print("✅ Model loaded to CPU successfully!")
|
| 22 |
+
|
| 23 |
# Prompt template
|
| 24 |
TEMPLATE = """
|
| 25 |
# Role
|
|
|
|
| 60 |
{}
|
| 61 |
"""
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
@spaces.GPU(duration=120)
|
| 64 |
def generate_motion_prompt(action_input: str, max_new_tokens: int = 512) -> str:
|
| 65 |
"""
|
|
|
|
| 75 |
if not action_input.strip():
|
| 76 |
return "Please enter an action description."
|
| 77 |
|
| 78 |
+
# Move model to GPU
|
| 79 |
+
model.to("cuda")
|
| 80 |
|
| 81 |
# Prepare the prompt
|
| 82 |
messages = [
|
|
|
|
| 89 |
add_generation_prompt=True,
|
| 90 |
)
|
| 91 |
|
| 92 |
+
model_inputs = tokenizer([text], return_tensors="pt").to("cuda")
|
| 93 |
|
| 94 |
# Generate response
|
| 95 |
with torch.no_grad():
|