Teapack1 commited on
Commit
deeb85c
·
0 Parent(s):

Initial commit

Browse files
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ venv/
2
+ __pycache__/
3
+ -files
Dockerfile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+
5
+ COPY . .
6
+
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: VoltageDrop
3
+ emoji: 🐢
4
+ colorFrom: green
5
+ colorTo: indigo
6
+ sdk: static
7
+ pinned: false
8
+ license: apache-2.0
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
database.db ADDED
Binary file (16.4 kB). View file
 
main.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, Depends, HTTPException, Form
2
+ from fastapi.responses import HTMLResponse, RedirectResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.templating import Jinja2Templates
5
+ from sqlalchemy.orm import Session
6
+ from pydantic import BaseModel
7
+ from models import Base, Cable, SessionLocal, init_db
8
+
9
+ app = FastAPI()
10
+
11
+ # Initialize the database
12
+ init_db()
13
+
14
+ # Serve static files
15
+ app.mount("/static", StaticFiles(directory="static"), name="static")
16
+
17
+ templates = Jinja2Templates(directory="templates")
18
+
19
+ class VoltageDropRequest(BaseModel):
20
+ voltage: float
21
+ load: float
22
+ length: float
23
+ cable_type: str
24
+
25
+ def get_db():
26
+ db = SessionLocal()
27
+ try:
28
+ yield db
29
+ finally:
30
+ db.close()
31
+
32
+ def calculate_voltage_drop(voltage, load, length, resistance):
33
+ return voltage - (load * length * resistance)
34
+
35
+ @app.get("/", response_class=HTMLResponse)
36
+ async def read_root(request: Request, db: Session = Depends(get_db)):
37
+ cables = db.query(Cable).all()
38
+ return templates.TemplateResponse("index.html", {"request": request, "cables": cables})
39
+
40
+ @app.post("/calculate")
41
+ async def calculate(request: VoltageDropRequest, db: Session = Depends(get_db)):
42
+ cable = db.query(Cable).filter(Cable.type == request.cable_type).first()
43
+ if not cable:
44
+ raise HTTPException(status_code=400, detail="Invalid cable type")
45
+ result = calculate_voltage_drop(request.voltage, request.load, request.length, cable.resistance)
46
+ return {"voltage_drop": result}
47
+
48
+ @app.get("/edit_cables", response_class=HTMLResponse)
49
+ async def edit_cables(request: Request, db: Session = Depends(get_db)):
50
+ cables = db.query(Cable).all()
51
+ return templates.TemplateResponse("edit_cables.html", {"request": request, "cables": cables})
52
+
53
+ @app.post("/add_cable")
54
+ async def add_cable(type: str = Form(...), resistance: float = Form(...), db: Session = Depends(get_db)):
55
+ cable = Cable(type=type, resistance=resistance)
56
+ db.add(cable)
57
+ db.commit()
58
+ return RedirectResponse(url="/edit_cables", status_code=303)
59
+
60
+ @app.post("/edit_cable/{cable_id}")
61
+ async def edit_cable(cable_id: int, type: str = Form(...), resistance: float = Form(...), db: Session = Depends(get_db)):
62
+ cable = db.query(Cable).filter(Cable.id == cable_id).first()
63
+ if not cable:
64
+ raise HTTPException(status_code=404, detail="Cable not found")
65
+ cable.type = type
66
+ cable.resistance = resistance
67
+ db.commit()
68
+ return RedirectResponse(url="/edit_cables", status_code=303)
69
+
70
+ @app.post("/delete_cable/{cable_id}")
71
+ async def delete_cable(cable_id: int, db: Session = Depends(get_db)):
72
+ cable = db.query(Cable).filter(Cable.id == cable_id).first()
73
+ if not cable:
74
+ raise HTTPException(status_code=404, detail="Cable not found")
75
+ db.delete(cable)
76
+ db.commit()
77
+ return RedirectResponse(url="/edit_cables", status_code=303)
models.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sqlalchemy import Column, Integer, String, Float, create_engine
2
+ from sqlalchemy.ext.declarative import declarative_base
3
+ from sqlalchemy.orm import sessionmaker
4
+
5
+ DATABASE_URL = "sqlite:///./database.db"
6
+
7
+ Base = declarative_base()
8
+
9
+ class Cable(Base):
10
+ __tablename__ = "cables"
11
+ id = Column(Integer, primary_key=True, index=True)
12
+ type = Column(String, unique=True, index=True)
13
+ resistance = Column(Float)
14
+
15
+ engine = create_engine(DATABASE_URL)
16
+ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
17
+
18
+ def init_db():
19
+ Base.metadata.create_all(bind=engine)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ jinja2
4
+ sqlalchemy
5
+ python-multipart
space.yml ADDED
@@ -0,0 +1 @@
 
 
1
+ sdk: docker
static/app.js ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', function() {
2
+ const form = document.getElementById('calculator-form');
3
+ const calculateButton = document.querySelector('.btn-calculate');
4
+
5
+ // Function to check if all form fields are filled
6
+ function checkFormValidity() {
7
+ const isValid = form.checkValidity();
8
+ if (isValid) {
9
+ calculateButton.disabled = false;
10
+ calculateButton.classList.add('enabled');
11
+ } else {
12
+ calculateButton.disabled = true;
13
+ calculateButton.classList.remove('enabled');
14
+ }
15
+ }
16
+
17
+ // Listen for input events on the form to trigger validation
18
+ form.addEventListener('input', checkFormValidity);
19
+
20
+ // Initially check the form validity on page load
21
+ checkFormValidity();
22
+
23
+ // Handle form submission
24
+ form.addEventListener('submit', async function(event) {
25
+ event.preventDefault();
26
+
27
+ const formData = new FormData(event.target);
28
+ const data = {
29
+ voltage: parseFloat(formData.get('voltage')),
30
+ load: parseFloat(formData.get('load')),
31
+ length: parseFloat(formData.get('length')),
32
+ cable_type: formData.get('cable_type')
33
+ };
34
+
35
+ const response = await fetch('/calculate', {
36
+ method: 'POST',
37
+ headers: {
38
+ 'Content-Type': 'application/json'
39
+ },
40
+ body: JSON.stringify(data)
41
+ });
42
+
43
+ const result = await response.json();
44
+ document.getElementById('result').innerText = `Result Voltage: ${result.voltage_drop} V`;
45
+ });
46
+ });
static/logo.png ADDED
static/styles.css ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ display: flex;
4
+ flex-direction: column;
5
+ align-items: center;
6
+ margin: 0;
7
+ padding: 0;
8
+ background-color: #f8f9fa;
9
+ }
10
+
11
+ header {
12
+ display: flex;
13
+ align-items: center;
14
+ justify-content: center;
15
+ background-color: #343a40;
16
+ color: white;
17
+ width: 100%;
18
+ padding: 20px 0;
19
+ }
20
+
21
+ .calculator-form {
22
+ width: 100%;
23
+ display: flex;
24
+ flex-direction: column;
25
+ }
26
+
27
+ #logo {
28
+ height: 50px;
29
+ margin-right: 20px;
30
+ }
31
+
32
+ main {
33
+ display: flex;
34
+ flex-direction: column;
35
+ align-items: center;
36
+ margin-top: 30px;
37
+ width: 100%;
38
+ max-width: 600px;
39
+ padding: 20px;
40
+ background-color: white;
41
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
42
+ border-radius: 8px;
43
+ }
44
+
45
+ .add-cable-section,
46
+ .existing-cables-section {
47
+ width: 100%;
48
+ margin-bottom: 30px;
49
+ }
50
+
51
+ .add-cable-section h2,
52
+ .existing-cables-section h2 {
53
+ margin-bottom: 20px;
54
+ color: #343a40;
55
+ }
56
+
57
+ .form-row {
58
+ display: flex;
59
+ align-items: center;
60
+ justify-content: space-between;
61
+ margin-bottom: 15px;
62
+ }
63
+
64
+ .form-row label {
65
+ flex-basis: 30%;
66
+ font-weight: bold;
67
+ }
68
+
69
+ .form-row input {
70
+ flex-basis: 65%;
71
+ padding: 8px;
72
+ font-size: 16px;
73
+ border: 1px solid #ccc;
74
+ border-radius: 8px;
75
+ }
76
+
77
+ .form-row select {
78
+ flex-basis: 65%;
79
+ padding: 8px;
80
+ font-size: 16px;
81
+ border: 1px solid #ccc;
82
+ border-radius: 8px;
83
+ }
84
+
85
+ .btn {
86
+ padding: 8px 16px;
87
+ font-size: 14px;
88
+ cursor: pointer;
89
+ border: none;
90
+ color: white;
91
+ border-radius: 4px;
92
+ }
93
+
94
+ .btn-add {
95
+ background-color: #28a745;
96
+ width: 100%;
97
+ }
98
+
99
+ .btn-save {
100
+ background-color: #007bff;
101
+ margin-right: 5px;
102
+ }
103
+
104
+ .btn-delete {
105
+ background-color: #dc3545;
106
+ }
107
+
108
+ .btn-group {
109
+ display: flex;
110
+ justify-content: flex-end;
111
+ margin-top: 10px;
112
+ }
113
+
114
+ .section-divider {
115
+ margin: 40px 0;
116
+ border: 0;
117
+ border-top: 2px solid #dee2e6;
118
+ width: 100%;
119
+ }
120
+
121
+ .cable-divider {
122
+ margin: 20px 0;
123
+ border: 0;
124
+ border-top: 1px solid #dee2e6;
125
+ width: 100%;
126
+ }
127
+
128
+ .btn-back {
129
+ background-color: #007bff;
130
+ color: white;
131
+ padding: 10px 20px;
132
+ font-size: 16px;
133
+ text-align: center;
134
+ text-decoration: none;
135
+ margin-top: 30px;
136
+ display: inline-block;
137
+ border-radius: 5px;
138
+ width: 100%;
139
+ }
140
+
141
+ .btn-back:hover {
142
+ background-color: #0056b3;
143
+ }
144
+ .btn-calculate {
145
+ background-color: #6c757d; /* Disabled state */
146
+ color: white;
147
+ padding: 10px 20px;
148
+ font-size: 18px;
149
+ border: none;
150
+ cursor: not-allowed;
151
+ border-radius: 4px;
152
+ margin-top: 20px;
153
+ width: 100%;
154
+ text-align: center;
155
+ transition: background-color 0.3s ease;
156
+ }
157
+
158
+ .btn-calculate.enabled {
159
+ background-color: #28a745; /* Enabled state */
160
+ cursor: pointer;
161
+ }
162
+
163
+ .btn-calculate:hover.enabled {
164
+ background-color: #218838;
165
+ }
166
+
167
+ .result-display {
168
+ margin-top: 30px;
169
+ font-size: 24px;
170
+ color: #28a745;
171
+ text-align: center;
172
+ padding: 20px;
173
+ border-top: 2px solid #dee2e6;
174
+ width: 100%;
175
+ }
176
+
templates/edit_cables.html ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Edit Cables</title>
7
+ <link rel="stylesheet" href="/static/styles.css">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <h1>Edit Cables</h1>
12
+ </header>
13
+ <main>
14
+ <section class="add-cable-section">
15
+ <h2>Add Cable</h2>
16
+ <form action="/add_cable" method="post" class="cable-form">
17
+ <div class="form-row">
18
+ <label for="type">Cable Type:</label>
19
+ <input type="text" id="type" name="type" required>
20
+ </div>
21
+ <div class="form-row">
22
+ <label for="resistance">Resistance:</label>
23
+ <input type="number" step="0.01" id="resistance" name="resistance" required>
24
+ </div>
25
+ <div class="form-group">
26
+ <button type="submit" class="btn btn-add">Add Cable</button>
27
+ </div>
28
+ </form>
29
+ </section>
30
+
31
+ <hr class="section-divider">
32
+
33
+ <section class="existing-cables-section">
34
+ <h2>Existing Cables</h2>
35
+ <div class="cable-list">
36
+ {% for cable in cables %}
37
+ <form action="/edit_cable/{{ cable.id }}" method="post" class="cable-form">
38
+ <div class="form-row">
39
+ <label for="type">Cable Type:</label>
40
+ <input type="text" id="type" name="type" value="{{ cable.type }}" required>
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="resistance">Resistance:</label>
44
+ <input type="number" step="0.01" id="resistance" name="resistance" value="{{ cable.resistance }}" required>
45
+ </div>
46
+ <div class="form-row btn-group">
47
+ <button type="submit" class="btn btn-save">Save</button>
48
+ <button formaction="/delete_cable/{{ cable.id }}" formmethod="post" class="btn btn-delete">Delete</button>
49
+ </div>
50
+ </form>
51
+ <hr class="cable-divider">
52
+ {% endfor %}
53
+ </div>
54
+ </section>
55
+ <a href="/" class="btn btn-back">Back to Calculator</a>
56
+ </main>
57
+ </body>
58
+ </html>
templates/index.html ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Voltage Drop Calculator</title>
7
+ <link rel="stylesheet" href="/static/styles.css">
8
+ </head>
9
+ <body>
10
+ <header>
11
+ <img src="/static/logo.png" alt="Company Logo" id="logo">
12
+ <h1>Voltage Drop Calculator</h1>
13
+ </header>
14
+ <main class="calculator-main">
15
+ <form id="calculator-form" class="calculator-form">
16
+ <div class="form-row">
17
+ <label for="cable_type">Cable Type:</label>
18
+ <select id="cable_type" name="cable_type" required>
19
+ {% for cable in cables %}
20
+ <option value="{{ cable.type }}">{{ cable.type }}</option>
21
+ {% endfor %}
22
+ </select>
23
+ </div>
24
+
25
+ <div class="form-row">
26
+ <label for="voltage">Voltage:</label>
27
+ <input type="number" id="voltage" name="voltage" required>
28
+ </div>
29
+
30
+ <div class="form-row">
31
+ <label for="load">Load (A):</label>
32
+ <input type="number" id="load" name="load" required>
33
+ </div>
34
+
35
+ <div class="form-row">
36
+ <label for="length">Length (m):</label>
37
+ <input type="number" id="length" name="length" required>
38
+ </div>
39
+
40
+ <button type="submit" class="btn btn-calculate">Calculate</button>
41
+ </form>
42
+
43
+ <div id="result" class="result-display"></div>
44
+
45
+ <a href="/edit_cables" class="btn btn-back">Edit Cables</a>
46
+ </main>
47
+ <script src="/static/app.js"></script>
48
+ </body>
49
+ </html>