Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,23 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
from peft import PeftModel, PeftConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Load the model and tokenizer
|
7 |
base_model_name = "meta-llama/Llama-3.2-1B"
|
8 |
fine_tuned_model_path = "./gross_llama" # Path to your fine-tuned model
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
peft_model.to("cuda")
|
15 |
peft_model.eval()
|
@@ -25,27 +33,18 @@ Sign Language Translator for converting sentences to Gloss. Glosses are written
|
|
25 |
### Response:
|
26 |
"""
|
27 |
|
28 |
-
import re
|
29 |
-
|
30 |
def translate_to_gloss(input_text):
|
31 |
-
inputs = tokenizer(
|
32 |
-
[tinyllama_prompt.format(input_text)],
|
33 |
-
return_tensors="pt"
|
34 |
-
).to("cuda")
|
35 |
-
|
36 |
with torch.no_grad():
|
37 |
outputs = peft_model.generate(**inputs, max_new_tokens=64, use_cache=True)
|
38 |
-
|
39 |
decoded_output = tokenizer.batch_decode(outputs)[0]
|
40 |
|
41 |
# Extract the response part
|
42 |
response_start = decoded_output.find("### Response")
|
43 |
if response_start != -1:
|
44 |
response = decoded_output[response_start + len("### Response"):].strip()
|
45 |
-
|
46 |
# Remove any remaining prompt parts
|
47 |
response = re.sub(r'###.*$', '', response, flags=re.DOTALL).strip()
|
48 |
-
|
49 |
# Remove any non-gloss text (assuming gloss is in all caps)
|
50 |
gloss_parts = re.findall(r'\b[A-Z]+(?:\s+[A-Z]+)*\b', response)
|
51 |
gloss = ' '.join(gloss_parts)
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
from peft import PeftModel, PeftConfig
|
6 |
+
import re
|
7 |
+
|
8 |
+
# Securely get the Hugging Face token
|
9 |
+
hf_token = os.environ.get("HUGGINGFACE_TOKEN")
|
10 |
+
if not hf_token:
|
11 |
+
raise ValueError("HUGGINGFACE_TOKEN not found in environment variables")
|
12 |
|
13 |
# Load the model and tokenizer
|
14 |
base_model_name = "meta-llama/Llama-3.2-1B"
|
15 |
fine_tuned_model_path = "./gross_llama" # Path to your fine-tuned model
|
16 |
|
17 |
+
# Use the token for authentication
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, token=hf_token)
|
19 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, token=hf_token)
|
20 |
+
peft_model = PeftModel.from_pretrained(base_model, fine_tuned_model_path, token=hf_token)
|
21 |
|
22 |
peft_model.to("cuda")
|
23 |
peft_model.eval()
|
|
|
33 |
### Response:
|
34 |
"""
|
35 |
|
|
|
|
|
36 |
def translate_to_gloss(input_text):
|
37 |
+
inputs = tokenizer([tinyllama_prompt.format(input_text)], return_tensors="pt").to("cuda")
|
|
|
|
|
|
|
|
|
38 |
with torch.no_grad():
|
39 |
outputs = peft_model.generate(**inputs, max_new_tokens=64, use_cache=True)
|
|
|
40 |
decoded_output = tokenizer.batch_decode(outputs)[0]
|
41 |
|
42 |
# Extract the response part
|
43 |
response_start = decoded_output.find("### Response")
|
44 |
if response_start != -1:
|
45 |
response = decoded_output[response_start + len("### Response"):].strip()
|
|
|
46 |
# Remove any remaining prompt parts
|
47 |
response = re.sub(r'###.*$', '', response, flags=re.DOTALL).strip()
|
|
|
48 |
# Remove any non-gloss text (assuming gloss is in all caps)
|
49 |
gloss_parts = re.findall(r'\b[A-Z]+(?:\s+[A-Z]+)*\b', response)
|
50 |
gloss = ' '.join(gloss_parts)
|