Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,8 +26,13 @@ logger = logging.getLogger(__name__)
|
|
| 26 |
# Create FastAPI app
|
| 27 |
fastapi_app = FastAPI()
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Pydantic models for API
|
| 33 |
class TrainingData(BaseModel):
|
|
@@ -149,7 +154,7 @@ def train_model(training_request: Dict[str, Any]) -> Dict[str, Any]:
|
|
| 149 |
total_loss = epoch_loss
|
| 150 |
total_accuracy = epoch_accuracy
|
| 151 |
|
| 152 |
-
# FIXED: Use HF Hub API
|
| 153 |
model_filename = f"model_{session_id}.pth"
|
| 154 |
temp_model_path = f"/tmp/{model_filename}"
|
| 155 |
|
|
@@ -157,19 +162,24 @@ def train_model(training_request: Dict[str, Any]) -> Dict[str, Any]:
|
|
| 157 |
torch.save(model.state_dict(), temp_model_path)
|
| 158 |
logger.info(f"Model saved to temp path: {temp_model_path}")
|
| 159 |
|
| 160 |
-
# Upload to HF Hub using API
|
| 161 |
try:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
except Exception as upload_error:
|
| 171 |
logger.error(f"Failed to upload model to HF Hub: {upload_error}")
|
| 172 |
-
#
|
| 173 |
|
| 174 |
# Clean up temp file
|
| 175 |
try:
|
|
|
|
| 26 |
# Create FastAPI app
|
| 27 |
fastapi_app = FastAPI()
|
| 28 |
|
| 29 |
+
# Set up authentication - the Space needs a token to upload to itself
|
| 30 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Repository secret
|
| 31 |
+
if not HF_TOKEN:
|
| 32 |
+
logger.warning("No HF_TOKEN found - model uploads may fail")
|
| 33 |
+
|
| 34 |
+
# Initialize HF API with token
|
| 35 |
+
hf_api = HfApi(token=HF_TOKEN) if HF_TOKEN else HfApi()
|
| 36 |
|
| 37 |
# Pydantic models for API
|
| 38 |
class TrainingData(BaseModel):
|
|
|
|
| 154 |
total_loss = epoch_loss
|
| 155 |
total_accuracy = epoch_accuracy
|
| 156 |
|
| 157 |
+
# FIXED: Use HF Hub API with proper authentication
|
| 158 |
model_filename = f"model_{session_id}.pth"
|
| 159 |
temp_model_path = f"/tmp/{model_filename}"
|
| 160 |
|
|
|
|
| 162 |
torch.save(model.state_dict(), temp_model_path)
|
| 163 |
logger.info(f"Model saved to temp path: {temp_model_path}")
|
| 164 |
|
| 165 |
+
# Upload to HF Hub using API with authentication
|
| 166 |
try:
|
| 167 |
+
if HF_TOKEN:
|
| 168 |
+
hf_api.upload_file(
|
| 169 |
+
path_or_fileobj=temp_model_path,
|
| 170 |
+
path_in_repo=model_filename,
|
| 171 |
+
repo_id="rysatech/rysa-ocr-trainer",
|
| 172 |
+
repo_type="space",
|
| 173 |
+
commit_message=f"Add trained model {session_id}",
|
| 174 |
+
token=HF_TOKEN # Explicit token
|
| 175 |
+
)
|
| 176 |
+
logger.info(f"Model uploaded to HF Hub: {model_filename}")
|
| 177 |
+
else:
|
| 178 |
+
logger.error("No HF_TOKEN available for upload")
|
| 179 |
+
raise Exception("Authentication required for model upload")
|
| 180 |
except Exception as upload_error:
|
| 181 |
logger.error(f"Failed to upload model to HF Hub: {upload_error}")
|
| 182 |
+
raise upload_error # Don't continue if upload fails
|
| 183 |
|
| 184 |
# Clean up temp file
|
| 185 |
try:
|