Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,47 @@ import random
|
|
| 22 |
import time
|
| 23 |
from requests.adapters import HTTPAdapter
|
| 24 |
from urllib3.util.retry import Retry
|
| 25 |
-
from huggingface_hub import HfApi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# =============================================
|
| 28 |
# HUGGING FACE DATASET CONFIGURATION
|
|
@@ -34,6 +74,7 @@ DATASET_ID = f"{HF_USERNAME}/{DATASET_NAME}"
|
|
| 34 |
|
| 35 |
print(f"π¦ HF Dataset: {DATASET_ID}")
|
| 36 |
print(f"π HF Token: {'β
Set' if HF_TOKEN else 'β Missing'}")
|
|
|
|
| 37 |
|
| 38 |
# Create local directories for test images
|
| 39 |
PERSISTENT_IMAGE_DIR = "generated_test_images"
|
|
@@ -77,7 +118,7 @@ class StorybookRequest(BaseModel):
|
|
| 77 |
style: str = "childrens_book"
|
| 78 |
callback_url: Optional[str] = None
|
| 79 |
consistency_seed: Optional[int] = None
|
| 80 |
-
project_id: Optional[str] = None
|
| 81 |
|
| 82 |
class JobStatusResponse(BaseModel):
|
| 83 |
job_id: str
|
|
@@ -495,10 +536,6 @@ def refresh_local_images():
|
|
| 495 |
print(f"Error refreshing local images: {e}")
|
| 496 |
return []
|
| 497 |
|
| 498 |
-
# =============================================
|
| 499 |
-
# REMOVED: OCI BUCKET FUNCTIONS
|
| 500 |
-
# =============================================
|
| 501 |
-
|
| 502 |
# JOB MANAGEMENT FUNCTIONS
|
| 503 |
def create_job(story_request: StorybookRequest) -> str:
|
| 504 |
job_id = str(uuid.uuid4())
|
|
@@ -688,6 +725,17 @@ def generate_storybook_background(job_id: str):
|
|
| 688 |
}
|
| 689 |
}
|
| 690 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
status_message = f"π Storybook completed! {len(generated_pages)} pages created."
|
| 692 |
if hf_success_count > 0:
|
| 693 |
status_message += f" {hf_success_count} images uploaded to HF Dataset."
|
|
@@ -709,6 +757,16 @@ def generate_storybook_background(job_id: str):
|
|
| 709 |
error_msg = f"Story generation failed: {str(e)}"
|
| 710 |
print(f"β {error_msg}")
|
| 711 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
update_job_status(job_id, JobStatus.FAILED, 0, error_msg)
|
| 713 |
|
| 714 |
# FASTAPI ENDPOINTS
|
|
@@ -851,6 +909,7 @@ async def root():
|
|
| 851 |
return {
|
| 852 |
"message": "Storybook Generator API with HF Dataset",
|
| 853 |
"hf_dataset": DATASET_ID if HF_TOKEN else "Disabled",
|
|
|
|
| 854 |
"endpoints": {
|
| 855 |
"generate": "POST /api/generate-storybook",
|
| 856 |
"status": "GET /api/job-status/{job_id}",
|
|
@@ -866,6 +925,7 @@ if __name__ == "__main__":
|
|
| 866 |
if os.environ.get('SPACE_ID'):
|
| 867 |
print("π Running on Hugging Face Spaces")
|
| 868 |
print(f"π¦ HF Dataset: {DATASET_ID if HF_TOKEN else 'Disabled'}")
|
|
|
|
| 869 |
gr.mount_gradio_app(app, demo, path="/ui")
|
| 870 |
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|
| 871 |
else:
|
|
|
|
| 22 |
import time
|
| 23 |
from requests.adapters import HTTPAdapter
|
| 24 |
from urllib3.util.retry import Retry
|
| 25 |
+
from huggingface_hub import HfApi
|
| 26 |
+
import threading # ADDED for background reporting (already imported)
|
| 27 |
+
|
| 28 |
+
# =============================================
|
| 29 |
+
# ADDED: Tracker Configuration
|
| 30 |
+
# =============================================
|
| 31 |
+
TRACKER_URL = os.environ.get("TRACKER_URL", "https://yukee1992-status-tracker.hf.space")
|
| 32 |
+
|
| 33 |
+
# =============================================
|
| 34 |
+
# ADDED: Status Reporting Function
|
| 35 |
+
# =============================================
|
| 36 |
+
def report_to_tracker(project_id, service_type, status, file_urls=None, error=None):
|
| 37 |
+
"""Report generation status to central tracker"""
|
| 38 |
+
if not project_id: # Don't report if no project_id
|
| 39 |
+
return
|
| 40 |
+
|
| 41 |
+
def _report():
|
| 42 |
+
try:
|
| 43 |
+
payload = {
|
| 44 |
+
"project_id": project_id,
|
| 45 |
+
"service_type": service_type,
|
| 46 |
+
"status": status,
|
| 47 |
+
"file_urls": file_urls or []
|
| 48 |
+
}
|
| 49 |
+
if error:
|
| 50 |
+
payload["error"] = error
|
| 51 |
+
|
| 52 |
+
response = requests.post(
|
| 53 |
+
f"{TRACKER_URL}/update",
|
| 54 |
+
json=payload,
|
| 55 |
+
timeout=5
|
| 56 |
+
)
|
| 57 |
+
print(f"π€ Reported to tracker: {response.status_code}")
|
| 58 |
+
if response.status_code != 200:
|
| 59 |
+
print(f"β οΈ Tracker response: {response.text}")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"β οΈ Failed to report to tracker: {e}")
|
| 62 |
+
|
| 63 |
+
# Run in background thread to not block
|
| 64 |
+
thread = threading.Thread(target=_report, daemon=True)
|
| 65 |
+
thread.start()
|
| 66 |
|
| 67 |
# =============================================
|
| 68 |
# HUGGING FACE DATASET CONFIGURATION
|
|
|
|
| 74 |
|
| 75 |
print(f"π¦ HF Dataset: {DATASET_ID}")
|
| 76 |
print(f"π HF Token: {'β
Set' if HF_TOKEN else 'β Missing'}")
|
| 77 |
+
print(f"π‘ Tracker URL: {TRACKER_URL}") # ADDED
|
| 78 |
|
| 79 |
# Create local directories for test images
|
| 80 |
PERSISTENT_IMAGE_DIR = "generated_test_images"
|
|
|
|
| 118 |
style: str = "childrens_book"
|
| 119 |
callback_url: Optional[str] = None
|
| 120 |
consistency_seed: Optional[int] = None
|
| 121 |
+
project_id: Optional[str] = None
|
| 122 |
|
| 123 |
class JobStatusResponse(BaseModel):
|
| 124 |
job_id: str
|
|
|
|
| 536 |
print(f"Error refreshing local images: {e}")
|
| 537 |
return []
|
| 538 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
# JOB MANAGEMENT FUNCTIONS
|
| 540 |
def create_job(story_request: StorybookRequest) -> str:
|
| 541 |
job_id = str(uuid.uuid4())
|
|
|
|
| 725 |
}
|
| 726 |
}
|
| 727 |
|
| 728 |
+
# =============================================
|
| 729 |
+
# ADDED: Report to tracker when all images are done
|
| 730 |
+
# =============================================
|
| 731 |
+
if project_id and image_urls:
|
| 732 |
+
report_to_tracker(
|
| 733 |
+
project_id=project_id,
|
| 734 |
+
service_type="image",
|
| 735 |
+
status="completed",
|
| 736 |
+
file_urls=image_urls
|
| 737 |
+
)
|
| 738 |
+
|
| 739 |
status_message = f"π Storybook completed! {len(generated_pages)} pages created."
|
| 740 |
if hf_success_count > 0:
|
| 741 |
status_message += f" {hf_success_count} images uploaded to HF Dataset."
|
|
|
|
| 757 |
error_msg = f"Story generation failed: {str(e)}"
|
| 758 |
print(f"β {error_msg}")
|
| 759 |
traceback.print_exc()
|
| 760 |
+
|
| 761 |
+
# Report failure to tracker
|
| 762 |
+
if 'project_id' in locals() and project_id:
|
| 763 |
+
report_to_tracker(
|
| 764 |
+
project_id=project_id,
|
| 765 |
+
service_type="image",
|
| 766 |
+
status="failed",
|
| 767 |
+
error=error_msg
|
| 768 |
+
)
|
| 769 |
+
|
| 770 |
update_job_status(job_id, JobStatus.FAILED, 0, error_msg)
|
| 771 |
|
| 772 |
# FASTAPI ENDPOINTS
|
|
|
|
| 909 |
return {
|
| 910 |
"message": "Storybook Generator API with HF Dataset",
|
| 911 |
"hf_dataset": DATASET_ID if HF_TOKEN else "Disabled",
|
| 912 |
+
"tracker_url": TRACKER_URL, # ADDED for info
|
| 913 |
"endpoints": {
|
| 914 |
"generate": "POST /api/generate-storybook",
|
| 915 |
"status": "GET /api/job-status/{job_id}",
|
|
|
|
| 925 |
if os.environ.get('SPACE_ID'):
|
| 926 |
print("π Running on Hugging Face Spaces")
|
| 927 |
print(f"π¦ HF Dataset: {DATASET_ID if HF_TOKEN else 'Disabled'}")
|
| 928 |
+
print(f"π‘ Tracker URL: {TRACKER_URL}") # ADDED
|
| 929 |
gr.mount_gradio_app(app, demo, path="/ui")
|
| 930 |
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|
| 931 |
else:
|