Spaces:
Running
Running
Victoria Oberascher
commited on
Commit
·
002d3c8
1
Parent(s):
99065cf
add flask server to render images
Browse files- README.md +1 -1
- app.py +46 -6
- requirements.txt +2 -1
README.md
CHANGED
|
@@ -286,7 +286,7 @@ fig = module.generate_confidence_curves(results, confidence_config)
|
|
| 286 |
fig.show()
|
| 287 |
```
|
| 288 |
|
| 289 |
-

|
| 290 |
|
| 291 |
|
| 292 |
## Further References
|
app.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import sys
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def read_markdown(filepath):
|
|
@@ -16,17 +21,52 @@ def read_markdown(filepath):
|
|
| 16 |
end_index = i
|
| 17 |
break
|
| 18 |
if end_index is None:
|
| 19 |
-
return ''.join(
|
| 20 |
-
lines) # If no end delimiter found, return entire content
|
| 21 |
else:
|
| 22 |
-
return ''.join(lines[end_index +
|
| 23 |
-
1:]) # Return content after end delimiter
|
| 24 |
-
|
| 25 |
|
| 26 |
local_path = Path(sys.path[0])
|
| 27 |
filepath = local_path / "README.md"
|
| 28 |
markdown_content = read_markdown(filepath)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
with gr.Blocks() as demo:
|
| 31 |
gr.Markdown(markdown_content)
|
| 32 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
import sys
|
| 4 |
+
import threading
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
import gradio as gr
|
| 8 |
+
from flask import Flask, abort, send_from_directory
|
| 9 |
|
| 10 |
|
| 11 |
def read_markdown(filepath):
|
|
|
|
| 21 |
end_index = i
|
| 22 |
break
|
| 23 |
if end_index is None:
|
| 24 |
+
return ''.join(lines) # If no end delimiter found, return entire content
|
|
|
|
| 25 |
else:
|
| 26 |
+
return ''.join(lines[end_index + 1:]) # Return content after end delimiter
|
|
|
|
|
|
|
| 27 |
|
| 28 |
local_path = Path(sys.path[0])
|
| 29 |
filepath = local_path / "README.md"
|
| 30 |
markdown_content = read_markdown(filepath)
|
| 31 |
|
| 32 |
+
# Debug: Print the markdown content to check if it includes the image syntax
|
| 33 |
+
print("Markdown Content:")
|
| 34 |
+
print(markdown_content)
|
| 35 |
+
|
| 36 |
+
app = Flask(__name__)
|
| 37 |
+
|
| 38 |
+
@app.route('/assets/<path:filename>')
|
| 39 |
+
def serve_assets(filename):
|
| 40 |
+
# Safeguard against path traversal attacks and invalid paths
|
| 41 |
+
safe_path = os.path.normpath(filename)
|
| 42 |
+
if safe_path.startswith("..") or os.path.isabs(safe_path):
|
| 43 |
+
print(f"Invalid path: {filename}")
|
| 44 |
+
abort(400) # Bad request for invalid paths
|
| 45 |
+
|
| 46 |
+
asset_path = local_path / 'assets' / safe_path
|
| 47 |
+
if not asset_path.is_file():
|
| 48 |
+
print(f"File not found: {asset_path}")
|
| 49 |
+
abort(404) # File not found
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
return send_from_directory(local_path / 'assets', safe_path)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error serving file: {e}")
|
| 55 |
+
abort(403) # Forbidden
|
| 56 |
+
|
| 57 |
+
def run_flask():
|
| 58 |
+
# Use '0.0.0.0' to ensure the server is accessible from any network interface
|
| 59 |
+
app.run(port=7861, host='0.0.0.0')
|
| 60 |
+
|
| 61 |
+
flask_thread = threading.Thread(target=run_flask)
|
| 62 |
+
flask_thread.start()
|
| 63 |
+
|
| 64 |
+
# Replace this with your Hugging Face Space's external URL
|
| 65 |
+
external_url = "https://sea-ai-det-metrics.hf.space/" # Update this to match your space's URL
|
| 66 |
+
|
| 67 |
+
# Update the image paths in the markdown to use the external Flask server URL
|
| 68 |
+
markdown_content = re.sub(r'assets/([^)\s]+)', rf'{external_url}/assets/\1', markdown_content)
|
| 69 |
+
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown(markdown_content)
|
| 72 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
git+https://github.com/huggingface/evaluate@main
|
| 2 |
git+https://github.com/SEA-AI/seametrics@develop
|
| 3 |
fiftyone
|
| 4 |
-
plotly
|
|
|
|
|
|
| 1 |
git+https://github.com/huggingface/evaluate@main
|
| 2 |
git+https://github.com/SEA-AI/seametrics@develop
|
| 3 |
fiftyone
|
| 4 |
+
plotly
|
| 5 |
+
flask
|