Update app.py
Browse files
app.py
CHANGED
|
@@ -474,5 +474,65 @@ htmlTemplate = f"""
|
|
| 474 |
</html>
|
| 475 |
"""
|
| 476 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
if __name__ == '__main__':
|
| 478 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
|
|
| 474 |
</html>
|
| 475 |
"""
|
| 476 |
|
| 477 |
+
USERNAME = os.environ.get("USERNAME", "yangtb24") # Default username
|
| 478 |
+
|
| 479 |
+
def fetch_instances(username):
|
| 480 |
+
try:
|
| 481 |
+
response = requests.get(f"https://huggingface.co/api/spaces?author={username}")
|
| 482 |
+
response.raise_for_status()
|
| 483 |
+
user_instances = response.json()
|
| 484 |
+
return [{"id": instance["id"].split('/')[1], "owner": username} for instance in user_instances]
|
| 485 |
+
except requests.exceptions.RequestException as e:
|
| 486 |
+
print(f"Error fetching instances: {e}")
|
| 487 |
+
return []
|
| 488 |
+
|
| 489 |
+
@app.route('/')
|
| 490 |
+
def index():
|
| 491 |
+
return render_template_string(htmlTemplate, username=USERNAME)
|
| 492 |
+
|
| 493 |
+
@app.route('/instances')
|
| 494 |
+
def get_instances():
|
| 495 |
+
instances = fetch_instances(USERNAME)
|
| 496 |
+
return jsonify(instances)
|
| 497 |
+
|
| 498 |
+
@app.route('/metrics/<username>/<instance_id>')
|
| 499 |
+
def stream_metrics(username, instance_id):
|
| 500 |
+
url = f"https://api.hf.space/v1/{username}/{instance_id}/live-metrics/sse"
|
| 501 |
+
|
| 502 |
+
def generate():
|
| 503 |
+
try:
|
| 504 |
+
response = requests.get(url, stream=True, headers={"Accept": "text/event-stream"}, timeout=15)
|
| 505 |
+
response.raise_for_status()
|
| 506 |
+
|
| 507 |
+
buffer = ""
|
| 508 |
+
for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
|
| 509 |
+
if chunk:
|
| 510 |
+
buffer += chunk
|
| 511 |
+
while "\n\n" in buffer:
|
| 512 |
+
event_data, buffer = buffer.split("\n\n", 1)
|
| 513 |
+
lines = event_data.split("\n")
|
| 514 |
+
event_type = "message"
|
| 515 |
+
data_lines = []
|
| 516 |
+
for line in lines:
|
| 517 |
+
if line.startswith("event:"):
|
| 518 |
+
event_type = line.split(":", 1)[1].strip()
|
| 519 |
+
elif line.startswith("data:"):
|
| 520 |
+
data_lines.append(line.split(":", 1)[1].strip())
|
| 521 |
+
|
| 522 |
+
if event_type == "metric":
|
| 523 |
+
# Correctly yield the JSON data:
|
| 524 |
+
yield f"event: {event_type}\ndata: {json.dumps(json.loads(''.join(data_lines)))}\n\n"
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
except requests.exceptions.RequestException as e:
|
| 528 |
+
print(f"Request Exception: {e}")
|
| 529 |
+
yield f"event: error\ndata: Connection error: {e}\\n\\n"
|
| 530 |
+
except Exception as e:
|
| 531 |
+
print(f"An error occurred: {e}")
|
| 532 |
+
yield f"event: error\ndata: An error occurred: {e}\\n\\n"
|
| 533 |
+
|
| 534 |
+
return Response(generate(), mimetype='text/event-stream')
|
| 535 |
+
|
| 536 |
if __name__ == '__main__':
|
| 537 |
app.run(debug=True, host='0.0.0.0', port=7860)
|
| 538 |
+
|