fingpt-complete / examples /simple_demo.py
Starfish55's picture
Initial upload of FinGPT complete package with all modules and examples
2dc7acf verified
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
FinGPT Simple Demo - Sử dụng model nhỏ hơn để test
"""
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import warnings
warnings.filterwarnings("ignore")
def simple_sentiment_demo():
"""
Demo đơn giản sử dụng model có sẵn
"""
print("🚀 FinGPT Simple Demo")
print("=" * 40)
# Sử dụng model nhỏ hơn để test
model_name = "microsoft/DialoGPT-medium"
try:
print("📝 Đang tải tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
print("🧠 Đang tải model...")
model = AutoModelForCausalLM.from_pretrained(model_name)
print("✅ Model đã được tải thành công!")
# Test cases
test_texts = [
"Apple stock is rising today",
"Market crash causes panic",
"New product launch successful"
]
print("\n📊 Test sentiment analysis:")
print("-" * 40)
for i, text in enumerate(test_texts, 1):
print(f"\n{i}. Text: {text}")
# Simple sentiment analysis using text patterns
text_lower = text.lower()
if any(word in text_lower for word in ['rising', 'successful', 'good', 'positive', 'up']):
sentiment = "positive"
elif any(word in text_lower for word in ['crash', 'panic', 'down', 'negative', 'falling']):
sentiment = "negative"
else:
sentiment = "neutral"
print(f" Sentiment: {sentiment}")
return True
except Exception as e:
print(f"❌ Lỗi: {e}")
return False
def test_fingpt_installation():
"""
Test xem FinGPT có thể import được không
"""
print("\n🔍 Kiểm tra FinGPT installation...")
try:
# Test import các thư viện cần thiết
import transformers
print(f"✅ Transformers version: {transformers.__version__}")
import peft
print(f"✅ PEFT version: {peft.__version__}")
import torch
print(f"✅ PyTorch version: {torch.__version__}")
if torch.cuda.is_available():
print(f"✅ CUDA available: {torch.cuda.get_device_name(0)}")
else:
print("⚠️ CUDA không khả dụng, sẽ sử dụng CPU")
return True
except ImportError as e:
print(f"❌ Import error: {e}")
return False
def main():
"""
Main function
"""
# Test installation
if not test_fingpt_installation():
print("\n❌ FinGPT installation có vấn đề!")
return
# Simple demo
print("\n" + "=" * 50)
simple_sentiment_demo()
print("\n" + "=" * 50)
print("📝 Hướng dẫn sử dụng FinGPT:")
print("1. Để sử dụng model đầy đủ, cần GPU với ít nhất 8GB VRAM")
print("2. Model FinGPT sentiment analysis có sẵn trên HuggingFace")
print("3. Có thể sử dụng CPU nhưng sẽ chậm hơn")
print("4. Xem thêm tại: https://huggingface.co/FinGPT")
print("\n🎯 Các ứng dụng FinGPT:")
print("- Financial Sentiment Analysis")
print("- Financial Report Analysis")
print("- Market Forecasting")
print("- Robo-Advisor")
print("- Trading Strategy")
if __name__ == "__main__":
main()