Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,6 @@ from fastapi.responses import HTMLResponse
|
|
12 |
from fastapi.staticfiles import StaticFiles
|
13 |
from fastapi.middleware.gzip import GZipMiddleware
|
14 |
|
15 |
-
# Initialize Argilla client
|
16 |
client = rg.Argilla(
|
17 |
api_url=os.getenv("ARGILLA_API_URL", ""),
|
18 |
api_key=os.getenv("ARGILLA_API_KEY", "")
|
@@ -101,14 +100,9 @@ countries = {
|
|
101 |
}
|
102 |
}
|
103 |
|
104 |
-
|
105 |
-
# This significantly reduces load on the Argilla server
|
106 |
@lru_cache(maxsize=32)
|
107 |
def count_answers_per_space_cached(country: str, cache_buster: int):
|
108 |
-
"""
|
109 |
-
Cached version of count_answers_per_space
|
110 |
-
cache_buster is used to invalidate the cache when needed
|
111 |
-
"""
|
112 |
return count_answers_per_space(country)
|
113 |
|
114 |
def count_answers_per_space(country: str):
|
@@ -119,14 +113,10 @@ def count_answers_per_space(country: str):
|
|
119 |
|
120 |
try:
|
121 |
dataset = client.datasets(dataset_name)
|
122 |
-
|
123 |
-
# Optimization: Get all records with their responses in one call
|
124 |
records = list(dataset.records(with_responses=True))
|
125 |
|
126 |
-
# Count total questions
|
127 |
total_questions = len(records)
|
128 |
|
129 |
-
# Count answered questions
|
130 |
answered_questions = 0
|
131 |
total_answers = 0
|
132 |
answers_per_user = {}
|
@@ -137,7 +127,6 @@ def count_answers_per_space(country: str):
|
|
137 |
answered_questions += 1
|
138 |
total_answers += len(responses)
|
139 |
|
140 |
-
# Count per user
|
141 |
for response in responses:
|
142 |
user_id = str(response.user_id)
|
143 |
answers_per_user[user_id] = answers_per_user.get(user_id, 0) + 1
|
@@ -154,7 +143,6 @@ def count_answers_per_space(country: str):
|
|
154 |
}
|
155 |
|
156 |
except Exception as e:
|
157 |
-
# If space doesn't exist, return zero values
|
158 |
print(f"No dataset found for {dataset_name}: {e}")
|
159 |
return {
|
160 |
"name": country,
|
@@ -165,71 +153,57 @@ def count_answers_per_space(country: str):
|
|
165 |
"documents": 0
|
166 |
}
|
167 |
|
168 |
-
# Create a FastAPI app
|
169 |
app = FastAPI()
|
170 |
|
171 |
-
#
|
172 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
173 |
|
174 |
-
# Global variable to track when data was last updated
|
175 |
last_update_time = time.time()
|
176 |
cached_html_content = None
|
177 |
|
178 |
-
# Route to serve the map visualization
|
179 |
@app.get("/d3-map")
|
180 |
async def serve_map(request: Request, refresh: bool = False):
|
181 |
global last_update_time, cached_html_content
|
182 |
current_time = time.time()
|
183 |
|
184 |
-
#
|
185 |
if cached_html_content and current_time - last_update_time < 300 and not refresh:
|
186 |
return HTMLResponse(content=cached_html_content)
|
187 |
|
188 |
-
#
|
189 |
-
# Use async gathering to fetch all data in parallel
|
190 |
-
cache_buster = int(current_time) # Use current time to bust cache when refresh=True
|
191 |
|
192 |
country_data = {}
|
193 |
for country in countries.keys():
|
194 |
country_data[countries[country]["iso"]] = count_answers_per_space_cached(country, cache_buster)
|
195 |
|
196 |
-
# Convert to JSON for JavaScript
|
197 |
country_data_json = json.dumps(country_data)
|
198 |
|
199 |
-
# Replace the placeholder with actual data
|
200 |
with open('template.txt', 'r') as f:
|
201 |
html_template = f.read()
|
202 |
|
203 |
html_content = html_template.replace("COUNTRY_DATA_PLACEHOLDER", country_data_json)
|
204 |
|
205 |
-
# Update the cache
|
206 |
cached_html_content = html_content
|
207 |
last_update_time = current_time
|
208 |
|
209 |
return HTMLResponse(content=html_content)
|
210 |
|
211 |
-
# Create a simple Gradio interface with an iframe
|
212 |
def create_iframe(refresh=False):
|
213 |
-
# Add a random parameter to force reload if refresh is True
|
214 |
param = f"refresh={str(refresh).lower()}&t={random.randint(1, 10000)}"
|
215 |
return f'<iframe src="/d3-map?{param}" style="width:100%; height:650px; border:none;"></iframe>'
|
216 |
|
217 |
-
# Create the Gradio blocks
|
218 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="purple")) as demo:
|
219 |
gr.Markdown("# Mapa anotaci贸n")
|
220 |
|
221 |
iframe_output = gr.HTML(create_iframe())
|
222 |
|
223 |
-
# Refresh button to generate new random data and force cache refresh
|
224 |
def refresh():
|
225 |
return create_iframe(refresh=True)
|
226 |
|
227 |
gr.Button("Actualizar Datos").click(fn=refresh, outputs=iframe_output)
|
228 |
|
229 |
-
# Mount the Gradio app to the FastAPI app
|
230 |
gr.mount_gradio_app(app, demo, path="/")
|
231 |
|
232 |
-
# Start the server
|
233 |
if __name__ == "__main__":
|
234 |
import uvicorn
|
235 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
12 |
from fastapi.staticfiles import StaticFiles
|
13 |
from fastapi.middleware.gzip import GZipMiddleware
|
14 |
|
|
|
15 |
client = rg.Argilla(
|
16 |
api_url=os.getenv("ARGILLA_API_URL", ""),
|
17 |
api_key=os.getenv("ARGILLA_API_KEY", "")
|
|
|
100 |
}
|
101 |
}
|
102 |
|
103 |
+
|
|
|
104 |
@lru_cache(maxsize=32)
|
105 |
def count_answers_per_space_cached(country: str, cache_buster: int):
|
|
|
|
|
|
|
|
|
106 |
return count_answers_per_space(country)
|
107 |
|
108 |
def count_answers_per_space(country: str):
|
|
|
113 |
|
114 |
try:
|
115 |
dataset = client.datasets(dataset_name)
|
|
|
|
|
116 |
records = list(dataset.records(with_responses=True))
|
117 |
|
|
|
118 |
total_questions = len(records)
|
119 |
|
|
|
120 |
answered_questions = 0
|
121 |
total_answers = 0
|
122 |
answers_per_user = {}
|
|
|
127 |
answered_questions += 1
|
128 |
total_answers += len(responses)
|
129 |
|
|
|
130 |
for response in responses:
|
131 |
user_id = str(response.user_id)
|
132 |
answers_per_user[user_id] = answers_per_user.get(user_id, 0) + 1
|
|
|
143 |
}
|
144 |
|
145 |
except Exception as e:
|
|
|
146 |
print(f"No dataset found for {dataset_name}: {e}")
|
147 |
return {
|
148 |
"name": country,
|
|
|
153 |
"documents": 0
|
154 |
}
|
155 |
|
|
|
156 |
app = FastAPI()
|
157 |
|
158 |
+
# gzip compression middleware reduces transferred data size
|
159 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
160 |
|
|
|
161 |
last_update_time = time.time()
|
162 |
cached_html_content = None
|
163 |
|
|
|
164 |
@app.get("/d3-map")
|
165 |
async def serve_map(request: Request, refresh: bool = False):
|
166 |
global last_update_time, cached_html_content
|
167 |
current_time = time.time()
|
168 |
|
169 |
+
# use cached content if available and not expired (5 minute cache)
|
170 |
if cached_html_content and current_time - last_update_time < 300 and not refresh:
|
171 |
return HTMLResponse(content=cached_html_content)
|
172 |
|
173 |
+
cache_buster = int(current_time) # use current time to bust cache when refresh=True
|
|
|
|
|
174 |
|
175 |
country_data = {}
|
176 |
for country in countries.keys():
|
177 |
country_data[countries[country]["iso"]] = count_answers_per_space_cached(country, cache_buster)
|
178 |
|
|
|
179 |
country_data_json = json.dumps(country_data)
|
180 |
|
|
|
181 |
with open('template.txt', 'r') as f:
|
182 |
html_template = f.read()
|
183 |
|
184 |
html_content = html_template.replace("COUNTRY_DATA_PLACEHOLDER", country_data_json)
|
185 |
|
|
|
186 |
cached_html_content = html_content
|
187 |
last_update_time = current_time
|
188 |
|
189 |
return HTMLResponse(content=html_content)
|
190 |
|
|
|
191 |
def create_iframe(refresh=False):
|
|
|
192 |
param = f"refresh={str(refresh).lower()}&t={random.randint(1, 10000)}"
|
193 |
return f'<iframe src="/d3-map?{param}" style="width:100%; height:650px; border:none;"></iframe>'
|
194 |
|
|
|
195 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="pink", secondary_hue="purple")) as demo:
|
196 |
gr.Markdown("# Mapa anotaci贸n")
|
197 |
|
198 |
iframe_output = gr.HTML(create_iframe())
|
199 |
|
|
|
200 |
def refresh():
|
201 |
return create_iframe(refresh=True)
|
202 |
|
203 |
gr.Button("Actualizar Datos").click(fn=refresh, outputs=iframe_output)
|
204 |
|
|
|
205 |
gr.mount_gradio_app(app, demo, path="/")
|
206 |
|
|
|
207 |
if __name__ == "__main__":
|
208 |
import uvicorn
|
209 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|