File size: 1,867 Bytes
b545b79
7c64e2d
0745eff
 
7c64e2d
 
 
9a75f00
ccc5d72
0745eff
ccc5d72
0745eff
ccc5d72
b545b79
0745eff
7c64e2d
ccc5d72
b545b79
0745eff
7c64e2d
 
 
 
 
b545b79
 
7c64e2d
0745eff
ccc5d72
0745eff
 
ccc5d72
7c64e2d
0745eff
7c64e2d
 
0745eff
7c64e2d
 
 
0745eff
 
 
7c64e2d
 
 
 
0745eff
7c64e2d
 
0745eff
7c64e2d
0745eff
 
 
 
 
 
7c64e2d
0745eff
 
7c64e2d
0745eff
 
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
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
import torch
from accelerate import Accelerator
import sys  # Added for input handling

# Initialize accelerator
accelerator = Accelerator()

def load_models():
    print("🔄 Loading optimized models...")
    
    # 1. DistilBERT (Intent) - Lightweight
    intent_classifier = pipeline(
        "text-classification", 
        model="distilbert-base-uncased",
        device=accelerator.device
    )
    
    # 2. GPT-2 Small (Reply) - Faster than TinyLlama
    reply_generator = pipeline(
        "text-generation",
        model="gpt2",
        device=accelerator.device,
        torch_dtype=torch.float16
    )
    
    print("✅ Models loaded!")
    return intent_classifier, reply_generator

# Load models
intent_classifier, reply_generator = load_models()

def analyze_comment(comment):
    # 1. Intent Detection
    intent_result = intent_classifier(comment)
    
    # 2. Generate Reply
    reply = reply_generator(
        f"Generate a 10-word Instagram DM reply to: '{comment}'",
        max_length=30,
        do_sample=True,
        pad_token_id=50256  # GPT-2's pad token
    )[0]['generated_text'].split(":")[-1].strip()
    
    return {
        "intent": intent_result[0]['label'],
        "confidence": intent_result[0]['score'],
        "reply": reply
    }

# Non-interactive mode (for Hugging Face Spaces/Docker)
if __name__ == "__main__":
    # Test with hardcoded comments if no input
    test_comments = [
        "Can you send me the link?",
        "This is awesome!",
        "How does this work?"
    ]
    
    for comment in test_comments:
        print(f"\n📝 Comment: {comment}")
        result = analyze_comment(comment)
        print(f"🎯 Intent: {result['intent']} (Confidence: {result['confidence']:.2f})")
        print(f"💬 Reply: {result['reply']}")