Spaces:
Sleeping
Sleeping
devic1
commited on
Commit
·
4afafa1
1
Parent(s):
f84aec0
added training Scenerio
Browse files- __pycache__/app.cpython-310.pyc +0 -0
- app.py +51 -4
- flagged/log.csv +2 -0
__pycache__/app.cpython-310.pyc
ADDED
|
Binary file (2.12 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,8 +1,55 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import time
|
| 5 |
+
import math
|
| 6 |
|
| 7 |
+
class Lreg(nn.Module):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super(Lreg,self).__init__()
|
| 10 |
+
self.l = nn.Linear(1,1)
|
| 11 |
|
| 12 |
+
def forward(self,x):
|
| 13 |
+
y = self.l(x)
|
| 14 |
+
return y
|
| 15 |
|
| 16 |
+
model = Lreg()
|
| 17 |
+
crit = nn.MSELoss()
|
| 18 |
+
optim = torch.optim.SGD(model.parameters(),lr=0.01)
|
| 19 |
+
|
| 20 |
+
def weightsbias(weight,bias,predicted,progress=gr.Progress()):
|
| 21 |
+
weight = float(weight)
|
| 22 |
+
bias = float(bias)
|
| 23 |
+
predicted = float(predicted)
|
| 24 |
+
x = torch.randn(1000)
|
| 25 |
+
y = (x*weight)+bias
|
| 26 |
+
progress(0,desc="Starting")
|
| 27 |
+
model.train()
|
| 28 |
+
for i in progress.tqdm(range(1,10)):
|
| 29 |
+
for idx,ele in enumerate(x):
|
| 30 |
+
ele = ele.unsqueeze(0)
|
| 31 |
+
out = model(ele)
|
| 32 |
+
optim.zero_grad()
|
| 33 |
+
loss = crit(out,y[idx].unsqueeze(0))
|
| 34 |
+
loss.backward()
|
| 35 |
+
optim.step()
|
| 36 |
+
model.eval()
|
| 37 |
+
t = torch.tensor([predicted],dtype=torch.float32)
|
| 38 |
+
rout = model(t)
|
| 39 |
+
c = model.state_dict()
|
| 40 |
+
def ceil(x):
|
| 41 |
+
return math.ceil(x)
|
| 42 |
+
return [ceil(rout.item()),ceil(c['l.weight'].item()),ceil(c['l.bias'].item())]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
with gr.Blocks() as iface:
|
| 46 |
+
weights = gr.Textbox(label="Weights")
|
| 47 |
+
bias = gr.Textbox(label="Bias")
|
| 48 |
+
predicted = gr.Textbox(label="Prediction Number")
|
| 49 |
+
output = gr.Number(label="Output Predicted")
|
| 50 |
+
w = gr.Number(label="Calculated Weight ")
|
| 51 |
+
b = gr.Number(label="Calculated Bias ")
|
| 52 |
+
train = gr.Button("Train")
|
| 53 |
+
train.click(fn=weightsbias, inputs=[weights,bias,predicted], outputs=[output,w,b])
|
| 54 |
+
|
| 55 |
+
iface.queue(concurrency_count=10).launch()
|
flagged/log.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name,output,flag,username,timestamp
|
| 2 |
+
HEllo,Hello HEllo!!,,,2023-03-31 10:59:14.701814
|