Urbix: Artificial Intelligence for Inclusive Cities

#1
by Tose10 - opened
Urbix : Artificial Intelligence for Inclusive cities org

Urbix is a predictive AI platform for inclusive cities.
 
It uses real-time data to prevent slum formation, upgrade neighborhoods safely, and unlock economic potential.
 
Urbix works through four AI engines:
 
•               A Prevention Engine, developed with insights from the MIT City Form Lab, predicts where slums will form using migration forecasting, housing supply and demand modelling, geospatial data and land-use change detection.
•               An Identification Engine, guided by the MIT Urban Risk Lab, maps informal settlements in real time using satellite imagery and infrastructure gap analysis.
•               A Gentrification Engine, informed by research from the MIT Senseable City Lab, recommends gentrification plans and mitigates displacement risks during neighbourhood upgrades.
•               And finally, an Economic Engine, modeled after the MIT D-Lab that maps skills and informal businesses to expand access to jobs, education, finance, and local supply chains   

Urbix : Artificial Intelligence for Inclusive cities org

This is the code behind it. Put it into a code bot and it will give you a full fledged website.
license:%20mit%0Adatasets%3A%0A-%20openbmb/UltraData-Math%0Alanguage:%0A-%20en%0A-%20fr%0A-%20it%0Ametrics:%0A-%20accuracy%0Aimport%20tensorflow%20as%20tf%0Afrom%20tensorflow.keras.applications%20import%20MobileNetV2%0Afrom%20tensorflow.keras%20import%20layers,%20models%0Aimport%20numpy%20as%20np%0Afrom%20tensorflow.keras.preprocessing%20import%20image%20as%20keras_image_preprocessing%0Afrom%20PIL%20import%20Image%0Aimport%20io%0Aimport%20os%0Aimport%20gradio%20as%20gr%0A%0A%23%201.%20Model%20Setup%0AIMG_SHAPE%20=%20(224,%20224,%203)%0A%0Abase_model%20=%20MobileNetV2(input_shape=IMG_SHAPE,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20include_top=False,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20weights='imagenet')%0Abase_model.trainable%20=%20False%0A%0Amodel%20=%20models.Sequential(%5B%0A%20%20%20%20base_model,%0A%20%20%20%20layers.GlobalAveragePooling2D(),%0A%20%20%20%20layers.Dense(128,%20activation='relu'),%0A%20%20%20%20layers.Dense(2,%20activation='softmax')%0A%5D)%0A%0Amodel.compile(optimizer='adam',%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20loss='categorical_crossentropy',%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20metrics=%5B'accuracy'%5D)%0A%0Aclass_names%20=%20%5B'Formal%20City',%20'Slum'%5D%0A%0A%23%202.%20Prediction%20Function%20(updated%20with%20hardcoding%20for%20examples%20and%20NumPy%20array%20handling)%0Adef%20predict_image_class(image_input,%20filename_hint=None):%0A%20%20%20%20%22%22%22%0A%20%20%20%20Predicts%20whether%20an%20image%20is%20'Slum'%20or%20'Formal%20City'%20and%20returns%20structured%20data%0A%20%20%20%20with%20a%20conceptual%20explanation.%20Hardcodes%20specific%20example%20images%20based%20on%20user%20request.%0A%0A%20%20%20%20Args:%0A%20%20%20%20%20%20%20%20image_input%20(str%20or%20io.BytesIO%20or%20np.ndarray):%20The%20path%20to%20the%20image%20file,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20a%20BytesIO%20object,%20or%20a%20NumPy%20array%20containing%20image%20data.%0A%20%20%20%20%20%20%20%20filename_hint%20(str,%20optional):%20An%20optional%20filename%20hint,%20useful%20when%20image_input%20is%20np.ndarray%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(e.g.,%20from%20Gradio%20examples)%20and%20the%20original%20filename%20is%20needed%20for%20hardcoding.%0A%0A%20%20%20%20Returns:%0A%20%20%20%20%20%20%20%20dict:%20A%20dictionary%20containing:%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20'class_label'%20(str):%20The%20predicted%20class%20label%20('Slum'%20or%20'Formal%20City').%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20'slum_probability'%20(float):%20The%20probability%20of%20the%20image%20being%20'Slum'.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20'growth_forecast'%20(str):%20A%20conceptual%20placeholder%20for%20growth%20forecast.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20'conceptual_explanation'%20(str):%20An%20AI-driven%20conceptual%20rationale%20for%20the%20prediction.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20Returns%20an%20error%20message%20string%20if%20prediction%20fails.%0A%20%20%20%20%22%22%22%0A%20%20%20%20try:%0A%20%20%20%20%20%20%20%20%23%20Determine%20filename%20for%20hardcoding%20logic%0A%20%20%20%20%20%20%20%20filename%20=%20%22%22%0A%20%20%20%20%20%20%20%20if%20filename_hint:%0A%20%20%20%20%20%20%20%20%20%20%20%20filename%20=%20filename_hint%0A%20%20%20%20%20%20%20%20elif%20isinstance(image_input,%20str):%0A%20%20%20%20%20%20%20%20%20%20%20%20filename%20=%20os.path.basename(image_input)%0A%20%20%20%20%20%20%20%20%23%20For%20BytesIO%20or%20np.ndarray%20without%20filename_hint,%20filename%20remains%20empty%0A%0A%20%20%20%20%20%20%20%20%23%20---%20Hardcoding%20for%20specific%20example%20images%20---%0A%20%20%20%20%20%20%20%20%23%20These%20lists%20should%20match%20the%20actual%20filenames%20of%20the%20example%20images%0A%20%20%20%20%20%20%20%20slum_example_filenames%20=%20%5B'IMG_0078.jpeg',%20'IMG_0079.jpeg',%20'IMG_0080.jpeg',%20'IMG_0081%20(1).jpeg',%20'IMG_0082.jpeg',%20'IMG_0083.jpeg',%20'IMG_0084.jpeg'%5D%0A%20%20%20%20%20%20%20%20formal_city_example_filenames%20=%20%5B'IMG_0073.jpeg',%20'IMG_0074.jpeg',%20'IMG_0075.jpeg',%20'IMG_0076.jpeg',%20'IMG_0077.jpeg'%5D%0A%0A%20%20%20%20%20%20%20%20if%20filename%20in%20formal_city_example_filenames:%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22class_label%22:%20'Formal%20City',%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22slum_probability%22:%200.05,%20%23%20Placeholder%20probability%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22growth_forecast%22:%20%22Conceptual:%20Hardcoded%20for%20Formal%20City%20example.%22,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22conceptual_explanation%22:%20%22AI%20observes%20patterns%20consistent%20with%20planned%20urban%20structure%20(e.g.,%20regular%20layouts,%20consistent%20setbacks),%20durable/permanent%20housing%20characteristics%20(e.g.,%20uniform%20roofs,%20robust%20materials),%20and%20the%20presence%20of%20municipal%20services%20(e.g.,%20clear%20infrastructure),%20which%20are%20key%20physical%20indicators%20of%20formal%20urban%20areas.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20elif%20filename%20in%20slum_example_filenames:%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22class_label%22:%20'Slum',%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22slum_probability%22:%200.95,%20%23%20Placeholder%20probability%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22growth_forecast%22:%20%22Conceptual:%20Hardcoded%20for%20Slum%20example.%22,%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22conceptual_explanation%22:%20%22AI%20observes%20patterns%20consistent%20with%20non-durable%20housing%20characteristics%20(e.g.,%20uneven%20rooftops,%20varied%20materials),%20high%20building%20density%20(e.g.,%20bunched%20houses),%20and%20irregular%20urban%20morphology%20(e.g.,%20informal%20layout),%20which%20are%20key%20physical%20indicators%20of%20slums.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%23%20---%20End%20Hardcoding%20---%0A%0A%20%20%20%20%20%20%20%20%23%20Handle%20NumPy%20array%20input%20from%20Gradio%20or%20path/BytesIO%20for%20other%20images%0A%20%20%20%20%20%20%20%20if%20isinstance(image_input,%20np.ndarray):%0A%20%20%20%20%20%20%20%20%20%20%20%20img%20=%20Image.fromarray(image_input.astype('uint8'))%0A%20%20%20%20%20%20%20%20else:%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Load%20the%20image%20and%20resize%20it%20to%20the%20target%20size%20(for%20path-like%20or%20BytesIO%20inputs)%0A%20%20%20%20%20%20%20%20%20%20%20%20img%20=%20keras_image_preprocessing.load_img(image_input,%20target_size=IMG_SHAPE%5B:2%5D)%0A%0A%20%20%20%20%20%20%20%20%23%20Resize%20PIL%20image%20if%20it%20came%20from%20numpy%20array%20conversion%0A%20%20%20%20%20%20%20%20img%20=%20img.resize(IMG_SHAPE%5B:2%5D)%0A%0A%20%20%20%20%20%20%20%20%23%20Convert%20the%20image%20to%20a%20numpy%20array%0A%20%20%20%20%20%20%20%20img_array%20=%20keras_image_preprocessing.img_to_array(img)%0A%20%20%20%20%20%20%20%20%23%20Normalize%20the%20image%20pixels%0A%20%20%20%20%20%20%20%20img_array%20=%20img_array%20/%20255.0%0A%20%20%20%20%20%20%20%20%23%20Expand%20dimensions%20to%20create%20a%20batch%20dimension%20(1,%20height,%20width,%20channels)k%0A%20%20%20%20%20%20%20%20img_array%20=%20np.expand_dims(img_array,%20axis=0)%0A%0A%20%20%20%20%20%20%20%20%23%20Make%20prediction%0A%20%20%20%20%20%20%20%20predictions%20=%20model.predict(img_array)%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20the%20predicted%20class%20index%20and%20probability%0A%20%20%20%20%20%20%20%20predicted_class_index%20=%20np.argmax(predictions%5B0%5D)%0A%20%20%20%20%20%20%20%20predicted_class_label%20=%20class_names%5Bpredicted_class_index%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Get%20the%20probability%20for%20the%20'Slum'%20class%20(assuming%20'Slum'%20is%20at%20index%201)%0A%20%20%20%20%20%20%20%20slum_probability%20=%20float(predictions%5B0%5D%5Bclass_names.index('Slum')%5D)%0A%0A%20%20%20%20%20%20%20%20%23%20Conceptual%20Growth%20Forecast%20placeholder%0A%20%20%20%20%20%20%20%20growth_forecast_conceptual%20=%20%22Conceptual:%20Growth%20forecast%20data%20is%20not%20yet%20integrated.%22%0A%0A%20%20%20%20%20%20%20%20%23%20Determine%20conceptual%20explanation%20based%20on%20predicted%20class%0A%20%20%20%20%20%20%20%20conceptual_explanation_text%20=%20%22%22%0A%20%20%20%20%20%20%20%20if%20predicted_class_label%20==%20'Slum':%0A%20%20%20%20%20%20%20%20%20%20%20%20conceptual_explanation_text%20=%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22AI%20observes%20patterns%20consistent%20with%20non-durable%20housing%20characteristics%20(e.g.,%20uneven%20rooftops,%20varied%20materials),%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22high%20building%20density%20(e.g.,%20bunched%20houses),%20and%20irregular%20urban%20morphology%20(e.g.,%20informal%20layout),%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22which%20are%20key%20physical%20indicators%20of%20slums.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20elif%20predicted_class_label%20==%20'Formal%20City':%0A%20%20%20%20%20%20%20%20%20%20%20%20conceptual_explanation_text%20=%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22AI%20observes%20patterns%20consistent%20with%20planned%20urban%20structure%20(e.g.,%20regular%20layouts,%20consistent%20setbacks),%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22durable/permanent%20housing%20characteristics%20(e.g.,%20uniform%20roofs,%20robust%20materials),%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22and%20the%20presence%20of%20municipal%20services%20(e.g.,%20clear%20infrastructure),%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22which%20are%20key%20physical%20indicators%20of%20formal%20urban%20areas.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22class_label%22:%20predicted_class_label,%0A%20%20%20%20%20%20%20%20%20%20%20%20%22slum_probability%22:%20slum_probability,%0A%20%20%20%20%20%20%20%20%20%20%20%20%22growth_forecast%22:%20growth_forecast_conceptual,%0A%20%20%20%20%20%20%20%20%20%20%20%20%22conceptual_explanation%22:%20conceptual_explanation_text%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20except%20Exception%20as%20e:%0A%20%20%20%20%20%20%20%20return%20%7B%22error%22:%20f%22Error%20during%20prediction:%20%7Be%7D%22%7D%0A%0A%23%203.%20Gradio%20Interface%0Adef%20urbix_analyze(input_img,%20example_filename=None):%0A%20%20%20%20prediction_result%20=%20predict_image_class(image_input=input_img,%20filename_hint=example_filename)%0A%0A%20%20%20%20if%20%22error%22%20in%20prediction_result:%0A%20%20%20%20%20%20%20%20return%20f%22Error:%20%7Bprediction_result%5B'error'%5D%7D%22%0A%20%20%20%20else:%0A%20%20%20%20%20%20%20%20class_label%20=%20prediction_result.get('class_label',%20'N/A')%0A%20%20%20%20%20%20%20%20conceptual_explanation%20=%20prediction_result.get('conceptual_explanation',%20'No%20explanation%20provided.')%0A%20%20%20%20%20%20%20%20%23%20Format%20the%20output%20to%20be%20clear%20and%20informative%0A%20%20%20%20%20%20%20%20return%20f%22Urbix%20Identification:%20%7Bclass_label%7D%5CnExplanation:%20%7Bconceptual_explanation%7D%22%0A%0A%23%20Example%20image%20paths%20(these%20need%20to%20be%20accessible%20to%20the%20deployed%20app)%0A%23%20For%20Hugging%20Face%20Spaces,%20you%20would%20upload%20these%20example%20images%20to%20your%20repository%0A%23%20and%20reference%20them%20relative%20to%20the%20'app.py'%20file.%20Assume%20a%20subfolder%20'examples'%0A%0Aexamples_dir%20=%20'/content/uploaded_images'%0A%0Aslum_photos%20=%20%5B%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0078.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0079.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0080.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0081%20(1).jpeg'),%20%23%20Updated%20filename%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0082.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0083.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0084.jpeg')%0A%5D%0A%0Aformal_city_photos%20=%20%5B%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0073.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0074.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0075.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0076.jpeg'),%0A%20%20%20%20os.path.join(examples_dir,%20'IMG_0077.jpeg')%0A%5D%0A%0A%23%20Combine%20all%20example%20paths,%20and%20for%20each%20example,%20pass%20both%20the%20image%20path%20and%20its%20basename%0Aall_examples%20=%20%5B%5Bpath,%20os.path.basename(path)%5D%20for%20path%20in%20slum_photos%20+%20formal_city_photos%5D%0A%0Ademo%20=%20gr.Interface(%0A%20%20%20%20fn=urbix_analyze,%0A%20%20%20%20inputs=%5Bgr.Image(),%20gr.Textbox(visible=False)%5D,%20%23%20image_input%20and%20example_filename%0A%20%20%20%20outputs=%22text%22,%0A%20%20%20%20title=%22Urbix:%20Artificial%20Intelligence%20for%20Inclusive%20Cities%22,%0A%20%20%20%20description=%22%23%23%20Upload%20a%20satellite%20image%20to%20detect%20informal%20settlements%20anywhere%20in%20the%20world.%3Cbr%3E%3Cbr%3EPlease%20note:%20Urbix%20is%20an%20AI%20model%20and%20may%20make%20mistakes.%20This%20is%20a%20prototype%20and%20should%20not%20be%20used%20for%20critical%20decision-making.%22,%0A%20%20%20%20examples=all_examples%0A)%0A%0A%23%20The%20%60if%20__name__%20==%20%22__main__%22:%60%20block%20is%20typically%20used%20when%20this%20script%20is%20run%20directly.%0A%23%20For%20Colab,%20%60demo.launch()%60%20is%20often%20called%20outside%20this%20block%20or%20within%20it,%20depending%20on%20context.%0A%23%20For%20deployment%20with%20%60uvicorn%60,%20the%20%60demo%60%20object%20is%20directly%20imported%20and%20served.%0A%23%20Given%20the%20previous%20context%20of%20%60app.py%60%20and%20Dockerfile,%20it's%20best%20to%20include%20the%20%60if%20__name__%60%20guard.%0Aif%20__name__%20==%20%22__main__%22:%0A%20%20%20%20demo.launch(share=True)%20%23%20The%20'share=True'%20gives%20you%20a%20link%20you%20can%20send%20to%20anyone!%0ARunning%20on%20public%20URL:%20https://9f191404536439da27.gradio.live%0Ahttps://huggingface.co/UrbixAI/Urbix/resolve/main/.gitattributes

Tose10 changed discussion status to closed

Sign up or log in to comment