Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import uuid
|
|
| 3 |
import requests
|
| 4 |
import threading
|
| 5 |
import time
|
| 6 |
-
from flask import Flask, request, jsonify, render_template
|
| 7 |
from werkzeug.utils import secure_filename
|
| 8 |
from itertools import cycle
|
| 9 |
from dotenv import load_dotenv
|
|
@@ -41,15 +41,12 @@ def get_permanent_link(job_id, temp_render_url, worker_url):
|
|
| 41 |
response.raise_for_status()
|
| 42 |
data = response.json()
|
| 43 |
|
| 44 |
-
|
| 45 |
-
if not
|
| 46 |
-
|
| 47 |
-
path_part = uploader_proxy_url.split('/proxy/')[1]
|
| 48 |
-
final_proxy_url = f"/proxy/{path_part}"
|
| 49 |
|
| 50 |
with lock:
|
| 51 |
jobs[job_id]["status"] = "completed"
|
| 52 |
-
jobs[job_id]["result"] =
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
print(f"Error making link permanent for {job_id}: {e}")
|
|
@@ -152,23 +149,5 @@ def check_status():
|
|
| 152 |
with lock: job = jobs.get(job_id, {"status": "not_found", "result": None})
|
| 153 |
return jsonify({"status": job["status"], "result": job["result"]})
|
| 154 |
|
| 155 |
-
@app.route('/proxy/<user>/<repo>/<path:file_path>')
|
| 156 |
-
def file_proxy(user, repo, file_path):
|
| 157 |
-
if not HF_TOKEN: return "HF_TOKEN not set", 500
|
| 158 |
-
repo_id = f"{user}/{repo}"
|
| 159 |
-
file_url = f"https://huggingface.co/datasets/{repo_id}/resolve/main/{file_path}"
|
| 160 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 161 |
-
try:
|
| 162 |
-
hf_response = requests.get(file_url, headers=headers, stream=True, timeout=30)
|
| 163 |
-
hf_response.raise_for_status()
|
| 164 |
-
return Response(hf_response.iter_content(chunk_size=8192),
|
| 165 |
-
content_type=hf_response.headers.get('Content-Type', 'application/octet-stream'))
|
| 166 |
-
except requests.exceptions.HTTPError as e:
|
| 167 |
-
if e.response.status_code == 404: return "File not found", 404
|
| 168 |
-
return f"Hugging Face Server Error: {e.response.status_code}", e.response.status_code
|
| 169 |
-
except Exception as e:
|
| 170 |
-
print(f"Proxy error: {e}")
|
| 171 |
-
return "Error fetching file", 500
|
| 172 |
-
|
| 173 |
if __name__ == '__main__':
|
| 174 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
|
| 3 |
import requests
|
| 4 |
import threading
|
| 5 |
import time
|
| 6 |
+
from flask import Flask, request, jsonify, render_template
|
| 7 |
from werkzeug.utils import secure_filename
|
| 8 |
from itertools import cycle
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
| 41 |
response.raise_for_status()
|
| 42 |
data = response.json()
|
| 43 |
|
| 44 |
+
final_url = data.get("hf_url") or data.get("url")
|
| 45 |
+
if not final_url: raise Exception("Invalid response from uploader service")
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with lock:
|
| 48 |
jobs[job_id]["status"] = "completed"
|
| 49 |
+
jobs[job_id]["result"] = final_url
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Error making link permanent for {job_id}: {e}")
|
|
|
|
| 149 |
with lock: job = jobs.get(job_id, {"status": "not_found", "result": None})
|
| 150 |
return jsonify({"status": job["status"], "result": job["result"]})
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
if __name__ == '__main__':
|
| 153 |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|