Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
peft_model_id = f"kuyesu22/sunbird-ug-lang-v1.0-bloom-7b1-lora"
|
6 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
8 |
+
config.base_model_name_or_path,
|
9 |
+
return_dict=True,
|
10 |
+
device_map="auto"
|
11 |
+
)
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
13 |
+
|
14 |
+
# Load the Lora model
|
15 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
16 |
+
|
17 |
+
|
18 |
+
def make_inference(english):
|
19 |
+
batch = tokenizer(
|
20 |
+
f"### English:\n{english}: \n\n### Runyankole:",
|
21 |
+
return_tensors="pt",
|
22 |
+
)
|
23 |
+
|
24 |
+
with torch.cuda.amp.autocast():
|
25 |
+
output_tokens = model.generate(**batch, max_new_tokens=100)
|
26 |
+
|
27 |
+
return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
# make a gradio interface
|
32 |
+
import gradio as gr
|
33 |
+
|
34 |
+
gr.Interface(
|
35 |
+
make_inference,
|
36 |
+
[
|
37 |
+
gr.inputs.Textbox(lines=2, label="Englis"),
|
38 |
+
],
|
39 |
+
gr.outputs.Textbox(label="Ad"),
|
40 |
+
title="Sunbird lang Ug",
|
41 |
+
description="English to Runyankole.",
|
42 |
+
).launch()
|