Suchinthana commited on
Commit
42897ae
·
1 Parent(s): 0c2f440

removing selenium

Browse files
Files changed (2) hide show
  1. app.py +19 -36
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import os
2
  import json
3
- import cv2
4
  import numpy as np
5
  import torch
6
- from PIL import Image
7
- import io
8
  import gradio as gr
9
  from openai import OpenAI
10
  from geopy.geocoders import Nominatim
11
- from folium import Map, GeoJson
12
- from gradio_folium import Folium
13
  from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline
14
  import spaces
15
 
@@ -67,7 +64,7 @@ Ensure all responses are descriptive and relevant to city names only, without co
67
  "content": [
68
  {
69
  "type": "text",
70
- "text": query
71
  }
72
  ]
73
  }
@@ -109,38 +106,25 @@ def generate_geojson(response):
109
  }]
110
  }
111
 
112
- # Function to compute bounds from GeoJSON
113
  @spaces.GPU
114
- def get_bounds(geojson):
115
- coordinates = []
116
- for feature in geojson["features"]:
117
  geom_type = feature["geometry"]["type"]
118
  coords = feature["geometry"]["coordinates"]
 
119
  if geom_type == "Point":
120
- coordinates.append(coords)
121
  elif geom_type in ["MultiPoint", "LineString"]:
122
- coordinates.extend(coords)
123
- elif geom_type in ["MultiLineString", "Polygon"]:
124
- for part in coords:
125
- coordinates.extend(part)
126
- elif geom_type == "MultiPolygon":
127
  for polygon in coords:
128
- for part in polygon:
129
- coordinates.extend(part)
130
- lats = [coord[1] for coord in coordinates]
131
- lngs = [coord[0] for coord in coordinates]
132
- return [[min(lats), min(lngs)], [max(lats), max(lngs)]]
133
 
134
- # Generate map image in memory
135
- @spaces.GPU
136
- def generate_map_image(geojson_data):
137
- m = Map()
138
- geo_layer = GeoJson(geojson_data, name="Feature map")
139
- geo_layer.add_to(m)
140
- bounds = get_bounds(geojson_data)
141
- m.fit_bounds(bounds)
142
- img_data = m._to_png(5)
143
- return Image.open(io.BytesIO(img_data))
144
 
145
  # ControlNet pipeline setup
146
  controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16)
@@ -175,13 +159,12 @@ def handle_query(query):
175
  geojson_data = generate_geojson(response)
176
 
177
  # Generate map image
178
- map_image = generate_map_image(geojson_data)
179
 
180
  # Generate mask for ControlNet
181
- empty_map = cv2.cvtColor(np.array(generate_map_image({"type": "FeatureCollection", "features": []})), cv2.COLOR_BGR2GRAY)
182
- map_image_array = cv2.cvtColor(np.array(map_image), cv2.COLOR_BGR2GRAY)
183
- difference = cv2.absdiff(empty_map, map_image_array)
184
- _, mask = cv2.threshold(difference, 15, 255, cv2.THRESH_BINARY)
185
 
186
  # Convert mask to PIL Image
187
  mask_image = Image.fromarray(mask)
 
1
  import os
2
  import json
 
3
  import numpy as np
4
  import torch
5
+ from PIL import Image, ImageDraw
 
6
  import gradio as gr
7
  from openai import OpenAI
8
  from geopy.geocoders import Nominatim
9
+ from staticmap import StaticMap, CircleMarker, Polygon
 
10
  from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline
11
  import spaces
12
 
 
64
  "content": [
65
  {
66
  "type": "text",
67
+ "text": "draw a map in coconut triangle of sri lanka: The Coconut Triangle is a region in Sri Lanka that's known for its coconut production. It's made up of the districts of Kurunegala, Puttalam, and Gampaha."
68
  }
69
  ]
70
  }
 
106
  }]
107
  }
108
 
109
+ # Generate static map image
110
  @spaces.GPU
111
+ def generate_static_map(geojson_data):
112
+ m = StaticMap(500, 500)
113
+ for feature in geojson_data["features"]:
114
  geom_type = feature["geometry"]["type"]
115
  coords = feature["geometry"]["coordinates"]
116
+
117
  if geom_type == "Point":
118
+ m.add_marker(CircleMarker((coords[0], coords[1]), 'blue', 10))
119
  elif geom_type in ["MultiPoint", "LineString"]:
120
+ for coord in coords:
121
+ m.add_marker(CircleMarker((coord[0], coord[1]), 'red', 10))
122
+ elif geom_type in ["Polygon", "MultiPolygon"]:
 
 
123
  for polygon in coords:
124
+ m.add_polygon(Polygon([(c[0], c[1]) for c in polygon], 'green', 3))
 
 
 
 
125
 
126
+ image = m.render(zoom=10)
127
+ return Image.fromarray(image)
 
 
 
 
 
 
 
 
128
 
129
  # ControlNet pipeline setup
130
  controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16)
 
159
  geojson_data = generate_geojson(response)
160
 
161
  # Generate map image
162
+ map_image = generate_static_map(geojson_data)
163
 
164
  # Generate mask for ControlNet
165
+ empty_map = Image.new("RGB", map_image.size, "white")
166
+ difference = np.array(map_image) - np.array(empty_map)
167
+ mask = np.any(difference != 0, axis=-1).astype(np.uint8) * 255
 
168
 
169
  # Convert mask to PIL Image
170
  mask_image = Image.fromarray(mask)
requirements.txt CHANGED
@@ -10,4 +10,5 @@ spaces
10
  torchvision
11
  opencv-python
12
  torch
 
13
  selenium
 
10
  torchvision
11
  opencv-python
12
  torch
13
+ staticmap
14
  selenium