broadfield-dev commited on
Commit
d7bc9bf
·
verified ·
1 Parent(s): c5fcf5b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -86
app.py CHANGED
@@ -1,86 +1 @@
1
- from flask import Flask, render_template
2
- import gradio as gr
3
- import os
4
-
5
- app = Flask(__name__)
6
-
7
- # Gradio sketch interface
8
- def sketch_interface():
9
- # Simple sketching demo
10
- sketch = gr.Interface(
11
- lambda x: x, # Echo back the sketch
12
- gr.Sketchpad(),
13
- gr.Image(),
14
- live=True,
15
- title="Sketch Pad Demo"
16
- )
17
- return sketch
18
-
19
- # HTML template with Twitter Card metadata
20
- html_template = """
21
- <!DOCTYPE html>
22
- <html>
23
- <head>
24
- <meta charset="UTF-8">
25
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
26
- <!-- Twitter Card metadata -->
27
- <meta name="twitter:card" content="player">
28
- <meta name="twitter:site" content="@broadfield-dev">
29
- <meta name="twitter:title" content="Sketch Pad Demo">
30
- <meta name="twitter:description" content="Interactive sketching application">
31
- <meta name="twitter:image" content="https://your-domain.com/preview-image.jpg">
32
- <meta name="twitter:player" content="{{ iframe_url }}">
33
- <meta name="twitter:player:width" content="480">
34
- <meta name="twitter:player:height" content="480">
35
- <meta name="twitter:player:stream" content="https://your-domain.com/stream">
36
- <meta name="twitter:player:stream:content_type" content="video/mp4">
37
- <title>Sketch Pad Demo</title>
38
- </head>
39
- <body>
40
- <div id="gradio-container">
41
- {{ gradio_content | safe }}
42
- </div>
43
- </body>
44
- </html>
45
- """
46
-
47
- @app.route('/')
48
- def home():
49
- # Launch Gradio interface
50
- demo = sketch_interface()
51
-
52
- # Get the Gradio app's iframe URL when running on Hugging Face Spaces
53
- # For local testing, you might need to adjust this
54
- iframe_url = os.environ.get("SYSTEM_URL", "http://localhost:5000/gradio")
55
-
56
- # Launch Gradio and get its HTML content
57
- gradio_app = demo.launch(prevent_thread_lock=True, show_error=True)
58
-
59
- # For Hugging Face Spaces, we need to get the correct URL
60
- gradio_content = f'<iframe src="{iframe_url}" width="100%" height="500px"></iframe>'
61
-
62
- return render_template_string(
63
- html_template,
64
- iframe_url=iframe_url,
65
- gradio_content=gradio_content
66
- )
67
-
68
- # Route for Gradio to work within Flask
69
- @app.route('/gradio/<path:path>')
70
- def gradio_proxy(path):
71
- demo = sketch_interface()
72
- return demo.app.handle_request(request)
73
-
74
- @app.route('/stream')
75
- def stream():
76
- # This is a placeholder for the stream endpoint
77
- # You would need to implement actual streaming logic here
78
- return "Streaming endpoint placeholder"
79
-
80
- if __name__ == '__main__':
81
- # Install required packages
82
- os.system("pip install --upgrade gradio flask")
83
-
84
- # For Hugging Face Spaces, port 7860 is typically used
85
- port = int(os.environ.get("PORT", 7860))
86
- app.run(host='0.0.0.0', port=port, debug=True)
 
1
+ flask