File size: 3,765 Bytes
aa6cf16 109eade aa6cf16 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# Malawi Financial Assistant - Usage Example
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
# Method 1: Using pipeline (simplest)
print("=== Using Pipeline ===")
financial_assistant = pipeline(
"text-generation",
model="Nellissa/Malawi-Financial-model",
max_length=512, # Increased for longer responses
temperature=0.7,
do_sample=True
)
# Test different types of questions
test_questions = [
# Short questions
"How to open a savings account in Malawi?",
# Medium questions
"What are the steps to apply for a small business loan in Malawi and what documents are needed?",
# Long detailed questions
"Can you explain the complete process of buying property in Malawi including land registration, legal requirements, typical costs involved, and common pitfalls to avoid for first-time home buyers?",
# Mixed questions (multiple topics)
"What are the best savings strategies for a family in Malawi considering both short-term needs like school fees and long-term goals like retirement, and how do mobile money services like Airtel Money and TNM Mpamba fit into this?",
# Complex scenario-based questions
"I'm a small farmer in rural Malawi with seasonal income. I want to save for my children's education, invest in better farming equipment, and have emergency funds for medical needs. What financial planning approach would you recommend considering the challenges of irregular income and limited bank access in rural areas?",
# Bilingual/mixed language questions
"What are the interest rates for microloans in Malawi and ndingosunga bwanji ndalama? (how to save money?)",
# Comparative questions
"Compare the benefits and drawbacks of using traditional banks versus mobile money services in Malawi for someone who earns around 200,000 MWK per month and lives in both urban and rural areas periodically."
]
print("Testing various question types...")
print("=" * 60)
for i, question in enumerate(test_questions, 1):
print(f"\nπ Question {i}: {question[:100]}..." if len(question) > 100 else f"\nπ Question {i}: {question}")
print("-" * 80)
try:
response = financial_assistant(question)
print(f"π‘ Response: {response[0]['generated_text']}")
print("=" * 80)
except Exception as e:
print(f"β Error: {e}")
# Method 2: Using model directly (more control)
print("\n=== Using Model Directly (Advanced) ===")
tokenizer = AutoTokenizer.from_pretrained("Nellissa/Malawi-Financial-model")
model = AutoModelForCausalLM.from_pretrained("Nellissa/Malawi-Financial-model")
# Example with longer context
complex_prompt = """As a young professional in Lilongwe earning 500,000 MWK monthly, I need advice on:
1. Creating a sustainable budget that covers rent, utilities, transportation, and food
2. Saving strategies for both short-term goals (vacation) and long-term goals (house down payment)
3. Investment options available in Malawi with moderate risk
4. How to build an emergency fund while still having money for social activities
Please provide detailed guidance on these aspects:"""
print("π€ Complex multi-part question:")
print(complex_prompt)
print("-" * 80)
inputs = tokenizer(complex_prompt, return_tensors="pt", max_length=1024, truncation=True)
outputs = model.generate(
**inputs,
max_length=600, # Longer for complex questions
temperature=0.7,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3,
early_stopping=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"π‘ Detailed Response: {response}")
|