broadfield-dev commited on
Commit
7d573e7
·
verified ·
1 Parent(s): c9e2350

Update app.py

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