Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gradio/certificate.pem +31 -0
- .ipynb_checkpoints/README-checkpoint.md +17 -0
- .ipynb_checkpoints/lighthouse-checkpoint.ipynb +6 -0
- .ipynb_checkpoints/lighthouse-checkpoint.py +235 -0
- .ipynb_checkpoints/lighthouses-checkpoint.json +1 -0
- .ipynb_checkpoints/requirements-checkpoint.txt +1 -0
- README.md +19 -7
- lighthouse.ipynb +462 -0
- lighthouse.py +234 -0
- lighthouses.json +1 -0
- requirements.txt +1 -0
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/R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qkSsisRcOj/KK
|
| 23 |
+
NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
|
| 24 |
+
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
|
| 25 |
+
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
|
| 26 |
+
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
|
| 27 |
+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
|
| 28 |
+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
|
| 29 |
+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
| 30 |
+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
| 31 |
+
-----END CERTIFICATE-----
|
.ipynb_checkpoints/README-checkpoint.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Lighthouse Application
|
| 2 |
+
|
| 3 |
+
There are two versions of this application - a standalone Python application, and a version running in a notebook.
|
| 4 |
+
- lighthouse.ipynb - Jupyter Notebook version
|
| 5 |
+
- lighthouse.py - Python version
|
| 6 |
+
- lighthouses.json - Used for local storage by the lighthouse application. Should be empty to start.
|
| 7 |
+
|
| 8 |
+
Gradio, an accessible library for visualising machine learning processes developed by HuggingFace, is used as a way to provide a basic interface.
|
| 9 |
+
You should not need to develop a complex understanding of this library for this assignment, but the documentation is available here: [Gradio Docs](https://www.gradio.app/docs)
|
| 10 |
+
By default, the jupyter notebook environment runs version [3.11.7 of Python](https://www.python.org/downloads/release/python-3117/) - you should not run into problems with other versions of Python, but be aware that different versions of Python and different interpreters have their own functionalities.
|
| 11 |
+
|
| 12 |
+
# To run this app in a notebook:
|
| 13 |
+
# 1. Run the bash cell to install the gradio library. You should only need to do this once.
|
| 14 |
+
|
| 15 |
+
# To run this app locally:
|
| 16 |
+
# 1. In your terminal, install Gradio: pip install gradio
|
| 17 |
+
# 2. Run from your terminal: python lighthouse.py
|
.ipynb_checkpoints/lighthouse-checkpoint.ipynb
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [],
|
| 3 |
+
"metadata": {},
|
| 4 |
+
"nbformat": 4,
|
| 5 |
+
"nbformat_minor": 5
|
| 6 |
+
}
|
.ipynb_checkpoints/lighthouse-checkpoint.py
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 4 |
+
import gradio as gr
|
| 5 |
+
import json
|
| 6 |
+
from datetime import date
|
| 7 |
+
|
| 8 |
+
# ==
|
| 9 |
+
# ===================================================================================
|
| 10 |
+
# PROJECT CODE STARTS HERE
|
| 11 |
+
# Students will modify the code below this line.
|
| 12 |
+
# ===================================================================================
|
| 13 |
+
|
| 14 |
+
# ### WEEK 1: The Blueprint - Modeling a Lighthouse ###
|
| 15 |
+
# Focus: Abstraction, Automation, Analysis
|
| 16 |
+
# -----------------------------------------------------------------------------------
|
| 17 |
+
# TODO: Implement the Lighthouse data model.
|
| 18 |
+
# - Use a class to represent a lighthouse.
|
| 19 |
+
# - Add validation logic inside its __init__ method.
|
| 20 |
+
|
| 21 |
+
class Lighthouse:
|
| 22 |
+
"""Represents a single lighthouse with validation."""
|
| 23 |
+
def __init__(self, name: str, location: str, is_operational: bool, last_inspected_date: str):
|
| 24 |
+
# Validate name: must be a non-empty string
|
| 25 |
+
if not isinstance(name, str) or not name.strip():
|
| 26 |
+
raise ValueError("Name must be a non-empty string.")
|
| 27 |
+
# Validate location: must be a non-empty string
|
| 28 |
+
if not isinstance(location, str) or not location.strip():
|
| 29 |
+
raise ValueError("Location must be a non-empty string.")
|
| 30 |
+
# Validate is_operational: must be a boolean
|
| 31 |
+
if not isinstance(is_operational, bool):
|
| 32 |
+
raise TypeError("Is Operational must be a boolean (True/False).")
|
| 33 |
+
# Validate date: must be in 'YYYY-MM-DD' format
|
| 34 |
+
try:
|
| 35 |
+
parsed_date = date.fromisoformat(last_inspected_date)
|
| 36 |
+
except (ValueError, TypeError):
|
| 37 |
+
raise ValueError("Last Inspected Date must be in YYYY-MM-DD format.")
|
| 38 |
+
|
| 39 |
+
self.name = name.strip()
|
| 40 |
+
self.location = location.strip()
|
| 41 |
+
self.is_operational = is_operational
|
| 42 |
+
self.last_inspected_date = parsed_date
|
| 43 |
+
|
| 44 |
+
def to_dict(self):
|
| 45 |
+
"""Converts the object to a dictionary for JSON serialization."""
|
| 46 |
+
return {
|
| 47 |
+
"name": self.name,
|
| 48 |
+
"location": self.location,
|
| 49 |
+
"is_operational": self.is_operational,
|
| 50 |
+
"last_inspected_date": self.last_inspected_date.isoformat()
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
def __repr__(self):
|
| 54 |
+
"""Provides a developer-friendly string representation of the object."""
|
| 55 |
+
status = "Operational" if self.is_operational else "Non-Operational"
|
| 56 |
+
return f"β
{self.name} ({self.location}) - Status: {status}, Last Inspected: {self.last_inspected_date.isoformat()}"
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# ### WEEK 5: Scaling Up - The Lighthouse Authority ###
|
| 60 |
+
# Focus: Abstraction, Automation, Analysis
|
| 61 |
+
# -----------------------------------------------------------------------------------
|
| 62 |
+
# TODO: Refactor all functions into this LighthouseRegistry class.
|
| 63 |
+
|
| 64 |
+
class LighthouseRegistry:
|
| 65 |
+
"""Manages the collection of all lighthouses."""
|
| 66 |
+
def __init__(self, filepath="lighthouses.json"):
|
| 67 |
+
self.lighthouses = []
|
| 68 |
+
self.filepath = filepath
|
| 69 |
+
|
| 70 |
+
# ### WEEK 2: First Steps - The Lighthouse Logbook ###
|
| 71 |
+
# TODO: Implement the add_lighthouse method.
|
| 72 |
+
def add_lighthouse(self, name: str, location: str, is_operational: bool, last_inspected_date: str):
|
| 73 |
+
"""Creates and adds a new Lighthouse to the registry after validation."""
|
| 74 |
+
try:
|
| 75 |
+
new_lighthouse = Lighthouse(name, location, is_operational, last_inspected_date)
|
| 76 |
+
self.lighthouses.append(new_lighthouse)
|
| 77 |
+
return f"Success: Added '{name}' to the registry."
|
| 78 |
+
except (ValueError, TypeError) as e:
|
| 79 |
+
return f"β Error: Could not add lighthouse. {e}"
|
| 80 |
+
|
| 81 |
+
# ### WEEK 3: Making Sense of the Data - Fleet Status Reports ###
|
| 82 |
+
# TODO: Implement the generate_status_report method for ANALYSIS.
|
| 83 |
+
def generate_status_report(self):
|
| 84 |
+
"""Analyzes the current list of lighthouses and generates a summary report."""
|
| 85 |
+
if not self.lighthouses:
|
| 86 |
+
return "Cannot generate report: No lighthouses in the registry."
|
| 87 |
+
|
| 88 |
+
total = len(self.lighthouses)
|
| 89 |
+
operational_count = sum(1 for lh in self.lighthouses if lh.is_operational)
|
| 90 |
+
non_operational_count = total - operational_count
|
| 91 |
+
|
| 92 |
+
most_recent_lighthouse = max(self.lighthouses, key=lambda lh: lh.last_inspected_date)
|
| 93 |
+
|
| 94 |
+
report = (
|
| 95 |
+
"π ===== Lighthouse Fleet Status Report ===== π\n"
|
| 96 |
+
f"Total Lighthouses: {total}\n"
|
| 97 |
+
f" - Operational: {operational_count}\n"
|
| 98 |
+
f" - Non-Operational: {non_operational_count}\n\n"
|
| 99 |
+
f"Most Recently Inspected:\n"
|
| 100 |
+
f" - Name: {most_recent_lighthouse.name}\n"
|
| 101 |
+
f" - Date: {most_recent_lighthouse.last_inspected_date.isoformat()}\n"
|
| 102 |
+
"=========================================="
|
| 103 |
+
)
|
| 104 |
+
return report
|
| 105 |
+
|
| 106 |
+
def list_lighthouses(self):
|
| 107 |
+
"""Returns a formatted string listing all lighthouses in the registry."""
|
| 108 |
+
if not self.lighthouses:
|
| 109 |
+
return "No lighthouses in the registry."
|
| 110 |
+
|
| 111 |
+
header = "--- List of All Lighthouses ---"
|
| 112 |
+
# Use the __repr__ from the Lighthouse class for a clean format
|
| 113 |
+
report_lines = [repr(lh) for lh in self.lighthouses]
|
| 114 |
+
return f"{header}\n" + "\n".join(report_lines)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
# ### WEEK 4: Making It Real - The Official Record ###
|
| 118 |
+
# TODO: Implement save_to_file and load_from_file to AUTOMATE persistence.
|
| 119 |
+
def save_to_file(self):
|
| 120 |
+
"""Saves the current list of lighthouses to the JSON file."""
|
| 121 |
+
try:
|
| 122 |
+
# Convert each Lighthouse object to its dictionary representation
|
| 123 |
+
data_to_save = [lh.to_dict() for lh in self.lighthouses]
|
| 124 |
+
with open(self.filepath, 'w') as f:
|
| 125 |
+
json.dump(data_to_save, f, indent=4)
|
| 126 |
+
return f"πΎ Success: Saved {len(self.lighthouses)} lighthouses to {self.filepath}."
|
| 127 |
+
except (IOError, TypeError) as e:
|
| 128 |
+
return f"β Error: Could not save to file. {e}"
|
| 129 |
+
|
| 130 |
+
def load_from_file(self):
|
| 131 |
+
"""Loads lighthouses from the JSON file, recreating the objects."""
|
| 132 |
+
try:
|
| 133 |
+
with open(self.filepath, 'r') as f:
|
| 134 |
+
data = json.load(f)
|
| 135 |
+
self.lighthouses = [] # Clear the current list before loading
|
| 136 |
+
for item in data:
|
| 137 |
+
# The Lighthouse constructor handles validation of the loaded data
|
| 138 |
+
lh = Lighthouse(**item)
|
| 139 |
+
self.lighthouses.append(lh)
|
| 140 |
+
return f"π Success: Loaded {len(self.lighthouses)} lighthouses from {self.filepath}."
|
| 141 |
+
except FileNotFoundError:
|
| 142 |
+
return f"βΉοΈ Info: No save file found at '{self.filepath}'. Starting with an empty registry."
|
| 143 |
+
except (json.JSONDecodeError, ValueError, TypeError) as e:
|
| 144 |
+
# Catches corrupted JSON, or data that fails Lighthouse validation
|
| 145 |
+
return f"β Error: Could not load from file. File may be corrupt or malformed. {e}"
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# ===================================================================================
|
| 149 |
+
# GRADIO UI CODE
|
| 150 |
+
# Students should not need to modify below this line initially,
|
| 151 |
+
# but can extend it in Week 5.
|
| 152 |
+
# ===================================================================================
|
| 153 |
+
|
| 154 |
+
def create_ui(registry: LighthouseRegistry):
|
| 155 |
+
"""Creates the Gradio web interface."""
|
| 156 |
+
# Helper functions to interact with the registry and update the UI
|
| 157 |
+
def add_and_update(name, location, is_op, date_str, current_log):
|
| 158 |
+
result = registry.add_lighthouse(name, location, is_op, date_str)
|
| 159 |
+
new_log = f"{current_log}\n> {result}"
|
| 160 |
+
return new_log
|
| 161 |
+
|
| 162 |
+
def report_and_update(current_log):
|
| 163 |
+
result = registry.generate_status_report()
|
| 164 |
+
new_log = f"{current_log}\n\n{result}\n"
|
| 165 |
+
return new_log
|
| 166 |
+
|
| 167 |
+
def list_and_update(current_log):
|
| 168 |
+
result = registry.list_lighthouses()
|
| 169 |
+
new_log = f"{current_log}\n\n{result}\n"
|
| 170 |
+
return new_log
|
| 171 |
+
|
| 172 |
+
def save_and_update(current_log):
|
| 173 |
+
result = registry.save_to_file()
|
| 174 |
+
new_log = f"{current_log}\n> {result}"
|
| 175 |
+
return new_log
|
| 176 |
+
|
| 177 |
+
def load_and_update(current_log):
|
| 178 |
+
result = registry.load_from_file()
|
| 179 |
+
new_log = f"{current_log}\n> {result}"
|
| 180 |
+
# After loading, also list the lighthouses to show what was loaded
|
| 181 |
+
list_result = registry.list_lighthouses()
|
| 182 |
+
return f"{new_log}\n\n{list_result}\n"
|
| 183 |
+
|
| 184 |
+
# Define the Gradio interface layout
|
| 185 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), title="Lighthouse Lookout") as app:
|
| 186 |
+
gr.Markdown("# π’ Lighthouse Lookout")
|
| 187 |
+
gr.Markdown("A tool for monitoring and managing a fleet of lighthouses.")
|
| 188 |
+
log_textbox = gr.Textbox(
|
| 189 |
+
label="Log & Reports",
|
| 190 |
+
value="Welcome! Your command results will appear here.",
|
| 191 |
+
lines=15,
|
| 192 |
+
interactive=False,
|
| 193 |
+
elem_classes="terminal" # Just for potential styling
|
| 194 |
+
)
|
| 195 |
+
with gr.Row():
|
| 196 |
+
report_btn = gr.Button("π Generate Status Report")
|
| 197 |
+
list_btn = gr.Button("π List All Lighthouses")
|
| 198 |
+
|
| 199 |
+
with gr.Accordion("β Add a New Lighthouse", open=False):
|
| 200 |
+
with gr.Row():
|
| 201 |
+
name_input = gr.Textbox(label="Name", placeholder="e.g., Trinity Lighthouse")
|
| 202 |
+
location_input = gr.Textbox(label="Location", placeholder="e.g., 50.18, -0.16")
|
| 203 |
+
with gr.Row():
|
| 204 |
+
is_operational_input = gr.Checkbox(label="Is Operational?", value=True)
|
| 205 |
+
# Using a string for the date to align with the Lighthouse class constructor
|
| 206 |
+
date_input = gr.Textbox(label="Last Inspected Date", placeholder="YYYY-MM-DD")
|
| 207 |
+
add_btn = gr.Button("Add Lighthouse")
|
| 208 |
+
|
| 209 |
+
with gr.Row():
|
| 210 |
+
save_btn = gr.Button("πΎ Save Registry")
|
| 211 |
+
load_btn = gr.Button("π Load Registry")
|
| 212 |
+
|
| 213 |
+
# Connect UI components to the helper functions
|
| 214 |
+
add_btn.click(
|
| 215 |
+
fn=add_and_update,
|
| 216 |
+
inputs=[name_input, location_input, is_operational_input, date_input, log_textbox],
|
| 217 |
+
outputs=[log_textbox]
|
| 218 |
+
)
|
| 219 |
+
report_btn.click(fn=report_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 220 |
+
list_btn.click(fn=list_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 221 |
+
save_btn.click(fn=save_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 222 |
+
load_btn.click(fn=load_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 223 |
+
|
| 224 |
+
return app
|
| 225 |
+
|
| 226 |
+
# --- Main execution block ---
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
# Create a single instance of our registry that the app will use.
|
| 229 |
+
lighthouse_registry = LighthouseRegistry()
|
| 230 |
+
# Load any existing data on startup
|
| 231 |
+
initial_load_message = lighthouse_registry.load_from_file()
|
| 232 |
+
print(initial_load_message) # Print to terminal for the developer
|
| 233 |
+
# Create and launch the Gradio app
|
| 234 |
+
web_app = create_ui(lighthouse_registry)
|
| 235 |
+
web_app.launch(share=True)
|
.ipynb_checkpoints/lighthouses-checkpoint.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[]
|
.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio==5.45.0
|
README.md
CHANGED
|
@@ -1,12 +1,24 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.47.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: lighthouse
|
| 3 |
+
app_file: lighthouse.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.47.0
|
|
|
|
|
|
|
| 6 |
---
|
| 7 |
+
# Lighthouse Application
|
| 8 |
|
| 9 |
+
There are two versions of this application - a standalone Python application, and a version running in a notebook.
|
| 10 |
+
- lighthouse.ipynb - Jupyter Notebook version
|
| 11 |
+
- lighthouse.py - Python version
|
| 12 |
+
- lighthouses.json - Used for local storage by the lighthouse application. Should be empty to start.
|
| 13 |
+
|
| 14 |
+
Gradio, an accessible library for visualising machine learning processes developed by HuggingFace, is used as a way to provide a basic interface.
|
| 15 |
+
You should not need to develop a complex understanding of this library for this assignment, but the documentation is available here: [Gradio Docs](https://www.gradio.app/docs)
|
| 16 |
+
|
| 17 |
+
By default, the jupyter notebook environment runs version [3.11.7 of Python](https://www.python.org/downloads/release/python-3117/) - you should not run into problems with other versions of Python, but be aware that different versions of Python and different interpreters have their own functionalities.
|
| 18 |
+
|
| 19 |
+
## To run this app in a notebook:
|
| 20 |
+
1. Run the bash cell to install the gradio library. You should only need to do this once.
|
| 21 |
+
|
| 22 |
+
## To run this app locally:
|
| 23 |
+
1. In your terminal, install Gradio: pip install gradio
|
| 24 |
+
2. Run from your terminal: python lighthouse.py
|
lighthouse.ipynb
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"id": "f8f72ea7-273c-49da-8af7-09e9fa934b83",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"Collecting gradio\n",
|
| 14 |
+
" Downloading gradio-5.47.0-py3-none-any.whl.metadata (16 kB)\n",
|
| 15 |
+
"Collecting aiofiles<25.0,>=22.0 (from gradio)\n",
|
| 16 |
+
" Using cached aiofiles-24.1.0-py3-none-any.whl.metadata (10 kB)\n",
|
| 17 |
+
"Requirement already satisfied: anyio<5.0,>=3.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (4.2.0)\n",
|
| 18 |
+
"Requirement already satisfied: brotli>=1.1.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (1.1.0)\n",
|
| 19 |
+
"Collecting fastapi<1.0,>=0.115.2 (from gradio)\n",
|
| 20 |
+
" Downloading fastapi-0.117.1-py3-none-any.whl.metadata (28 kB)\n",
|
| 21 |
+
"Collecting ffmpy (from gradio)\n",
|
| 22 |
+
" Using cached ffmpy-0.6.1-py3-none-any.whl.metadata (2.9 kB)\n",
|
| 23 |
+
"Collecting gradio-client==1.13.2 (from gradio)\n",
|
| 24 |
+
" Downloading gradio_client-1.13.2-py3-none-any.whl.metadata (7.1 kB)\n",
|
| 25 |
+
"Collecting groovy~=0.1 (from gradio)\n",
|
| 26 |
+
" Using cached groovy-0.1.2-py3-none-any.whl.metadata (6.1 kB)\n",
|
| 27 |
+
"Collecting httpx<1.0,>=0.24.1 (from gradio)\n",
|
| 28 |
+
" Using cached httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n",
|
| 29 |
+
"Collecting huggingface-hub<1.0,>=0.33.5 (from gradio)\n",
|
| 30 |
+
" Downloading huggingface_hub-0.35.1-py3-none-any.whl.metadata (14 kB)\n",
|
| 31 |
+
"Requirement already satisfied: jinja2<4.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (3.1.3)\n",
|
| 32 |
+
"Requirement already satisfied: markupsafe<4.0,>=2.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (2.1.3)\n",
|
| 33 |
+
"Requirement already satisfied: numpy<3.0,>=1.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (1.26.3)\n",
|
| 34 |
+
"Collecting orjson~=3.0 (from gradio)\n",
|
| 35 |
+
" Using cached orjson-3.11.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (41 kB)\n",
|
| 36 |
+
"Requirement already satisfied: packaging in /opt/conda/lib/python3.11/site-packages (from gradio) (23.2)\n",
|
| 37 |
+
"Requirement already satisfied: pandas<3.0,>=1.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (2.1.4)\n",
|
| 38 |
+
"Requirement already satisfied: pillow<12.0,>=8.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (10.2.0)\n",
|
| 39 |
+
"Collecting pydantic<2.12,>=2.0 (from gradio)\n",
|
| 40 |
+
" Downloading pydantic-2.11.9-py3-none-any.whl.metadata (68 kB)\n",
|
| 41 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m68.4/68.4 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 42 |
+
"\u001b[?25hCollecting pydub (from gradio)\n",
|
| 43 |
+
" Using cached pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
|
| 44 |
+
"Collecting python-multipart>=0.0.18 (from gradio)\n",
|
| 45 |
+
" Using cached python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n",
|
| 46 |
+
"Requirement already satisfied: pyyaml<7.0,>=5.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (6.0.1)\n",
|
| 47 |
+
"Collecting ruff>=0.9.3 (from gradio)\n",
|
| 48 |
+
" Downloading ruff-0.13.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n",
|
| 49 |
+
"Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n",
|
| 50 |
+
" Using cached safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n",
|
| 51 |
+
"Collecting semantic-version~=2.0 (from gradio)\n",
|
| 52 |
+
" Using cached semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
|
| 53 |
+
"Collecting starlette<1.0,>=0.40.0 (from gradio)\n",
|
| 54 |
+
" Downloading starlette-0.48.0-py3-none-any.whl.metadata (6.3 kB)\n",
|
| 55 |
+
"Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n",
|
| 56 |
+
" Using cached tomlkit-0.13.3-py3-none-any.whl.metadata (2.8 kB)\n",
|
| 57 |
+
"Collecting typer<1.0,>=0.12 (from gradio)\n",
|
| 58 |
+
" Downloading typer-0.19.2-py3-none-any.whl.metadata (16 kB)\n",
|
| 59 |
+
"Requirement already satisfied: typing-extensions~=4.0 in /opt/conda/lib/python3.11/site-packages (from gradio) (4.9.0)\n",
|
| 60 |
+
"Collecting uvicorn>=0.14.0 (from gradio)\n",
|
| 61 |
+
" Downloading uvicorn-0.37.0-py3-none-any.whl.metadata (6.6 kB)\n",
|
| 62 |
+
"Requirement already satisfied: fsspec in /opt/conda/lib/python3.11/site-packages (from gradio-client==1.13.2->gradio) (2023.12.2)\n",
|
| 63 |
+
"Collecting websockets<16.0,>=13.0 (from gradio-client==1.13.2->gradio)\n",
|
| 64 |
+
" Downloading websockets-15.0.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.8 kB)\n",
|
| 65 |
+
"Requirement already satisfied: idna>=2.8 in /opt/conda/lib/python3.11/site-packages (from anyio<5.0,>=3.0->gradio) (3.6)\n",
|
| 66 |
+
"Requirement already satisfied: sniffio>=1.1 in /opt/conda/lib/python3.11/site-packages (from anyio<5.0,>=3.0->gradio) (1.3.0)\n",
|
| 67 |
+
"Requirement already satisfied: certifi in /opt/conda/lib/python3.11/site-packages (from httpx<1.0,>=0.24.1->gradio) (2024.2.2)\n",
|
| 68 |
+
"Collecting httpcore==1.* (from httpx<1.0,>=0.24.1->gradio)\n",
|
| 69 |
+
" Using cached httpcore-1.0.9-py3-none-any.whl.metadata (21 kB)\n",
|
| 70 |
+
"Collecting h11>=0.16 (from httpcore==1.*->httpx<1.0,>=0.24.1->gradio)\n",
|
| 71 |
+
" Using cached h11-0.16.0-py3-none-any.whl.metadata (8.3 kB)\n",
|
| 72 |
+
"Collecting filelock (from huggingface-hub<1.0,>=0.33.5->gradio)\n",
|
| 73 |
+
" Using cached filelock-3.19.1-py3-none-any.whl.metadata (2.1 kB)\n",
|
| 74 |
+
"Requirement already satisfied: requests in /opt/conda/lib/python3.11/site-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (2.31.0)\n",
|
| 75 |
+
"Requirement already satisfied: tqdm>=4.42.1 in /opt/conda/lib/python3.11/site-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (4.66.1)\n",
|
| 76 |
+
"Collecting hf-xet<2.0.0,>=1.1.3 (from huggingface-hub<1.0,>=0.33.5->gradio)\n",
|
| 77 |
+
" Downloading hf_xet-1.1.10-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.7 kB)\n",
|
| 78 |
+
"Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.11/site-packages (from pandas<3.0,>=1.0->gradio) (2.8.2)\n",
|
| 79 |
+
"Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.11/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n",
|
| 80 |
+
"Requirement already satisfied: tzdata>=2022.1 in /opt/conda/lib/python3.11/site-packages (from pandas<3.0,>=1.0->gradio) (2023.4)\n",
|
| 81 |
+
"Collecting annotated-types>=0.6.0 (from pydantic<2.12,>=2.0->gradio)\n",
|
| 82 |
+
" Using cached annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",
|
| 83 |
+
"Collecting pydantic-core==2.33.2 (from pydantic<2.12,>=2.0->gradio)\n",
|
| 84 |
+
" Using cached pydantic_core-2.33.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.8 kB)\n",
|
| 85 |
+
"Collecting typing-extensions~=4.0 (from gradio)\n",
|
| 86 |
+
" Using cached typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB)\n",
|
| 87 |
+
"Collecting typing-inspection>=0.4.0 (from pydantic<2.12,>=2.0->gradio)\n",
|
| 88 |
+
" Using cached typing_inspection-0.4.1-py3-none-any.whl.metadata (2.6 kB)\n",
|
| 89 |
+
"Requirement already satisfied: click>=8.0.0 in /opt/conda/lib/python3.11/site-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
|
| 90 |
+
"Collecting shellingham>=1.3.0 (from typer<1.0,>=0.12->gradio)\n",
|
| 91 |
+
" Using cached shellingham-1.5.4-py2.py3-none-any.whl.metadata (3.5 kB)\n",
|
| 92 |
+
"Collecting rich>=10.11.0 (from typer<1.0,>=0.12->gradio)\n",
|
| 93 |
+
" Using cached rich-14.1.0-py3-none-any.whl.metadata (18 kB)\n",
|
| 94 |
+
"Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.16.0)\n",
|
| 95 |
+
"Collecting markdown-it-py>=2.2.0 (from rich>=10.11.0->typer<1.0,>=0.12->gradio)\n",
|
| 96 |
+
" Using cached markdown_it_py-4.0.0-py3-none-any.whl.metadata (7.3 kB)\n",
|
| 97 |
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/conda/lib/python3.11/site-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.17.2)\n",
|
| 98 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.11/site-packages (from requests->huggingface-hub<1.0,>=0.33.5->gradio) (3.3.2)\n",
|
| 99 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.11/site-packages (from requests->huggingface-hub<1.0,>=0.33.5->gradio) (2.1.0)\n",
|
| 100 |
+
"Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio)\n",
|
| 101 |
+
" Using cached mdurl-0.1.2-py3-none-any.whl.metadata (1.6 kB)\n",
|
| 102 |
+
"Downloading gradio-5.47.0-py3-none-any.whl (60.4 MB)\n",
|
| 103 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m60.4/60.4 MB\u001b[0m \u001b[31m51.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
|
| 104 |
+
"\u001b[?25hDownloading gradio_client-1.13.2-py3-none-any.whl (325 kB)\n",
|
| 105 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m325.3/325.3 kB\u001b[0m \u001b[31m8.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n",
|
| 106 |
+
"\u001b[?25hUsing cached aiofiles-24.1.0-py3-none-any.whl (15 kB)\n",
|
| 107 |
+
"Downloading fastapi-0.117.1-py3-none-any.whl (95 kB)\n",
|
| 108 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m96.0/96.0 kB\u001b[0m \u001b[31m3.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 109 |
+
"\u001b[?25hUsing cached groovy-0.1.2-py3-none-any.whl (14 kB)\n",
|
| 110 |
+
"Using cached httpx-0.28.1-py3-none-any.whl (73 kB)\n",
|
| 111 |
+
"Using cached httpcore-1.0.9-py3-none-any.whl (78 kB)\n",
|
| 112 |
+
"Downloading huggingface_hub-0.35.1-py3-none-any.whl (563 kB)\n",
|
| 113 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m563.3/563.3 kB\u001b[0m \u001b[31m17.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 114 |
+
"\u001b[?25hUsing cached orjson-3.11.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (132 kB)\n",
|
| 115 |
+
"Downloading pydantic-2.11.9-py3-none-any.whl (444 kB)\n",
|
| 116 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m444.9/444.9 kB\u001b[0m \u001b[31m16.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 117 |
+
"\u001b[?25hUsing cached pydantic_core-2.33.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB)\n",
|
| 118 |
+
"Using cached python_multipart-0.0.20-py3-none-any.whl (24 kB)\n",
|
| 119 |
+
"Downloading ruff-0.13.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.2 MB)\n",
|
| 120 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m13.2/13.2 MB\u001b[0m \u001b[31m72.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m0:01\u001b[0m\n",
|
| 121 |
+
"\u001b[?25hUsing cached safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n",
|
| 122 |
+
"Using cached semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
|
| 123 |
+
"Downloading starlette-0.48.0-py3-none-any.whl (73 kB)\n",
|
| 124 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m73.7/73.7 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 125 |
+
"\u001b[?25hUsing cached tomlkit-0.13.3-py3-none-any.whl (38 kB)\n",
|
| 126 |
+
"Downloading typer-0.19.2-py3-none-any.whl (46 kB)\n",
|
| 127 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m46.7/46.7 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 128 |
+
"\u001b[?25hUsing cached typing_extensions-4.15.0-py3-none-any.whl (44 kB)\n",
|
| 129 |
+
"Downloading uvicorn-0.37.0-py3-none-any.whl (67 kB)\n",
|
| 130 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m68.0/68.0 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 131 |
+
"\u001b[?25hUsing cached ffmpy-0.6.1-py3-none-any.whl (5.5 kB)\n",
|
| 132 |
+
"Using cached pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
|
| 133 |
+
"Using cached annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
|
| 134 |
+
"Using cached h11-0.16.0-py3-none-any.whl (37 kB)\n",
|
| 135 |
+
"Downloading hf_xet-1.1.10-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.2 MB)\n",
|
| 136 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m3.2/3.2 MB\u001b[0m \u001b[31m61.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n",
|
| 137 |
+
"\u001b[?25hUsing cached rich-14.1.0-py3-none-any.whl (243 kB)\n",
|
| 138 |
+
"Using cached shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB)\n",
|
| 139 |
+
"Using cached typing_inspection-0.4.1-py3-none-any.whl (14 kB)\n",
|
| 140 |
+
"Downloading websockets-15.0.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182 kB)\n",
|
| 141 |
+
"\u001b[2K \u001b[90mββββββββββββββββββββββββββββββββββββββββ\u001b[0m \u001b[32m182.3/182.3 kB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
| 142 |
+
"\u001b[?25hUsing cached filelock-3.19.1-py3-none-any.whl (15 kB)\n",
|
| 143 |
+
"Using cached markdown_it_py-4.0.0-py3-none-any.whl (87 kB)\n",
|
| 144 |
+
"Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n",
|
| 145 |
+
"Installing collected packages: pydub, websockets, typing-extensions, tomlkit, shellingham, semantic-version, ruff, python-multipart, orjson, mdurl, hf-xet, h11, groovy, filelock, ffmpy, annotated-types, aiofiles, uvicorn, typing-inspection, starlette, pydantic-core, markdown-it-py, huggingface-hub, httpcore, rich, pydantic, httpx, typer, safehttpx, gradio-client, fastapi, gradio\n",
|
| 146 |
+
" Attempting uninstall: websockets\n",
|
| 147 |
+
" Found existing installation: websockets 12.0\n",
|
| 148 |
+
" Uninstalling websockets-12.0:\n",
|
| 149 |
+
" Successfully uninstalled websockets-12.0\n",
|
| 150 |
+
" Attempting uninstall: typing-extensions\n",
|
| 151 |
+
" Found existing installation: typing_extensions 4.9.0\n",
|
| 152 |
+
" Uninstalling typing_extensions-4.9.0:\n",
|
| 153 |
+
" Successfully uninstalled typing_extensions-4.9.0\n",
|
| 154 |
+
"Successfully installed aiofiles-24.1.0 annotated-types-0.7.0 fastapi-0.117.1 ffmpy-0.6.1 filelock-3.19.1 gradio-5.47.0 gradio-client-1.13.2 groovy-0.1.2 h11-0.16.0 hf-xet-1.1.10 httpcore-1.0.9 httpx-0.28.1 huggingface-hub-0.35.1 markdown-it-py-4.0.0 mdurl-0.1.2 orjson-3.11.3 pydantic-2.11.9 pydantic-core-2.33.2 pydub-0.25.1 python-multipart-0.0.20 rich-14.1.0 ruff-0.13.1 safehttpx-0.1.6 semantic-version-2.10.0 shellingham-1.5.4 starlette-0.48.0 tomlkit-0.13.3 typer-0.19.2 typing-extensions-4.15.0 typing-inspection-0.4.1 uvicorn-0.37.0 websockets-15.0.1\n"
|
| 155 |
+
]
|
| 156 |
+
}
|
| 157 |
+
],
|
| 158 |
+
"source": [
|
| 159 |
+
"# To install gradio into the notebook environment\n",
|
| 160 |
+
"!pip install gradio"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "code",
|
| 165 |
+
"execution_count": 4,
|
| 166 |
+
"id": "e15773a1-3e7f-4d09-b777-124247954e35",
|
| 167 |
+
"metadata": {},
|
| 168 |
+
"outputs": [
|
| 169 |
+
{
|
| 170 |
+
"name": "stdout",
|
| 171 |
+
"output_type": "stream",
|
| 172 |
+
"text": [
|
| 173 |
+
"π Success: Loaded 0 lighthouses from lighthouses.json.\n",
|
| 174 |
+
"* Running on local URL: http://127.0.0.1:7861\n",
|
| 175 |
+
"* Running on public URL: https://f80ff2f7140868d8d0.gradio.live\n",
|
| 176 |
+
"\n",
|
| 177 |
+
"This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"data": {
|
| 182 |
+
"text/html": [
|
| 183 |
+
"<div><iframe src=\"https://f80ff2f7140868d8d0.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 184 |
+
],
|
| 185 |
+
"text/plain": [
|
| 186 |
+
"<IPython.core.display.HTML object>"
|
| 187 |
+
]
|
| 188 |
+
},
|
| 189 |
+
"metadata": {},
|
| 190 |
+
"output_type": "display_data"
|
| 191 |
+
}
|
| 192 |
+
],
|
| 193 |
+
"source": [
|
| 194 |
+
"\n",
|
| 195 |
+
"# To run this app:\n",
|
| 196 |
+
"# 1. Install Gradio: !pip install gradio\n",
|
| 197 |
+
"# 2. Run from your terminal: python lighthouse.py\n",
|
| 198 |
+
"\n",
|
| 199 |
+
"import gradio as gr\n",
|
| 200 |
+
"import json\n",
|
| 201 |
+
"from datetime import date\n",
|
| 202 |
+
"\n",
|
| 203 |
+
"# ===================================================================================\n",
|
| 204 |
+
"# PROJECT CODE STARTS HERE\n",
|
| 205 |
+
"# Students will modify the code below this line.\n",
|
| 206 |
+
"# ===================================================================================\n",
|
| 207 |
+
"\n",
|
| 208 |
+
"# ### WEEK 1: The Blueprint - Modeling a Lighthouse ###\n",
|
| 209 |
+
"# Focus: Abstraction, Automation, Analysis\n",
|
| 210 |
+
"# -----------------------------------------------------------------------------------\n",
|
| 211 |
+
"# TODO: Implement the Lighthouse data model.\n",
|
| 212 |
+
"# - Use a class to represent a lighthouse.\n",
|
| 213 |
+
"# - Add validation logic inside its __init__ method.\n",
|
| 214 |
+
"\n",
|
| 215 |
+
"class Lighthouse:\n",
|
| 216 |
+
" \"\"\"Represents a single lighthouse with validation.\"\"\"\n",
|
| 217 |
+
" def __init__(self, name: str, location: str, is_operational: bool, last_inspected_date: str):\n",
|
| 218 |
+
" # Validate name: must be a non-empty string\n",
|
| 219 |
+
" if not isinstance(name, str) or not name.strip():\n",
|
| 220 |
+
" raise ValueError(\"Name must be a non-empty string.\")\n",
|
| 221 |
+
" # Validate location: must be a non-empty string\n",
|
| 222 |
+
" if not isinstance(location, str) or not location.strip():\n",
|
| 223 |
+
" raise ValueError(\"Location must be a non-empty string.\")\n",
|
| 224 |
+
" # Validate is_operational: must be a boolean\n",
|
| 225 |
+
" if not isinstance(is_operational, bool):\n",
|
| 226 |
+
" raise TypeError(\"Is Operational must be a boolean (True/False).\")\n",
|
| 227 |
+
" # Validate date: must be in 'YYYY-MM-DD' format\n",
|
| 228 |
+
" try:\n",
|
| 229 |
+
" parsed_date = date.fromisoformat(last_inspected_date)\n",
|
| 230 |
+
" except (ValueError, TypeError):\n",
|
| 231 |
+
" raise ValueError(\"Last Inspected Date must be in YYYY-MM-DD format.\")\n",
|
| 232 |
+
"\n",
|
| 233 |
+
" self.name = name.strip()\n",
|
| 234 |
+
" self.location = location.strip()\n",
|
| 235 |
+
" self.is_operational = is_operational\n",
|
| 236 |
+
" self.last_inspected_date = parsed_date\n",
|
| 237 |
+
"\n",
|
| 238 |
+
" def to_dict(self):\n",
|
| 239 |
+
" \"\"\"Converts the object to a dictionary for JSON serialization.\"\"\"\n",
|
| 240 |
+
" return {\n",
|
| 241 |
+
" \"name\": self.name,\n",
|
| 242 |
+
" \"location\": self.location,\n",
|
| 243 |
+
" \"is_operational\": self.is_operational,\n",
|
| 244 |
+
" \"last_inspected_date\": self.last_inspected_date.isoformat()\n",
|
| 245 |
+
" }\n",
|
| 246 |
+
"\n",
|
| 247 |
+
" def __repr__(self):\n",
|
| 248 |
+
" \"\"\"Provides a developer-friendly string representation of the object.\"\"\"\n",
|
| 249 |
+
" status = \"Operational\" if self.is_operational else \"Non-Operational\"\n",
|
| 250 |
+
" return f\"β
{self.name} ({self.location}) - Status: {status}, Last Inspected: {self.last_inspected_date.isoformat()}\"\n",
|
| 251 |
+
"\n",
|
| 252 |
+
"\n",
|
| 253 |
+
"# ### WEEK 5: Scaling Up - The Lighthouse Authority ###\n",
|
| 254 |
+
"# Focus: Abstraction, Automation, Analysis\n",
|
| 255 |
+
"# -----------------------------------------------------------------------------------\n",
|
| 256 |
+
"# TODO: Refactor all functions into this LighthouseRegistry class.\n",
|
| 257 |
+
"\n",
|
| 258 |
+
"class LighthouseRegistry:\n",
|
| 259 |
+
" \"\"\"Manages the collection of all lighthouses.\"\"\"\n",
|
| 260 |
+
" def __init__(self, filepath=\"lighthouses.json\"):\n",
|
| 261 |
+
" self.lighthouses = []\n",
|
| 262 |
+
" self.filepath = filepath\n",
|
| 263 |
+
"\n",
|
| 264 |
+
" # ### WEEK 2: First Steps - The Lighthouse Logbook ###\n",
|
| 265 |
+
" # TODO: Implement the add_lighthouse method.\n",
|
| 266 |
+
" def add_lighthouse(self, name: str, location: str, is_operational: bool, last_inspected_date: str):\n",
|
| 267 |
+
" \"\"\"Creates and adds a new Lighthouse to the registry after validation.\"\"\"\n",
|
| 268 |
+
" try:\n",
|
| 269 |
+
" new_lighthouse = Lighthouse(name, location, is_operational, last_inspected_date)\n",
|
| 270 |
+
" self.lighthouses.append(new_lighthouse)\n",
|
| 271 |
+
" return f\"Success: Added '{name}' to the registry.\"\n",
|
| 272 |
+
" except (ValueError, TypeError) as e:\n",
|
| 273 |
+
" return f\"β Error: Could not add lighthouse. {e}\"\n",
|
| 274 |
+
"\n",
|
| 275 |
+
" # ### WEEK 3: Making Sense of the Data - Fleet Status Reports ###\n",
|
| 276 |
+
" # TODO: Implement the generate_status_report method for ANALYSIS.\n",
|
| 277 |
+
" def generate_status_report(self):\n",
|
| 278 |
+
" \"\"\"Analyzes the current list of lighthouses and generates a summary report.\"\"\"\n",
|
| 279 |
+
" if not self.lighthouses:\n",
|
| 280 |
+
" return \"Cannot generate report: No lighthouses in the registry.\"\n",
|
| 281 |
+
"\n",
|
| 282 |
+
" total = len(self.lighthouses)\n",
|
| 283 |
+
" operational_count = sum(1 for lh in self.lighthouses if lh.is_operational)\n",
|
| 284 |
+
" non_operational_count = total - operational_count\n",
|
| 285 |
+
"\n",
|
| 286 |
+
" most_recent_lighthouse = max(self.lighthouses, key=lambda lh: lh.last_inspected_date)\n",
|
| 287 |
+
"\n",
|
| 288 |
+
" report = (\n",
|
| 289 |
+
" \"π ===== Lighthouse Fleet Status Report ===== π\\n\"\n",
|
| 290 |
+
" f\"Total Lighthouses: {total}\\n\"\n",
|
| 291 |
+
" f\" - Operational: {operational_count}\\n\"\n",
|
| 292 |
+
" f\" - Non-Operational: {non_operational_count}\\n\\n\"\n",
|
| 293 |
+
" f\"Most Recently Inspected:\\n\"\n",
|
| 294 |
+
" f\" - Name: {most_recent_lighthouse.name}\\n\"\n",
|
| 295 |
+
" f\" - Date: {most_recent_lighthouse.last_inspected_date.isoformat()}\\n\"\n",
|
| 296 |
+
" \"==========================================\"\n",
|
| 297 |
+
" )\n",
|
| 298 |
+
" return report\n",
|
| 299 |
+
"\n",
|
| 300 |
+
" def list_lighthouses(self):\n",
|
| 301 |
+
" \"\"\"Returns a formatted string listing all lighthouses in the registry.\"\"\"\n",
|
| 302 |
+
" if not self.lighthouses:\n",
|
| 303 |
+
" return \"No lighthouses in the registry.\"\n",
|
| 304 |
+
"\n",
|
| 305 |
+
" header = \"--- List of All Lighthouses ---\"\n",
|
| 306 |
+
" # Use the __repr__ from the Lighthouse class for a clean format\n",
|
| 307 |
+
" report_lines = [repr(lh) for lh in self.lighthouses]\n",
|
| 308 |
+
" return f\"{header}\\n\" + \"\\n\".join(report_lines)\n",
|
| 309 |
+
"\n",
|
| 310 |
+
"\n",
|
| 311 |
+
" # ### WEEK 4: Making It Real - The Official Record ###\n",
|
| 312 |
+
" # TODO: Implement save_to_file and load_from_file to AUTOMATE persistence.\n",
|
| 313 |
+
" def save_to_file(self):\n",
|
| 314 |
+
" \"\"\"Saves the current list of lighthouses to the JSON file.\"\"\"\n",
|
| 315 |
+
" try:\n",
|
| 316 |
+
" # Convert each Lighthouse object to its dictionary representation\n",
|
| 317 |
+
" data_to_save = [lh.to_dict() for lh in self.lighthouses]\n",
|
| 318 |
+
" with open(self.filepath, 'w') as f:\n",
|
| 319 |
+
" json.dump(data_to_save, f, indent=4)\n",
|
| 320 |
+
" return f\"πΎ Success: Saved {len(self.lighthouses)} lighthouses to {self.filepath}.\"\n",
|
| 321 |
+
" except (IOError, TypeError) as e:\n",
|
| 322 |
+
" return f\"β Error: Could not save to file. {e}\"\n",
|
| 323 |
+
"\n",
|
| 324 |
+
" def load_from_file(self):\n",
|
| 325 |
+
" \"\"\"Loads lighthouses from the JSON file, recreating the objects.\"\"\"\n",
|
| 326 |
+
" try:\n",
|
| 327 |
+
" with open(self.filepath, 'r') as f:\n",
|
| 328 |
+
" data = json.load(f)\n",
|
| 329 |
+
" self.lighthouses = [] # Clear the current list before loading\n",
|
| 330 |
+
" for item in data:\n",
|
| 331 |
+
" # The Lighthouse constructor handles validation of the loaded data\n",
|
| 332 |
+
" lh = Lighthouse(**item)\n",
|
| 333 |
+
" self.lighthouses.append(lh)\n",
|
| 334 |
+
" return f\"π Success: Loaded {len(self.lighthouses)} lighthouses from {self.filepath}.\"\n",
|
| 335 |
+
" except FileNotFoundError:\n",
|
| 336 |
+
" return f\"βΉοΈ Info: No save file found at '{self.filepath}'. Starting with an empty registry.\"\n",
|
| 337 |
+
" except (json.JSONDecodeError, ValueError, TypeError) as e:\n",
|
| 338 |
+
" # Catches corrupted JSON, or data that fails Lighthouse validation\n",
|
| 339 |
+
" return f\"β Error: Could not load from file. File may be corrupt or malformed. {e}\"\n",
|
| 340 |
+
"\n",
|
| 341 |
+
"\n",
|
| 342 |
+
"# ===================================================================================\n",
|
| 343 |
+
"# GRADIO UI CODE\n",
|
| 344 |
+
"# Students should not need to modify below this line initially,\n",
|
| 345 |
+
"# but can extend it in Week 5.\n",
|
| 346 |
+
"# ===================================================================================\n",
|
| 347 |
+
"\n",
|
| 348 |
+
"def create_ui(registry: LighthouseRegistry):\n",
|
| 349 |
+
" \"\"\"Creates the Gradio web interface.\"\"\"\n",
|
| 350 |
+
" # Helper functions to interact with the registry and update the UI\n",
|
| 351 |
+
" def add_and_update(name, location, is_op, date_str, current_log):\n",
|
| 352 |
+
" result = registry.add_lighthouse(name, location, is_op, date_str)\n",
|
| 353 |
+
" new_log = f\"{current_log}\\n> {result}\"\n",
|
| 354 |
+
" return new_log\n",
|
| 355 |
+
"\n",
|
| 356 |
+
" def report_and_update(current_log):\n",
|
| 357 |
+
" result = registry.generate_status_report()\n",
|
| 358 |
+
" new_log = f\"{current_log}\\n\\n{result}\\n\"\n",
|
| 359 |
+
" return new_log\n",
|
| 360 |
+
"\n",
|
| 361 |
+
" def list_and_update(current_log):\n",
|
| 362 |
+
" result = registry.list_lighthouses()\n",
|
| 363 |
+
" new_log = f\"{current_log}\\n\\n{result}\\n\"\n",
|
| 364 |
+
" return new_log\n",
|
| 365 |
+
"\n",
|
| 366 |
+
" def save_and_update(current_log):\n",
|
| 367 |
+
" result = registry.save_to_file()\n",
|
| 368 |
+
" new_log = f\"{current_log}\\n> {result}\"\n",
|
| 369 |
+
" return new_log\n",
|
| 370 |
+
"\n",
|
| 371 |
+
" def load_and_update(current_log):\n",
|
| 372 |
+
" result = registry.load_from_file()\n",
|
| 373 |
+
" new_log = f\"{current_log}\\n> {result}\"\n",
|
| 374 |
+
" # After loading, also list the lighthouses to show what was loaded\n",
|
| 375 |
+
" list_result = registry.list_lighthouses()\n",
|
| 376 |
+
" return f\"{new_log}\\n\\n{list_result}\\n\"\n",
|
| 377 |
+
"\n",
|
| 378 |
+
" # Define the Gradio interface layout\n",
|
| 379 |
+
" with gr.Blocks(theme=gr.themes.Monochrome(), title=\"Lighthouse Lookout\") as app:\n",
|
| 380 |
+
" gr.Markdown(\"# π’ Lighthouse Lookout\")\n",
|
| 381 |
+
" gr.Markdown(\"A tool for monitoring and managing a fleet of lighthouses.\")\n",
|
| 382 |
+
" log_textbox = gr.Textbox(\n",
|
| 383 |
+
" label=\"Log & Reports\",\n",
|
| 384 |
+
" value=\"Welcome! Your command results will appear here.\",\n",
|
| 385 |
+
" lines=15,\n",
|
| 386 |
+
" interactive=False,\n",
|
| 387 |
+
" elem_classes=\"terminal\" # Just for potential styling\n",
|
| 388 |
+
" )\n",
|
| 389 |
+
" with gr.Row():\n",
|
| 390 |
+
" report_btn = gr.Button(\"π Generate Status Report\")\n",
|
| 391 |
+
" list_btn = gr.Button(\"π List All Lighthouses\")\n",
|
| 392 |
+
"\n",
|
| 393 |
+
" with gr.Accordion(\"β Add a New Lighthouse\", open=False):\n",
|
| 394 |
+
" with gr.Row():\n",
|
| 395 |
+
" name_input = gr.Textbox(label=\"Name\", placeholder=\"e.g., Trinity Lighthouse\")\n",
|
| 396 |
+
" location_input = gr.Textbox(label=\"Location\", placeholder=\"e.g., 50.18, -0.16\")\n",
|
| 397 |
+
" with gr.Row():\n",
|
| 398 |
+
" is_operational_input = gr.Checkbox(label=\"Is Operational?\", value=True)\n",
|
| 399 |
+
" # Using a string for the date to align with the Lighthouse class constructor\n",
|
| 400 |
+
" date_input = gr.Textbox(label=\"Last Inspected Date\", placeholder=\"YYYY-MM-DD\")\n",
|
| 401 |
+
" add_btn = gr.Button(\"Add Lighthouse\")\n",
|
| 402 |
+
"\n",
|
| 403 |
+
" with gr.Row():\n",
|
| 404 |
+
" save_btn = gr.Button(\"πΎ Save Registry\")\n",
|
| 405 |
+
" load_btn = gr.Button(\"π Load Registry\")\n",
|
| 406 |
+
"\n",
|
| 407 |
+
" # Connect UI components to the helper functions\n",
|
| 408 |
+
" add_btn.click(\n",
|
| 409 |
+
" fn=add_and_update,\n",
|
| 410 |
+
" inputs=[name_input, location_input, is_operational_input, date_input, log_textbox],\n",
|
| 411 |
+
" outputs=[log_textbox]\n",
|
| 412 |
+
" )\n",
|
| 413 |
+
" report_btn.click(fn=report_and_update, inputs=[log_textbox], outputs=[log_textbox])\n",
|
| 414 |
+
" list_btn.click(fn=list_and_update, inputs=[log_textbox], outputs=[log_textbox])\n",
|
| 415 |
+
" save_btn.click(fn=save_and_update, inputs=[log_textbox], outputs=[log_textbox])\n",
|
| 416 |
+
" load_btn.click(fn=load_and_update, inputs=[log_textbox], outputs=[log_textbox])\n",
|
| 417 |
+
"\n",
|
| 418 |
+
" return app\n",
|
| 419 |
+
"\n",
|
| 420 |
+
"# --- Main execution block ---\n",
|
| 421 |
+
"if __name__ == \"__main__\":\n",
|
| 422 |
+
" # Create a single instance of our registry that the app will use.\n",
|
| 423 |
+
" lighthouse_registry = LighthouseRegistry()\n",
|
| 424 |
+
" # Load any existing data on startup\n",
|
| 425 |
+
" initial_load_message = lighthouse_registry.load_from_file()\n",
|
| 426 |
+
" print(initial_load_message) # Print to terminal for the developer\n",
|
| 427 |
+
" # Create and launch the Gradio app\n",
|
| 428 |
+
" web_app = create_ui(lighthouse_registry)\n",
|
| 429 |
+
" web_app.launch(share=True)"
|
| 430 |
+
]
|
| 431 |
+
},
|
| 432 |
+
{
|
| 433 |
+
"cell_type": "code",
|
| 434 |
+
"execution_count": null,
|
| 435 |
+
"id": "6df3b922-6bd8-49c1-99d7-8bfd8f791337",
|
| 436 |
+
"metadata": {},
|
| 437 |
+
"outputs": [],
|
| 438 |
+
"source": []
|
| 439 |
+
}
|
| 440 |
+
],
|
| 441 |
+
"metadata": {
|
| 442 |
+
"kernelspec": {
|
| 443 |
+
"display_name": "Python 3 (ipykernel)",
|
| 444 |
+
"language": "python",
|
| 445 |
+
"name": "python3"
|
| 446 |
+
},
|
| 447 |
+
"language_info": {
|
| 448 |
+
"codemirror_mode": {
|
| 449 |
+
"name": "ipython",
|
| 450 |
+
"version": 3
|
| 451 |
+
},
|
| 452 |
+
"file_extension": ".py",
|
| 453 |
+
"mimetype": "text/x-python",
|
| 454 |
+
"name": "python",
|
| 455 |
+
"nbconvert_exporter": "python",
|
| 456 |
+
"pygments_lexer": "ipython3",
|
| 457 |
+
"version": "3.11.7"
|
| 458 |
+
}
|
| 459 |
+
},
|
| 460 |
+
"nbformat": 4,
|
| 461 |
+
"nbformat_minor": 5
|
| 462 |
+
}
|
lighthouse.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# To run this app:
|
| 2 |
+
# 1. Install Gradio: !pip install gradio
|
| 3 |
+
# 2. Run from your terminal: python lighthouse.py
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import json
|
| 6 |
+
from datetime import date
|
| 7 |
+
|
| 8 |
+
# ===================================================================================
|
| 9 |
+
# PROJECT CODE STARTS HERE
|
| 10 |
+
# Students will modify the code below this line.
|
| 11 |
+
# ===================================================================================
|
| 12 |
+
|
| 13 |
+
# ### WEEK 1: The Blueprint - Modeling a Lighthouse ###
|
| 14 |
+
# Focus: Abstraction, Automation, Analysis
|
| 15 |
+
# -----------------------------------------------------------------------------------
|
| 16 |
+
# TODO: Implement the Lighthouse data model.
|
| 17 |
+
# - Use a class to represent a lighthouse.
|
| 18 |
+
# - Add validation logic inside its __init__ method.
|
| 19 |
+
|
| 20 |
+
class Lighthouse:
|
| 21 |
+
"""Represents a single lighthouse with validation."""
|
| 22 |
+
def __init__(self, name: str, location: str, is_operational: bool, last_inspected_date: str):
|
| 23 |
+
# Validate name: must be a non-empty string
|
| 24 |
+
if not isinstance(name, str) or not name.strip():
|
| 25 |
+
raise ValueError("Name must be a non-empty string.")
|
| 26 |
+
# Validate location: must be a non-empty string
|
| 27 |
+
if not isinstance(location, str) or not location.strip():
|
| 28 |
+
raise ValueError("Location must be a non-empty string.")
|
| 29 |
+
# Validate is_operational: must be a boolean
|
| 30 |
+
if not isinstance(is_operational, bool):
|
| 31 |
+
raise TypeError("Is Operational must be a boolean (True/False).")
|
| 32 |
+
# Validate date: must be in 'YYYY-MM-DD' format
|
| 33 |
+
try:
|
| 34 |
+
parsed_date = date.fromisoformat(last_inspected_date)
|
| 35 |
+
except (ValueError, TypeError):
|
| 36 |
+
raise ValueError("Last Inspected Date must be in YYYY-MM-DD format.")
|
| 37 |
+
|
| 38 |
+
self.name = name.strip()
|
| 39 |
+
self.location = location.strip()
|
| 40 |
+
self.is_operational = is_operational
|
| 41 |
+
self.last_inspected_date = parsed_date
|
| 42 |
+
|
| 43 |
+
def to_dict(self):
|
| 44 |
+
"""Converts the object to a dictionary for JSON serialization."""
|
| 45 |
+
return {
|
| 46 |
+
"name": self.name,
|
| 47 |
+
"location": self.location,
|
| 48 |
+
"is_operational": self.is_operational,
|
| 49 |
+
"last_inspected_date": self.last_inspected_date.isoformat()
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
def __repr__(self):
|
| 53 |
+
"""Provides a developer-friendly string representation of the object."""
|
| 54 |
+
status = "Operational" if self.is_operational else "Non-Operational"
|
| 55 |
+
return f"β
{self.name} ({self.location}) - Status: {status}, Last Inspected: {self.last_inspected_date.isoformat()}"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# ### WEEK 5: Scaling Up - The Lighthouse Authority ###
|
| 59 |
+
# Focus: Abstraction, Automation, Analysis
|
| 60 |
+
# -----------------------------------------------------------------------------------
|
| 61 |
+
# TODO: Refactor all functions into this LighthouseRegistry class.
|
| 62 |
+
|
| 63 |
+
class LighthouseRegistry:
|
| 64 |
+
"""Manages the collection of all lighthouses."""
|
| 65 |
+
def __init__(self, filepath="lighthouses.json"):
|
| 66 |
+
self.lighthouses = []
|
| 67 |
+
self.filepath = filepath
|
| 68 |
+
|
| 69 |
+
# ### WEEK 2: First Steps - The Lighthouse Logbook ###
|
| 70 |
+
# TODO: Implement the add_lighthouse method.
|
| 71 |
+
def add_lighthouse(self, name: str, location: str, is_operational: bool, last_inspected_date: str):
|
| 72 |
+
"""Creates and adds a new Lighthouse to the registry after validation."""
|
| 73 |
+
try:
|
| 74 |
+
new_lighthouse = Lighthouse(name, location, is_operational, last_inspected_date)
|
| 75 |
+
self.lighthouses.append(new_lighthouse)
|
| 76 |
+
return f"Success: Added '{name}' to the registry."
|
| 77 |
+
except (ValueError, TypeError) as e:
|
| 78 |
+
return f"β Error: Could not add lighthouse. {e}"
|
| 79 |
+
|
| 80 |
+
# ### WEEK 3: Making Sense of the Data - Fleet Status Reports ###
|
| 81 |
+
# TODO: Implement the generate_status_report method for ANALYSIS.
|
| 82 |
+
def generate_status_report(self):
|
| 83 |
+
"""Analyzes the current list of lighthouses and generates a summary report."""
|
| 84 |
+
if not self.lighthouses:
|
| 85 |
+
return "Cannot generate report: No lighthouses in the registry."
|
| 86 |
+
|
| 87 |
+
total = len(self.lighthouses)
|
| 88 |
+
operational_count = sum(1 for lh in self.lighthouses if lh.is_operational)
|
| 89 |
+
non_operational_count = total - operational_count
|
| 90 |
+
|
| 91 |
+
most_recent_lighthouse = max(self.lighthouses, key=lambda lh: lh.last_inspected_date)
|
| 92 |
+
|
| 93 |
+
report = (
|
| 94 |
+
"π ===== Lighthouse Fleet Status Report ===== π\n"
|
| 95 |
+
f"Total Lighthouses: {total}\n"
|
| 96 |
+
f" - Operational: {operational_count}\n"
|
| 97 |
+
f" - Non-Operational: {non_operational_count}\n\n"
|
| 98 |
+
f"Most Recently Inspected:\n"
|
| 99 |
+
f" - Name: {most_recent_lighthouse.name}\n"
|
| 100 |
+
f" - Date: {most_recent_lighthouse.last_inspected_date.isoformat()}\n"
|
| 101 |
+
"=========================================="
|
| 102 |
+
)
|
| 103 |
+
return report
|
| 104 |
+
|
| 105 |
+
def list_lighthouses(self):
|
| 106 |
+
"""Returns a formatted string listing all lighthouses in the registry."""
|
| 107 |
+
if not self.lighthouses:
|
| 108 |
+
return "No lighthouses in the registry."
|
| 109 |
+
|
| 110 |
+
header = "--- List of All Lighthouses ---"
|
| 111 |
+
# Use the __repr__ from the Lighthouse class for a clean format
|
| 112 |
+
report_lines = [repr(lh) for lh in self.lighthouses]
|
| 113 |
+
return f"{header}\n" + "\n".join(report_lines)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# ### WEEK 4: Making It Real - The Official Record ###
|
| 117 |
+
# TODO: Implement save_to_file and load_from_file to AUTOMATE persistence.
|
| 118 |
+
def save_to_file(self):
|
| 119 |
+
"""Saves the current list of lighthouses to the JSON file."""
|
| 120 |
+
try:
|
| 121 |
+
# Convert each Lighthouse object to its dictionary representation
|
| 122 |
+
data_to_save = [lh.to_dict() for lh in self.lighthouses]
|
| 123 |
+
with open(self.filepath, 'w') as f:
|
| 124 |
+
json.dump(data_to_save, f, indent=4)
|
| 125 |
+
return f"πΎ Success: Saved {len(self.lighthouses)} lighthouses to {self.filepath}."
|
| 126 |
+
except (IOError, TypeError) as e:
|
| 127 |
+
return f"β Error: Could not save to file. {e}"
|
| 128 |
+
|
| 129 |
+
def load_from_file(self):
|
| 130 |
+
"""Loads lighthouses from the JSON file, recreating the objects."""
|
| 131 |
+
try:
|
| 132 |
+
with open(self.filepath, 'r') as f:
|
| 133 |
+
data = json.load(f)
|
| 134 |
+
self.lighthouses = [] # Clear the current list before loading
|
| 135 |
+
for item in data:
|
| 136 |
+
# The Lighthouse constructor handles validation of the loaded data
|
| 137 |
+
lh = Lighthouse(**item)
|
| 138 |
+
self.lighthouses.append(lh)
|
| 139 |
+
return f"π Success: Loaded {len(self.lighthouses)} lighthouses from {self.filepath}."
|
| 140 |
+
except FileNotFoundError:
|
| 141 |
+
return f"βΉοΈ Info: No save file found at '{self.filepath}'. Starting with an empty registry."
|
| 142 |
+
except (json.JSONDecodeError, ValueError, TypeError) as e:
|
| 143 |
+
# Catches corrupted JSON, or data that fails Lighthouse validation
|
| 144 |
+
return f"β Error: Could not load from file. File may be corrupt or malformed. {e}"
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# ===================================================================================
|
| 148 |
+
# GRADIO UI CODE
|
| 149 |
+
# Students should not need to modify below this line initially,
|
| 150 |
+
# but can extend it in Week 5.
|
| 151 |
+
# ===================================================================================
|
| 152 |
+
|
| 153 |
+
def create_ui(registry: LighthouseRegistry):
|
| 154 |
+
"""Creates the Gradio web interface."""
|
| 155 |
+
# Helper functions to interact with the registry and update the UI
|
| 156 |
+
def add_and_update(name, location, is_op, date_str, current_log):
|
| 157 |
+
result = registry.add_lighthouse(name, location, is_op, date_str)
|
| 158 |
+
new_log = f"{current_log}\n> {result}"
|
| 159 |
+
return new_log
|
| 160 |
+
|
| 161 |
+
def report_and_update(current_log):
|
| 162 |
+
result = registry.generate_status_report()
|
| 163 |
+
new_log = f"{current_log}\n\n{result}\n"
|
| 164 |
+
return new_log
|
| 165 |
+
|
| 166 |
+
def list_and_update(current_log):
|
| 167 |
+
result = registry.list_lighthouses()
|
| 168 |
+
new_log = f"{current_log}\n\n{result}\n"
|
| 169 |
+
return new_log
|
| 170 |
+
|
| 171 |
+
def save_and_update(current_log):
|
| 172 |
+
result = registry.save_to_file()
|
| 173 |
+
new_log = f"{current_log}\n> {result}"
|
| 174 |
+
return new_log
|
| 175 |
+
|
| 176 |
+
def load_and_update(current_log):
|
| 177 |
+
result = registry.load_from_file()
|
| 178 |
+
new_log = f"{current_log}\n> {result}"
|
| 179 |
+
# After loading, also list the lighthouses to show what was loaded
|
| 180 |
+
list_result = registry.list_lighthouses()
|
| 181 |
+
return f"{new_log}\n\n{list_result}\n"
|
| 182 |
+
|
| 183 |
+
# Define the Gradio interface layout
|
| 184 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), title="Lighthouse Lookout") as app:
|
| 185 |
+
gr.Markdown("# π’ Lighthouse Lookout")
|
| 186 |
+
gr.Markdown("A tool for monitoring and managing a fleet of lighthouses.")
|
| 187 |
+
log_textbox = gr.Textbox(
|
| 188 |
+
label="Log & Reports",
|
| 189 |
+
value="Welcome! Your command results will appear here.",
|
| 190 |
+
lines=15,
|
| 191 |
+
interactive=False,
|
| 192 |
+
elem_classes="terminal" # Just for potential styling
|
| 193 |
+
)
|
| 194 |
+
with gr.Row():
|
| 195 |
+
report_btn = gr.Button("π Generate Status Report")
|
| 196 |
+
list_btn = gr.Button("π List All Lighthouses")
|
| 197 |
+
|
| 198 |
+
with gr.Accordion("β Add a New Lighthouse", open=False):
|
| 199 |
+
with gr.Row():
|
| 200 |
+
name_input = gr.Textbox(label="Name", placeholder="e.g., Trinity Lighthouse")
|
| 201 |
+
location_input = gr.Textbox(label="Location", placeholder="e.g., 50.18, -0.16")
|
| 202 |
+
with gr.Row():
|
| 203 |
+
is_operational_input = gr.Checkbox(label="Is Operational?", value=True)
|
| 204 |
+
# Using a string for the date to align with the Lighthouse class constructor
|
| 205 |
+
date_input = gr.Textbox(label="Last Inspected Date", placeholder="YYYY-MM-DD")
|
| 206 |
+
add_btn = gr.Button("Add Lighthouse")
|
| 207 |
+
|
| 208 |
+
with gr.Row():
|
| 209 |
+
save_btn = gr.Button("πΎ Save Registry")
|
| 210 |
+
load_btn = gr.Button("π Load Registry")
|
| 211 |
+
|
| 212 |
+
# Connect UI components to the helper functions
|
| 213 |
+
add_btn.click(
|
| 214 |
+
fn=add_and_update,
|
| 215 |
+
inputs=[name_input, location_input, is_operational_input, date_input, log_textbox],
|
| 216 |
+
outputs=[log_textbox]
|
| 217 |
+
)
|
| 218 |
+
report_btn.click(fn=report_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 219 |
+
list_btn.click(fn=list_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 220 |
+
save_btn.click(fn=save_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 221 |
+
load_btn.click(fn=load_and_update, inputs=[log_textbox], outputs=[log_textbox])
|
| 222 |
+
|
| 223 |
+
return app
|
| 224 |
+
|
| 225 |
+
# --- Main execution block ---
|
| 226 |
+
if __name__ == "__main__":
|
| 227 |
+
# Create a single instance of our registry that the app will use.
|
| 228 |
+
lighthouse_registry = LighthouseRegistry()
|
| 229 |
+
# Load any existing data on startup
|
| 230 |
+
initial_load_message = lighthouse_registry.load_from_file()
|
| 231 |
+
print(initial_load_message) # Print to terminal for the developer
|
| 232 |
+
# Create and launch the Gradio app
|
| 233 |
+
web_app = create_ui(lighthouse_registry)
|
| 234 |
+
web_app.launch(share=True)
|
lighthouses.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[]
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio==5.45.0
|