Spaces:
Sleeping
Sleeping
File size: 2,750 Bytes
9dab839 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
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>
"""
@app.route('/')
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
@app.route('/gradio/<path:path>')
def gradio_proxy(path):
demo = sketch_interface()
return demo.app.handle_request(request)
@app.route('/stream')
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) |