Update app.py
Browse files
app.py
CHANGED
|
@@ -11,10 +11,25 @@ base = AutoModelForCausalLM.from_pretrained(base_model)
|
|
| 11 |
model = PeftModel.from_pretrained(base, adapter_model)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
demo = gr.Interface(fn=infer, inputs="text", outputs="text", title="Phi-2 Sylheti Translator")
|
| 20 |
demo.launch()
|
|
|
|
| 11 |
model = PeftModel.from_pretrained(base, adapter_model)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
+
def translate(model, tokenizer, input_text, direction=0, max_new_tokens=256):
|
| 15 |
+
if direction == 0:
|
| 16 |
+
prompt = f"Translate Bangla to Sylheti: {input_text}\nOutput:"
|
| 17 |
+
else:
|
| 18 |
+
prompt = f"Translate Sylheti to Bangla: {input_text}\nOutput:"
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
+
output_ids = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 22 |
+
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
# Gradio function wrapper
|
| 25 |
+
def infer(text, direction):
|
| 26 |
+
return translate(model, tokenizer, text, direction=direction)
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=infer,
|
| 30 |
+
inputs=[gr.Textbox(label="Input Text"), gr.Radio(["Bangla to Sylheti", "Sylheti to Bangla"], type="index", label="Translation Direction")],
|
| 31 |
+
outputs="text",
|
| 32 |
+
title="Phi-2 Sylheti Translator"
|
| 33 |
+
)
|
| 34 |
|
|
|
|
| 35 |
demo.launch()
|