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 = """
Sketch Pad Demo
{{ gradio_content | safe }}
"""
@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''
return render_template_string(
html_template,
iframe_url=iframe_url,
gradio_content=gradio_content
)
# Route for Gradio to work within Flask
@app.route('/gradio/')
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)