hieunm commited on
Commit
70f8d8a
·
1 Parent(s): 34e7fc1

first working demo

Browse files
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ import time
3
+ from pathlib import Path
4
+ from sqlite3.dbapi2 import Connection
5
+
6
+ import gradio as gr
7
+ import uvicorn
8
+ from fastapi import FastAPI
9
+ from fastapi.staticfiles import StaticFiles
10
+ from PIL import Image, ImageDraw, ImageFont
11
+
12
+
13
+ # Function to fill the template, save to SQLite, and convert to image
14
+ def write_text_to_image(
15
+ name: str,
16
+ distance: float,
17
+ conn: Connection,
18
+ name_x: int = 410,
19
+ name_y: int = 410,
20
+ distance_x: int = 435,
21
+ distance_y: int = 450,
22
+ ) -> str:
23
+ # Save the input data to the SQLite database
24
+ cursor = conn.cursor()
25
+ cursor.execute(
26
+ "INSERT INTO user_inputs (name, distance) VALUES (?, ?)",
27
+ (name, distance),
28
+ )
29
+ conn.commit()
30
+
31
+ # Open the background image
32
+ background_image_path = f"{static_dir}/phocao_run_2024.jpg"
33
+ image = Image.open(background_image_path)
34
+ draw = ImageDraw.Draw(image)
35
+
36
+ # Set the font (adjust the font size and style)
37
+ font = ImageFont.truetype("font/RobotoSlab-Bold.ttf", 23)
38
+
39
+ # Define text colors
40
+ text_color = (0, 255, 0) # Green
41
+
42
+ # Write the name on the image at the specified coordinates
43
+ draw.text((name_x, name_y), name, font=font, fill=text_color)
44
+
45
+ # Write the distance on the image at the specified coordinates
46
+ draw.text((distance_x, distance_y), f"{distance} km", font=font, fill=text_color)
47
+
48
+ # Save the image with the text
49
+ timestamp = int(time.perf_counter_ns())
50
+ output_image_path = f"{static_dir}/{timestamp}.jpg"
51
+ image.save(output_image_path)
52
+
53
+ # Return the path to the generated image
54
+ return output_image_path
55
+
56
+
57
+ # Serve the app
58
+ if __name__ == "__main__":
59
+ # Create a FastAPI app
60
+ app = FastAPI()
61
+
62
+ # Create a static directory to store the static files
63
+ static_dir = Path("./static")
64
+ static_dir.mkdir(parents=True, exist_ok=True)
65
+
66
+ # Mount FastAPI StaticFiles server
67
+ app.mount("/static", StaticFiles(directory=static_dir), name="static")
68
+
69
+ # Connect to SQLite database
70
+ conn = sqlite3.connect("user_input.db", check_same_thread=False)
71
+ cursor = conn.cursor()
72
+
73
+ # Create a table if it doesn't already exist
74
+ cursor.execute(
75
+ """
76
+ CREATE TABLE IF NOT EXISTS user_inputs (
77
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
78
+ name TEXT,
79
+ distance FLOAT
80
+ )
81
+ """
82
+ )
83
+ conn.commit()
84
+
85
+ # Gradio Blocks interface
86
+ with gr.Blocks(title="PhoCao Run 2024 - Chứng Nhận Hoàn Thành", css="footer {visibility: hidden}") as demo:
87
+ gr.Markdown("## PhoCao Run 2024 - Chứng Nhận Hoàn Thành")
88
+ name_input = gr.Textbox(label="Tên/Nickname")
89
+ distance_input = gr.Number(label="Số km")
90
+
91
+ submit_button = gr.Button("Chạm Đích")
92
+
93
+ output_image = gr.Image(label="Kết quả")
94
+
95
+ # Event handler for the button click
96
+ def on_submit(name, distance):
97
+ image_path = write_text_to_image(name, distance, conn)
98
+ submit_button = gr.Button(visible=False)
99
+ return submit_button, image_path
100
+
101
+ submit_button.click(
102
+ on_submit,
103
+ inputs=[name_input, distance_input],
104
+ outputs=[submit_button, output_image],
105
+ scroll_to_output=True,
106
+ api_name=None,
107
+ max_batch_size=1,
108
+ show_api=False,
109
+ )
110
+
111
+ # Mount Gradio app to FastAPI app
112
+ demo.show_api = False
113
+ app = gr.mount_gradio_app(app, demo, path="/")
114
+
115
+ uvicorn.run(app, host="0.0.0.0", port=7860)
font/GoogleFont_README.txt ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Roboto Slab Variable Font
2
+ =========================
3
+
4
+ This download contains Roboto Slab as both a variable font and static fonts.
5
+
6
+ Roboto Slab is a variable font with this axis:
7
+ wght
8
+
9
+ This means all the styles are contained in a single file:
10
+ RobotoSlab-VariableFont_wght.ttf
11
+
12
+ If your app fully supports variable fonts, you can now pick intermediate styles
13
+ that aren’t available as static fonts. Not all apps support variable fonts, and
14
+ in those cases you can use the static font files for Roboto Slab:
15
+ static/RobotoSlab-Thin.ttf
16
+ static/RobotoSlab-ExtraLight.ttf
17
+ static/RobotoSlab-Light.ttf
18
+ static/RobotoSlab-Regular.ttf
19
+ static/RobotoSlab-Medium.ttf
20
+ static/RobotoSlab-SemiBold.ttf
21
+ static/RobotoSlab-Bold.ttf
22
+ static/RobotoSlab-ExtraBold.ttf
23
+ static/RobotoSlab-Black.ttf
24
+
25
+ Get started
26
+ -----------
27
+
28
+ 1. Install the font files you want to use
29
+
30
+ 2. Use your app's font picker to view the font family and all the
31
+ available styles
32
+
33
+ Learn more about variable fonts
34
+ -------------------------------
35
+
36
+ https://developers.google.com/web/fundamentals/design-and-ux/typography/variable-fonts
37
+ https://variablefonts.typenetwork.com
38
+ https://medium.com/variable-fonts
39
+
40
+ In desktop apps
41
+
42
+ https://theblog.adobe.com/can-variable-fonts-illustrator-cc
43
+ https://helpx.adobe.com/nz/photoshop/using/fonts.html#variable_fonts
44
+
45
+ Online
46
+
47
+ https://developers.google.com/fonts/docs/getting_started
48
+ https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Fonts/Variable_Fonts_Guide
49
+ https://developer.microsoft.com/en-us/microsoft-edge/testdrive/demos/variable-fonts
50
+
51
+ Installing fonts
52
+
53
+ MacOS: https://support.apple.com/en-us/HT201749
54
+ Linux: https://www.google.com/search?q=how+to+install+a+font+on+gnu%2Blinux
55
+ Windows: https://support.microsoft.com/en-us/help/314960/how-to-install-or-remove-a-font-in-windows
56
+
57
+ Android Apps
58
+
59
+ https://developers.google.com/fonts/docs/android
60
+ https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts
61
+
62
+ License
63
+ -------
64
+ Please read the full license text (LICENSE.txt) to understand the permissions,
65
+ restrictions and requirements for usage, redistribution, and modification.
66
+
67
+ You can use them in your products & projects – print or digital,
68
+ commercial or otherwise.
69
+
70
+ This isn't legal advice, please consider consulting a lawyer and see the full
71
+ license for all details.
font/RobotoSlab-Black.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-Bold.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-ExtraBold.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-ExtraLight.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-Light.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-Medium.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-Regular.ttf ADDED
Binary file (126 kB). View file
 
