clessvna commited on
Commit
cd4563d
·
1 Parent(s): 4d82660

no more FastAPI

Browse files
Files changed (1) hide show
  1. app.py +5 -38
app.py CHANGED
@@ -1,13 +1,7 @@
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.middleware.cors import CORSMiddleware
10
- from fastapi.staticfiles import StaticFiles
11
  from PIL import Image, ImageDraw, ImageFont
12
 
13
 
@@ -21,7 +15,7 @@ def write_text_to_image(
21
  name_y: int = 410,
22
  distance_x: int = 435,
23
  distance_y: int = 450,
24
- ) -> str:
25
  # Save the input data to the SQLite database
26
  cursor = conn.cursor()
27
  cursor.execute(
@@ -36,7 +30,7 @@ def write_text_to_image(
36
  draw = ImageDraw.Draw(image)
37
 
38
  # Set the font (adjust the font size and style)
39
- font = ImageFont.truetype("font/RobotoSlab-Bold.ttf", 25)
40
 
41
  # Define text colors
42
  text_color = (0, 255, 0) # Green
@@ -47,36 +41,11 @@ def write_text_to_image(
47
  # Write the distance on the image at the specified coordinates
48
  draw.text((distance_x, distance_y), f"{distance} km", font=font, fill=text_color)
49
 
50
- # Save the image with the text
51
- timestamp = int(time.perf_counter_ns())
52
- output_image_path = f"{static_dir}/{timestamp}.jpg"
53
- image.save(output_image_path)
54
-
55
- # Return the path to the generated image
56
- return output_image_path
57
 
58
 
59
  # Serve the app
60
  if __name__ == "__main__":
61
- # Create a FastAPI app
62
- app = FastAPI()
63
-
64
- # Allow CORS (optional, but recommended for Hugging Face Spaces)
65
- app.add_middleware(
66
- CORSMiddleware,
67
- allow_origins=["*"],
68
- allow_credentials=True,
69
- allow_methods=["*"],
70
- allow_headers=["*"],
71
- )
72
-
73
- # Create a static directory to store the static files
74
- static_dir = Path("./static")
75
- static_dir.mkdir(parents=True, exist_ok=True)
76
-
77
- # Mount FastAPI StaticFiles server
78
- app.mount("/static", StaticFiles(directory=static_dir), name="static")
79
-
80
  # Connect to SQLite database
81
  conn = sqlite3.connect("user_input.db", check_same_thread=False)
82
  cursor = conn.cursor()
@@ -105,7 +74,7 @@ if __name__ == "__main__":
105
 
106
  # Event handler for the button click
107
  def on_submit(name, distance):
108
- image_path = write_text_to_image(name, distance, conn)
109
  submit_button = gr.Button(visible=False)
110
  return submit_button, image_path
111
 
@@ -121,6 +90,4 @@ if __name__ == "__main__":
121
 
122
  # Mount Gradio app to FastAPI app
123
  demo.show_api = False
124
- app = gr.mount_gradio_app(app, demo, path="/")
125
-
126
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import sqlite3
 
 
2
  from sqlite3.dbapi2 import Connection
3
 
4
  import gradio as gr
 
 
 
 
5
  from PIL import Image, ImageDraw, ImageFont
6
 
7
 
 
15
  name_y: int = 410,
16
  distance_x: int = 435,
17
  distance_y: int = 450,
18
+ ):
19
  # Save the input data to the SQLite database
20
  cursor = conn.cursor()
21
  cursor.execute(
 
30
  draw = ImageDraw.Draw(image)
31
 
32
  # Set the font (adjust the font size and style)
33
+ font = ImageFont.truetype("font/RobotoSlab-Medium.ttf", 25)
34
 
35
  # Define text colors
36
  text_color = (0, 255, 0) # Green
 
41
  # Write the distance on the image at the specified coordinates
42
  draw.text((distance_x, distance_y), f"{distance} km", font=font, fill=text_color)
43
 
44
+ return image
 
 
 
 
 
 
45
 
46
 
47
  # Serve the app
48
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  # Connect to SQLite database
50
  conn = sqlite3.connect("user_input.db", check_same_thread=False)
51
  cursor = conn.cursor()
 
74
 
75
  # Event handler for the button click
76
  def on_submit(name, distance):
77
+ image_path = write_text_to_image(name, distance, conn, static_dir="static")
78
  submit_button = gr.Button(visible=False)
79
  return submit_button, image_path
80
 
 
90
 
91
  # Mount Gradio app to FastAPI app
92
  demo.show_api = False
93
+ demo.launch()