LuisBlanche
commited on
Commit
•
e5451f2
1
Parent(s):
705643c
rework
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
|
5 |
-
|
6 |
import re
|
7 |
from io import BytesIO
|
8 |
|
@@ -26,23 +25,23 @@ from unidecode import unidecode
|
|
26 |
|
27 |
|
28 |
class PDFPoster:
|
29 |
-
def __init__(self, deputy_name: str
|
30 |
self.deputy_name = deputy_name
|
31 |
-
self.vote_id_list = [f"vote_{i}" for i in vote_id_list]
|
32 |
|
33 |
def retrieve_deputy_data(self):
|
34 |
self.deputy_data = self.get_deputy_votes_page()
|
35 |
self.votes = self.get_votes_from_politic_page()
|
36 |
self.img_url = self.get_politic_image()
|
37 |
self.party = self.get_politic_party()
|
|
|
38 |
|
39 |
def generate_poster(
|
40 |
self,
|
|
|
41 |
message_1: str = "Les votes de vos députés sont souvent différents de ce que les responsables de partis annoncent dans les médias. Les données de votes sont ouvertes!",
|
42 |
message_2: str = "Les 30 juin, et 7 juin, renseignez vous, et votez en connaissance de cause !",
|
43 |
):
|
44 |
-
self.
|
45 |
-
df_subset = self.votes[self.votes["vote_id"].isin(self.vote_id_list)]
|
46 |
|
47 |
pdf_filename = f"{self.deputy_name}.pdf"
|
48 |
document = SimpleDocTemplate(pdf_filename, pagesize=A4)
|
@@ -109,7 +108,6 @@ class PDFPoster:
|
|
109 |
]
|
110 |
)
|
111 |
)
|
112 |
-
return pdf_filename
|
113 |
|
114 |
# Function to apply conditional formatting
|
115 |
def apply_conditional_styles(table, data):
|
@@ -158,6 +156,7 @@ class PDFPoster:
|
|
158 |
vote_text,
|
159 |
]
|
160 |
document.build(elements)
|
|
|
161 |
|
162 |
def get_deputy_votes_page(self):
|
163 |
"""Fetches the webpage containing the voting records of a specified deputy.
|
@@ -227,7 +226,6 @@ class PDFPoster:
|
|
227 |
vote_element.find("span", class_="date").text.replace("\n", "").strip()
|
228 |
)
|
229 |
vote_category = vote_categories[i]["class"][-1]
|
230 |
-
# color = "green" if for_or_against == "Pour" else "red"
|
231 |
votes.append(
|
232 |
[
|
233 |
vote_id,
|
@@ -272,65 +270,72 @@ class PDFPoster:
|
|
272 |
return party
|
273 |
|
274 |
|
275 |
-
def
|
276 |
-
pdfposter = PDFPoster(deputy_name
|
277 |
-
|
278 |
-
|
|
|
279 |
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
|
282 |
-
|
283 |
-
css="""
|
284 |
#col-container {
|
285 |
margin: 0 auto;
|
286 |
max-width: 520px;
|
287 |
}
|
288 |
"""
|
289 |
|
290 |
-
|
291 |
-
|
292 |
with gr.Blocks(css=css) as demo:
|
293 |
-
|
294 |
with gr.Column(elem_id="col-container"):
|
295 |
-
gr.Markdown(
|
296 |
# Réalise une affiche des votes de ton député !
|
297 |
""")
|
298 |
-
|
299 |
with gr.Row():
|
300 |
-
|
301 |
-
deputy = gr.Text(
|
302 |
label="deputy_name",
|
303 |
show_label=False,
|
304 |
max_lines=1,
|
305 |
placeholder="Nom du député, si tu ne le connais pas RDV sur www.datan.fr ou www.nosdeputes.fr",
|
306 |
container=False,
|
307 |
)
|
308 |
-
|
309 |
-
|
310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
result = gr.File(label="Result", show_label=True)
|
312 |
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
placeholder="Les 30 juin, et 7 juin, renseignez vous, et votez en connaissance de cause !",
|
324 |
-
visible=True,
|
325 |
-
)
|
326 |
-
|
327 |
-
vote_list = gr.CheckBoxGroup(placeholder=[3213, 1403, 325, 210, 44])
|
328 |
-
|
329 |
-
|
330 |
-
run_button.click(
|
331 |
-
fn = infer,
|
332 |
-
inputs = [deputy, message_1, message_2, vote_list],
|
333 |
-
outputs = [result]
|
334 |
)
|
335 |
|
336 |
-
demo.queue().launch()
|
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
|
|
|
5 |
import re
|
6 |
from io import BytesIO
|
7 |
|
|
|
25 |
|
26 |
|
27 |
class PDFPoster:
|
28 |
+
def __init__(self, deputy_name: str):
|
29 |
self.deputy_name = deputy_name
|
|
|
30 |
|
31 |
def retrieve_deputy_data(self):
|
32 |
self.deputy_data = self.get_deputy_votes_page()
|
33 |
self.votes = self.get_votes_from_politic_page()
|
34 |
self.img_url = self.get_politic_image()
|
35 |
self.party = self.get_politic_party()
|
36 |
+
return self.votes
|
37 |
|
38 |
def generate_poster(
|
39 |
self,
|
40 |
+
vote_id_list,
|
41 |
message_1: str = "Les votes de vos députés sont souvent différents de ce que les responsables de partis annoncent dans les médias. Les données de votes sont ouvertes!",
|
42 |
message_2: str = "Les 30 juin, et 7 juin, renseignez vous, et votez en connaissance de cause !",
|
43 |
):
|
44 |
+
df_subset = self.votes[self.votes["vote_id"].isin(vote_id_list)]
|
|
|
45 |
|
46 |
pdf_filename = f"{self.deputy_name}.pdf"
|
47 |
document = SimpleDocTemplate(pdf_filename, pagesize=A4)
|
|
|
108 |
]
|
109 |
)
|
110 |
)
|
|
|
111 |
|
112 |
# Function to apply conditional formatting
|
113 |
def apply_conditional_styles(table, data):
|
|
|
156 |
vote_text,
|
157 |
]
|
158 |
document.build(elements)
|
159 |
+
return pdf_filename
|
160 |
|
161 |
def get_deputy_votes_page(self):
|
162 |
"""Fetches the webpage containing the voting records of a specified deputy.
|
|
|
226 |
vote_element.find("span", class_="date").text.replace("\n", "").strip()
|
227 |
)
|
228 |
vote_category = vote_categories[i]["class"][-1]
|
|
|
229 |
votes.append(
|
230 |
[
|
231 |
vote_id,
|
|
|
270 |
return party
|
271 |
|
272 |
|
273 |
+
def fetch_votes(deputy_name):
|
274 |
+
pdfposter = PDFPoster(deputy_name)
|
275 |
+
votes = pdfposter.retrieve_deputy_data()
|
276 |
+
vote_list = votes['vote_id'].tolist()
|
277 |
+
return vote_list
|
278 |
|
279 |
+
def generate_poster(deputy_name, message_1, message_2, vote_id_list):
|
280 |
+
pdfposter = PDFPoster(deputy_name)
|
281 |
+
pdfposter.retrieve_deputy_data()
|
282 |
+
pdf_filename = pdfposter.generate_poster(vote_id_list, message_1, message_2)
|
283 |
+
return pdf_filename
|
284 |
|
285 |
|
286 |
+
css = """
|
|
|
287 |
#col-container {
|
288 |
margin: 0 auto;
|
289 |
max-width: 520px;
|
290 |
}
|
291 |
"""
|
292 |
|
|
|
|
|
293 |
with gr.Blocks(css=css) as demo:
|
294 |
+
|
295 |
with gr.Column(elem_id="col-container"):
|
296 |
+
gr.Markdown("""
|
297 |
# Réalise une affiche des votes de ton député !
|
298 |
""")
|
299 |
+
|
300 |
with gr.Row():
|
301 |
+
deputy_name = gr.Text(
|
|
|
302 |
label="deputy_name",
|
303 |
show_label=False,
|
304 |
max_lines=1,
|
305 |
placeholder="Nom du député, si tu ne le connais pas RDV sur www.datan.fr ou www.nosdeputes.fr",
|
306 |
container=False,
|
307 |
)
|
308 |
+
fetch_button = gr.Button("Fetch Votes", scale=0)
|
309 |
+
|
310 |
+
vote_list = gr.CheckboxGroup(label="Select Votes", choices=[])
|
311 |
+
|
312 |
+
with gr.Row():
|
313 |
+
message_1 = gr.Text(
|
314 |
+
label="message_1",
|
315 |
+
max_lines=1,
|
316 |
+
placeholder="Les votes de vos députés sont souvent différents de ce que les responsables de partis annoncent dans les médias. Les données de votes sont ouvertes!",
|
317 |
+
visible=True,
|
318 |
+
)
|
319 |
+
message_2 = gr.Text(
|
320 |
+
label="message_2",
|
321 |
+
max_lines=1,
|
322 |
+
placeholder="Les 30 juin, et 7 juin, renseignez vous, et votez en connaissance de cause !",
|
323 |
+
visible=True,
|
324 |
+
)
|
325 |
+
|
326 |
+
generate_button = gr.Button("Generate Poster", scale=0)
|
327 |
result = gr.File(label="Result", show_label=True)
|
328 |
|
329 |
+
fetch_button.click(
|
330 |
+
fn=fetch_votes,
|
331 |
+
inputs=deputy_name,
|
332 |
+
outputs=vote_list
|
333 |
+
)
|
334 |
+
|
335 |
+
generate_button.click(
|
336 |
+
fn=generate_poster,
|
337 |
+
inputs=[deputy_name, message_1, message_2, vote_list],
|
338 |
+
outputs=result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
)
|
340 |
|
341 |
+
demo.queue().launch()
|