font/RobotoSlab-SemiBold.ttf ADDED
Binary file (127 kB). View file
 
font/RobotoSlab-Thin.ttf ADDED
Binary file (126 kB). View file
 
font/RobotoSlab-VariableFont_wght.ttf ADDED
Binary file (250 kB). View file
 
font/Sriracha-Regular.ttf ADDED
Binary file (310 kB). View file
 
font/l_10646.ttf ADDED
Binary file (310 kB). View file
 
note.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ - App generated from chatgpt.com
2
+ - https://www.gradio.app/guides/quickstart
3
+
4
+ Publish
5
+ - https://www.gradio.app/guides/sharing-your-app#hosting-on-hf-spaces
6
+ - https://huggingface.co/docs/hub/en/spaces-dependencies
static/phocao_run_2024.jpg ADDED
static/styles.css ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* styles.css */
2
+ .container {
3
+ position: relative;
4
+ text-align: center;
5
+ }
6
+
7
+ .background-image {
8
+ width: 100%;
9
+ height: auto;
10
+ }
11
+
12
+ .text {
13
+ position: absolute;
14
+ color: orange;
15
+ font-weight: bold;
16
+ font-size: 24px; /* Adjust as needed */
17
+ }
template.html ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <link rel="stylesheet" href="/static/styles.css" />
7
+ <title>Template with Background Image</title>
8
+ </head>
9
+ <body>
10
+ <div class="container">
11
+ <img
12
+ src="/static/phocao_run_2024.jpg"
13
+ alt="background"
14
+ class="background-image"
15
+ />
16
+ <div class="text" style="left: {{ name_x }}px; top: {{ name_y }}px;">
17
+ <h2>{{ name }}</h2>
18
+ </div>
19
+ <div class="text" style="left: {{ age_x }}px; top: {{ age_y }}px;">
20
+ <h2>{{ age }} years old</h2>
21
+ </div>
22
+ </div>
23
+ </body>
24
+ </html>