Spaces:
Runtime error
Runtime error
Update app/api/image_router.py
Browse files- app/api/image_router.py +83 -0
app/api/image_router.py
CHANGED
|
@@ -6,6 +6,7 @@ from app.schemas.image import ShoeGenerateRequest
|
|
| 6 |
from io import BytesIO
|
| 7 |
from app.services import image_service
|
| 8 |
from app.services.image_service import generate_shoe_images
|
|
|
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
|
@@ -197,3 +198,85 @@ async def generate_shoe(request: ShoeGenerateRequest):
|
|
| 197 |
except Exception as e:
|
| 198 |
logger.exception("Error in /generate-shoe: %s", e)
|
| 199 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
from app.services import image_service
|
| 8 |
from app.services.image_service import generate_shoe_images
|
| 9 |
+
from app.schemas.image import ShoeCheckResponse, UserInput, DressCheckResponse
|
| 10 |
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
logger.exception("Error in /generate-shoe: %s", e)
|
| 200 |
raise HTTPException(status_code=500, detail=str(e))
|
| 201 |
+
|
| 202 |
+
from app.core.config import settings
|
| 203 |
+
|
| 204 |
+
from groq import Groq
|
| 205 |
+
import json
|
| 206 |
+
|
| 207 |
+
client = Groq(api_key=settings.LANGCHAIN_API_KEY)
|
| 208 |
+
|
| 209 |
+
# --- Endpoint ---
|
| 210 |
+
@router.post("/check-shoe", response_model=ShoeCheckResponse)
|
| 211 |
+
async def check_shoe(user_input: UserInput):
|
| 212 |
+
"""
|
| 213 |
+
Check if the user input text is related to shoe images using Groq model.
|
| 214 |
+
Returns {"answer": "Yes"} or {"answer": "No"}.
|
| 215 |
+
"""
|
| 216 |
+
try:
|
| 217 |
+
# Define the JSON schema for response
|
| 218 |
+
response_format = {
|
| 219 |
+
"type": "json_schema",
|
| 220 |
+
"json_schema": {
|
| 221 |
+
"name": "shoe_check",
|
| 222 |
+
"schema": ShoeCheckResponse.model_json_schema()
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
# System + user prompt
|
| 227 |
+
messages = [
|
| 228 |
+
{"role": "system", "content": "You are a strict classifier that determines if text is about shoe images."},
|
| 229 |
+
{"role": "user", "content": user_input.text}
|
| 230 |
+
]
|
| 231 |
+
|
| 232 |
+
# Call Groq model
|
| 233 |
+
response = client.chat.completions.create(
|
| 234 |
+
model="moonshotai/kimi-k2-instruct-0905",
|
| 235 |
+
messages=messages,
|
| 236 |
+
response_format=response_format
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
# Parse model output
|
| 240 |
+
result = ShoeCheckResponse.model_validate(json.loads(response.choices[0].message.content))
|
| 241 |
+
return result
|
| 242 |
+
|
| 243 |
+
except Exception as e:
|
| 244 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
# --- Dress Check Endpoint ---
|
| 248 |
+
@router.post("/check-dress", response_model=DressCheckResponse)
|
| 249 |
+
async def check_dress(user_input: UserInput):
|
| 250 |
+
"""
|
| 251 |
+
Check if the user input text is related to dress images using Groq model.
|
| 252 |
+
Returns {"answer": "Yes"} or {"answer": "No"}.
|
| 253 |
+
"""
|
| 254 |
+
try:
|
| 255 |
+
response_format = {
|
| 256 |
+
"type": "json_schema",
|
| 257 |
+
"json_schema": {
|
| 258 |
+
"name": "dress_check",
|
| 259 |
+
"schema": DressCheckResponse.model_json_schema()
|
| 260 |
+
}
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
# System + user prompt
|
| 264 |
+
messages = [
|
| 265 |
+
{"role": "system", "content": "You are a strict classifier that determines if text is about dress images."},
|
| 266 |
+
{"role": "user", "content": user_input.text}
|
| 267 |
+
]
|
| 268 |
+
|
| 269 |
+
# Call Groq model
|
| 270 |
+
response = client.chat.completions.create(
|
| 271 |
+
model="moonshotai/kimi-k2-instruct-0905",
|
| 272 |
+
messages=messages,
|
| 273 |
+
response_format=response_format
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# Parse model output
|
| 277 |
+
result = DressCheckResponse.model_validate(json.loads(response.choices[0].message.content))
|
| 278 |
+
return result
|
| 279 |
+
|
| 280 |
+
except Exception as e:
|
| 281 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 282 |
+
|