Spaces:
Runtime error
Runtime error
import gradio as gr | |
import psycopg2 | |
def connect_to_db(): | |
return psycopg2.connect( | |
dbname="glprui_jloddr", | |
user="glprui_jloddr", | |
password="612ef773", | |
host="db.qgiscloud.com", | |
port="5432", | |
sslmode="prefer" | |
) | |
def submit_data(age, gender, transport, multi_transport, time_of_day, day_of_week, description, start_point, lost_point, end_point): | |
conn = connect_to_db() | |
cursor = conn.cursor() | |
try: | |
cursor.execute( | |
""" | |
INSERT INTO public.gettinglost_tracking (Age, Gender, Transport, TimeOfDay, DayOfWeek, Description) | |
VALUES (%s, %s, %s, %s, %s, %s) RETURNING ID; | |
""", | |
(age, gender, transport, time_of_day, day_of_week, description) | |
) | |
record_id = cursor.fetchone()[0] | |
points = {'start': start_point, 'lost': lost_point, 'end': end_point} | |
for pointType, point in points.items(): | |
if point: | |
cursor.execute( | |
""" | |
INSERT INTO public.gettinglost_geom (ID, PointType, geom) | |
VALUES (%s, %s, ST_SetSRID(ST_Point(%s, %s), 4326)); | |
""", | |
(record_id, pointType, point[1], point[0]) | |
) | |
conn.commit() | |
return "Data recorded successfully!" | |
except Exception as e: | |
conn.rollback() | |
return f"Error: {str(e)}" | |
finally: | |
cursor.close() | |
conn.close() | |
age = gr.Dropdown(choices=["0-10", "11-20", "21-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "91-100"], label="Age") | |
gender = gr.Radio(choices=["M", "F", "O", "PNTS"], label="Gender") | |
transport = gr.Radio(choices=["Walk", "Car", "Bike", "Train", "Other", "Multi"], label="Mode of Transport") | |
multi_transport = gr.CheckboxGroup(choices=["Walk", "Car", "Bike", "Train", "Other"], label="If Multi, Select Modes Used", visible=False) | |
time_of_day = gr.Dropdown(choices=["Morning", "Afternoon", "Evening", "Night"], label="Time of Day") | |
day_of_week = gr.Dropdown(choices=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], label="Day of the Week") | |
description = gr.Textbox(lines=4, placeholder="Why did you get lost?", label="Description") | |
start_point = gr.Map(label="Select Start Point") | |
lost_point = gr.Map(label="Select Getting Lost Point") | |
end_point = gr.Map(label="Select End Point") | |
def update_visibility(transport_mode): | |
if transport_mode == "Multi": | |
return gr.update(visible=True) | |
else: | |
return gr.update(visible=False) | |
transport.change(fn=update_visibility, inputs=transport, outputs=multi_transport) | |
iface = gr.Interface( | |
fn=submit_data, | |
inputs=[age, gender, transport, multi_transport, time_of_day, day_of_week, description, start_point, lost_point, end_point], | |
outputs="text", | |
live=True | |
) | |
if __name__ == "__main__": | |
iface.launch() | |