Spaces:
Running
Running
update
Browse files- backend_api.py +27 -1
backend_api.py
CHANGED
|
@@ -239,6 +239,7 @@ class CodeGenerationResponse(BaseModel):
|
|
| 239 |
class ImportRequest(BaseModel):
|
| 240 |
url: str
|
| 241 |
prefer_local: bool = False
|
|
|
|
| 242 |
|
| 243 |
|
| 244 |
class ImportResponse(BaseModel):
|
|
@@ -248,6 +249,8 @@ class ImportResponse(BaseModel):
|
|
| 248 |
language: str
|
| 249 |
url: str
|
| 250 |
metadata: Dict
|
|
|
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
class PullRequestRequest(BaseModel):
|
|
@@ -1554,6 +1557,27 @@ async def import_project(request: ImportRequest):
|
|
| 1554 |
result['metadata']['code_type'] = 'local'
|
| 1555 |
result['message'] = result['message'].replace('inference', 'local')
|
| 1556 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1557 |
return ImportResponse(**result)
|
| 1558 |
|
| 1559 |
except Exception as e:
|
|
@@ -1563,7 +1587,9 @@ async def import_project(request: ImportRequest):
|
|
| 1563 |
code="",
|
| 1564 |
language="unknown",
|
| 1565 |
url=request.url,
|
| 1566 |
-
metadata={}
|
|
|
|
|
|
|
| 1567 |
)
|
| 1568 |
|
| 1569 |
|
|
|
|
| 239 |
class ImportRequest(BaseModel):
|
| 240 |
url: str
|
| 241 |
prefer_local: bool = False
|
| 242 |
+
username: Optional[str] = None # Username of authenticated user for ownership check
|
| 243 |
|
| 244 |
|
| 245 |
class ImportResponse(BaseModel):
|
|
|
|
| 249 |
language: str
|
| 250 |
url: str
|
| 251 |
metadata: Dict
|
| 252 |
+
owned_by_user: bool = False # True if user owns the imported repo
|
| 253 |
+
repo_id: Optional[str] = None # The repo ID (username/repo-name) if applicable
|
| 254 |
|
| 255 |
|
| 256 |
class PullRequestRequest(BaseModel):
|
|
|
|
| 1557 |
result['metadata']['code_type'] = 'local'
|
| 1558 |
result['message'] = result['message'].replace('inference', 'local')
|
| 1559 |
|
| 1560 |
+
# Check if user owns this repo (for HuggingFace Spaces)
|
| 1561 |
+
owned_by_user = False
|
| 1562 |
+
repo_id = None
|
| 1563 |
+
|
| 1564 |
+
if request.username and result['status'] == 'success':
|
| 1565 |
+
# Extract repo_id from URL
|
| 1566 |
+
url = result.get('url', '')
|
| 1567 |
+
if 'huggingface.co/spaces/' in url:
|
| 1568 |
+
# Extract username/repo from URL
|
| 1569 |
+
match = re.search(r'huggingface\.co/spaces/([^/]+/[^/?#]+)', url)
|
| 1570 |
+
if match:
|
| 1571 |
+
repo_id = match.group(1)
|
| 1572 |
+
# Check if user owns this space
|
| 1573 |
+
if repo_id.startswith(f"{request.username}/"):
|
| 1574 |
+
owned_by_user = True
|
| 1575 |
+
print(f"[Import] User {request.username} owns the imported space: {repo_id}")
|
| 1576 |
+
|
| 1577 |
+
# Add ownership info to response
|
| 1578 |
+
result['owned_by_user'] = owned_by_user
|
| 1579 |
+
result['repo_id'] = repo_id
|
| 1580 |
+
|
| 1581 |
return ImportResponse(**result)
|
| 1582 |
|
| 1583 |
except Exception as e:
|
|
|
|
| 1587 |
code="",
|
| 1588 |
language="unknown",
|
| 1589 |
url=request.url,
|
| 1590 |
+
metadata={},
|
| 1591 |
+
owned_by_user=False,
|
| 1592 |
+
repo_id=None
|
| 1593 |
)
|
| 1594 |
|
| 1595 |
|