Views
No views yet
pip install transformers torch1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4# Load model and tokenizer
5model_name = "wcosmas/gemma-3-270m-sbcc"
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 torch_dtype=torch.float16,
9 device_map="auto"
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name)
12
13# System prompt
14SYSTEM_PROMPT = """You are an expert Social and Behavior Change Communication (SBCC) message generator for rural households.
15
16Generate TWO outputs:
171. SBCC MESSAGE (3-5 sentences)
182. ADDITIONAL INSTRUCTIONS (3 bullet points that address the SPECIFIC barrier)
19
20FORMAT:
21Message: [Your 3-5 sentence SBCC message here]
22
23Additional Instructions:
24• [Barrier-specific instruction 1]
25• [Barrier-specific instruction 2]
26• [Barrier-specific instruction 3]
27
28MESSAGE REQUIREMENTS:
29
301. ACCURATE - Address the SPECIFIC barrier mentioned (not generic), include precise measurements
312. CLEAR - Simple, accessible language with specific, implementable steps
323. RESPECTFUL - Begin with varied empathetic acknowledgments, NEVER mention country/region
334. ENCOURAGING - Focus on practical benefits, emphasize what IS possible despite barriers
34
35ADDITIONAL INSTRUCTIONS REQUIREMENTS:
36The 3 bullet points MUST directly solve the barrier (SPACE → smaller dimensions, TIME → faster methods, COST → free materials, etc.)"""
37
38def generate_sbcc(practice, barrier=None, deficiencies=None, message_type="adoption"):
39 """Generate SBCC message and barrier-specific instructions.
40
41 Args:
42 practice: Health/agriculture practice (e.g., "Pit Composting", "Handwashing")
43 barrier: Adoption barrier (e.g., "Land size is too small")
44 deficiencies: Practice deficiencies for improvement messages
45 message_type: "adoption" or "improvement"
46
47 Returns:
48 Generated SBCC message with instructions
49 """
50 # Construct user prompt
51 if message_type == 'adoption' and barrier:
52 user_content = f"Practice: Pit Composting\nBarrier: None\nGenerate SBCC message and barrier-specific additional instructions."
53 elif message_type == 'improvement' and deficiencies:
54 user_content = f"Practice: Pit Composting\nDeficiencies: No visible manure, Compost pits too small\nGenerate SBCC message and deficiency-specific additional instructions."
55 else:
56 user_content = f"Practice: Pit Composting\nGenerate SBCC message and additional instructions."
57
58 # Prepare messages
59 messages = [
60 {"role": "system", "content": SYSTEM_PROMPT},
61 {"role": "user", "content": user_content},
62 ]
63
64 # Apply chat template and tokenize
65 text = tokenizer.apply_chat_template(
66 messages,
67 add_generation_prompt=True,
68 tokenize=False
69 )
70 inputs = tokenizer(text, return_tensors="pt").to(model.device)
71
72 # Generate
73 with torch.no_grad():
74 outputs = model.generate(
75 **inputs,
76 max_new_tokens=500,
77 temperature=0.8,
78 top_p=0.95,
79 top_k=64,
80 do_sample=True,
81 )
82
83 # Decode only the generated part
84 generated_tokens = outputs[0][inputs['input_ids'].shape[1]:]
85 return tokenizer.decode(generated_tokens, skip_special_tokens=True)
86
87# Example usage
88result = generate_sbcc(
89 practice="Pit Composting",
90 barrier="Land size is too small",
91 message_type="adoption"
92)
93print(result)1generate_sbcc(
2 practice="Pit Composting",
3 barrier="Land size is too small"
4)Message: I understand your land is small, and finding space is not easy. You can still compost in a small space—build a simple heap 3 ft × 3 ft × 3 ft against your fence or house wall. Layer kitchen scraps, dry leaves, and a bit of soil. Turn it every week and keep moist. In 6-8 weeks you'll have rich compost to improve your vegetables.
Additional Instructions:
• Start with small pit (3 ft × 3 ft × 3 ft) in corner
• Try above-ground heap if space very tight
• Use vertical space with stacking where possible1generate_sbcc(
2 practice="Handwashing",
3 barrier="Soap is too expensive for daily use"
4)Message: I know cost is a big worry. You don't need special soap for handwashing. Use clean wood ash as a free alternative—it removes germs just as well when rubbed for 20 seconds. Store ash in a covered container near your latrine and kitchen. This way, your family stays healthy without spending money.
Additional Instructions:
• Use clean wood ash as free soap alternative
• Store ash in covered container to keep dry
• Rub hands with ash for 20 seconds minimum1@misc{gemma-3-270m-sbcc-2025,
2 author = {Your Name},
3 title = {SBCC Message Generator - Gemma 3 270M SFT},
4 year = {2025},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/wcosmas/gemma-3-270m-sbcc}
7}