Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,70 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
from huggingface_hub import login
|
4 |
-
from peft import PeftModel
|
5 |
import torch
|
|
|
|
|
|
|
6 |
import os
|
|
|
7 |
|
8 |
# Login to Hugging Face Hub
|
9 |
access_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
10 |
login(token=access_token)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
# Load
|
14 |
-
|
15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
16 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
17 |
-
model = PeftModel.from_pretrained(model, model_name)
|
18 |
|
19 |
-
#
|
20 |
model.eval()
|
21 |
|
22 |
-
# Define the
|
23 |
-
def
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
with torch.no_grad():
|
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 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
from huggingface_hub import login
|
5 |
import os
|
6 |
+
import gradio as gr
|
7 |
|
8 |
# Login to Hugging Face Hub
|
9 |
access_token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
10 |
login(token=access_token)
|
11 |
|
12 |
+
# Define model details
|
13 |
+
peft_model_id = "kuyesu22/sunbird-ug-lang-v1.0-llama-2-7b-hf-lora" # Update with the correct ID for your fine-tuned Llama 2 model
|
14 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
15 |
+
|
16 |
+
# Load base model and tokenizer
|
17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
18 |
+
config.base_model_name_or_path,
|
19 |
+
torch_dtype=torch.float16, # Mixed precision for faster inference
|
20 |
+
device_map="auto", # Automatically allocate to available devices
|
21 |
+
offload_folder="./offload" # Directory for offloading layers if needed
|
22 |
+
)
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
24 |
|
25 |
+
# Load the LoRA fine-tuned model
|
26 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
|
|
|
|
|
|
27 |
|
28 |
+
# Set model to evaluation mode
|
29 |
model.eval()
|
30 |
|
31 |
+
# Define the inference function for translation
|
32 |
+
def make_inference(english_text):
|
33 |
+
# Format the prompt based on the language pair
|
34 |
+
prompt = f"### English:\n{english_text}\n\n### Runyankole:"
|
35 |
+
batch = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(model.device)
|
36 |
+
|
37 |
+
# Generate the translation
|
38 |
with torch.no_grad():
|
39 |
+
with torch.cuda.amp.autocast(): # Mixed precision inference for speed
|
40 |
+
output_tokens = model.generate(
|
41 |
+
input_ids=batch["input_ids"],
|
42 |
+
attention_mask=batch["attention_mask"],
|
43 |
+
max_new_tokens=100,
|
44 |
+
do_sample=True,
|
45 |
+
temperature=0.7,
|
46 |
+
num_return_sequences=1,
|
47 |
+
pad_token_id=tokenizer.eos_token_id
|
48 |
+
)
|
49 |
+
|
50 |
+
# Decode the generated tokens to obtain the translation
|
51 |
+
translated_text = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
52 |
+
return translated_text
|
53 |
+
|
54 |
+
# Gradio Interface
|
55 |
+
def launch_gradio_interface():
|
56 |
+
inputs = gr.components.Textbox(lines=2, label="English Text") # Input text in English
|
57 |
+
outputs = gr.components.Textbox(label="Translated Runyankole Text") # Output in Runyankole
|
58 |
+
|
59 |
+
# Launch Gradio app
|
60 |
+
gr.Interface(
|
61 |
+
fn=make_inference,
|
62 |
+
inputs=inputs,
|
63 |
+
outputs=outputs,
|
64 |
+
title="Sunbird UG Lang Translator",
|
65 |
+
description="Translate English to Runyankole using Llama 2 model fine-tuned with LoRA.",
|
66 |
+
).launch()
|
67 |
+
|
68 |
+
# Entry point to run the Gradio app
|
69 |
+
if __name__ == "__main__":
|
70 |
+
launch_gradio_interface()
|
|