Spaces:
Sleeping
Sleeping
from flask import Flask, render_template | |
import gradio as gr | |
import os | |
app = Flask(__name__) | |
# Gradio sketch interface | |
def sketch_interface(): | |
# Simple sketching demo | |
sketch = gr.Interface( | |
lambda x: x, # Echo back the sketch | |
gr.Sketchpad(), | |
gr.Image(), | |
live=True, | |
title="Sketch Pad Demo" | |
) | |
return sketch | |
# HTML template with Twitter Card metadata | |
html_template = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<!-- Twitter Card metadata --> | |
<meta name="twitter:card" content="player"> | |
<meta name="twitter:site" content="@broadfield-dev"> | |
<meta name="twitter:title" content="Sketch Pad Demo"> | |
<meta name="twitter:description" content="Interactive sketching application"> | |
<meta name="twitter:image" content="https://your-domain.com/preview-image.jpg"> | |
<meta name="twitter:player" content="{{ iframe_url }}"> | |
<meta name="twitter:player:width" content="480"> | |
<meta name="twitter:player:height" content="480"> | |
<meta name="twitter:player:stream" content="https://your-domain.com/stream"> | |
<meta name="twitter:player:stream:content_type" content="video/mp4"> | |
<title>Sketch Pad Demo</title> | |
</head> | |
<body> | |
<div id="gradio-container"> | |
{{ gradio_content | safe }} | |
</div> | |
</body> | |
</html> | |
""" | |
def home(): | |
# Launch Gradio interface | |
demo = sketch_interface() | |
# Get the Gradio app's iframe URL when running on Hugging Face Spaces | |
# For local testing, you might need to adjust this | |
iframe_url = os.environ.get("SYSTEM_URL", "http://localhost:5000/gradio") | |
# Launch Gradio and get its HTML content | |
gradio_app = demo.launch(prevent_thread_lock=True, show_error=True) | |
# For Hugging Face Spaces, we need to get the correct URL | |
gradio_content = f'<iframe src="{iframe_url}" width="100%" height="500px"></iframe>' | |
return render_template_string( | |
html_template, | |
iframe_url=iframe_url, | |
gradio_content=gradio_content | |
) | |
# Route for Gradio to work within Flask | |
def gradio_proxy(path): | |
demo = sketch_interface() | |
return demo.app.handle_request(request) | |
def stream(): | |
# This is a placeholder for the stream endpoint | |
# You would need to implement actual streaming logic here | |
return "Streaming endpoint placeholder" | |
if __name__ == '__main__': | |
# Install required packages | |
os.system("pip install --upgrade gradio flask") | |
# For Hugging Face Spaces, port 7860 is typically used | |
port = int(os.environ.get("PORT", 7860)) | |
app.run(host='0.0.0.0', port=port, debug=True) |