forked from NiharRD/ArtStyleTransfer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
236 lines (200 loc) · 6.81 KB
/
Copy pathmain.py
File metadata and controls
236 lines (200 loc) · 6.81 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from typing import List
from llama_cpp import Llama
import uvicorn
# Initialize FastAPI app
app = FastAPI(
title="LightArt Autocomplete API",
description="Text autocomplete service using local Llama model with dynamic suggestions",
version="1.0.0"
)
# Add CORS middleware to allow requests from web clients
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure this to specific origins in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Model configuration
MODEL_PATH = "./llama-3.2-3b-instruct-q4_k_m.gguf"
# Initialize Llama model
print(f"Loading model from {MODEL_PATH}...")
try:
llm = Llama(
model_path=MODEL_PATH,
n_ctx=2048,
n_gpu_layers=0,
verbose=False,
)
print("Model loaded successfully!")
except Exception as e:
print(f"Error loading model: {e}")
llm = None
# Pydantic models for request/response
class AutocompleteRequest(BaseModel):
sentence: str = Field(..., description="The base sentence to autocomplete", example="soften the overall")
suggestions: List[str] = Field(..., description="List of suggestions to use for autocompletion", min_length=1)
class AutocompleteResponse(BaseModel):
completion: str = Field(..., description="The autocompleted text")
full_text: str = Field(..., description="The base sentence + completion combined")
# Autocomplete function
def autocomplete_lightart(base_sentence: str, light_suggestions: List[str]) -> str:
"""
Autocompletes a sentence using ONLY the given suggestions.
"""
prompt = f"""
You are an AUTOCOMPLETE assistant.
Use ONLY these light_suggestions:
{light_suggestions}
RULES:
- Continue the sentence EXACTLY from where it ends.
- Do NOT change the base sentence.
- ONLY use provided light_suggestions to autocomplete.
- ONLY natural color/tone language.
- ONLY autocomplete using the light_suggestions.
Complete: {base_sentence}
"""
response = llm.create_chat_completion(
messages=[
{"role": "system", "content": "Autocomplete using ONLY the allowed suggestions."},
{"role": "user", "content": prompt},
],
max_tokens=20,
temperature=0.1,
top_p=0.9,
)
return response["choices"][0]["message"]["content"].strip()
# API Endpoints
@app.get("/")
async def root():
"""Root endpoint with API information"""
return {
"message": "LightArt Autocomplete API",
"version": "1.0.0",
"endpoints": {
"/autocomplete": "POST - Generate text autocompletion",
"/health": "GET - Check API health status",
"/docs": "GET - Interactive API documentation"
}
}
@app.get("/health")
async def health_check():
"""Health check endpoint to verify model is loaded and ready"""
if llm is None:
raise HTTPException(
status_code=503,
detail="Model not loaded. Please check server logs."
)
return {
"status": "healthy",
"model_loaded": True,
"model_path": MODEL_PATH
}
@app.post("/autocomplete", response_model=AutocompleteResponse)
async def autocomplete(request: AutocompleteRequest):
"""
Generate autocomplete suggestion for a given sentence using provided suggestions.
- **sentence**: The base sentence to autocomplete
- **suggestions**: List of allowed suggestions to use for autocompletion
"""
if llm is None:
raise HTTPException(
status_code=503,
detail="Model not loaded. Please restart the server."
)
if not request.sentence.strip():
raise HTTPException(
status_code=400,
detail="Sentence cannot be empty"
)
try:
# Generate autocomplete
completion = autocomplete_lightart(
base_sentence=request.sentence,
light_suggestions=request.suggestions
)
# Combine base sentence with completion
full_text = f"{request.sentence} {completion}".strip()
return AutocompleteResponse(
completion=completion,
full_text=full_text
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error generating autocomplete: {str(e)}"
)
# Refine function (longer response ~12 words)
def refine_lightart(base_sentence: str, light_suggestions: List[str]) -> str:
"""
Refines a sentence with a longer completion (~12 words) using ONLY the given suggestions.
"""
prompt = f"""
You are a REFINE assistant.
Use ONLY these light_suggestions:
{light_suggestions}
RULES:
- Continue the sentence EXACTLY from where it ends.
- Do NOT change the base sentence.
- ONLY use provided light_suggestions to refine.
- ONLY natural color/tone language.
- Generate approximately 12 words for the completion.
- ONLY refine using the light_suggestions.
Complete: {base_sentence}
"""
response = llm.create_chat_completion(
messages=[
{"role": "system", "content": "Refine using ONLY the allowed suggestions. Generate approximately 12 words."},
{"role": "user", "content": prompt},
],
max_tokens=50,
temperature=0.2,
top_p=0.9,
)
return response["choices"][0]["message"]["content"].strip()
@app.post("/refine", response_model=AutocompleteResponse)
async def refine(request: AutocompleteRequest):
"""
Generate a refined, longer completion (~12 words) for a given sentence using provided suggestions.
- **sentence**: The base sentence to refine
- **suggestions**: List of allowed suggestions to use for refinement
"""
if llm is None:
raise HTTPException(
status_code=503,
detail="Model not loaded. Please restart the server."
)
if not request.sentence.strip():
raise HTTPException(
status_code=400,
detail="Sentence cannot be empty"
)
try:
# Generate refined completion
completion = refine_lightart(
base_sentence=request.sentence,
light_suggestions=request.suggestions
)
# Combine base sentence with completion
full_text = f"{request.sentence} {completion}".strip()
return AutocompleteResponse(
completion=completion,
full_text=full_text
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error generating refinement: {str(e)}"
)
# Server startup
if __name__ == "__main__":
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=True,
log_level="info"
)