Update app.py
Browse files
app.py
CHANGED
|
@@ -6,27 +6,27 @@ HUGGING_FACE_USER_NAME = "elalimy"
|
|
| 6 |
model_name = "my_awesome_peft_finetuned_helsinki_model"
|
| 7 |
peft_model_id = f"{HUGGING_FACE_USER_NAME}/{model_name}"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
config = PeftConfig.from_pretrained(peft_model_id)
|
| 16 |
-
# Load the base model from its local directory (replace with actual model type)
|
| 17 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=False)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
# Load the Peft model (assuming it's a custom class or adaptation)
|
| 23 |
-
AI_model = PeftModel.from_pretrained(model, peft_model_id)
|
| 24 |
|
|
|
|
| 25 |
# Encode the source text
|
| 26 |
input_ids = tokenizer.encode(source_text, return_tensors='pt').to(device)
|
| 27 |
|
| 28 |
# Move the model to the same device as input_ids
|
| 29 |
-
model =
|
| 30 |
|
| 31 |
# Generate the translation with adjusted decoding parameters
|
| 32 |
generated_ids = model.generate(
|
|
@@ -62,4 +62,4 @@ def translate_text():
|
|
| 62 |
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
app.run()
|
|
|
|
| 6 |
model_name = "my_awesome_peft_finetuned_helsinki_model"
|
| 7 |
peft_model_id = f"{HUGGING_FACE_USER_NAME}/{model_name}"
|
| 8 |
|
| 9 |
+
# Load model configuration (assuming it's saved locally)
|
| 10 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
| 11 |
+
# Load the base model from its local directory (replace with actual model type)
|
| 12 |
+
base_model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=False)
|
| 13 |
|
| 14 |
+
# Load the tokenizer from its local directory (replace with actual tokenizer type)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
| 16 |
|
| 17 |
+
# Load the Peft model (assuming it's a custom class or adaptation)
|
| 18 |
+
AI_model = PeftModel.from_pretrained(base_model, peft_model_id)
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Flask app class
|
| 21 |
+
app = Flask(__name__, template_folder='templates') # Specify the templates folder
|
| 22 |
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
def generate_translation(source_text, device="cpu"):
|
| 25 |
# Encode the source text
|
| 26 |
input_ids = tokenizer.encode(source_text, return_tensors='pt').to(device)
|
| 27 |
|
| 28 |
# Move the model to the same device as input_ids
|
| 29 |
+
model = base_model.to(device)
|
| 30 |
|
| 31 |
# Generate the translation with adjusted decoding parameters
|
| 32 |
generated_ids = model.generate(
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
app.run()
|