diff options
Diffstat (limited to 'backend')
20 files changed, 880 insertions, 55 deletions
diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs index 3d008744..bc7448e1 100644 --- a/backend/api/api/Controllers/DatasetController.cs +++ b/backend/api/api/Controllers/DatasetController.cs @@ -1,6 +1,9 @@ using api.Models; using api.Services; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Net.Http.Headers; +using System.Net.Http.Headers; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -11,24 +14,64 @@ namespace api.Controllers public class DatasetController : ControllerBase { private readonly IDatasetService _datasetService; + private JwtToken jwtToken; - public DatasetController(IDatasetService datasetService) + public DatasetController(IDatasetService datasetService, IConfiguration configuration) { _datasetService = datasetService; + jwtToken = new JwtToken(configuration); } - // GET: api/<DatasetController>/{username}/datasets - [HttpGet("{username}/datasets")] - public ActionResult<List<Dataset>> Get(string username) + // GET: api/<DatasetController>/mydatasets + [HttpGet("/mydatasets")] + [Authorize(Roles = "User")] + public ActionResult<List<Dataset>> Get() { - return _datasetService.GetAllDatesets(username); + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + //ako bude trebao ID, samo iz baze uzeti + + return _datasetService.GetMyDatesets(username); + } + + // GET: api/<DatasetController>/publicdatasets + [HttpGet("/datasets")] + public ActionResult<List<Dataset>> GetPublicDS() + { + return _datasetService.GetPublicDatesets(); } - // GET api/<DatasetController>/{username}/{name} - [HttpGet("{username}/{name}")] - public ActionResult<Dataset> Get(string username, string name) + // GET api/<DatasetController>/{name} + //get odredjeni dataset + [HttpGet("/{name}")] + [Authorize(Roles = "User")] + public ActionResult<Dataset> Get(string name) { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + var dataset = _datasetService.GetOneDataset(username, name); if (dataset == null) @@ -37,10 +80,27 @@ namespace api.Controllers return dataset; } + /*za pretragu vratiti dataset koji je public + public ActionResult<Dataset> Get(string name) + { + + + var dataset = _datasetService.GetOneDataset(username, name); + + if (dataset == null) + return NotFound($"Dataset with name = {name} or user with username = {username} not found"); + + return dataset; + } + */ + // POST api/<DatasetController>/add [HttpPost("add")] + [Authorize(Roles = "User")] public ActionResult<Dataset> Post([FromBody] Dataset dataset) { + //da li ce preko tokena da se ubaci username ili front salje + //dataset.username = usernameToken; var existingDataset = _datasetService.GetOneDataset(dataset.username, dataset.name); if (existingDataset != null) @@ -53,10 +113,24 @@ namespace api.Controllers } } - // PUT api/<DatasetController>/{username}/{name} - [HttpPut("{username}/{name}")] - public ActionResult Put(string username, string name, [FromBody] Dataset dataset) + // PUT api/<DatasetController>/{name} + [HttpPut("/{name}")] + [Authorize(Roles = "User")] + public ActionResult Put(string name, [FromBody] Dataset dataset) { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + var existingDataset = _datasetService.GetOneDataset(username, name); //ne mora da se proverava @@ -64,13 +138,28 @@ namespace api.Controllers return NotFound($"Dataset with name = {name} or user with username = {username} not found"); _datasetService.Update(username, name, dataset); - return NoContent(); + + return Ok($"Dataset with name = {name} updated"); } - // DELETE api/<DatasetController>/username/name - [HttpDelete("{username}/{name}")] - public ActionResult Delete(string username, string name) + // DELETE api/<DatasetController>/name + [HttpDelete("/{name}")] + [Authorize(Roles = "User")] + public ActionResult Delete(string name) { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + var dataset = _datasetService.GetOneDataset(username, name); if (dataset == null) diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index deb622b8..1d03d924 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -3,6 +3,8 @@ using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Net.Http.Headers; +using System.Net.Http.Headers; namespace api.Controllers { @@ -13,12 +15,14 @@ namespace api.Controllers private IMlConnectionService _mlService; private readonly IModelService _modelService; - + private JwtToken jwtToken; - public ModelController(IMlConnectionService mlService, IModelService modelService) + + public ModelController(IMlConnectionService mlService, IModelService modelService, IConfiguration configuration) { _mlService = mlService; _modelService = modelService; + jwtToken = new JwtToken(configuration); } [HttpPost("sendModel")] @@ -29,18 +33,46 @@ namespace api.Controllers return Ok(result); } - // GET: api/<ModelController>/{username}/models - [HttpGet("{username}/models")] - public ActionResult<List<Model>> Get(string username) + // GET: api/<ModelController>/mymodels + [HttpGet("/mymodels")] + [Authorize(Roles = "User")] + public ActionResult<List<Model>> Get() { - return _modelService.GetAllModels(username); + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + return _modelService.GetMyModels(username); } - //id korisnika, name modela - // GET api/<ModelController>/{username}/{name} - [HttpGet("{username}/{name}")] - public ActionResult<Model> Get(string username, string name) + // name modela + // GET api/<ModelController>/{name} + [HttpGet("/{name}")] + [Authorize(Roles = "User")] + public ActionResult<Model> Get(string name) { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + var model = _modelService.GetOneModel(username, name); if (model == null) @@ -51,6 +83,7 @@ namespace api.Controllers // POST api/<ModelController>/add [HttpPost("add")] + [Authorize(Roles = "User")] public ActionResult<Model> Post([FromBody] Model model) { var existingModel = _modelService.GetOneModel(model.username, model.name); @@ -66,9 +99,24 @@ namespace api.Controllers } // PUT api/<ModelController>/{username}/{name} - [HttpPut("{username}/{name}")] - public ActionResult Put(string username, string name, [FromBody] Model model) + [HttpPut("{name}")] + [Authorize(Roles = "User")] + public ActionResult Put(string name, [FromBody] Model model) { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + var existingModel = _modelService.GetOneModel(username, name); if (existingModel == null) @@ -79,9 +127,23 @@ namespace api.Controllers } // DELETE api/<ModelController>/username - [HttpDelete("{username}/{name}")] - public ActionResult Delete(string username, string name) + [HttpDelete("{name}")] + [Authorize(Roles = "User")] + public ActionResult Delete(string name) { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + var model = _modelService.GetOneModel(username, name); if (model == null) diff --git a/backend/api/api/Controllers/PredictorController.cs b/backend/api/api/Controllers/PredictorController.cs new file mode 100644 index 00000000..d5a55b3c --- /dev/null +++ b/backend/api/api/Controllers/PredictorController.cs @@ -0,0 +1,159 @@ +using api.Models; +using api.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Net.Http.Headers; +using System.Net.Http.Headers; + +namespace api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class PredictorController : Controller + { + private readonly IPredictorService _predictorService; + private JwtToken jwtToken; + + public PredictorController(IPredictorService predictorService, IConfiguration configuration) + { + _predictorService = predictorService; + jwtToken = new JwtToken(configuration); + } + + // GET: api/<PredictorController>/mypredictors + [HttpGet("mypredictors")] + [Authorize(Roles = "User")] + public ActionResult<List<Predictor>> Get() + { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + return _predictorService.GetMyPredictors(username); + } + // GET: api/<PredictorController>/publicpredictors + [HttpGet("publicpredictors")] + public ActionResult<List<Predictor>> GetPublicPredictors() + { + return _predictorService.GetPublicPredictors(); + } + + // GET api/<PredictorController>/{name} + [HttpGet("/{name}")] + [Authorize(Roles = "User")] + public ActionResult<Predictor> Get(string name) + { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + var predictor = _predictorService.GetOnePredictor(username, name); + + if (predictor == null) + return NotFound($"Predictor with name = {name} or user with username = {username} not found"); + + return predictor; + } + + // POST api/<PredictorController>/add + [HttpPost("add")] + [Authorize(Roles = "User")] + public ActionResult<Predictor> Post([FromBody] Predictor predictor) + { + var existingModel = _predictorService.GetOnePredictor(predictor.username, predictor.name); + + if (existingModel != null) + return NotFound($"Predictor with name = {predictor.name} exisits"); + else + { + _predictorService.Create(predictor); + + return CreatedAtAction(nameof(Get), new { id = predictor._id }, predictor); + } + } + + + + // PUT api/<PredictorController>/{name} + [HttpPut("/{name}")] + [Authorize(Roles = "User")] + public ActionResult Put(string name, [FromBody] Predictor predictor) + { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + var existingDataset = _predictorService.GetOnePredictor(username, name); + + //ne mora da se proverava + if (existingDataset == null) + return NotFound($"Predictor with name = {name} or user with username = {username} not found"); + + _predictorService.Update(username, name, predictor); + + return Ok($"Predictor with name = {name} updated"); + } + + + // DELETE api/<PredictorController>/name + [HttpDelete("/{name}")] + [Authorize(Roles = "User")] + public ActionResult Delete(string name) + { + string username; + var header = Request.Headers[HeaderNames.Authorization]; + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + username = jwtToken.TokenToUsername(parameter); + if (username == null) + return null; + } + else + return BadRequest(); + + var predictor = _predictorService.GetOnePredictor(username, name); + + if (predictor == null) + return NotFound($"Predictor with name = {name} or user with username = {username} not found"); + + _predictorService.Delete(predictor.username, predictor.name); + + return Ok($"Predictor with name = {name} deleted"); + + } + + + + + } +} diff --git a/backend/api/api/Data/UserStoreDatabaseSettings.cs b/backend/api/api/Data/UserStoreDatabaseSettings.cs index 6416ab05..6841a3e0 100644 --- a/backend/api/api/Data/UserStoreDatabaseSettings.cs +++ b/backend/api/api/Data/UserStoreDatabaseSettings.cs @@ -10,7 +10,7 @@ namespace api.Data public string DatabaseName { get; set; } = String.Empty; public string CollectionName { get; set; } = String.Empty; public string DatasetCollectionName { get; set; } = String.Empty; - public string ModelCollectionName { get; set; } = String.Empty; + public string PredictorCollectionName { get; set; } = String.Empty; public string FilesCollectionName { get; set; } = String.Empty; } } diff --git a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs index 82312649..94d3e1fc 100644 --- a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs +++ b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs @@ -6,7 +6,7 @@ string DatabaseName { get; set; } string CollectionName { get; set; } string DatasetCollectionName { get; set; } - string ModelCollectionName { get; } + string PredictorCollectionName { get; } string FilesCollectionName { get; set; } } } diff --git a/backend/api/api/Models/Predictor.cs b/backend/api/api/Models/Predictor.cs new file mode 100644 index 00000000..638495bd --- /dev/null +++ b/backend/api/api/Models/Predictor.cs @@ -0,0 +1,23 @@ +using System; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace api.Models +{ + public class Predictor + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)]//mongo data type to .net + public string _id { get; set; } + public string username { get; set; } + + public string name { get; set; } + public string description { get; set; } + public string[] inputs { get; set; } + public string output { get; set; } + public bool isPublic { get; set; } + public bool accessibleByLink { get; set; } + public string dateCreated { get; set; } + } +} + diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index f3287b4c..65399bdf 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -30,6 +30,7 @@ builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IAuthService, AuthService>(); builder.Services.AddScoped<IMlConnectionService, MlConnectionService>(); builder.Services.AddScoped<IModelService, ModelService>(); +builder.Services.AddScoped<IPredictorService, PredictorService>(); builder.Services.AddScoped<IFileService, FileService>(); diff --git a/backend/api/api/Services/DatasetService.cs b/backend/api/api/Services/DatasetService.cs index 80c31758..27a8b3ee 100644 --- a/backend/api/api/Services/DatasetService.cs +++ b/backend/api/api/Services/DatasetService.cs @@ -26,15 +26,20 @@ namespace api.Services _dataset.DeleteOne(dataset => (dataset.username == username && dataset.name == name)); } - public List<Dataset> GetAllDatesets(string username) + public List<Dataset> GetMyDatesets(string username) { return _dataset.Find(dataset => dataset.username == username).ToList(); } + public List<Dataset> GetPublicDatesets() + { + return _dataset.Find(dataset => dataset.isPublic == true).ToList(); + } public Dataset GetOneDataset(string username, string name) { return _dataset.Find(dataset => dataset.username == username && dataset.name == name).FirstOrDefault(); } + //odraditi za pretragu getOne //ako je potrebno da se zameni name ili ekstenzija public void Update(string username, string name, Dataset dataset) diff --git a/backend/api/api/Services/IDatasetService.cs b/backend/api/api/Services/IDatasetService.cs index 49013e29..61a04b94 100644 --- a/backend/api/api/Services/IDatasetService.cs +++ b/backend/api/api/Services/IDatasetService.cs @@ -6,7 +6,8 @@ namespace api.Services public interface IDatasetService { Dataset GetOneDataset(string username, string name); - List<Dataset> GetAllDatesets(string username); + List<Dataset> GetMyDatesets(string username); + List<Dataset> GetPublicDatesets(); Dataset Create(Dataset dataset); void Update(string username, string name, Dataset dataset); void Delete(string username, string name); diff --git a/backend/api/api/Services/IModelService.cs b/backend/api/api/Services/IModelService.cs index 149afd4a..c1931ffa 100644 --- a/backend/api/api/Services/IModelService.cs +++ b/backend/api/api/Services/IModelService.cs @@ -6,7 +6,8 @@ namespace api.Services public interface IModelService { Model GetOneModel(string username, string name); - List<Model> GetAllModels(string username); + List<Model> GetMyModels(string username); + //List<Model> GetPublicModels(); Model Create(Model model); void Update(string username, string name, Model model); void Delete(string username, string name); diff --git a/backend/api/api/Services/IPredictorService.cs b/backend/api/api/Services/IPredictorService.cs new file mode 100644 index 00000000..594b233b --- /dev/null +++ b/backend/api/api/Services/IPredictorService.cs @@ -0,0 +1,16 @@ +using System; +using api.Models; + +namespace api.Services +{ + public interface IPredictorService + { + Predictor GetOnePredictor(string username, string name); + List<Predictor> GetMyPredictors(string username); + List<Predictor> GetPublicPredictors(); + Predictor Create(Predictor predictor); + void Update(string username, string name, Predictor predictor); + void Delete(string username, string name); + } +} + diff --git a/backend/api/api/Services/ModelService.cs b/backend/api/api/Services/ModelService.cs index 33dea30e..a3939b29 100644 --- a/backend/api/api/Services/ModelService.cs +++ b/backend/api/api/Services/ModelService.cs @@ -13,7 +13,7 @@ namespace api.Services public ModelService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) { var database = mongoClient.GetDatabase(settings.DatabaseName); - _model = database.GetCollection<Model>(settings.ModelCollectionName); + _model = database.GetCollection<Model>(settings.PredictorCollectionName); } public Model Create(Model model) @@ -27,11 +27,16 @@ namespace api.Services _model.DeleteOne(model => (model.username == username && model.name == name)); } - public List<Model> GetAllModels(string username) + public List<Model> GetMyModels(string username) { return _model.Find(model => model.username == username).ToList(); } - + /* + public List<Model> GetPublicModels() + { + return _model.Find(model => model.isPublic == true).ToList(); + } + */ public Model GetOneModel(string username, string name) { return _model.Find(model => model.username == username && model.name == name).FirstOrDefault(); diff --git a/backend/api/api/Services/PredictorService.cs b/backend/api/api/Services/PredictorService.cs new file mode 100644 index 00000000..69fb25c9 --- /dev/null +++ b/backend/api/api/Services/PredictorService.cs @@ -0,0 +1,50 @@ +using api.Interfaces; +using api.Models; +using MongoDB.Driver; + +namespace api.Services +{ + public class PredictorService : IPredictorService + { + private readonly IMongoCollection<Predictor> _predictor; + + public PredictorService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) + { + var database = mongoClient.GetDatabase(settings.DatabaseName); + _predictor = database.GetCollection<Predictor>(settings.PredictorCollectionName); + } + + public Predictor Create(Predictor predictor) + { + _predictor.InsertOne(predictor); + return predictor; + } + + public void Delete(string username, string name) + { + _predictor.DeleteOne(predictor => (predictor.username == username && predictor.name == name)); + } + + public List<Predictor> GetMyPredictors(string username) + { + return _predictor.Find(predictor => predictor.username == username).ToList(); + } + + public Predictor GetOnePredictor(string username, string name) + { + return _predictor.Find(predictor => predictor.username == username && predictor.name == name).FirstOrDefault(); + + } + + public List<Predictor> GetPublicPredictors() + { + return _predictor.Find(predictor => predictor.isPublic == true).ToList(); + } + + public void Update(string username, string name, Predictor predictor) + { + _predictor.ReplaceOne(predictor => predictor.username == username && predictor.name == name, predictor); + + } + } +} diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index 86363075..3ccba198 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -9,19 +9,20 @@ } }, "AllowedHosts": "*", - "UserStoreDatabaseSettings": { - /* LocalHost + "UserStoreDatabaseSettings": { + /* LocalHost "ConnectionString": "mongodb://127.0.0.1:27017/", "DatabaseName": "si_project", "CollectionName": "User", "DatasetCollectionName" : "Dataset", "ModelCollectionName" : "Model" */ - "ConnectionString": "mongodb+srv://si_user:si_user@sidatabase.twtfm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", - "DatabaseName": "si_db", - "CollectionName": "users", - "DatasetCollectionName": "Dataset", - "ModelCollectionName": "Model", - "FilesCollectionName": "Files" - } + "ConnectionString": "mongodb+srv://si_user:si_user@sidatabase.twtfm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", + "DatabaseName": "si_db", + "CollectionName": "users", + "DatasetCollectionName": "Dataset", + "ModelCollectionName": "Model", + "PredictorCollectionName": "Predictor", + "FilesCollectionName": "Files" + } } diff --git a/backend/microservice/PythonServer/project/api/api.py b/backend/microservice/PythonServer/project/api/api.py index 1f4afdeb..b2fb503b 100644 --- a/backend/microservice/PythonServer/project/api/api.py +++ b/backend/microservice/PythonServer/project/api/api.py @@ -7,7 +7,8 @@ import pandas as pd import keras import csv import json - +import mlservice +from mlservice import obuka app = flask.Flask(__name__) app.config["DEBUG"] = True @@ -20,13 +21,8 @@ def index(): @app.route('/data', methods = ['GET', 'POST']) def data(): if request.method == 'POST': - f = request.json['filepath'] - data = [] - with open(f) as file: - csvfile = csv.reader(file) - for row in csvfile: - data.append(row) - data = pd.DataFrame(data) + f = request.json['filepath'] + data = pd.read_csv(f) print(data) - return render_template('data.html', data = data.to_html(header=False, index=False)) + return obuka(data,request.json) app.run()
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/api/socket/client.py b/backend/microservice/PythonServer/project/api/socket/client.py new file mode 100644 index 00000000..d5740e25 --- /dev/null +++ b/backend/microservice/PythonServer/project/api/socket/client.py @@ -0,0 +1,10 @@ +import socket + +c = socket.socket() + +c.connect(('localhost', 9999)) + +name = input("Client name:") +c.send(bytes(name, 'utf-8')) + +print(c.recv(1024).decode())
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/api/socket/server.py b/backend/microservice/PythonServer/project/api/socket/server.py new file mode 100644 index 00000000..d6ff3f7c --- /dev/null +++ b/backend/microservice/PythonServer/project/api/socket/server.py @@ -0,0 +1,16 @@ +import socket + +s = socket.socket() +print('Socket Created') + +s.bind(('localhost', 9999)) + +s.listen(3) +print('waiting for connections') + +while True: + c, addr = s.accept() + name = c.recv(1024).decode() + print('Connected with ', addr, name) + c.send(bytes('Welcome', 'utf-8')) + c.close()
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/api/socket2/client.py b/backend/microservice/PythonServer/project/api/socket2/client.py new file mode 100644 index 00000000..65e76b55 --- /dev/null +++ b/backend/microservice/PythonServer/project/api/socket2/client.py @@ -0,0 +1,16 @@ +# Import socket module +import socket + +# Create a socket object +s = socket.socket() + +# Define the port on which you want to connect +port = 12345 + +# connect to the server on local computer +s.connect(('127.0.0.1', port)) + +# receive data from the server and decoding to get the string. +print (s.recv(1024).decode()) +# close the connection +s.close()
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/api/socket2/server.py b/backend/microservice/PythonServer/project/api/socket2/server.py new file mode 100644 index 00000000..c65dae78 --- /dev/null +++ b/backend/microservice/PythonServer/project/api/socket2/server.py @@ -0,0 +1,39 @@ +# first of all import the socket library +import socket + +# next create a socket object +s = socket.socket() +print ("Socket successfully created") + +# reserve a port on your computer in our +# case it is 12345 but it can be anything +port = 12345 + +# Next bind to the port +# we have not typed any ip in the ip field +# instead we have inputted an empty string +# this makes the server listen to requests +# coming from other computers on the network +s.bind(('', port)) +print ("socket binded to %s" %(port)) + +# put the socket into listening mode +s.listen(5) +print ("socket is listening") + +# a forever loop until we interrupt it or +# an error occurs +while True: + +# Establish connection with client. + c, addr = s.accept() + print ('Got connection from', addr ) + + # send a thank you message to the client. encoding to send byte type. + c.send('Thank you for connecting'.encode()) + + # Close the connection with the client + c.close() + + # Breaking once connection closed + break
\ No newline at end of file diff --git a/backend/microservice/mlservice.py b/backend/microservice/mlservice.py new file mode 100644 index 00000000..f8e98184 --- /dev/null +++ b/backend/microservice/mlservice.py @@ -0,0 +1,335 @@ +from typing_extensions import Self +import pandas as pd +import tensorflow as tf +import keras +import numpy as np + +from copyreg import constructor +import flask +from flask import request, jsonify, render_template +from sklearn.preprocessing import LabelEncoder +import csv +import json +class Response: + def __init__(self,history,rezultat,tacnost,preciznost,recall,spec,f1,classificationreport,mse,mae,mape,rmse,cmatrix,fpr,tpr): + self.history=history + self.rezultat=rezultat + self.tacnost=tacnost + self.preciznost=preciznost + self.recall=recall + self.spec=spec + self.f1=f1 + self.classificationreport=classificationreport + self.mse=mse + self.mae=mae + self.mape=mape + self.rmse=rmse + self.cmatrix=cmatrix + self.fpr=fpr + self.tpr=tpr + + ### 1)Ucitavanje vrednosti +def obuka(data,params): + import numpy as np + import pandas as pd + import tensorflow as tf + import matplotlib.pyplot as plt + #print(1) + #data1=pd.read_csv('titanic.csv') + #data=data1.copy() + #print(data.head()) + + ### U promenjivoj kolone nalaze se nazivi svih kolona seta podataka + kolone=data.columns + #print(kolone[1]) + #print(data[kolone[1]].isnull().sum()) + #print(data[kolone[1]].head(10)) + + + ### 2)Proveravanje svih kolona za null vrednosti i popunjavanje medijanom ili srednjom vrednosti ili birisanje + + #####Part2 ##### + ''' + brisanje=input("DA LI ZELITE DA IZBRSETE SVE KOLONE SA NULL VREDNOSTIMA? ") + + brisanje=True + if(brisanje=='da'): + data=data.dropna(axis=1) + elif(brisanje=='ne'): + brisanjer=input("DA LI ZELITE DA IZBRISETE SVE REDOVE SA NULL VREDNOSTINA ") + if(brisanjer=='da'): + data=data.dropna() + elif(brisanjer=='ne'): + + for i in range(len(kolone)): + if(isinstance(data[kolone[i]].dtype, pd.CategoricalDtype)): + print('cat') + + if(data[kolone[i]].isnull().any()): + tippodataka=data[kolone[i]].dtype + kolona=data[kolone[i]].copy() + + if(tippodataka==np.float64 or tippodataka==np.int64): + popunjavanje=input("UNETI NACIN POPUNJAVANJA PROMENJIVIH SA NULL VREDNOSTIMA ") + if(popunjavanje=='medijana'): + medijana=kolona.mean() + data[kolone[i]]=data[kolone[i]].fillna(medijana) + if(popunjavanje=='srednjavrednost'): + sv=data[kolone[i]].sum()/data[kolone[i]].count() + data[kolone[i]]=sv + if(popunjavanje=='brisanjekolone'): + data=data.dropna(axis=1) + + elif(tippodataka==np.object_): + najcescavrednost=kolona.value_counts().index[0] + data[kolone[i]]=data[kolone[i]].fillna(najcescavrednost) + + ''' + ### 3)Izbacivanje kolona koje ne uticu na rezultat PART2 + nredova=data.shape[0] + for i in range(len(kolone)): + if((data[kolone[i]].nunique()>(nredova/2)) and( data[kolone[i]].dtype==np.object_)): + data.pop(kolone[i]) + + + print(data.head(10)) + + ### 4)izbor tipa enkodiranja + kolone=data.columns ### Azuriranje postojecih kolona nakon moguceg brisanja + + #enc=input("UNETI TIP ENKODIRANJA ") + enc=params["encoding"] + onehot=0 + + ### 5)Enkodiranje svih kategorijskih promenjivih label-encode metodom + + if(enc=='label'): + from sklearn.preprocessing import LabelEncoder + encoder=LabelEncoder() + for k in range(len(kolone)): + if(data[kolone[k]].dtype==np.object_): + data[kolone[k]]=encoder.fit_transform(data[kolone[k]]) + print(data.head(20)) + + ### 6)Enkodiranje svih kategorijskih promenjivih onehot metodom + + elif(enc=='onehot'): + ### PART2### + onehot==1 + kategorijskekolone=[] + for k in range(len(kolone)): + if(data[kolone[k]].dtype==np.object_): + + kategorijskekolone.append(kolone[k]) ###U kategorijske kolone smestaju se nazivi svih kolona sa kategorijskim podacima + + print(kategorijskekolone) + + ### Enkodiranje + data=pd.get_dummies(data,columns=kategorijskekolone,prefix=kategorijskekolone) + print(data.head(10)) + + kolone=data.columns ### Azuriranje kolona nakon moguceg dodavanja + + ### 7)Podela skupa na skup za trening i skup za testiranje + + #predvidetikol=input("UNETI NAZIV KOLONE ČIJU VREDNOST TREBA PREDVIDETI ") + ###sta se cuva od promenjivih broj kolone ili naziv kolone??? + predvidetikol=params["columnToPredict"] + + xkolone=[] + for k in range(len(kolone)): + if(kolone[k]!=predvidetikol): + + xkolone.append(kolone[k])###U xkolone se smestaju nazivi kolona cije vrednosti nije potrebno predvideti !!!Prefiks one-hot!!! + + ### 7.1)Podela na x i y + ###Dodavanje vrednosti u x + x=data[xkolone].values + ###Dodavanje vrednosti u y, samo za label enkodiranje, bez prefiksa + y=data[predvidetikol].values + + print(data[xkolone].head(10)) + print(data[predvidetikol].head(10)) + + ### 7.2)Unos velicina za trening i test skup + #trening=int(input('UNETI VELIČINU TRENING SKUPA ')) + #test=int(input("UNETI VELICINU TESTNOG SKUPA")) + test=params["randomTestSetDistribution"] + ###Provera unetih velicina + if(test<=0 or test>=100): + print("POGREŠAN UNOS VELIČINE SKUPA ZA TRENING") + if(test>1): + test=test/100 + + ### 7.3)Da li korisnik zeli nasumicno rasporedjivanje podataka? + #nasumicno=input("DA LI ŽELITE NASUMIČNO RASPOREDJIVANJE PODATAKA U TRENING I TEST SKUP? ") + nasumicno=params["randomTestSet"] + ###!!!Dugme za nasumici izbor + if(nasumicno): + random=50 + else: + random=0 + + ### 7.4)Podela podataka + from sklearn.model_selection import train_test_split + x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=test,random_state=random) + + ### 8)Skaliranje podataka + from sklearn.preprocessing import StandardScaler + scaler=StandardScaler() + scaler.fit(x_train) + x_test=scaler.transform(x_test) + x_train=scaler.transform(x_train) + + #####ZAVRSENA PRIPREMA PODATAKA##### + + #####OBUCAVANJE MODELA##### + + ### 9)Inicijalizacija vestacke neuronske mreze + + classifier=tf.keras.Sequential() + + ### 10)Dodavanje prvog,ulaznog sloja + #aktivacijau=input("UNETI ŽELJENU AKTIVACIONU FUNKCIJU ULAZNOG SLOJA ") + #brojnu=int(input("UNETI BROJ NEURONA ULAZNOG SLOJA ")) + + aktivacijau=params["inputLayerActivationFunction"] + brojnu=params["inputNeurons"] + + classifier.add(tf.keras.layers.Dense(units=brojnu,activation=aktivacijau,input_dim=x_train.shape[1])) + + ### 11)Dodavanje drugog, skrivenog sloja + #aktivacijas=input("UNETI ŽELJENU AKTIVACIONU FUNKCIJU SKRIVENOG SLOJA ") + #brojns=int(input("UNETI BROJ NEURONA SKRIVENOG SLOJA ")) + + aktivacijas=params["hiddenLayerActivationFunction"] + brojns=params["hiddenLayerNeurons"] + + classifier.add(tf.keras.layers.Dense(units=brojns,activation=aktivacijas)) + + ### 12) Dodavanje treceg, izlaznog sloja + #aktivacijai=input("UNETI ŽELJENU AKTIVACIONU FUNKCIJU IZLAZNOG SLOJA ") + + aktivacijai=params["outputLayerActivationFunction"] + + classifier.add(tf.keras.layers.Dense(units=1,activation=aktivacijai)) + + + ### 13) Kompajliranje neuronske mreze + #gubici=input("UNETI FUNKCIJU OBRADE GUBITAKA ") + #optimizator=input("UNETI ŽELJENI OPTIMIZATOR ") + + optimizator=params["optimizer"] + + ### 13.1)Izbor metrike za kompajler PART2 + metrike=[] + while(1): + m=params['lossFunction'] + + if(m=='KRAJ'): + break + metrike.append(m) + classifier.compile(optimizer=optimizator, loss='binary_crossentropy',metrics = metrike) + + ### 14) + #uzorci=int(input("UNETI KOLIKO UZORAKA ĆE BITI UNETO U ISTO VREME ")) + #epohe=int(input("UNETI BROJ EPOHA")) + uzorci=params["batchSize"] + epohe=params["epochs"] + history=classifier.fit(x_train,y_train,batch_size=uzorci,epochs=epohe) + + ### 14.1)Parametri grafika iz history PART2 + metrikedf=pd.DataFrame() ###DataFrame u kom se nalaze podaci o rezultatima metrika za iscrtavanje na grafiku. Svaka kolona sadrzi vrednost metrike po epohama + for i in range(len(metrike)): + metrikedf[metrike[i]]=history.history[metrike[i]] + #print(history.history[metrike[i]]) + plt.plot(history.history[metrike[i]]) + plt.show() + + #print(metrikedf) + + #metrikedf.to_csv("metrike.csv") + + + ### 15) Predvidjanje + y_pred=classifier.predict(x_test) + + print(y_pred) + + ### 15.1) Formatiranje podataka za metrike PART2 + y_pred=(y_pred>=0.5).astype('int') + y_pred=y_pred.flatten() + + #print(y_pred) + + #print(y_test) + ### 15.2) Kreiranje DataFrame-a u kom se nalaze kolone koje predstavljaju stvarne i predvidjene vrednosti, potrebne za iscrtavanje grafika i metrike PART2 + rezultat=pd.DataFrame({"Stvarna vrednost ":y_test,"Predvidjena vrednost":y_pred}) + #print(rezultat.head(20)) + + #####METRIKE##### PART2 + + import sklearn.metrics as sm + + + ### 16)Tacnost + tacnost=sm.accuracy_score(y_test,y_pred) + print('tacnost ',tacnost) + + ### 17)Preciznost + preciznost=sm.precision_score(y_test,y_pred) + print('preciznost ',preciznost) + + ### 18)Recall + recall=sm.recall_score(y_test,y_pred) + print('recall ',recall) + + ### 19)Specificity + tn, fp, fn, tp = sm.confusion_matrix(y_test,y_pred).ravel() + spec = tn / (tn+fp) + print('spec ',spec) + + ### 20)F1 + f1=sm.f1_score(y_test,y_pred) + print('f1 ',f1) + + ### 21)Classification report + classificationreport=sm.classification_report(y_test,y_pred) + print('classification ',classificationreport) + + ### 22)Mean squared error (mse) + mse=sm.mean_squared_error(y_test,y_pred) + print('mse ',mse) + + ### 23)Mean absolute error (mae) + mae=sm.mean_absolute_error(y_test,y_pred) + print('mae ',mae) + + ### 24)Mean absolute percentage error (mape) + mape=sm.mean_absolute_percentage_error(y_test,y_pred) + print('mape ',mape) + + ### 25)Root mean square error (rmse) *** da bi se iskoristila u history, salje se u metrics preko funkcije + import numpy as np + rmse=np.sqrt(sm.mean_squared_error(y_test,y_pred)) + print("rmse ",rmse) + + ### 26)Confusion matrix + cmatrix=sm.confusion_matrix(y_test,y_pred) + print('cmatrix ',cmatrix) + + ### 27)ROC + fpr, tpr, _ = sm.roc_curve(y_test,y_pred) + plt.plot(fpr, tpr, color='blue') + plt.title('ROC') + plt.xlim([0.0, 1.0]) + plt.xlabel('False Positive Rate') + plt.ylim([0.0, 1.0]) + plt.ylabel('True Positive Rate') + plt.show() + + r=Response(history,rezultat,tacnost,preciznost,recall,spec,f1,classificationreport,mse,mae,mape,rmse,cmatrix,fpr,tpr) + + return "Done" + + |