File size: 5,466 Bytes
7865292
 
 
8391e3e
7865292
939d683
105b25f
 
 
 
 
939d683
 
 
 
 
7865292
 
 
 
939d683
105b25f
8391e3e
 
 
105b25f
939d683
105b25f
 
 
 
 
939d683
 
 
 
 
 
 
 
 
 
105b25f
 
939d683
105b25f
 
939d683
 
8391e3e
939d683
105b25f
939d683
 
7865292
 
 
 
8391e3e
 
7865292
 
 
 
 
 
 
 
 
 
 
939d683
7865292
 
 
 
 
 
 
 
939d683
7865292
 
 
 
105b25f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7865292
 
105b25f
 
 
 
 
 
8391e3e
939d683
 
105b25f
939d683
 
 
 
 
 
 
 
 
 
8391e3e
 
 
 
 
 
939d683
 
 
 
 
 
 
 
105b25f
 
939d683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105b25f
 
939d683
 
 
 
 
 
105b25f
 
939d683
105b25f
939d683
 
 
 
7865292
939d683
 
105b25f
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from contextlib import asynccontextmanager
import re
import os
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

try:
    from llama_cpp import Llama
except ImportError:
    raise ImportError("Install llama-cpp-python: pip install llama-cpp-python")

MODEL_REPO = "bartowski/Phi-3.5-mini-instruct-GGUF"
MODEL_FILE = "Phi-3.5-mini-instruct-Q4_K_M.gguf"

llm = None
model_loading = False

@asynccontextmanager
async def lifespan(app: FastAPI):
    global llm, model_loading
    try:
        logger.info("πŸš€ Starting model load...")
        model_loading = True
        
        # Set cache directory for Hugging Face Spaces
        cache_dir = os.getenv("HF_HOME", "./models")
        
        llm = Llama.from_pretrained(
            repo_id=MODEL_REPO,
            filename=MODEL_FILE,
            n_threads=4,
            n_ctx=2048,
            n_batch=256,
            n_gpu_layers=0,
            verbose=False,
        )
        model_loading = False
        logger.info("βœ… Model loaded and ready")
    except Exception as e:
        logger.error(f"❌ Model load error: {e}")
        model_loading = False
        llm = None
    
    yield
    
    logger.info("πŸ›‘ Shutting down...")
    if llm:
        del llm

app = FastAPI(
    title="AI Summarizer",
    description="Fast & Accurate AI Text Summarizer",
    version="1.0",
    lifespan=lifespan
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

class SummarizeRequest(BaseModel):
    text: str = Field(..., min_length=1, max_length=2000)
    length: str = Field(default="short", pattern="^(short|medium|long)$")

LENGTH_INSTRUCTIONS = {
    "short": "Summarize in 2–3 concise sentences.",
    "medium": "Summarize in 4–5 clear sentences.",
    "long": "Summarize in a detailed paragraph.",
}

def clean_output(text: str) -> str:
    """Clean model output from special tokens"""
    text = re.sub(r"<\|.*?\|>", "", text)
    text = re.sub(r"\s+", " ", text)
    return text.strip()

@app.get("/")
def root():
    """Root endpoint - returns status"""
    return {
        "status": "healthy",
        "model_loaded": llm is not None,
        "model_loading": model_loading,
        "message": "AI Summarizer API is running"
    }

@app.get("/health")
def health():
    """Health check endpoint for container orchestration"""
    if model_loading:
        return {
            "status": "starting",
            "model_loaded": False,
            "model_loading": True,
            "message": "Model is loading, please wait..."
        }
    
    if llm is None:
        return {
            "status": "unhealthy",
            "model_loaded": False,
            "model_loading": False,
            "message": "Model failed to load"
        }
    
    return {
        "status": "healthy",
        "model_loaded": True,
        "model_loading": False,
        "model_name": MODEL_FILE,
        "message": "Ready to summarize"
    }

@app.get("/ready")
def readiness():
    """Readiness probe - returns 200 only when model is loaded"""
    if llm is not None and not model_loading:
        return {"ready": True}
    raise HTTPException(status_code=503, detail="Model not ready")

@app.post("/api/summarize")
async def summarize(req: SummarizeRequest):
    if model_loading:
        raise HTTPException(
            status_code=503,
            detail="Model is still loading. Please wait and try again."
        )
    
    if llm is None:
        raise HTTPException(
            status_code=503, 
            detail="Model not loaded. Check server logs."
        )
    
    try:
        text = req.text.strip()
        length_instruction = LENGTH_INSTRUCTIONS.get(
            req.length, 
            LENGTH_INSTRUCTIONS["short"]
        )
        
        prompt = f"""<|user|>
You are an expert text summarizer.
{length_instruction}

Text:
{text}
<|end|>
<|assistant|>"""
        
        max_tokens_map = {
            "short": 140,
            "medium": 220,
            "long": 300
        }
        
        logger.info(f"Summarizing text (length: {req.length})")
        
        output = llm(
            prompt,
            max_tokens=max_tokens_map.get(req.length, 140),
            temperature=0.3,
            top_p=0.9,
            top_k=40,
            repeat_penalty=1.05,
            stop=["<|end|>", "<|user|>"],
            echo=False
        )
        
        summary = clean_output(output["choices"][0]["text"])
        
        if not summary:
            raise HTTPException(
                status_code=500,
                detail="Model produced empty output"
            )
        
        logger.info("βœ… Summary generated successfully")
        
        return {
            "summary": summary,
            "success": True,
            "length": req.length
        }
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Summarization error: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Summarization error: {str(e)}"
        )

if __name__ == "__main__":
    import uvicorn
    # Use PORT environment variable for Hugging Face Spaces
    port = int(os.getenv("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)