Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -13,39 +13,48 @@ base_model_id = "haoranxu/X-ALMA-13B-Group7"
|
|
| 13 |
default_adapter_id = "sandipghimire/X-ALMA-13B-Nepali-Legal-Translator"
|
| 14 |
adapter_path = default_adapter_id if not os.path.exists("./filtered_adapter") else "./filtered_adapter"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
-
|
| 21 |
-
# Load model in 4-bit unconditionally for Hugging Face ZeroGPU compatibility
|
| 22 |
-
print("Configuring 4-bit Quantization (QLoRA)...")
|
| 23 |
-
bnb_config = BitsAndBytesConfig(
|
| 24 |
-
load_in_4bit=True,
|
| 25 |
-
bnb_4bit_quant_type="nf4",
|
| 26 |
-
bnb_4bit_use_double_quant=True,
|
| 27 |
-
bnb_4bit_compute_dtype=torch.bfloat16
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
print(f"Loading base model {base_model_id} in 4-bit...")
|
| 31 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
| 32 |
-
base_model_id,
|
| 33 |
-
quantization_config=bnb_config,
|
| 34 |
-
device_map="auto",
|
| 35 |
-
torch_dtype=torch.bfloat16,
|
| 36 |
-
low_cpu_mem_usage=True,
|
| 37 |
-
trust_remote_code=True
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
print(f"Loading LoRA adapter from {adapter_path}...")
|
| 41 |
-
model = PeftModel.from_pretrained(base_model, adapter_path)
|
| 42 |
-
model.eval()
|
| 43 |
|
| 44 |
@spaces.GPU
|
| 45 |
def translate(text):
|
|
|
|
|
|
|
| 46 |
if not text.strip():
|
| 47 |
return "Please enter some text to translate."
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
prompt_content = f"Translate this from English to Nepali:\nEnglish: {text}\nNepali:"
|
| 50 |
messages = [{"role": "user", "content": prompt_content}]
|
| 51 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
@@ -99,6 +108,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo"))
|
|
| 99 |
],
|
| 100 |
inputs=input_text
|
| 101 |
)
|
|
|
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.launch()
|
|
|
|
| 13 |
default_adapter_id = "sandipghimire/X-ALMA-13B-Nepali-Legal-Translator"
|
| 14 |
adapter_path = default_adapter_id if not os.path.exists("./filtered_adapter") else "./filtered_adapter"
|
| 15 |
|
| 16 |
+
# Global model and tokenizer variables
|
| 17 |
+
model = None
|
| 18 |
+
tokenizer = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@spaces.GPU
|
| 21 |
def translate(text):
|
| 22 |
+
global model, tokenizer
|
| 23 |
+
|
| 24 |
if not text.strip():
|
| 25 |
return "Please enter some text to translate."
|
| 26 |
|
| 27 |
+
# Lazy load model and tokenizer on ZeroGPU
|
| 28 |
+
if model is None or tokenizer is None:
|
| 29 |
+
print("Initializing tokenizer and loading model on ZeroGPU...")
|
| 30 |
+
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(adapter_path, trust_remote_code=True)
|
| 32 |
+
if tokenizer.pad_token is None:
|
| 33 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 34 |
+
|
| 35 |
+
print("Configuring 4-bit Quantization (QLoRA)...")
|
| 36 |
+
bnb_config = BitsAndBytesConfig(
|
| 37 |
+
load_in_4bit=True,
|
| 38 |
+
bnb_4bit_quant_type="nf4",
|
| 39 |
+
bnb_4bit_use_double_quant=True,
|
| 40 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
print(f"Loading base model {base_model_id} on GPU...")
|
| 44 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 45 |
+
base_model_id,
|
| 46 |
+
quantization_config=bnb_config,
|
| 47 |
+
device_map="auto",
|
| 48 |
+
torch_dtype=torch.bfloat16,
|
| 49 |
+
low_cpu_mem_usage=True,
|
| 50 |
+
trust_remote_code=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
print(f"Loading LoRA adapter from {adapter_path}...")
|
| 54 |
+
model = PeftModel.from_pretrained(base_model, adapter_path)
|
| 55 |
+
model.eval()
|
| 56 |
+
print("Model initialization complete!")
|
| 57 |
+
|
| 58 |
prompt_content = f"Translate this from English to Nepali:\nEnglish: {text}\nNepali:"
|
| 59 |
messages = [{"role": "user", "content": prompt_content}]
|
| 60 |
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
|
|
|
| 108 |
],
|
| 109 |
inputs=input_text
|
| 110 |
)
|
| 111 |
+
|
| 112 |
+
translate_btn.click(fn=translate, inputs=input_text, outputs=output_text)
|
| 113 |
|
| 114 |
if __name__ == "__main__":
|
| 115 |
demo.launch()
|