| | import gradio as gr |
| | from transformers import AutoModelForCausalLM, AutoTokenizer |
| | from peft import PeftModel |
| |
|
| | |
| | base_model = "microsoft/phi-2" |
| | adapter_model = "Sabbir772/phi2_sylhet" |
| |
|
| | tokenizer = AutoTokenizer.from_pretrained(base_model) |
| | base = AutoModelForCausalLM.from_pretrained(base_model) |
| | model = PeftModel.from_pretrained(base, adapter_model) |
| | model.eval() |
| |
|
| | def translate(model, tokenizer, input_text, direction=0, max_new_tokens=256): |
| | if direction == 0: |
| | prompt = f"Translate Bangla to Sylheti: {input_text}\nOutput:" |
| | else: |
| | prompt = f"Translate Sylheti to Bangla: {input_text}\nOutput:" |
| |
|
| | inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
| | output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens) |
| | return tokenizer.decode(output_ids[0], skip_special_tokens=True) |
| |
|
| | |
| | def infer(text, direction): |
| | return translate(model, tokenizer, text, direction=direction) |
| |
|
| | demo = gr.Interface( |
| | fn=infer, |
| | inputs=[gr.Textbox(label="Input Text"), gr.Radio(["Bangla to Sylheti", "Sylheti to Bangla"], type="index", label="Translation Direction")], |
| | outputs="text", |
| | title="Phi-2 Sylheti Translator" |
| | ) |
| |
|
| | demo.launch() |
| |
|