Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
from flask import Flask, render_template_string, jsonify
|
| 2 |
import requests
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
from sseclient import SSEClient
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
@@ -225,7 +226,7 @@ htmlTemplate = f"""
|
|
| 225 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/js/all.min.js" integrity="sha512-yFjZbTYRCJodnuyGlsKamNE/LlEaEA/3uWCGാരി7eIq7jWqVl3J8jL/kof/tfu9Xqzh/y/VM5sJd/tq5iEew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
| 226 |
|
| 227 |
<script>
|
| 228 |
-
const username = '
|
| 229 |
|
| 230 |
async function fetchInstances() {{
|
| 231 |
try {{
|
|
@@ -415,7 +416,8 @@ htmlTemplate = f"""
|
|
| 415 |
</html>
|
| 416 |
"""
|
| 417 |
|
| 418 |
-
|
|
|
|
| 419 |
|
| 420 |
|
| 421 |
def fetch_instances(username):
|
|
@@ -430,7 +432,8 @@ def fetch_instances(username):
|
|
| 430 |
|
| 431 |
@app.route('/')
|
| 432 |
def index():
|
| 433 |
-
|
|
|
|
| 434 |
|
| 435 |
@app.route('/instances')
|
| 436 |
def get_instances():
|
|
@@ -442,7 +445,7 @@ def get_instances():
|
|
| 442 |
def stream_metrics(username, instance_id):
|
| 443 |
# Use requests.get with stream=True for SSE
|
| 444 |
url = f"https://api.hf.space/v1/{username}/{instance_id}/live-metrics/sse"
|
| 445 |
-
|
| 446 |
def generate():
|
| 447 |
try:
|
| 448 |
response = requests.get(url, stream=True, headers={"Accept": "text/event-stream"})
|
|
|
|
| 1 |
+
from flask import Flask, render_template_string, jsonify, Response # Import Response
|
| 2 |
import requests
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
from sseclient import SSEClient
|
| 6 |
+
import os # Import the 'os' module
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
|
|
| 226 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/js/all.min.js" integrity="sha512-yFjZbTYRCJodnuyGlsKamNE/LlEaEA/3uWCGാരി7eIq7jWqVl3J8jL/kof/tfu9Xqzh/y/VM5sJd/tq5iEew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
|
| 227 |
|
| 228 |
<script>
|
| 229 |
+
const username = '{{username}}'; // Get username from the template
|
| 230 |
|
| 231 |
async function fetchInstances() {{
|
| 232 |
try {{
|
|
|
|
| 416 |
</html>
|
| 417 |
"""
|
| 418 |
|
| 419 |
+
# Get the username from environment variable (Hugging Face Space Secret)
|
| 420 |
+
USERNAME = os.environ.get("USERNAME", "yangtb24") # Default to "yangtb24" if not set
|
| 421 |
|
| 422 |
|
| 423 |
def fetch_instances(username):
|
|
|
|
| 432 |
|
| 433 |
@app.route('/')
|
| 434 |
def index():
|
| 435 |
+
# Pass the username to the template
|
| 436 |
+
return render_template_string(htmlTemplate, username=USERNAME)
|
| 437 |
|
| 438 |
@app.route('/instances')
|
| 439 |
def get_instances():
|
|
|
|
| 445 |
def stream_metrics(username, instance_id):
|
| 446 |
# Use requests.get with stream=True for SSE
|
| 447 |
url = f"https://api.hf.space/v1/{username}/{instance_id}/live-metrics/sse"
|
| 448 |
+
|
| 449 |
def generate():
|
| 450 |
try:
|
| 451 |
response = requests.get(url, stream=True, headers={"Accept": "text/event-stream"})
|