YAML Metadata Warning: empty or missing yaml metadata in repo card (https://huggingface.co/docs/hub/model-cards#model-card-metadata)

Python

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch

# 1. Load Model từ Hugging Face
repo_id = "bnmbanhmi/finstructabsa-flan-t5-large"
device = "cuda" if torch.cuda.is_available() else "cpu"

print(f"⏳ Đang tải model từ {repo_id}...")
tokenizer = AutoTokenizer.from_pretrained(repo_id)
model = AutoModelForSeq2SeqLM.from_pretrained(repo_id).to(device)

# 2. Định nghĩa Prompt CHÍNH XÁC theo instructions.py
# Lưu ý: Python string giữ nguyên các dấu cách thụt đầu dòng
ATSC_INSTRUCT_1 = """Definition: You are a senior financial analyst. Determine the sentiment (Positive, Negative, or Neutral) of the specific Aspect in the input text.
    Example 1:
    input: Revenue grew significantly in the last quarter. The aspect is Revenue.
    output: positive
    
    input: """

ATSC_INSTRUCT_2 = """Definition: Analyst task. Classify the sentiment of the financial Aspect as Positive, Negative, or Neutral.
    Example 1:
    input: Revenue grew significantly in the last quarter. The aspect is Revenue.
    output: positive
    
    input: """

# Các token phân cách
DELIM_INSTRUCT = " The aspect is "
EOS_INSTRUCT = " \noutput:"

def predict_sentiment(text, aspect, instruction_type=2):
    """
    instruction_type=2: Dùng bos_instruct2 (Ngắn gọn - thường dùng cho test OOD hoặc tùy config lúc train)
    instruction_type=1: Dùng bos_instruct1 (Chi tiết - Senior Analyst)
    """
    
    # Chọn prompt gốc
    prefix = ATSC_INSTRUCT_2 if instruction_type == 2 else ATSC_INSTRUCT_1
    
    # Ghép chuỗi: BOS + Text + Delim + Aspect + EOS
    # Logic khớp 100% với hàm create_data_in_atsc_format trong data_prep.py
    full_prompt = f"{prefix}{text}{DELIM_INSTRUCT}{aspect}{EOS_INSTRUCT}"
    
    # Inference
    inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
    
    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=10)
    
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# 3. Test thử
text = "China’s exports to U.S. extend double-digit declines, dropping 29% in November."
aspect = "China"

# Thử cả 2 loại instruction xem cái nào model học tốt hơn (thường là cái 2 nếu bạn dùng config default của repo gốc)
print("-" * 50)
print(f"News: {text}")
print(f"Aspect: {aspect}")
print(f"Prediction (Instruct 2): {predict_sentiment(text, aspect, instruction_type=2)}") 
print(f"Prediction (Instruct 1): {predict_sentiment(text, aspect, instruction_type=1)}")
print("-" * 50)
Downloads last month
-
Safetensors
Model size
0.8B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support