File size: 1,230 Bytes
22a2ad6
 
 
 
 
 
 
 
 
 
 
 
 
5e937d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22a2ad6
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# Load base and adapter model
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)

# Gradio function wrapper
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()