V0.7
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ import torch
|
|
| 10 |
import gradio as gr
|
| 11 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 12 |
|
| 13 |
-
from dd import encode_prompt
|
| 14 |
|
| 15 |
# ------------------------------------------------------
|
| 16 |
# Logging
|
|
@@ -19,35 +19,35 @@ logging.basicConfig(
|
|
| 19 |
level=os.getenv("LOG_LEVEL", "INFO"),
|
| 20 |
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
|
| 21 |
)
|
| 22 |
-
logger = logging.getLogger("
|
| 23 |
|
| 24 |
# ------------------------------------------------------
|
| 25 |
# Config
|
| 26 |
# ------------------------------------------------------
|
| 27 |
-
TEXT_ENCODER_ID="Qwen/Qwen2.5-0.5B-Instruct"
|
| 28 |
TOKENIZER_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 29 |
-
DTYPE = torch.
|
| 30 |
|
| 31 |
# ------------------------------------------------------
|
| 32 |
-
#
|
| 33 |
# ------------------------------------------------------
|
| 34 |
-
logger.info("Loading
|
| 35 |
|
| 36 |
t0 = time.time()
|
| 37 |
text_encoder = AutoModelForCausalLM.from_pretrained(
|
| 38 |
TEXT_ENCODER_ID,
|
| 39 |
-
|
|
|
|
| 40 |
).to("cpu")
|
| 41 |
|
| 42 |
t1 = time.time()
|
| 43 |
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_ID)
|
| 44 |
-
logger.info("Loaded tokenizer in %.2fs", time.time() - t1)
|
| 45 |
|
|
|
|
| 46 |
torch.set_grad_enabled(False)
|
| 47 |
|
| 48 |
|
| 49 |
def get_vram_info():
|
| 50 |
-
"""Get current VRAM usage info."""
|
| 51 |
if torch.cuda.is_available():
|
| 52 |
return {
|
| 53 |
"vram_allocated_mb": round(torch.cuda.memory_allocated() / 1024 / 1024, 2),
|
|
@@ -56,6 +56,7 @@ def get_vram_info():
|
|
| 56 |
}
|
| 57 |
return {"vram": "CUDA not available"}
|
| 58 |
|
|
|
|
| 59 |
@spaces.GPU()
|
| 60 |
def encode_text(prompt: str):
|
| 61 |
"""Encode text and return a downloadable pytorch file."""
|
|
@@ -66,7 +67,7 @@ def encode_text(prompt: str):
|
|
| 66 |
|
| 67 |
t0 = time.time()
|
| 68 |
|
| 69 |
-
#
|
| 70 |
prompts = [p.strip() for p in prompt.strip().split("\n") if p.strip()]
|
| 71 |
if not prompts:
|
| 72 |
return None, "Please enter at least one prompt"
|
|
@@ -76,6 +77,7 @@ def encode_text(prompt: str):
|
|
| 76 |
|
| 77 |
logger.info("Encoding %d prompt(s)", num_prompts)
|
| 78 |
|
|
|
|
| 79 |
prompt_embeds, text_ids = encode_prompt(
|
| 80 |
text_encoder=text_encoder,
|
| 81 |
tokenizer=tokenizer,
|
|
@@ -90,11 +92,11 @@ def encode_text(prompt: str):
|
|
| 90 |
tuple(text_ids.shape),
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# Save tensor
|
| 94 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pt")
|
| 95 |
torch.save(prompt_embeds.cpu(), temp_file.name)
|
| 96 |
|
| 97 |
-
#
|
| 98 |
del prompt_embeds, text_ids
|
| 99 |
gc.collect()
|
| 100 |
if torch.cuda.is_available():
|
|
@@ -108,13 +110,13 @@ def encode_text(prompt: str):
|
|
| 108 |
)
|
| 109 |
|
| 110 |
return temp_file.name, status
|
| 111 |
-
|
| 112 |
|
| 113 |
# ------------------------------------------------------
|
| 114 |
# Gradio Interface
|
| 115 |
# ------------------------------------------------------
|
| 116 |
-
with gr.Blocks(title="
|
| 117 |
-
gr.Markdown("#
|
| 118 |
gr.Markdown("Enter text to encode. For multiple prompts, put each on a new line.")
|
| 119 |
|
| 120 |
with gr.Row():
|
|
|
|
| 10 |
import gradio as gr
|
| 11 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 12 |
|
| 13 |
+
from dd import encode_prompt # <-- version Qwen compatible
|
| 14 |
|
| 15 |
# ------------------------------------------------------
|
| 16 |
# Logging
|
|
|
|
| 19 |
level=os.getenv("LOG_LEVEL", "INFO"),
|
| 20 |
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
|
| 21 |
)
|
| 22 |
+
logger = logging.getLogger("qwen-text-encoding-gradio")
|
| 23 |
|
| 24 |
# ------------------------------------------------------
|
| 25 |
# Config
|
| 26 |
# ------------------------------------------------------
|
| 27 |
+
TEXT_ENCODER_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 28 |
TOKENIZER_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 29 |
+
DTYPE = torch.float32 # CPU safe
|
| 30 |
|
| 31 |
# ------------------------------------------------------
|
| 32 |
+
# Load model
|
| 33 |
# ------------------------------------------------------
|
| 34 |
+
logger.info("Loading Qwen 0.5B...")
|
| 35 |
|
| 36 |
t0 = time.time()
|
| 37 |
text_encoder = AutoModelForCausalLM.from_pretrained(
|
| 38 |
TEXT_ENCODER_ID,
|
| 39 |
+
torch_dtype=DTYPE,
|
| 40 |
+
low_cpu_mem_usage=True,
|
| 41 |
).to("cpu")
|
| 42 |
|
| 43 |
t1 = time.time()
|
| 44 |
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_ID)
|
|
|
|
| 45 |
|
| 46 |
+
logger.info("Loaded Qwen in %.2fs", time.time() - t0)
|
| 47 |
torch.set_grad_enabled(False)
|
| 48 |
|
| 49 |
|
| 50 |
def get_vram_info():
|
|
|
|
| 51 |
if torch.cuda.is_available():
|
| 52 |
return {
|
| 53 |
"vram_allocated_mb": round(torch.cuda.memory_allocated() / 1024 / 1024, 2),
|
|
|
|
| 56 |
}
|
| 57 |
return {"vram": "CUDA not available"}
|
| 58 |
|
| 59 |
+
|
| 60 |
@spaces.GPU()
|
| 61 |
def encode_text(prompt: str):
|
| 62 |
"""Encode text and return a downloadable pytorch file."""
|
|
|
|
| 67 |
|
| 68 |
t0 = time.time()
|
| 69 |
|
| 70 |
+
# Multi‑prompts support
|
| 71 |
prompts = [p.strip() for p in prompt.strip().split("\n") if p.strip()]
|
| 72 |
if not prompts:
|
| 73 |
return None, "Please enter at least one prompt"
|
|
|
|
| 77 |
|
| 78 |
logger.info("Encoding %d prompt(s)", num_prompts)
|
| 79 |
|
| 80 |
+
# 🔥 Qwen‑compatible encode_prompt (dd.py)
|
| 81 |
prompt_embeds, text_ids = encode_prompt(
|
| 82 |
text_encoder=text_encoder,
|
| 83 |
tokenizer=tokenizer,
|
|
|
|
| 92 |
tuple(text_ids.shape),
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Save tensor
|
| 96 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pt")
|
| 97 |
torch.save(prompt_embeds.cpu(), temp_file.name)
|
| 98 |
|
| 99 |
+
# Cleanup
|
| 100 |
del prompt_embeds, text_ids
|
| 101 |
gc.collect()
|
| 102 |
if torch.cuda.is_available():
|
|
|
|
| 110 |
)
|
| 111 |
|
| 112 |
return temp_file.name, status
|
| 113 |
+
|
| 114 |
|
| 115 |
# ------------------------------------------------------
|
| 116 |
# Gradio Interface
|
| 117 |
# ------------------------------------------------------
|
| 118 |
+
with gr.Blocks(title="Qwen Text Encoder") as demo:
|
| 119 |
+
gr.Markdown("# Qwen 2.5 Text Encoder")
|
| 120 |
gr.Markdown("Enter text to encode. For multiple prompts, put each on a new line.")
|
| 121 |
|
| 122 |
with gr.Row():
|