File size: 2,238 Bytes
81fd1ce
 
fc275b9
81fd1ce
 
 
 
 
864108f
 
 
 
81fd1ce
864108f
 
81fd1ce
864108f
 
81fd1ce
864108f
 
81fd1ce
 
864108f
81fd1ce
 
 
 
864108f
81fd1ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc275b9
 
 
 
 
81fd1ce
 
 
 
 
 
 
 
 
 
 
 
 
864108f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from peft import PeftModel, PeftConfig
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from flask import Flask, render_template, request, jsonify

HUGGING_FACE_USER_NAME = "elalimy"
model_name = "my_awesome_peft_finetuned_helsinki_model"
peft_model_id = f"{HUGGING_FACE_USER_NAME}/{model_name}"

# Load model configuration (assuming it's saved locally)
config = PeftConfig.from_pretrained(peft_model_id)
# Load the base model from its local directory (replace with actual model type)
base_model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=False)

# Load the tokenizer from its local directory (replace with actual tokenizer type)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)

# Load the Peft model (assuming it's a custom class or adaptation)
AI_model = PeftModel.from_pretrained(base_model, peft_model_id)

# Flask app class
app = Flask(__name__, template_folder='templates')  # Specify the templates folder


def generate_translation(source_text, device="cpu"):
    # Encode the source text
    input_ids = tokenizer.encode(source_text, return_tensors='pt').to(device)

    # Move the model to the same device as input_ids
    model = base_model.to(device)

    # Generate the translation with adjusted decoding parameters
    generated_ids = model.generate(
        input_ids=input_ids,
        max_length=512,  # Adjust max_length if needed
        num_beams=4,
        length_penalty=5,  # Adjust length_penalty if needed
        no_repeat_ngram_size=4,
        early_stopping=True
    )

    # Decode the generated translation excluding special tokens
    generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)

    return generated_text


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/translate', methods=['POST'])
def translate_text():
    data = request.get_json()
    if 'text' not in data:
        return jsonify({'error': 'No text to translate provided'}), 400

    text_to_translate = data['text']
    translated_text = generate_translation(text_to_translate)

    return jsonify({'translated_text': translated_text})


if __name__ == "__main__":
    app.run()