Spaces:
Runtime error
Runtime error
Download app.py from gampala1234/checkphi3mini: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/gampala1234/checkphi3mini/resolve/main/app.py
- Command line
-
hf download hf://spaces/gampala1234/checkphi3mini/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gampala1234/checkphi3mini/resolve/main/app.py
1.87 kB
| 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']}") |