Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
from transformers import
|
| 4 |
import logging
|
| 5 |
import os
|
| 6 |
|
|
@@ -28,26 +28,52 @@ async def predict(request: PredictionRequest):
|
|
| 28 |
logger.info(f"Loading model: {request.model}")
|
| 29 |
model_path = MODELS[request.model]
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
full_input = "Interpret this dream: " + request.inputs
|
| 35 |
logger.info(f"Processing input: {full_input}")
|
| 36 |
|
|
|
|
| 37 |
inputs = tokenizer(
|
| 38 |
full_input,
|
| 39 |
return_tensors="pt",
|
| 40 |
truncation=True,
|
| 41 |
-
max_length=512
|
|
|
|
| 42 |
)
|
|
|
|
| 43 |
|
|
|
|
| 44 |
outputs = model.generate(**inputs, max_length=200)
|
|
|
|
|
|
|
| 45 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 46 |
|
| 47 |
return PredictionResponse(generated_text=result)
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
logger.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
|
| 53 |
@app.get("/health")
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration # Changed to specific classes
|
| 4 |
import logging
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 28 |
logger.info(f"Loading model: {request.model}")
|
| 29 |
model_path = MODELS[request.model]
|
| 30 |
|
| 31 |
+
# Add debug logging
|
| 32 |
+
logger.info("Attempting to load tokenizer...")
|
| 33 |
+
tokenizer = T5Tokenizer.from_pretrained(
|
| 34 |
+
model_path,
|
| 35 |
+
token=HF_TOKEN,
|
| 36 |
+
local_files_only=False, # Force download if needed
|
| 37 |
+
return_special_tokens_mask=True
|
| 38 |
+
)
|
| 39 |
+
logger.info("Tokenizer loaded successfully")
|
| 40 |
+
|
| 41 |
+
logger.info("Attempting to load model...")
|
| 42 |
+
model = T5ForConditionalGeneration.from_pretrained(
|
| 43 |
+
model_path,
|
| 44 |
+
token=HF_TOKEN,
|
| 45 |
+
local_files_only=False # Force download if needed
|
| 46 |
+
)
|
| 47 |
+
logger.info("Model loaded successfully")
|
| 48 |
|
| 49 |
full_input = "Interpret this dream: " + request.inputs
|
| 50 |
logger.info(f"Processing input: {full_input}")
|
| 51 |
|
| 52 |
+
logger.info("Tokenizing input...")
|
| 53 |
inputs = tokenizer(
|
| 54 |
full_input,
|
| 55 |
return_tensors="pt",
|
| 56 |
truncation=True,
|
| 57 |
+
max_length=512,
|
| 58 |
+
padding=True
|
| 59 |
)
|
| 60 |
+
logger.info("Input tokenized successfully")
|
| 61 |
|
| 62 |
+
logger.info("Generating output...")
|
| 63 |
outputs = model.generate(**inputs, max_length=200)
|
| 64 |
+
logger.info("Output generated successfully")
|
| 65 |
+
|
| 66 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
+
logger.info(f"Final result: {result}")
|
| 68 |
|
| 69 |
return PredictionResponse(generated_text=result)
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
logger.error(f"Error: {str(e)}")
|
| 73 |
+
logger.error(f"Error type: {type(e)}")
|
| 74 |
+
# Log the full traceback
|
| 75 |
+
import traceback
|
| 76 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 77 |
raise HTTPException(status_code=500, detail=str(e))
|
| 78 |
|
| 79 |
@app.get("/health")
|