File size: 3,468 Bytes
70f8d8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sqlite3
import time
from pathlib import Path
from sqlite3.dbapi2 import Connection

import gradio as gr
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from PIL import Image, ImageDraw, ImageFont


# Function to fill the template, save to SQLite, and convert to image
def write_text_to_image(
    name: str,
    distance: float,
    conn: Connection,
    name_x: int = 410,
    name_y: int = 410,
    distance_x: int = 435,
    distance_y: int = 450,
) -> str:
    # Save the input data to the SQLite database
    cursor = conn.cursor()
    cursor.execute(
        "INSERT INTO user_inputs (name, distance) VALUES (?, ?)",
        (name, distance),
    )
    conn.commit()

    # Open the background image
    background_image_path = f"{static_dir}/phocao_run_2024.jpg"
    image = Image.open(background_image_path)
    draw = ImageDraw.Draw(image)

    # Set the font (adjust the font size and style)
    font = ImageFont.truetype("font/RobotoSlab-Bold.ttf", 23)

    # Define text colors
    text_color = (0, 255, 0)  # Green

    # Write the name on the image at the specified coordinates
    draw.text((name_x, name_y), name, font=font, fill=text_color)

    # Write the distance on the image at the specified coordinates
    draw.text((distance_x, distance_y), f"{distance} km", font=font, fill=text_color)

    # Save the image with the text
    timestamp = int(time.perf_counter_ns())
    output_image_path = f"{static_dir}/{timestamp}.jpg"
    image.save(output_image_path)

    # Return the path to the generated image
    return output_image_path


# Serve the app
if __name__ == "__main__":
    # Create a FastAPI app
    app = FastAPI()

    # Create a static directory to store the static files
    static_dir = Path("./static")
    static_dir.mkdir(parents=True, exist_ok=True)

    # Mount FastAPI StaticFiles server
    app.mount("/static", StaticFiles(directory=static_dir), name="static")

    # Connect to SQLite database
    conn = sqlite3.connect("user_input.db", check_same_thread=False)
    cursor = conn.cursor()

    # Create a table if it doesn't already exist
    cursor.execute(
        """
    CREATE TABLE IF NOT EXISTS user_inputs (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT,
        distance FLOAT
    )
    """
    )
    conn.commit()

    # Gradio Blocks interface
    with gr.Blocks(title="PhoCao Run 2024 - Chứng Nhận Hoàn Thành", css="footer {visibility: hidden}") as demo:
        gr.Markdown("## PhoCao Run 2024 - Chứng Nhận Hoàn Thành")
        name_input = gr.Textbox(label="Tên/Nickname")
        distance_input = gr.Number(label="Số km")

        submit_button = gr.Button("Chạm Đích")

        output_image = gr.Image(label="Kết quả")

        # Event handler for the button click
        def on_submit(name, distance):
            image_path = write_text_to_image(name, distance, conn)
            submit_button = gr.Button(visible=False)
            return submit_button, image_path

        submit_button.click(
            on_submit,
            inputs=[name_input, distance_input],
            outputs=[submit_button, output_image],
            scroll_to_output=True,
            api_name=None,
            max_batch_size=1,
            show_api=False,
        )

    # Mount Gradio app to FastAPI app
    demo.show_api = False
    app = gr.mount_gradio_app(app, demo, path="/")

    uvicorn.run(app, host="0.0.0.0", port=7860)