Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import torch
|
4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import os
|
6 |
|
7 |
# Recuperar el token de los secrets de Hugging Face Spaces
|
@@ -9,27 +9,32 @@ hf_token = os.getenv('hf_token')
|
|
9 |
|
10 |
# Configuración del modelo LLaMA
|
11 |
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
14 |
model = AutoModelForCausalLM.from_pretrained(
|
15 |
model_id,
|
16 |
torch_dtype=torch.bfloat16,
|
17 |
device_map="auto",
|
|
|
18 |
token=hf_token
|
19 |
)
|
20 |
|
21 |
SYS_PROMPT = """You are an AI assistant tasked with analyzing the sentiment of a given text at the aspect level, specifically related to public security issues in Ecuador, such as crime, violence, law enforcement, or corruption. For each aspect mentioned in the text, classify the sentiment as 'Positive', 'Neutral', or 'Negative' and provide a clear justification for each aspect. Return the response **only** in JSON format and in Spanish, without additional information."""
|
22 |
|
23 |
def generate(formatted_prompt):
|
24 |
-
|
25 |
-
input_text = SYS_PROMPT + formatted_prompt
|
26 |
-
|
27 |
-
# Tokenizar el texto
|
28 |
-
input_ids = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True).input_ids.to(model.device)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
|
34 |
attention_mask = input_ids.ne(tokenizer.pad_token_id)
|
35 |
|
|
|
1 |
import streamlit as st
|
2 |
import json
|
3 |
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
5 |
import os
|
6 |
|
7 |
# Recuperar el token de los secrets de Hugging Face Spaces
|
|
|
9 |
|
10 |
# Configuración del modelo LLaMA
|
11 |
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
12 |
+
bnb_config = BitsAndBytesConfig(
|
13 |
+
load_in_4bit=True,
|
14 |
+
bnb_4bit_use_double_quant=True,
|
15 |
+
bnb_4bit_quant_type="nf4",
|
16 |
+
bnb_4bit_compute_dtype=torch.bfloat16
|
17 |
+
)
|
18 |
|
19 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
20 |
model = AutoModelForCausalLM.from_pretrained(
|
21 |
model_id,
|
22 |
torch_dtype=torch.bfloat16,
|
23 |
device_map="auto",
|
24 |
+
quantization_config=bnb_config,
|
25 |
token=hf_token
|
26 |
)
|
27 |
|
28 |
SYS_PROMPT = """You are an AI assistant tasked with analyzing the sentiment of a given text at the aspect level, specifically related to public security issues in Ecuador, such as crime, violence, law enforcement, or corruption. For each aspect mentioned in the text, classify the sentiment as 'Positive', 'Neutral', or 'Negative' and provide a clear justification for each aspect. Return the response **only** in JSON format and in Spanish, without additional information."""
|
29 |
|
30 |
def generate(formatted_prompt):
|
31 |
+
messages = [{"role": "system", "content": SYS_PROMPT}, {"role": "user", "content": formatted_prompt}]
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
input_ids = tokenizer.apply_chat_template(
|
34 |
+
messages,
|
35 |
+
add_generation_prompt=True,
|
36 |
+
return_tensors="pt"
|
37 |
+
).to(model.device)
|
38 |
|
39 |
attention_mask = input_ids.ne(tokenizer.pad_token_id)
|
40 |
|