ming commited on
Commit
d42ed51
Β·
1 Parent(s): 05dbe4c

Fix transformers service to be optional to prevent import errors

Browse files
app/services/transformers_summarizer.py CHANGED
@@ -3,20 +3,32 @@ Transformers service for fast text summarization using Hugging Face models.
3
  """
4
  import asyncio
5
  import time
6
- from typing import Dict, Any, AsyncGenerator
7
-
8
- from transformers import pipeline
9
 
10
  from app.core.logging import get_logger
11
 
12
  logger = get_logger(__name__)
13
 
 
 
 
 
 
 
 
 
14
 
15
  class TransformersSummarizer:
16
  """Service for fast text summarization using Hugging Face Transformers."""
17
 
18
  def __init__(self):
19
  """Initialize the Transformers pipeline with distilbart model."""
 
 
 
 
 
 
20
  logger.info("Initializing Transformers pipeline...")
21
 
22
  try:
@@ -28,13 +40,17 @@ class TransformersSummarizer:
28
  logger.info("βœ… Transformers pipeline initialized successfully")
29
  except Exception as e:
30
  logger.error(f"❌ Failed to initialize Transformers pipeline: {e}")
31
- raise
32
 
33
  async def warm_up_model(self) -> None:
34
  """
35
  Warm up the model with a test input to load weights into memory.
36
  This speeds up subsequent requests.
37
  """
 
 
 
 
38
  test_text = "This is a test text to warm up the model."
39
 
40
  try:
@@ -50,7 +66,7 @@ class TransformersSummarizer:
50
  logger.info("βœ… Transformers model warmup successful")
51
  except Exception as e:
52
  logger.error(f"❌ Transformers model warmup failed: {e}")
53
- raise
54
 
55
  async def summarize_text_stream(
56
  self,
@@ -69,6 +85,16 @@ class TransformersSummarizer:
69
  Yields:
70
  Dict containing 'content' (word chunk) and 'done' (completion flag)
71
  """
 
 
 
 
 
 
 
 
 
 
72
  start_time = time.time()
73
  text_length = len(text)
74
 
 
3
  """
4
  import asyncio
5
  import time
6
+ from typing import Dict, Any, AsyncGenerator, Optional
 
 
7
 
8
  from app.core.logging import get_logger
9
 
10
  logger = get_logger(__name__)
11
 
12
+ # Try to import transformers, but make it optional
13
+ try:
14
+ from transformers import pipeline
15
+ TRANSFORMERS_AVAILABLE = True
16
+ except ImportError:
17
+ TRANSFORMERS_AVAILABLE = False
18
+ logger.warning("Transformers library not available. Pipeline endpoint will be disabled.")
19
+
20
 
21
  class TransformersSummarizer:
22
  """Service for fast text summarization using Hugging Face Transformers."""
23
 
24
  def __init__(self):
25
  """Initialize the Transformers pipeline with distilbart model."""
26
+ self.summarizer: Optional[Any] = None
27
+
28
+ if not TRANSFORMERS_AVAILABLE:
29
+ logger.warning("⚠️ Transformers not available - pipeline endpoint will not work")
30
+ return
31
+
32
  logger.info("Initializing Transformers pipeline...")
33
 
34
  try:
 
40
  logger.info("βœ… Transformers pipeline initialized successfully")
41
  except Exception as e:
42
  logger.error(f"❌ Failed to initialize Transformers pipeline: {e}")
43
+ self.summarizer = None
44
 
45
  async def warm_up_model(self) -> None:
46
  """
47
  Warm up the model with a test input to load weights into memory.
48
  This speeds up subsequent requests.
49
  """
50
+ if not self.summarizer:
51
+ logger.warning("⚠️ Transformers pipeline not initialized, skipping warmup")
52
+ return
53
+
54
  test_text = "This is a test text to warm up the model."
55
 
56
  try:
 
66
  logger.info("βœ… Transformers model warmup successful")
67
  except Exception as e:
68
  logger.error(f"❌ Transformers model warmup failed: {e}")
69
+ # Don't raise - allow app to start even if warmup fails
70
 
71
  async def summarize_text_stream(
72
  self,
 
85
  Yields:
86
  Dict containing 'content' (word chunk) and 'done' (completion flag)
87
  """
88
+ if not self.summarizer:
89
+ error_msg = "Transformers pipeline not available. Please install transformers and torch."
90
+ logger.error(f"❌ {error_msg}")
91
+ yield {
92
+ "content": "",
93
+ "done": True,
94
+ "error": error_msg,
95
+ }
96
+ return
97
+
98
  start_time = time.time()
99
  text_length = len(text)
100
 
scripts/deploy.sh ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Deploy script - pushes to both GitHub and Hugging Face
4
+ # Usage: ./scripts/deploy.sh [commit_message]
5
+
6
+ set -e # Exit on any error
7
+
8
+ echo "πŸš€ Deploying to GitHub and Hugging Face..."
9
+
10
+ # Get commit message from argument or use default
11
+ if [ -n "$1" ]; then
12
+ commit_msg="$1"
13
+ else
14
+ commit_msg="feat: Deploy latest changes"
15
+ fi
16
+
17
+ # Check if there are changes to commit
18
+ if git diff --quiet && git diff --cached --quiet; then
19
+ echo "ℹ️ No changes to commit"
20
+ else
21
+ echo "πŸ“ Committing changes: $commit_msg"
22
+ git add -A
23
+ git commit -m "$commit_msg"
24
+ fi
25
+
26
+ # Push to GitHub
27
+ echo "πŸ“€ Pushing to GitHub..."
28
+ if git push origin main; then
29
+ echo "βœ… Successfully pushed to GitHub"
30
+ else
31
+ echo "❌ Failed to push to GitHub"
32
+ exit 1
33
+ fi
34
+
35
+ # Push to Hugging Face
36
+ echo "πŸ“€ Pushing to Hugging Face..."
37
+ if git push hf main; then
38
+ echo "βœ… Successfully deployed to Hugging Face!"
39
+ echo "πŸ”— Check deployment at: https://huggingface.co/spaces/colin730/SummarizerApp"
40
+ echo "⏱️ Build time: ~5-10 minutes"
41
+ echo ""
42
+ echo "🎯 Monitor deployment with:"
43
+ echo " curl -s https://colin730-summarizerapp.hf.space/health"
44
+ else
45
+ echo "❌ Failed to push to Hugging Face"
46
+ exit 1
47
+ fi
48
+
49
+ echo ""
50
+ echo "πŸŽ‰ Deployment complete! Both GitHub and Hugging Face are updated."
51
+
52
+
53
+