arj7192 commited on
Commit
3e8e452
·
verified ·
1 Parent(s): 83d31ee

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -14
app.py CHANGED
@@ -23,37 +23,51 @@ import huggingface_hub
23
  huggingface_hub.login(os.getenv('HF_TOKEN_FLUX'))
24
 
25
 
26
- def download_from_github(repo, filepath, destination, token):
27
  """
28
- Download a file from a private GitHub repository.
29
-
30
  Args:
31
  repo (str): The GitHub repository in the format "owner/repo".
32
- filepath (str): The path to the file in the repository.
 
33
  destination (str): The local file path to save the downloaded file.
34
  token (str): The GitHub Personal Access Token.
35
-
36
  Returns:
37
  None
38
  """
39
- url = f"https://api.github.com/repos/{repo}/contents/{filepath}"
 
40
  headers = {"Authorization": f"token {token}"}
41
  response = requests.get(url, headers=headers)
42
-
43
  if response.status_code == 200:
44
- file_content = response.json().get('content')
45
- if file_content:
 
 
 
 
 
 
 
 
 
46
  os.makedirs(os.path.dirname(destination), exist_ok=True)
47
  with open(destination, 'wb') as f:
48
- f.write(base64.b64decode(file_content))
49
- print(f"Downloaded {filepath} to {destination}")
 
50
  else:
51
- raise Exception(f"File content not found for {filepath} in {repo}")
52
  else:
53
- raise Exception(f"Failed to download {filepath} from {repo}. Status code: {response.status_code}")
 
54
 
55
  # Example: Define GitHub repo and file paths
56
  repo_name = "arj7192/NativDemoLocal"
 
57
  github_files = {
58
  "speech_bubble_model.pt": "speech_bubble_model.pt",
59
  "craft_mlt_25k.pth": "craft_mlt_25k.pth",
@@ -68,7 +82,7 @@ token = os.getenv("HF_GITHUB_TOKEN")
68
  # Download each file
69
  for filename, repo_path in github_files.items():
70
  destination_path = f"{filename}"
71
- download_from_github(repo_name, repo_path, destination_path, token)
72
 
73
  bubble_detection_model = YOLO("speech_bubble_model.pt")
74
 
 
23
  huggingface_hub.login(os.getenv('HF_TOKEN_FLUX'))
24
 
25
 
26
+ def download_release_asset(repo, release_tag, asset_name, destination, token):
27
  """
28
+ Download a file from a GitHub Release.
29
+
30
  Args:
31
  repo (str): The GitHub repository in the format "owner/repo".
32
+ release_tag (str): The release tag (e.g., "v1.0.0").
33
+ asset_name (str): The name of the file in the release.
34
  destination (str): The local file path to save the downloaded file.
35
  token (str): The GitHub Personal Access Token.
36
+
37
  Returns:
38
  None
39
  """
40
+ # Fetch release assets
41
+ url = f"https://api.github.com/repos/{repo}/releases/tags/{release_tag}"
42
  headers = {"Authorization": f"token {token}"}
43
  response = requests.get(url, headers=headers)
44
+
45
  if response.status_code == 200:
46
+ release_data = response.json()
47
+ # Find the asset by name
48
+ asset = next((a for a in release_data['assets'] if a['name'] == asset_name), None)
49
+ if not asset:
50
+ raise Exception(f"Asset {asset_name} not found in release {release_tag}.")
51
+
52
+ # Download the asset
53
+ download_url = asset['browser_download_url']
54
+ download_response = requests.get(download_url, headers=headers, stream=True)
55
+
56
+ if download_response.status_code == 200:
57
  os.makedirs(os.path.dirname(destination), exist_ok=True)
58
  with open(destination, 'wb') as f:
59
+ for chunk in download_response.iter_content(chunk_size=1024):
60
+ f.write(chunk)
61
+ print(f"Downloaded {asset_name} to {destination}")
62
  else:
63
+ raise Exception(f"Failed to download {asset_name}. Status code: {download_response.status_code}")
64
  else:
65
+ raise Exception(f"Failed to fetch release {release_tag}. Status code: {response.status_code}")
66
+
67
 
68
  # Example: Define GitHub repo and file paths
69
  repo_name = "arj7192/NativDemoLocal"
70
+ release_tag = "v1"
71
  github_files = {
72
  "speech_bubble_model.pt": "speech_bubble_model.pt",
73
  "craft_mlt_25k.pth": "craft_mlt_25k.pth",
 
82
  # Download each file
83
  for filename, repo_path in github_files.items():
84
  destination_path = f"{filename}"
85
+ download_release_asset(repo_name, release_tag, filename, destination_path, token)
86
 
87
  bubble_detection_model = YOLO("speech_bubble_model.pt")
88