Views
No views yet
TorchDynamo를 먼저 비활성화 시키고 진행 시켜야합니다.1import os
2os.environ["TORCHDYNAMO_DISABLE"] = "1"1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# Model and Tokenizer Loading
5model_name = "UICHEOL-HWANG/EcomGen-Gemma3-4B"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12
13def generate_product_description(texts):
14 """
15 Generate product description using EcomGen-Gemma3-4B
16
17 Args:
18 texts (str): Input prompt for product description generation
19
20 Returns:
21 str: Generated product description
22 """
23 # Format the input using chat template
24 messages = [{
25 "role": "user",
26 "content": [{"type": "text", "text": texts}]
27 }]
28
29 text = tokenizer.apply_chat_template(
30 messages,
31 add_generation_prompt=True,
32 tokenize=False
33 )
34
35 inputs = tokenizer([text], return_tensors="pt").to(model.device)
36
37 # Generate response
38 with torch.no_grad():
39 outputs = model.generate(
40 **inputs,
41 max_new_tokens=512,
42 temperature=1.0,
43 top_p=0.95,
44 top_k=64,
45 do_sample=True,
46 pad_token_id=tokenizer.pad_token_id,
47 eos_token_id=tokenizer.eos_token_id
48 )
49
50 # Decode the response (excluding the input prompt)
51 response = tokenizer.decode(
52 outputs[0][inputs['input_ids'].shape[-1]:],
53 skip_special_tokens=True
54 )
55
56 return response.strip()
57
58# Example Usage
59if __name__ == "__main__":
60 # Example 1: Product description generation
61 prompt = """상품명: 프리미엄 유기농 쌀 10kg
62카테고리: 식품 > 쌀·잡곡
63가격: 45,000원
64핵심 키워드: 유기농, 쌀, 농부, 정성, 고가, 품질, 안전, 가족, 건강
65작성 톤: 신뢰감_있는_전문가_톤 (품질 중심, 프리미엄 상품 강조)"""
66
67 result = generate_product_description(prompt)
68 print("Generated Product Description:")
69 print(result)
70
71 # Example 2: Different product category
72 prompt2 = """상품명: 무선 블루투스 이어폰 AirPods Pro
73카테고리: 전자제품 > 오디오
74가격: 329,000원
75핵심 키워드: 무선, 블루투스, 노이즈캔슬링, 프리미엄, 애플, 고음질
76작성 톤: 트렌디한_젊은_톤 (기술과 라이프스타일 강조)"""
77
78 result2 = generate_product_description(prompt2)
79 print("\nGenerated Product Description 2:")
80 print(result2)