from collections import deque
m , n = map(int,input().split())

deq= deque(map(int,input().split()))
com=deq[n]
cnt=0
while 1:
    # print("deq는 ", deq)
    # print("n값은 {}".format(n))
    tmp = deq.popleft()
    for i in range(len(deq)):
        if tmp < deq[i]:
            deq.append(tmp)
            if n > 0:
                n = n -1
            elif n == 0:
                n = len(deq) - 1
            break
    else:
        cnt+=1
        if n!=0:
            n = n-1
        else:
            print(cnt)
            break

인프런코딩테스트준비/섹션 5 자료구조 활용/응급실.py


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from . models import *
# Create your views here.
@login_required
def index(request):
    context = {}
    context['languages'] = Language.objects.all()
    context['title'] = "Languages"
    context['language_section'] = "current_section"
    context['language_index'] = "act_item"
    return render(request, 'language/language.html', context )

@login_required
def view_setting(request):
    context = {}
    context['languages'] = Language.objects.all()
    context['title'] = "Languages Setting"
    context['language_section'] = "current_section"
    context['language_setting'] = "act_item"
    return render(request, 'language/setting.html', context )

@login_required
def event_add_language(request):
    context = {}
    if request.method == "POST":
        message = "Language added successfully"
        language = Language.objects.create(
            title = request.POST.get("title"),
            code = request.POST.get("code"),
            flag = request.POST.get("flag")
        )

        language.save()
        context['message'] = message

    context['title'] = "Add Languages"
    context['language_section'] = "current_section"
    context['language_index'] = "act_item"
    return render(request, 'language/add_language.html', context)

@login_required
def event_update_language(request, pk):
    language = Language.objects.get(id = pk)
    language.is_active = False if language.is_active else True
    language.save()
    return redirect("/language/")


from flask import Flask, request, jsonify, send_file
from lista import lista
import os
import asyncio
import sys

app = Flask(__name__)

@app.route('/')
def pagina_padrao():
    return 'Esta é a página padrão do meu site!'

@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files['file']
    if file:
        filename = file.filename
        excelFile = os.path.join('uploads', filename)
        file.save(excelFile)

        blocosFileZip = lista(excelFile)
        return send_file(blocosFileZip, as_attachment=True, download_name="BLOCOS-BRANCOS.zip", mimetype='application/zip')
    else:
        return jsonify({'message': 'Nenhum arquivo enviado.'}), 400

# Erro no Handling
def uncaught_exception_handler(ex):
    print("Uncaught Exception:", ex)

def unhandled_rejection_handler(promise, reason):
    print("[GRAVE] Rejeição possivelmente não tratada em: Promise", promise, "motivo:", reason)

sys.excepthook = uncaught_exception_handler

loop = asyncio.get_event_loop()
loop.set_exception_handler(unhandled_rejection_handler)

if __name__ == '__main__':
    from waitress import serve
    serve(app, host="0.0.0.0", port=8080)


import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

class PlotMarker():
    def __init__(self):
        self.plot_width = 50
        self.plot_forward = 20
        self.plot_backward = 40
        self.map_yaw = math.pi * 43/32

        self.map_rot = np.array([[math.cos(self.map_yaw), math.sin(self.map_yaw)],
                        [-math.sin(self.map_yaw), math.cos(self.map_yaw)]])
        self.map_rot_wh = np.array([[math.cos(self.map_yaw), 0],
                                    [0, math.sin(self.map_yaw)]])

        self.ego_vec_length = 3
        self.robot_length = 3.5
        self.robot_width = 1.5

    def plot_status(self,
                    ego_pose = None,
                    object_pose = None,
                    left_bound = None,
                    right_bound = None,
                    path=None,
                    path_index_left=None,
                    path_index_next_left=None,
                    path_index_right= None,
                    path_index_next_right=None,
                    rotation = False,
                    predicted_goal_pose=None,
                    predicted_trajectory=None,
                    curve_plot=None,
                    curve_forward_point=None,
                    curve_backward_point=None,
                    vis_point=None
                    ):

        if ego_pose is not None:
            plt.cla()

            ego_pose_origin = ego_pose
            if rotation:
                ego_pose = np.array([0, 0])

            ## Plot ego pose
            plot_xmin = ego_pose[0] - self.plot_width / 2.0
            plot_xmax = ego_pose[0] + self.plot_width / 2.0
            plot_ymin = ego_pose[1] - self.plot_backward/ 2.0
            plot_ymax = ego_pose[1] + self.plot_forward / 2.0
            plt.plot(ego_pose[0], ego_pose[1], "xr")

            yaw = ego_pose_origin[2]
            ego_rot = np.array([[math.cos(yaw), math.sin(yaw)],
                            [-math.sin(yaw), math.cos(yaw)]])

            outline = np.array([
                               [-self.robot_length / 2, self.robot_length / 2,
                                (self.robot_length / 2), -self.robot_length / 2,
                                -self.robot_length / 2],
                                [self.robot_width / 2, self.robot_width / 2,
                                - self.robot_width / 2, -self.robot_width / 2,
                                self.robot_width / 2]
                                ])
            outline = (outline.T.dot(ego_rot)).T

            outline[0, :] += ego_pose[0]
            outline[1, :] += ego_pose[1]
            plt.plot(np.array(outline[0, :]).flatten(),
                    np.array(outline[1, :]).flatten(), "-k")

            ## Plot allow
            ego_vec = np.array([np.cos(yaw), np.sin(yaw)]) * self.ego_vec_length
            ego_vec = ego_pose[0:2] + ego_vec
            trajec = np.array([[ego_pose[0], ego_pose[1]],
                              [ego_vec[0], ego_vec[1]]])
            plt.plot(trajec[:,0], trajec[:,1], "red")

            ## Plot object pose
            if object_pose is not None:
                for ob in object_pose:
                    object_xy = ob[0:2]
                    object_wh = ob[2:4]
                    if rotation:
                        object_xy = object_xy[0:2] - ego_pose_origin[0:2]
                        object_xy = object_xy[0:2] @ self.map_rot
                        object_wh = object_wh[0:2] @ self.map_rot_wh

                    object_x_min = object_xy[0] - object_wh[0] / 2.0
                    object_y_min = object_xy[1] - object_wh[1] / 2.0
                    object_x_max = object_xy[0] + object_wh[0] / 2.0
                    object_y_max = object_xy[1] + object_wh[1] / 2.0
                    ob_box = np.array([[object_x_min, object_y_min],
                                       [object_x_min, object_y_max],
                                       [object_x_max, object_y_max],
                                       [object_x_max, object_y_min],
                                       [object_x_min, object_y_min]])

                    plt.plot(ob_box[:,0], ob_box [:,1], "blue")

            if path is not None:
                self.plot_path(path)

            ## Plot bound
            if (left_bound is not None) and (right_bound is not None):
                if rotation:
                    left_bound = left_bound[:,0:2] - ego_pose_origin[0:2]
                    left_bound = left_bound[:,0:2] @ self.map_rot

                    right_bound = right_bound[:,0:2] - ego_pose_origin[0:2]
                    right_bound = right_bound[:,0:2] @ self.map_rot

                plt.plot(left_bound[:, 0], left_bound[:, 1], color="black")
                plt.plot(right_bound[:,0], right_bound[:,1], color="black")

                if (path_index_left is not None) and (path_index_right is not None):
                    if path_index_next_left is None:
                        path_index_next_left = path_index_left + 1
                    if path_index_next_left is None:
                        path_index_next_left = path_index_right + 1

                    plt.plot(left_bound[path_index_left:path_index_next_left + 1 , 0], left_bound[path_index_left:path_index_next_left + 1, 1], color="green")
                    plt.plot(right_bound[path_index_right:path_index_next_right + 1, 0], right_bound[path_index_right:path_index_next_right + 1, 1], color="green")

            ## Plot goal pose
            if predicted_goal_pose is not None:
                self.plot_point(predicted_goal_pose)

            ## Plot trajectory
            if predicted_trajectory is not None:
                self.plot_traj(predicted_trajectory)

            ## Plot curve path
            if curve_plot is not None:
                self.plot_red_line(curve_plot)

            if curve_forward_point is not None:
                self.plot_point_delta(curve_forward_point)

            if curve_backward_point is not None:
                self.plot_point_delta(curve_backward_point)

            if vis_point is not None:
                self.vis_point(vis_point)

            plt.xlim(plot_xmin, plot_xmax)
            plt.ylim(plot_ymin, plot_ymax)
            plt.pause(0.01)
            #plt.show()

        else:
            return

    def plot_red_line(self, line):
        plt.plot(line[:, 0], line[:, 1], color="Magenta")

    def plot_path(self, path):
        plt.plot(path[:, 0], path[:, 1], color="yellow", marker=".")
        #plt.plot(path[:, 0], path[:, 1], color="yellow")

    def plot_traj(self, traj):
        plt.plot(traj[:, 0], traj[:, 1], color="green", marker="^")

    def plot_point(self, point):
        plt.plot(point[0], point[1], marker="x")

    def plot_point_delta(self, point):
        plt.plot(point[0], point[1], marker="^",markersize=10)

    def vis_point(selfm, point):
        if len(point.shape) > 1:
            plt.scatter(point[:,0], point[:, 1], s=20, marker="D")
        else:
            plt.plot(point[0], point[1], marker="8",markersize=8)


from tkinter import *
import time
from PIL import ImageTk,Image

root=Tk()

canvas=Canvas(root,width=1350,height=700)
image=ImageTk.PhotoImage(Image.open("C:\youtube\\clock.png"))

canvas.create_image(0,0,anchor=NW,image=image)
canvas.pack()
def clock():
    h=str(time.strftime("%H"))
    m=str(time.strftime("%M"))
    s=str(time.strftime("%S"))
    #print(h,m,s)

    if int(h)>12 and int(m)>0:
        lbl_noon.config(text="PM")

    if int(h)>12:
        h=str((int(h)-12))

    lbl_hr.config(text=h)
    lbl_min.config(text=m)
    lbl_sec.config(text=s)

    lbl_hr.after(200,clock)

lbl_hr=Label(root,text="12",font=("times new romam",50,"bold"),bg="white",fg="#0875B7")
lbl_hr.place(x=250,y=200,width=200,height=200)

lbl_hr2=Label(root,text="Hour",font=("EB Garamond",35,"bold"),bg="white",fg="#0875B7")
lbl_hr2.place(x=250,y=420,width=200,height=80)

lbl_min=Label(root,text="12",font=("times new romam",50,"bold"),bg="#f5c32f",fg="white")
lbl_min.place(x=450,y=200,width=200,height=200)

lbl_min2=Label(root,text="Min",font=("EB Garamond",35,"bold"),bg="#f5c32f",fg="white")
lbl_min2.place(x=450,y=420,width=200,height=80)

lbl_sec=Label(root,text="12",font=("times new romam",50,"bold"),bg="#ff0000",fg="white")
lbl_sec.place(x=650,y=200,width=200,height=200)

lbl_sec2=Label(root,text="Secs",font=("EB Garamond",35,"bold"),bg="#ff0000",fg="white")
lbl_sec2.place(x=650,y=420,width=200,height=80)

lbl_noon=Label(root,text="AM",font=("times new romam",50,"bold"),bg="#59ff00",fg="white")
lbl_noon.place(x=850,y=200,width=200,height=200)

lbl_noon2=Label(root,text="Noon",font=("EB Garamond",35,"bold"),bg="#59ff00",fg="white")
lbl_noon2.place(x=850,y=420,width=200,height=80)
clock()
root.mainloop()


import xlsxwriter
import datetime
import pandas as pd
from openpyxl import load_workbook
from decimal import Decimal

import dbworker

exchanges = dbworker.get_all_exchange()

def df(seconds):
    id_operation = []
    t_id = []
    create_dt = []
    type = []
    X = []
    commission = []
    X_without_comm = []
    currency_formula = []
    currency_sum = []

    end_dt = []
    admin_id = []

    if exchanges != 0:
        for exchange in exchanges:

            if (((datetime.datetime.now().replace(tzinfo=None) - exchange.end_dt.replace(tzinfo=None)).seconds) +
                (datetime.datetime.now().replace(tzinfo=None) - exchange.end_dt.replace(
                    tzinfo=None)).days * 24 * 3600) < seconds\
                    and exchange.status == 2:
                id_operation.append(exchange.id)
                t_id.append(exchange.t_id)
                create_dt.append(exchange.create_dt)
                if exchange.type == 0:
                    type.append('ПРОДАЖА')
                    X.append(f'{exchange.btc_value:.8f} BTC')
                    commission.append(f'{exchange.commission:.8f} BTC')
                    X_without_comm.append(f'{(exchange.btc_value - exchange.commission):.8f} BTC')

                elif exchange.type == 1:
                    type.append('ПОКУПКА')
                    X.append(f'{exchange.rub_value:.2f} RUB')
                    commission.append(f'{exchange.commission:.2f} RUB')
                    X_without_comm.append(f'{(exchange.rub_value - exchange.commission):.2f} RUB')

                else:
                    type.append('НЕОПРЕДЕЛЕН')
                    X.append(' ')
                    commission.append(' ')
                    X_without_comm.append(' ')
                    currency_formula.append(' ')
                currency_formula.append(
                    f'{exchange.currency_btc}*{exchange.currency_usd}')
                currency_sum.append(f'{(exchange.currency_btc * exchange.currency_usd):.2f}₽')
                end_dt.append(exchange.end_dt)
                admin_id.append(exchange.admin_id)

    df = pd.DataFrame({
        'ID операции': id_operation,
        'ID пользователя': t_id,
        'Время создания операции': create_dt,
        'Направление операции': type,
        'Количество': X,
        'Комиссия': commission,
        'Без комиссии': X_without_comm,
        'Курс-формула': currency_formula,
        'Курс-сумма': currency_sum,
        'Завершение операции': end_dt,
        'ID администратора': admin_id
    })

    if len(df['ID операции']) != 0:
        df['Время создания операции'] = df['Время создания операции'].dt.tz_localize(None)
        df['Завершение операции'] = df['Завершение операции'].dt.tz_localize(None)
    return df

def columns(writer):
    workbook= writer.book

    worksheet = writer.sheets['Обмен']

    worksheet.set_column('A:Z', width=17, cell_format=workbook.add_format({'align': 'center'}))
    worksheet.set_column('D:D', 22)
    worksheet.set_column('C:C', 25)
    worksheet.set_column('H:H', 20)
    worksheet.set_column('I:I', 17)
    worksheet.set_column('K:K', 23)
    worksheet.set_column('J:J', 20)

'''
writer = pd.ExcelWriter('upload.xlsx', engine ='xlsxwriter')
df(24 * 3600).to_excel(writer, sheet_name='Обмен', index=False)
columns(writer)
writer.save()
'''
#print(pd.read_excel('upload.xlsx'))


# -*- coding: utf-8 -*-
from __future__ import absolute_import

# Standard Library Imports
from collections import defaultdict
import logging
import inspect
import re

# Kodi imports
import xbmcplugin

# Package imports
from codequick.script import Script
from codequick.support import logger_id, auto_sort
from codequick.utils import ensure_native_str

__all__ = ["Route", "validate_listitems"]
_UNSET = object()

# Logger specific to this module
logger = logging.getLogger("%s.route" % logger_id)

# Localized string Constants
SELECT_PLAYBACK_ITEM = 25006
NO_DATA = 33077

def validate_listitems(raw_listitems):
    """Check if listitems are valid"""

    # Convert a generator of listitem into a list of listitems
    if inspect.isgenerator(raw_listitems):
        raw_listitems = list(raw_listitems)
    # Silently ignore False values
    elif raw_listitems is False:
        return False

    if raw_listitems:
        if isinstance(raw_listitems, (list, tuple)):
            if len(raw_listitems) == 1 and raw_listitems[0] is False:
                return False
            else:
                return raw_listitems
        else:
            raise ValueError("Unexpected return object: {}".format(type(raw_listitems)))
    else:
        raise RuntimeError("No items found")

class Route(Script):
    """
    This class is used to create "Route" callbacks. “Route" callbacks, are callbacks that
    return "listitems" which will show up as folders in Kodi.

    Route inherits all methods and attributes from :class:`codequick.Script<codequick.script.Script>`.

    The possible return types from Route Callbacks are.
        * ``iterable``: "List" or "tuple", consisting of :class:`codequick.listitem<codequick.listing.Listitem>` objects.
        * ``generator``: A Python "generator" that return's :class:`codequick.listitem<codequick.listing.Listitem>` objects.
        * ``False``: This will cause the "plugin call" to quit silently, without raising a RuntimeError.

    :raises RuntimeError: If no content was returned from callback.

    :example:
        >>> from codequick import Route, Listitem
        >>>
        >>> @Route.register
        >>> def root(_):
        >>>     yield Listitem.from_dict("Extra videos", subfolder)
        >>>     yield Listitem.from_dict("Play video",
        >>>           "http://www.example.com/video1.mkv")
        >>>
        >>> @Route.register
        >>> def subfolder(_):
        >>>     yield Listitem.from_dict("Play extra video",
        >>>           "http://www.example.com/video2.mkv")
    """

    # Change listitem type to 'folder'
    is_folder = True

    def __init__(self):
        super(Route, self).__init__()
        self.update_listing = self.params.get(u"_updatelisting_", False)
        self.category = re.sub(u"\(\d+\)$", u"", self._title).strip()
        self.cache_to_disc = self.params.get(u"_cache_to_disc_", True)
        self.redirect_single_item = False
        self._manual_sort = list()
        self.content_type = _UNSET
        self.autosort = True

    def _process_results(self, raw_listitems):
        """Handle the processing of the listitems."""
        raw_listitems = validate_listitems(raw_listitems)
        if raw_listitems is False:
            xbmcplugin.endOfDirectory(self.handle, False)
            return None

        # Create a new list containing tuples, consisting of path, listitem, isfolder.
        listitems = []
        folder_counter = 0.0
        mediatypes = defaultdict(int)
        for listitem in raw_listitems:
            if listitem:  # pragma: no branch
                # noinspection PyProtectedMember
                listitem_tuple = listitem._close()
                listitems.append(listitem_tuple)
                if listitem_tuple[2]:  # pragma: no branch
                    folder_counter += 1

                if "mediatype" in listitem.info:
                    mediatypes[listitem.info["mediatype"]] += 1

        # If redirect_single_item is set to True then redirect view to the first
        # listitem if it's the only listitem and that listitem is a folder
        if self.redirect_single_item and len(listitems) == 1 and listitems[0][2] is True:
            return listitems[0][0]  # return the listitem path

        # Guess if this directory listing is primarily a folder or video listing.
        # Listings will be considered to be a folder if more that half the listitems are folder items.
        isfolder = folder_counter > (len(listitems) / 2)

        # Sets the category for skins to display modes.
        xbmcplugin.setPluginCategory(self.handle, ensure_native_str(self.category))

        if self.content_type is not None:
            self.__content_type("files" if isfolder else "videos", mediatypes)

        # Add sort methods only if not a folder(Video listing)
        if not isfolder:
            self.__add_sort_methods(self._manual_sort)

        # Pass the listitems and relevant data to kodi
        success = xbmcplugin.addDirectoryItems(self.handle, listitems, len(listitems))
        xbmcplugin.endOfDirectory(self.handle, success, self.update_listing, self.cache_to_disc)

    def __content_type(self, default_type, mediatypes):  # type: (str, defaultdict) -> None
        """Configure plugin properties, content, category and sort methods."""

        # See if we can guess the content_type based on the mediatypes from the listitem
        if mediatypes and self.content_type is _UNSET:
            if len(mediatypes) > 1:
                from operator import itemgetter
                # Sort mediatypes by there count, and return the highest count mediatype
                mediatype = sorted(mediatypes.items(), key=itemgetter(1))[-1][0]
            else:
                mediatype = mediatypes.popitem()[0]

            # Convert mediatype to a content_type, not all mediatypes can be converted directly
            if mediatype in ("video", "movie", "tvshow", "episode", "musicvideo", "song", "album", "artist"):
                self.content_type = mediatype + "s"

        # Set the add-on content type
        content_type = self.content_type if self.content_type and self.content_type is not _UNSET else default_type
        xbmcplugin.setContent(self.handle, content_type)
        logger.debug("Content-type: %s", content_type)

    def __add_sort_methods(self, sortmethods):  # type: (list) -> None
        """Add sort methods to kodi."""
        _addSortMethod = xbmcplugin.addSortMethod

        if self.autosort:
            # Add unsorted sort method if not sorted by date and no manually set sortmethods are given
            if auto_sort and not (sortmethods or xbmcplugin.SORT_METHOD_DATE in auto_sort):
                sortmethods.append(xbmcplugin.SORT_METHOD_UNSORTED)

            # Keep the order of the manually set sort methods
            # Only sort the auto sort methods
            for method in sorted(auto_sort):
                if method not in sortmethods:
                    sortmethods.append(method)

        if sortmethods:
            for sortMethod in sortmethods:
                _addSortMethod(self.handle, sortMethod)
        else:
            # If no sortmethods are given then set sort mehtod to unsorted
            _addSortMethod(self.handle, xbmcplugin.SORT_METHOD_UNSORTED)

    def add_sort_methods(self, *methods, **kwargs):
        """
        Add sorting method(s).

        Any number of sort method's can be given as multiple arguments.
        Normally this should not be needed, as sort method's are auto detected.

        You can pass an optional keyword only argument, 'disable_autosort' to disable auto sorting.

        :param int methods: One or more Kodi sort method's.

        .. seealso:: The full list of sort methods can be found at.\n
                     https://codedocs.xyz/xbmc/xbmc/group__python__xbmcplugin.html#ga85b3bff796fd644fb28f87b136025f40
        """
        # Disable autosort if requested
        if kwargs.get("disable_autosort", False):
            self.autosort = False

        # Can't use sets here as sets don't keep order
        for method in methods:
            self._manual_sort.append(method)


"""
Some common things, mostly based on specutils.

E.g. Collapsing, but also collapsing the uncertainty.
"""

import numpy as np
from astropy.nddata import StdDevUncertainty
from copy import deepcopy

def collapse_flux_and_unc(s3d):
    """

    s3d: 3D Spectrum1D cube with uncertainty

    returns: Spectrum1D
        Single spectrum

    """
    spec_avg = s3d.collapse(method=np.nanmean, axis="spatial")

    N = np.count_nonzero(s3d.uncertainty.array, axis=(0, 1))
    spec_avg_unc = np.sqrt(
        np.nansum(np.square(s3d.uncertainty.array), axis=(0, 1))
    ) / np.count_nonzero(np.isfinite(s3d.uncertainty.array), axis=(0, 1))
    spec_avg.uncertainty = StdDevUncertainty(spec_avg_unc * s3d.flux.unit)
    return spec_avg

def clean_spikes(s3d):
    """Some rules to mask out the worst of the spikes in the data"""
    spec2 = deepcopy(s3d)
    f = spec2.flux.value
    u = spec2.uncertainty.array
    median_per_slice = np.array(
        [np.nanmedian(f[..., i][f[..., i] > 0]) for i in range(f.shape[2])]
    )
    too_big = np.abs(f) > 125 * np.abs(median_per_slice)[np.newaxis, np.newaxis, :]
    zero_unc = u <= 0
    bad_unc = ~np.isfinite(u)

    spec2.flux.value[too_big | zero_unc | bad_unc] = np.nan
    spec2.uncertainty.array[too_big | zero_unc | bad_unc] = np.nan
    spec2.mask = ~(np.isfinite(f) & np.isfinite(u))
    spec2.meta = s3d.meta

    return spec2


import json
import re
import os
import time
import numpy as np
from random import shuffle, choice
from argparse import ArgumentParser
from gensim.models import KeyedVectors
import sys

class DataSet:
    def __init__(self, word2vec, training_features, training_label, testing_features, testing_label, batch_size=4):
        self.word2vec           = KeyedVectors.load(word2vec, mmap='r')
        self.embedding_dim      = self.word2vec['<BOS>'].shape[0]
        self.vocab_size         = len(self.word2vec.vocab)
        self.training_label_txt = {} # training label in words with video id as key
        self.training_label_vec = {} # training label in vector form with video id as key
        self.testing_label_txt  = {}
        self.training_ids       = [] # the ids of the videos for training
        self.testing_ids        = [] # the ids of the videos for testing
        self.batch_size         = batch_size
        self.max_length         = None
        self.batch_index        = 0
        self.training_num       = None
        self.max_batch_index    = None
        self.sentence_limit     = 30 # default max length of a sentence

        with open(training_label, 'r') as f:
            data = json.loads(f.read())
        for movie in data:
            self.training_label_txt[movie['id']]  = []
            self.training_label_txt[movie['id']] += [('<BOS> '+re.sub(',', '', x[:-1].lower())+' <EOS>').strip().split() for x in movie['caption']]

        with open(testing_label, 'r') as f:
            data = json.loads(f.read())
        for movie in data:
            self.testing_label_txt[movie['id']]  = []
            self.testing_label_txt[movie['id']] += [('<BOS> '+re.sub(',', '', x[:-1].lower())+' <EOS>').strip().split() for x in movie['caption']]

        with open(training_features, 'r') as f:
            for line in f:
                self.training_ids.append(line.strip())

        with open(testing_features, 'r') as f:
            for line in f:
                self.testing_ids.append(line.strip())

        # create mappings
        self.word2id = {'<PAD>':0, '<BOS>':1, '<EOS>':2}
        self.id2word = {0:'<PAD>', 1:'<BOS>', 2:'<EOS>'}

        curID = 3
        for word in self.word2vec.vocab:
            if word not in self.word2id:
                self.word2id[word]  = curID
                self.id2word[curID] = word
                curID += 1

        # remove sentences with OOV words
        self.check_labels()

        self.training_num    = len(self.training_label_txt)
        self.max_batch_index = self.training_num // self.batch_size - 1

        print ('word model    -> {}'.format(word2vec))
        print ('vocab size    -> {}'.format(self.vocab_size))
        print ('max length    -> {}'.format(self.max_length))
        print ('training data -> {}'.format(self.training_num))
        print ('batch num     -> {}'.format(self.max_batch_index+1))

    def check_labels(self):
        self.max_length = 0
        for id, labels in self.training_label_txt.items():
            self.training_label_vec[id] = []
            ignore_sent = []
            for sentence in labels:
                sent_vec   = []
                legal_sent = True
                if len(sentence) > self.sentence_limit: legal_sent = False
                for word in sentence:
                    if not legal_sent:
                        ignore_sent.append(sentence)
                        break
                    try:
                        word_vec = self.word2vec[word]
                    except:
                        ignore_sent.append(sentence)
                        legal_sent = False
                        break
                    sent_vec.append(word_vec)
                if legal_sent:
                    self.training_label_vec[id].append(sent_vec)
                    self.max_length = max(self.max_length, len(sentence))
            for sent in ignore_sent:
                self.training_label_txt[id].remove(sent)

    def Word2ID(self, word):
        return self.word2id[word]

    def ID2Word(self, id):
        return self.id2word[id]

    def Word2Vec(self, word):
        if word == '<PAD>': return np.zeros(self.embedding_dim)
        return self.word2vec[word]

    def VocabSize(self):
        return self.vocab_size

    def MaxSeqLength(self):
        return self.max_length

    def EmbeddingDim(self):
        return self.embedding_dim

    def next_batch(self):
        if self.batch_index == 0:
            shuffle(self.training_ids)

        '''
        shape:
            frames : (batch size, number of frames, number of features)
            labels : (batch size, max seq length,   latent dimension)
            onehot : (batch size, max seq length,   vocab size)
        '''

        ids    = self.training_ids[self.batch_index*self.batch_size:(self.batch_index+1)*self.batch_size]

        videos = [ os.path.join('MLDS_hw2_1_data/training_data/feat', x+'.npy') for x in ids ]
        frames = [ np.load(v) for v in videos ]

        index  = [ choice(np.arange(len(self.training_label_txt[ID]))) for ID in ids ]
        labels = [ self.training_label_vec[l][index[idx]] for idx, l in enumerate(ids) ]
        labels = self.padSeqVec(labels)

        texts  = [ self.training_label_txt[l][index[idx]] for idx, l in enumerate(ids) ]
        onehot = self.toOnehot(texts)
        onehot = self.padSeqOnehot(onehot)

        self.batch_index += 1
        if self.batch_index > self.max_batch_index: self.batch_index = 0

        return frames, labels, onehot

    def padSeqVec(self, labels):
        for i in range(len(labels)):
            while len(labels[i]) < self.max_length:
                labels[i].append(self.Word2Vec('<PAD>'))
            labels[i] = np.array(labels[i])
        return np.array(labels)

    def toOnehot(self, texts):
        OnehotLabels = []
        for sentence in texts:
            OnehotSentence = []
            for word in sentence:
                OnehotWord = np.zeros(self.vocab_size)
                OnehotWord[self.Word2ID(word)] = 1
                OnehotSentence.append(OnehotWord)
            OnehotLabels.append(OnehotSentence)
        return OnehotLabels

    def IDs2VECs(self, ids):
        vecs = []
        for ID in ids:
            vecs.append(self.Word2Vec(self.ID2Word(ID)))
        return np.array(vecs)

    def padSeqOnehot(self, onehot):
        for i in range(len(onehot)):
            while len(onehot[i]) < self.max_length:
                OnehotPad = np.zeros(self.vocab_size)
                OnehotPad[self.Word2ID('<PAD>')] = 1
                onehot[i].append(OnehotPad)
            onehot[i] = np.array(onehot[i])
        return np.array(onehot)

    def test_batch(self):
        '''
        returns None when all of the data are tested
        '''
        ids    = self.testing_ids

        videos = [ os.path.join('MLDS_hw2_1_data/testing_data/feat', x+'.npy') for x in ids ]

        frames = [ np.load(v) for v in videos ]

        return frames, ids

def main(args):
    training_label    = args.file1
    testing_label     = args.file2
    training_features = args.file3
    testing_features  = args.file4
    word2vec          = args.model

    dataset = DataSet(word2vec, training_features, training_label, testing_features, testing_label)
    print (dataset.EmbeddingDim())

def parse():
    parser = ArgumentParser()
    parser.add_argument('--file1', '-f1',  default='MLDS_hw2_1_data/training_label.json', help='the file that contains texts')
    parser.add_argument('--file2', '-f2',  default='MLDS_hw2_1_data/testing_label.json', help='the file that contains texts')
    parser.add_argument('--file3', '-f3',  default='MLDS_hw2_1_data/training_data/id.txt', help='the file that contains training features')
    parser.add_argument('--file4', '-f4',  default='MLDS_hw2_1_data/testing_data/id.txt', help='the file that contains testing features')
    parser.add_argument('--model', '-m',  default='word_model/model.wv', help='the pretrained word2vec model')
    return parser.parse_args()

if __name__ == '__main__':
    main(parse())


from django.urls import path, include
from django.contrib import admin

urlpatterns = [
    path('archive/', include('archive.urls')),
    path('auth/', include('authentication.urls')),
    path('oauth/', include('oauth2_provider.urls', namespace='oauth2_provider')),
    path('timeline/', include('timeline.urls')),
    path('source/', include('source.urls')),
    path('destination/', include('destination.urls')),
]


from setuptools import find_packages, setup

INSTALL_REQUIRES = [
    "attrs>=21.4.0",
    "bs4>=0.0.1",
    "click>=7.1.2",
    "requests>=2.27.1",
    "python-dotenv>=0.19.2",
    "azure-cognitiveservices-speech>=1.20.0",
]

config = {
    "name": "chat-to-speech",
    "version": "0.0.1",
    "description": "Module for converting chat logs in html format to speech using TTS",
    "author": "Marlene Staib",
    "author_email": "marlene.staib@gmail.com",
    "packages": find_packages(include=["chat_to_speech", "chat_to_speech.*"]),
    "install_requires": INSTALL_REQUIRES,
    "entry_points": {
        "console_scripts": ["chat-to-speech=chat_to_speech.cli.main:cli"],
    },
}

setup(**config)


import argparse
import glob
import os
import subprocess
import time
from fontTools.ttLib import TTFont

# Initialize flag parser
parser = argparse.ArgumentParser()
parser.add_argument(
    "--drawbot", help="Render a specimen with DrawBot", action="store_true"
)
parser.add_argument(
    "--fontbakery", help="Test fonts with fontbakery", action="store_true"
)
parser.add_argument(
    "--googlefonts", help="Store GoogleFonts directory name"
)
parser.add_argument(
    "--ttfautohint", help="Store ttfautohint flags"
)
parser.add_argument(
    "--static", help="Build static fonts", action="store_true"
)
parser.add_argument(
    "--fixnonhinting", help="Fix nonhinting with gs tools", action="store_true"
)
args = parser.parse_args()

# Initialize empty lists
sources = []
sources_styles = []

def printR(prt):
    """
    Print in red
    """
    print("\033[91m {}\033[00m".format(prt))

def printG(prt):
    """
    Print in green
    """
    print("\033[92m {}\033[00m".format(prt))

def printY(prt):
    """
    Print in yellow
    """
    print("\033[93m {}\033[00m".format(prt))

def intro():
    """
    Gives basic script info.
    """
    printG("#    # #####        #####    ################")
    printG("#    # #            #   #    #   ##         #")
    printG(" #  #  ####          #   #  #   # #   #######")
    printG(" #  #  #     <---->  #    ##    # #      #")
    printG("  ##   #              #        #  #   ####")
    printG("  ##   #              ##########  #####")
    print("\n**** Starting variable font build script:")
    print("     [+]", time.ctime())
    printG("    [!] Done")
    time.sleep(4)

def display_args():
    """
    Gives info about the flags.
    """
    print("\n**** Settings:")
    print("     [+] --drawbot\t\t", args.drawbot)
    print("     [+] --googlefonts\t\t", args.googlefonts)
    print("     [+] --ttfautohint\t\t", args.ttfautohint)
    print("     [+] --fontbakery\t\t", args.fontbakery)
    print("     [+] --static\t\t", args.static)
    print("     [+] --fixnonhinting\t", args.fixnonhinting)
    printG("    [!] Done")
    time.sleep(4)

def check_root_dir():
    """
    Checks to make sure script is run from a git repo root directory.
    """
    print("\n**** Looking for the font repo root directory:")
    REPO_ROOT = [".gitignore", ".git"]
    repo_test = os.listdir(path=".")
    repo_test_result = all(elem in repo_test for elem in REPO_ROOT)
    if repo_test_result:
        print("     [+] OK: Looks good")
        printG("    [!] Done")
    else:
        printR("     [!] ERROR: Run script from the root directory")
    time.sleep(2)

def get_source_list():
    """
    Gets a list of source files.
    """
    print("\n**** Making a list of Glyphsapp source files:")
    os.chdir("sources")
    for name in glob.glob("*.glyphs"):
        sources.append(os.path.splitext(name)[0])
    os.chdir("..")
    print("     [+] SOURCES: List of sources =", sources)
    time.sleep(1)
    printG("    [!] Done")

def get_style_list():
    """
    Gets a list of styles from the source list.
    """
    print("\n**** Starting build process:")
    for source in sources:
        time.sleep(0.5)
        print("     [+] SOURCES: Preparing to build", source)
        print("     [+] SOURCES: Style =", source.rpartition("-")[2])
        sources_style = str(source.rpartition("-")[2])
        sources_styles.append(sources_style)
    print("     [+] SOURCES: Styles =", sources_styles)
    time.sleep(1)
    printG("    [!] Done")

def run_fontmake_variable():
    """
    Builds ttf variable font files with FontMake.
    """
    for source in sources:
        print("\n**** Building %s variable font files with FontMake:" % source)
        print("     [+] Run: fontmake ")
        subprocess.call(
            "fontmake \
                      -g sources/%s.glyphs \
                      -o variable \
                      --output-path fonts/%s-VF.ttf"
            % (source, source),
            shell=True,
        )
        print("     [!] Done")
    printG("    [!] Done")

def run_fontmake_static():
    """
    Builds ttf static font files with FontMake.
    """
    for source in sources:
        print("\n**** Building %s static font files with FontMake:" % source)
        print("     [+] Run: fontmake ")
        subprocess.call(
            "fontmake \
                      -g sources/%s.glyphs \
                      -o ttf \
                      --keep-overlaps -i"
            % (source),
            shell=True,
        )
        print("     [!] Done")
    printG("    [!] Done")

def prep_static_fonts():
    """
    Move static fonts to the fonts/static directory.
    Run ttfautohint on all fonts and fix missing dsig
    """
    print("\n**** Moving static fonts:")

    for path in glob.glob("instance_ttf/*.ttf"):
        print(path)
        subprocess.call("cp %s fonts/static-fonts/" % path, shell=True)
    subprocess.call("rm -rf instance_ttf", shell=True)

    for static_font in glob.glob("fonts/static-fonts/*.ttf"):
        print(static_font)
        subprocess.call("gftools fix-dsig %s --autofix" % static_font, shell=True)

        if args.fixnonhinting == True:
            print("FIXING NONHINTING")
            subprocess.call("gftools fix-nonhinting %s %s.fix" % (static_font, static_font), shell=True)
            subprocess.call("mv %s.fix %s" % (static_font, static_font), shell=True)
            subprocess.call("rm -rf %s.fix" % static_font, shell=True)
            subprocess.call("rm -rf fonts/static-fonts/*gasp.ttf", shell=True)
            print("     [+] Done:", static_font)

        if args.ttfautohint == True:
            subprocess.call(
                "ttfautohint %s %s temp.ttf"
                % (args.ttfautohint, static_font),
                shell=True,
            )
            subprocess.call("cp temp.ttf %s" % static_font, shell=True)
            subprocess.call("rm -rf temp.ttf", shell=True)
    time.sleep(1)
    printG("    [!] Done")

def rm_build_dirs():
    """
    Cleanup build dirs
    """
    print("\n**** removing build directories")
    print("     [+] run: rm -rf variable_ttf master_ufo instance_ufo")
    subprocess.call("rm -rf variable_ttf master_ufo instance_ufo", shell=True)
    printG("    [!] Done")
    time.sleep(1)

def fix_dsig():
    """
    Fixes DSIG table
    """
    print("\n**** Run: gftools: fix DSIG")
    for source in sources:
        subprocess.call(
            "gftools fix-dsig fonts/%s-VF.ttf --autofix"
            % source,
            shell=True,
        )
        print("     [+] Done:", source)
    printG("    [!] Done")
    time.sleep(1)

def fix_nonhinting():
    """
    Fixes non-hinting
    """
    print("\n**** Run: gftools: fix nonhinting")
    for path in glob.glob("fonts/*.ttf"):
        print(path)
        subprocess.call("gftools fix-nonhinting %s %s.fix" % (path, path), shell=True)
        subprocess.call("mv %s.fix %s" % (path, path), shell=True)
        subprocess.call("rm -rf %s.fix" % path, shell=True)
        subprocess.call("rm -rf fonts/*gasp.ttf", shell=True)
        print("     [+] Done:", path)
    printG("    [!] Done")
    time.sleep(1)

def ttfautohint():
    """
    Runs ttfautohint with various flags set. For more info run: ttfautohint --help
    """
    print("\n**** Run: ttfautohint")
    os.chdir("fonts")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    for source in sources:
        subprocess.call(
            "ttfautohint %s %s-VF.ttf %s-VF-Fix.ttf"
            % (args.ttfautohint, source, source),
            shell=True,
        )
        subprocess.call("cp %s-VF-Fix.ttf %s-VF.ttf" % (source, source), shell=True)
        subprocess.call("rm -rf %s-VF-Fix.ttf" % source, shell=True)
        print("     [+] Done:", source)
    os.chdir("..")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    printG("    [!] Done")
    time.sleep(1)

def ttfautohint_static():
    """
    Runs ttfautohint with various flags set. For more info run: ttfautohint --help
    """
    print("\n**** Run: ttfautohint")
    os.chdir("fonts")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    for source in sources:
        subprocess.call(
            "ttfautohint %s %s-VF.ttf %s-VF-Fix.ttf"
            % (args.ttfautohint, source, source),
            shell=True,
        )
        subprocess.call("cp %s-VF-Fix.ttf %s-VF.ttf" % (source, source), shell=True)
        subprocess.call("rm -rf %s-VF-Fix.ttf" % source, shell=True)
        print("     [+] Done:", source)
    os.chdir("..")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    printG("    [!] Done")
    time.sleep(1)

def google_fonts():
    """
    Copy font output to the GoogleFonts repo.
    """
    print("\n**** Copying font output to the GoogleFonts repo.")
    if args.googlefonts is not None:
        for source in sources:
            subprocess.call(
                "cp fonts/%s-VF.ttf %s/" % (source, args.googlefonts), shell=True
            )
            print("     [+] Done:", source)
    for path in glob.glob("fonts/static-fonts/*.ttf"):
        print(path)
        subprocess.call("cp %s %s/static/" % (path, args.googlefonts), shell=True)
    else:
        pass
    printG("    [!] Done")
    time.sleep(1)

def fontbakery():
    """
    Run FontBakery on the GoogleFonts repo.
    """
    print("\n**** Run: FontBakery:")
    for source in sources:
        subprocess.call("fontbakery check-googlefonts %s/%s-VF.ttf --ghmarkdown docs/FONTBAKERY-REPORT-%s.md" % (args.googlefonts, source, source), shell=True, )
        print("     [+] Done:", source)
    printG("    [!] Done")
    time.sleep(1)

def render_specimens():
    """
    Render specimens
    """
    print("\n**** Run: DrawBot")
    subprocess.call(
        "python3 docs/drawbot-sources/basic-specimen.py \
        > /dev/null 2>&1",
        shell=True,
    )
    printG("    [!] Done")
    time.sleep(1)

def main():
    """
    Executes font build sequence
    """
    intro()
    display_args()
    check_root_dir()
    get_source_list()
    get_style_list()
    run_fontmake_variable()

    # fix non-hinting
    if args.fixnonhinting == True:
        fix_nonhinting()
    else:
        pass

    # make static fonts
    if args.static == True:
        run_fontmake_static()
        prep_static_fonts()
    else:
        pass

    rm_build_dirs()
    fix_dsig()

    # ttfautohint
    if args.ttfautohint is not None:
        ttfautohint()
    else:
        pass

    # GoogleFonts
    if args.googlefonts is not None:
        google_fonts()
    else:
        pass

    # FontBakery
    if args.fontbakery == True:
        fontbakery()
    else:
        pass

    # DrawBot
    if args.drawbot == True:
        render_specimens()
    else:
        pass

if __name__ == "__main__":
    main()


import os
from flask import Flask

app = Flask(__name__)

# 使用Session必须配置如下信息
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SECRET_KEY'] = os.urandom(24)

def get_current_time_string():
    import time
    return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

@app.route("/")
def index():
    return """

cookie

session

    """

@app.route("/cookie")
def interface_cookie():
    def get_current_cookies_string():
        import json
        from flask import request
        return json.dumps(request.cookies)
    # end def

    from flask import make_response
    response = make_response(get_current_cookies_string())
    response.headers['Content-Type'] = 'text/json'
    #
    response.set_cookie('cookie_last', get_current_time_string())
    return response

@app.route("/session")
def interface_session():
    def get_current_sessions_string(sess):
        import json
        return json.dumps(dict(sess))
    # end def

    from flask import session
    from flask import make_response
    response = make_response(get_current_sessions_string(session))
    response.headers['Content-Type'] = 'text/json'
    #
    session['session_last'] = get_current_time_string()
    return response

if __name__ == '__main__':
    app.run()


import time
from typing import Dict

import tensorflow as tf

from .helpers import get_mnist, MNIST_CLASSES, MNIST_FEATURES

def full_precision_net(features: tf.placeholder, weights: Dict[str, tf.Variable]):
    """
    Constructs full precision model
    :param features: model input in form of placeholder
    :param weights: dictionary of weights
    """
    # First hidden layer
    z_1 = tf.matmul(features, weights['h1'])
    a_1 = tf.nn.relu(z_1)

    # Second hidden layer
    z_2 = tf.matmul(a_1, weights['h2'])
    a_2 = tf.nn.relu(z_2)

    # Third hidden layer
    z_3 = tf.matmul(a_2, weights['h3'])
    a_3 = tf.nn.relu(z_3)

    # Network output
    out_layer = tf.matmul(a_3, weights['out'])
    return out_layer

def run_full_precision_mlp(learning_rate: float,
                           num_steps: int,
                           batch_size: int,
                           display_step: int,
                           hidden_size: int):
    # get MNIST dataset
    mnist = get_mnist()

    # region <<<Network build>>>

    # construct stateless graph
    features = tf.placeholder("float", [None, MNIST_FEATURES])
    targets = tf.placeholder("float", [None, MNIST_CLASSES])

    # store layers weights
    weights = {
        'h1': tf.Variable(tf.random_normal([MNIST_FEATURES, hidden_size])),
        'h2': tf.Variable(tf.random_normal([hidden_size, hidden_size])),
        'h3': tf.Variable(tf.random_normal([hidden_size, hidden_size])),
        'out': tf.Variable(tf.random_normal([hidden_size, MNIST_CLASSES]))
    }

    # construct model
    logits = full_precision_net(
        features=features,
        weights=weights
    )

    prediction = tf.nn.softmax(logits)

    # define loss and optimizer
    loss_op = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=targets)
    )

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op)

    # evaluate model
    correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(targets, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    # initialize the variables
    init = tf.global_variables_initializer()
    # endregion

    # region <<<Training>>>
    with tf.Session() as sess:

        # run initializer
        sess.run(init)

        start_time = time.time()

        # repeat required number of steps
        for step in range(1, num_steps + 1):

            # get mini-batch
            batch_features, batch_targets = mnist.train.next_batch(batch_size)

            # feed batch into network and optimize
            sess.run(
                train_op,
                feed_dict={
                    features: batch_features,
                    targets: batch_targets
                }
            )

            # logging
            if step % display_step == 0 or step == 1:
                # calculate loss and accuracy for the batch
                loss, acc = sess.run(
                    [loss_op, accuracy],
                    feed_dict={
                        features: batch_features,
                        targets: batch_targets
                    }
                )

                # print the result
                print('[{:5d}] loss: {:10.2f}\taccuracy: {:2.2f}%'.format(
                    step, loss, acc * 100.0
                ))
        # endregion

        elapsed_time = time.time() - start_time
        print("elapsed time: {:.4f} seconds".format(elapsed_time))

        # region <<<Testing>>>
        test_accuracy = sess.run(
            accuracy,
            feed_dict={
                features: mnist.test.images,
                targets: mnist.test.labels
            }
        ) * 100.0
        print("Testing Accuracy: {:.4f}%".format(test_accuracy))
    # endregion


import numpy as np  # linear algebra
import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)

import os
from datetime import datetime

import matplotlib.pyplot as plt
import torchvision

import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
from PIL import Image
import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F

train_dir = "../sign_data/train"
train_csv = "../sign_data/train_data.csv"
test_csv = "../sign_data/test_data.csv"
test_dir = "../sign_data/test"
model_name = 'model_4.pt'
train_losses = []
val_losses = []

class MyDataset(Dataset):
    # default constuctor for assigning values
    def __init__(self, train_dir=None, train_csv=None, transform=None):
        self.train_dir = train_dir
        self.train_data = pd.read_csv(train_csv)
        self.train_data.columns = ['image1', 'image2', 'class']
        self.transform = transform

    def __getitem__(self, idx):  ## __getitem__ returns a sample data given index, idx=index

        img1_path = os.path.join(self.train_dir, self.train_data.iat[idx, 0])
        img2_path = os.path.join(self.train_dir, self.train_data.iat[idx, 1])

        img1 = Image.open(img1_path)
        img2 = Image.open(img2_path)

        img1 = img1.convert(
            'L')  # L mode image, that means it is a single channel image - normally interpreted as greyscale.
        img2 = img2.convert('L')

        img1 = self.transform(img1)
        img2 = self.transform(img2)

        return img1, img2, torch.from_numpy(np.array([int(self.train_data.iat[idx, 2])], dtype=np.float32))

    def __len__(self):  ## __len__ returns the size of the dataset..
        return len(self.train_data)

class SiameseNetwork(nn.Module):

    def __init__(self):
        super(SiameseNetwork, self).__init__()

        self.conv1 = nn.Conv2d(1, 50, kernel_size=5)
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        # L1 ImgIn shape=(?, 28, 28, 1)      # (n-f+2*p/s)+1
        #    Conv     -> (?, 24, 24, 50)
        #    Pool     -> (?, 12, 12, 50)

        self.conv2 = nn.Conv2d(50, 60, kernel_size=5)
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        # L2 ImgIn shape=(?, 12, 12, 50)
        #    Conv      ->(?, 8, 8, 60)
        #    Pool      ->(?, 4, 4, 60)

        self.conv3 = nn.Conv2d(60, 80, kernel_size=3)
        # L3 ImgIn shape=(?, 4, 4, 60)
        #    Conv      ->(?, 2, 2, 80)

        self.batch_norm1 = nn.BatchNorm2d(50)
        self.batch_norm2 = nn.BatchNorm2d(60)

        #         self.dropout1 = nn.Dropout2d()

        # L4 FC 2*2*80 inputs -> 250 outputs
        self.fc1 = nn.Linear(32000, 128)
        self.fc2 = nn.Linear(128, 2)

    def forward_once(self, x):
        x = self.conv1(x)
        x = self.batch_norm1(x)
        x = F.relu(x)
        x = self.pool1(x)

        x = self.conv2(x)
        x = self.batch_norm2(x)
        x = F.relu(x)
        x = self.pool2(x)

        x = self.conv3(x)
        x = F.relu(x)
        #         print(x.size())
        x = x.view(x.size()[0], -1)
        #         print('Output2')
        #         print(x.size()) #32000 thats why the input of fully connected layer is 32000
        x = F.relu(self.fc1(x))
        x = self.fc2(x)

        return x

    def forward(self, input1, input2):
        # forward pass of input 1
        output1 = self.forward_once(input1)
        # forward pass of input 2
        output2 = self.forward_once(input2)

        return output1, output2

class ContrastiveLoss(torch.nn.Module):

    def __init__(self, margin=2.0):
        super(ContrastiveLoss, self).__init__()
        self.margin = margin

    def forward(self, output1, output2, label):
        euclidean_distance = F.pairwise_distance(output1, output2)
        loss_contrastive = torch.mean((1 - label) * torch.pow(euclidean_distance, 2) +
                                      (label) * torch.pow(torch.clamp(self.margin - euclidean_distance, min=0.0), 2))

        return loss_contrastive

def train(nn_model, criterion, device, train_dataloader, optimizer, epoch):
    nn_model.train()

    batch_i_log = 50
    steps_count = len(train_dataloader) - 1
    train_loss = 0.0
    for batch_i, batch_data in enumerate(train_dataloader):
        img0, img1, label = batch_data
        img0, img1, label = img0.to(device), img1.to(device), label.to(device)

        optimizer.zero_grad()
        output1, output2 = nn_model(img0, img1)

        loss = criterion(output1, output2, label)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()

        if batch_i % batch_i_log == 0:
            print("Train Epoch {}; Step {}/{}; Current loss {:.6f}"
                  .format(epoch, batch_i, steps_count, loss.item()))
        elif batch_i == steps_count:
            print("Train Epoch {}; Step {}/{}; Current loss {:.6f}"
                  .format(epoch, batch_i, steps_count, loss.item()))

    # return loss_contrastive

def test_acc_loss(nn_model, criterion, device, test_loader):
    test_loss = 0.0
    correct = 0
    threshold = 0.41
    nn_model.eval()

    with torch.no_grad():
        for batch_data in test_loader:
            img0, img1, label_1 = batch_data
            img0, img1, label = img0.to(device), img1.to(device), label_1.to(device)

            output1, output2 = nn_model(img0, img1)
            loss = criterion(output1, output2, label)
            test_loss += loss.item()

            euclidean_distance = F.pairwise_distance(output1, output2)
            if label_1 == torch.FloatTensor([[0]]):
                label = "Orig"
            else:
                label = "Forg"

            predict = "Forg"
            if euclidean_distance < threshold:
                predict = "Orig"

            if label[0] == predict[0]:
                correct += 1

    test_acc = 100 * correct / len(test_loader)
    test_loss /= len(test_loader)
    print('Test acc: {}/{} ({:.4f}%) Test loss: {} \n'.format(
        correct, len(test_loader.dataset), test_acc, test_loss))

def train_valid(train_dataloader, val_dataloader, device, num_epochs):
    nn_model = SiameseNetwork().to(device)
    criterion = ContrastiveLoss()
    optimizer = optim.RMSprop(nn_model.parameters(), lr=1e-4, alpha=0.99)

    batch_i_log = 50
    steps_count = len(train_dataloader) - 1
    min_valid_loss = np.inf

    for epoch in range(1, num_epochs + 1):

        train_loss = 0.0
        nn_model.train()

        for batch_i, batch_data in enumerate(train_dataloader):
            img0, img1, label = batch_data
            img0, img1, label = img0.to(device), img1.to(device), label.to(device)

            optimizer.zero_grad()
            output1, output2 = nn_model(img0, img1)

            loss = criterion(output1, output2, label)
            loss.backward()
            optimizer.step()

            train_loss += loss.item()

            if batch_i % batch_i_log == 0:
                print("Train Epoch {}; Step {}/{}; Current loss {:.6f}"
                      .format(epoch, batch_i, steps_count, loss.item()))
            elif batch_i == steps_count:
                print("Train Epoch {}; Step {}/{}; Current loss {:.6f}"
                      .format(epoch, batch_i, steps_count, loss.item()))

        valid_loss = 0.0
        nn_model.eval()

        with torch.no_grad():
            for batch_data in val_dataloader:
                img0, img1, label_1 = batch_data
                img0, img1, label = img0.to(device), img1.to(device), label_1.to(device)

                output1, output2 = nn_model(img0, img1)
                loss = criterion(output1, output2, label)
                valid_loss += loss.item()

        train_loss /= len(train_dataloader.dataset)
        valid_loss /= len(val_dataloader.dataset)
        print("Epoch {}; Train loss {:.6f}; Val loss {:.6f}\n".format(epoch, train_loss, valid_loss, ))

        if min_valid_loss > valid_loss:
            print("Val loss decreased ({:.6f}->{:.6f})\t Saving the model\n".format(min_valid_loss, valid_loss))
            min_valid_loss = valid_loss
            torch.save(nn_model.state_dict(), model_name)

class TestInfo:
    euclidean_distance = []
    labels = []
    predicts = []

def imshow(img, text=None, should_save=False):
    npimg = img.numpy()
    plt.axis("off")
    if text:
        plt.text(75, 8, text, style='italic', fontweight='bold',
                 bbox={'facecolor': 'white', 'alpha': 0.8, 'pad': 10})
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()

def test_with_stats(nn_model, device, test_dataloader):
    if torch.cuda.is_available():
        nn_model.load_state_dict(torch.load(model_name))
    else:
        nn_model.load_state_dict(torch.load(model_name, map_location=torch.device('cpu')))

    test_info = TestInfo()
    threshold = 0.41
    test_acc = 0.0
    for i, batch_data in enumerate(test_dataloader, 0):
        img0, img1, label_1 = batch_data
        img0, img1, label = img0.to(device), img1.to(device), label_1.to(device)

        output1, output2 = nn_model(img0, img1)
        euclidean_distance = F.pairwise_distance(output1, output2)

        if label_1 == torch.FloatTensor([[0]]):
            label = "Orig"
        else:
            label = "Forg"

        predict = "Forg"
        if euclidean_distance < threshold:
            predict = "Orig"
        if label[0] == predict[0]:
            test_acc += 1

        test_info.euclidean_distance.append(euclidean_distance.item())
        test_info.labels.append(label)
        test_info.predicts.append(predict)
        if i == 10:
            break
    with open('test results.txt', 'w', encoding='utf-8') as f:
        f.write("Predicted Euclidean Distance, Actual Label, Predicted Label\n")
        for j in range(len(test_dataloader)):
            f.write(str(test_info.euclidean_distance[j]) + ', '
                    + test_info.labels[j] + ', ' + test_info.predicts[j] + '\n')
    test_acc /= len(test_dataloader)
    print(test_acc)

def test(dataloader, nn_model, device):
    for i, data in enumerate(dataloader, 0):
        img0, img1, label_1 = data
        concat = torch.cat((img0, img1), 0)
        img0, img1, label = img0.to(device), img1.to(device), label_1.to(device)

        output1, output2 = nn_model(img0, img1)
        euclidean_distance = F.pairwise_distance(output1, output2)

        if label_1 == torch.FloatTensor([[0]]):
            label = "Original"
        else:
            label = "Forged"

        imshow(torchvision.utils.make_grid(concat), 'Label: {}\nEuclidean Distance: {:.4f}'
               .format(label, euclidean_distance.item()))
        if i == 10:
            break

def main():
    # Training settings
    cpu_cores_number = 4
    num_epochs = 10
    batch_size = 32
    seed = 10
    torch.manual_seed(seed)

    train_dataset = MyDataset(train_dir, train_csv,
                              transform=transforms.Compose([transforms.Resize((100, 100)), transforms.ToTensor()]))
    train_dataloader = DataLoader(train_dataset, num_workers=cpu_cores_number, batch_size=batch_size, shuffle=True)

    val_dataset = MyDataset(test_dir, test_csv,
                            transform=transforms.Compose([transforms.Resize((100, 100)), transforms.ToTensor()]))
    val_dataloader = DataLoader(val_dataset, num_workers=cpu_cores_number, batch_size=batch_size, shuffle=True)

    test_dataset = MyDataset(test_dir, test_csv,
                             transform=transforms.Compose([transforms.Resize((100, 100)), transforms.ToTensor()]))
    test_dataloader = DataLoader(test_dataset, num_workers=1, batch_size=1, shuffle=True)

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    print(device)

    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Start Time =", current_time)
    train_valid(train_dataloader, val_dataloader, device, num_epochs)
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("End Time =", current_time)

    # Test
    nn_model = SiameseNetwork().to(device)
    if torch.cuda.is_available():
        nn_model.load_state_dict(torch.load(model_name))
    else:
        nn_model.load_state_dict(torch.load(model_name, map_location=torch.device('cpu')))

    test(test_dataloader, nn_model, device)

if __name__ == '__main__':
    main()


import sys
import re
import subprocess
from typing import List

# decorator to handle ctrl+c in cli
def handle_ctrl_c(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except KeyboardInterrupt:
            print("\n\nExiting due to keyboard interrupt...")
            sys.exit(0)
    return wrapper

def fuzzy_match(query: str, string: str) -> bool:
    """fuzzy match query as regex to string
    parse and accept query as regex if it looks like it
    """
    if query.startswith("/") and query.endswith("/"):
        query = query[1:-1]
        return bool(re.search(query, string, re.IGNORECASE))
    return query in string

def interactive_pick_text(options: List[str]) -> str:
    """interactive pick from options"""
    print("Pick one of the following options:")
    for i, option in enumerate(options):
        print(f"{i+1}. {option}")
    while True:
        try:
            choice = int(input("Enter your choice: "))
            if choice < 1 or choice > len(options):
                raise ValueError
            return options[choice-1]
        except ValueError:
            print("Invalid choice, try again...")

def interactive_pick_dmenu(options: List[str]) -> str:
    out = subprocess.run(
        ["_themenu.sh", "-p", "Select SSID"],
        input="\n".join(options),
        capture_output=True,
        text=True,
    )
    return out.stdout.strip()


import zipfile
# import cloudshell.api.cloudshell_api as api
import qualipy.api.cloudshell_api as api

NameOfDriver = 'AdminOptions_CreateConnection'
ZipAddress = NameOfDriver + '.zip'
z = zipfile.ZipFile(ZipAddress, "w")
# z.write("drivermetadata.xml")
# z.write("requirements.txt")
# z.write("CreateResource.py")
z.write("CreateConnection.py")
z.write("get_cs_session.py")
z.write("__main__.py")
z.close()

ss = api.CloudShellAPISession('q1.cisco.com', 'admin', 'admin', 'Global')
# ss.UpdateScript(NameOfDriver, ZipAddress)


from os import name
import numpy as np
import pandas as pd
from sklearn import preprocessing
import matplotlib.pyplot as plt

# df = pd.read_csv(r'C:\Users\Testing\Fauzan1-02-03-20.csv')
# x=df.values
# min_max = preprocessing.MinMaxScaler()
# x_scaled = min_max.fit_transform(x)
# # df = pd.Dataframe(data=x_scaled[1:,1:], index=x_scaled[1:,0], columns=x_scaled[0,1:])
# df = pd.DataFrame.from_records(x_scaled)

# plt.plot(df.iloc[-200:,44], color = "red", label = "roll")
# plt.plot(df.iloc[-200:,45], color = "green", label = "yaw")
# plt.plot(df.iloc[-200:,46], color = "blue", label = "pitch")
# plt.plot(df.iloc[-200:,47], color = "orange", label = "pressure")

df = pd.read_csv("C:\\Users\\Testing\\Downloads\\position3d.csv")
df.drop(columns = ['Unnamed: 0'], inplace = True)

# df['Xdif'] = df['Left_ankleX'] - df['Left_ankleX'].min()
# df['Ydif'] = df['Left_ankleY'] - df['Left_ankleY'].min()
# df['Zdif'] = df['Left_ankleZ'] - df['Left_ankleZ'].min()

for i in df.columns:
    df['{}dif'.format(i)] = df[i] - df[i].mean()

for i in df.columns[-54:][2::3][8:14][:3]:
    plt.plot(df.loc[1150:1200,i], label = i)
    # plt.plot(df[i], label = i)

# plt.plot(df.loc[1000:1200,'Xdif'], color = "red", label = "X")
# plt.plot(df.loc[1000:1200,'Ydif'], color = "green", label = "Y")
# plt.plot(df.loc[1000:1200,'Zdif'], color = "blue", label = "Z")

plt.legend()
plt.title('Right Lower joints normalized with mean')
plt.show()

for i in df.columns[-54:][2::3][8:14][3:]:
    plt.plot(df.loc[1150:1200,i], label = i)

plt.legend()
plt.title('Left Lower joints normalized with mean')
plt.show()

for i in df.columns[0:54][2::3][8:14]:
    plt.scatter(df[i].std(),df[i].mean(), label = i)

plt.legend()
plt.title('Mean vs Standard Deviation')
plt.show()

for i in df.columns[0:54][2::3][8:14]:
    plt.hist(df[i], bins=72)
    plt.axvline(df[i].mean(), color='blue', linestyle='dashed', linewidth=1, label='Mean')
    plt.axvline(df[i].median(), color='red', linestyle='dashed', linewidth=1, label='Median')
    plt.legend()
    plt.title(i)
    plt.show()


from import_list import *
from population import population
from datetime import datetime, timedelta
import multiprocessing
import sys
import random

import matplotlib.pyplot as plt
import numpy as np

def stop_condition(pop):
    if  pop.best_fitness["HC1"]==0 and \
        pop.best_fitness["HC2"]==0 and \
        pop.best_fitness["HC3"]==0 and \
        pop.best_fitness["SC1_max"]<=5:
        return True
    return False

def run(args):
    print("Main seed:",args["seed"])

    time_pop = datetime.now()
    pop = population(args)
    print(datetime.now()-time_pop)

    # print(pop.best_fitness, pop.best_tot_fitness)

    i = 0
    while not stop_condition(pop):
    # while datetime.now() - time_pop < timedelta(minutes=20):
    # while i < args["n_gen"]:
        # if stop_condition(pop):
        #     break
        i+=1

        print("\nGeneration",i)
        time_gen = datetime.now()

        pop.find_pop_diversity()

        # pop.tournament_selection()
        pop.variation_tournament_selection()

        pop.mutate_mp()

        pop.elitism()

        pop.find_best_individual()

        print(pop.best_fitness)
        print("tot fitness",pop.best_tot_fitness)
        print(datetime.now()-time_gen)

    # show best
    print("-----------------------------------------------")
    print("Best individual")
    print(pop.best_fitness, pop.best_tot_fitness)
    print(datetime.now()-time_pop)

if __name__ == '__main__':

    args = {}
    args["fname"] = "./data.csv"
    args["fixed_fname"] = "./fixed_data.csv"

    args["pop_size"] = 20
    args["n_gen"] = 50

    args["cpu"] = 5

    args["seed"] = random.randint(0,sys.maxsize) // args["pop_size"]

    args["t_size"] = int(0.2*args["pop_size"]) if args["pop_size"]>=10 else 1
    args["hill_climb"] = True
    args["swaps"] = 10

    run(args)


import requests
a = requests.get("https://www.okex.com/api/v1/future_kline.do?symbol=eth_usd&type=1min&since=15000000000&contract_type=quater&size=3000").json()
len(a)

import psycopg2
import subprocess
import os
import glob
import pandas as pd
import time
from enum import Enum

def get_conn_url():
    url = 'postgresql://{}:{}@{}:5432/{}'.format(
            'crypto',
            'analyzer',
            '199.169.0.99',
            'crypto_analyzer_dev')
    return url

def conn(n_try=5):
    exception = None
    while n_try > 0:
        try:
            return psycopg2.connect(get_conn_url())
        except Exception as e:
            exception = e
            time.sleep(0.5)
            n_try -= 1
    print(exception)

def do_sql(sql):
    c = conn()
    with c.cursor() as cursor:
        try:
            cursor.execute(sql)
            c.commit()
        except Exception as e:
            print(e)

def fetch_all_sql(sql):
    c = conn()
    with c.cursor() as cursor:
        try:
            cursor.execute(sql)
            result = cursor.fetchall()
            c.commit()
            return result
        except Exception as e:
            print(e)
            return None

def fetch_one_sql(sql):
    c = conn()
    with c.cursor() as cursor:
        try:
            cursor.execute(sql)
            result = cursor.fetchone()
            c.commit()
            return result
        except Exception as e:
            print(e)
            return None

def execute_sql_path(sql_path):
    from crypto_analyzer.config import app_config
    print('\nexecute {}...'.format(os.path.basename(sql_path)))
    db = app_config().DB
    args = db.connect_kwargs
    command = 'psql -h {} -U {} -p 5432 {} < {}'.format(
            args['host'],
            args['user'],
            db.database,
            sql_path)
    print(subprocess.getoutput(command))

def do_migration():
    dir_name = os.path.dirname(os.path.abspath(__file__))
    for sql_path in sorted(glob.glob(os.path.join(dir_name, 'migrations', '*.sql'))):
        execute_sql_path(sql_path)

class Query:
    class Type(Enum):
        INSERT = 'INSERT'
        SELECT_MANY = 'SELECT_MANY'
        SELECT_ONE = 'SELECT_ONE'
        PANDAS = 'PANDAS'

    def __init__(self):
        self.sql = None
        self.mode = 'normal'

    def query(self, query):
        self.sql = query
        return self

    def fetch_mode(self, mode):
        self.mode = mode
        return self

    def run(self):
        if self.sql is None:
            return None

        if self.mode is Query.Type.PANDAS:
            return pd.read_sql_query(self.sql, get_conn_url())
        elif self.mode is Query.Type.SELECT_MANY:
            return fetch_all_sql(self.sql)
        elif self.mode is Query.Type.SELECT_ONE:
            return fetch_one_sql(self.sql)
        elif self.mode is Query.Type.INSERT:
            return do_sql(self.sql)
        else:
            return None

def candlestick_prices(period, currency_code, from_time, to_time, offset, limit,
        desc=False, exchange=None):
    order_by = 'DESC' if desc else ''
    where_exchange = "AND exchange = '{}'".format(exchange) if exchange else ''
    sql = """
    SELECT time_bucket('{}', time) AS period,
    last(last, time) as v_t,
    MAX(last) as v_hi,
    MIN(last) as v_lo,
    last(bid, time) as bid,
    last(ask, time) as ask
    FROM exchange_tickers
    WHERE currency_code = '{}' AND '{}' <= time AND time <= '{}' {}
    GROUP BY period
    ORDER BY period {}
    OFFSET {}
    LIMIT {};
    """.format(period, currency_code, from_time, to_time, where_exchange, order_by, offset, limit)
    return Query().query(sql).fetch_mode(Query.Type.PANDAS).run()

def BTC_csv(candle, path):
    csv = candle.to_csv(path)
    return

def BTC_df(path):
    df = pd.read_csv(path)
    df.drop(columns={"Unnamed: 0", "bid", "ask"},inplace=True)
    df["Open"] = df["v_t"].shift()
    df.rename(columns={"v_t":"Close", "v_hi":"High", "v_lo":"Low"}, inplace=True)
    df = df.set_index("period")
    return df


"""
Class: Single Attempt of application
"""

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time
# from PIL import Image

class Attempt:

    def __init__(self, client):
        self.driver = webdriver.Chrome('./bin/chromedriver')
        self.client = client
        self.alive = False

    def go(self):

        while not self.alive:

            # Make sure no cookie
            self.driver.delete_all_cookies()

            self.alive = self.processFirstPage()

            if self.alive:
                self.alive = self.processSencondPage()
            # if self.alive:
            #     self.alive = self.processWarningPage()
            if self.alive:
                self.alive = self.processThirdPage()

        print("** Server is Up! **")
        if self.alive:
            self.alive = self.processForthPage()
        if self.alive:
            self.alive = self.processFifthPage()
        if self.alive:
            self.processLastPage()

    def killAttempt(self):
        self.driver.quit()

    def processFirstPage(self):

        # create the chrome driver
        driver = self.driver

        # Page 1:
        # https://eapps2.td.gov.hk/repoes/td-es-app517/Welcome.do?language=zh
        # task 1: select radio button 2
        driver.get(
            'https://eapps2.td.gov.hk/repoes/td-es-app517' +
            '/Welcome.do?language=zh')

        print("[Start]: " + driver.current_url + "\n")
        radioChoice = driver.find_element_by_xpath(
            '//*[@id="contentPanel"]/div[3]/div[1]/div/' +
            'div[2]/table/tbody/tr/td/table/tbody/tr/td/form/input[2]')
        radioChoice.click()

        # task 2: goto next Page
        nextPage = driver.find_element_by_xpath(
            '//*[@id="contentPanel"]/div[4]/div/a[@class="redbutton"]')
        nextPage.click()

        return True

    def processSencondPage(self):

        driver = self.driver
        # Page 2:
        # https://eapps2.td.gov.hk/repoes/td-es-app517/SelectServiceAction.do
        # task 1: wait the page to be loaded
        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.NAME, "app517Form"))
            )
        except:
            driver.quit()
            return False

        print("[Start]: " + driver.current_url + "\n")

        radioChoice = driver.find_element_by_xpath(
            '//input[@name="serviceChoice"][@value="appointment"]')
        radioChoice.click()

        # task 2: goto next page, rely on the class="redbutton"
        nextButton = driver.find_element_by_xpath('//a[@class="redbutton"]')
        nextButton.click()

        return True

    def processWarningPage(self):

        driver = self.driver
        # Page 2:
        # https://eapps2.td.gov.hk/repoes/checkclientconfig/warning.jsp
        # task 1: wait the page to be loaded
        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.CLASS_NAME, "errorPanel"))
            )
        except:
            driver.quit()
            return False

        print("[Start]: " + driver.current_url + "\n")

        # task 2: goto next page, rely on the class="redbutton"
        nextButton = driver.find_element_by_css_selector('input.actionBtnCom')
        nextButton.click()

        return True

    def processThirdPage(self):

        driver = self.driver

        def waitForInput():

            count = 0
            elem = driver.find_element_by_id('jcaptcha_response')

            while True:
                count += 1
                if count > 40:
                    print(">> Key entered or Timing out after 20 seconds..\n")
                    return False
                if len(elem.get_attribute('value')) == 6:
                    return True
                time.sleep(.5)

            return True

        # Page 3:
        # https://eapps2.td.gov.hk/repoes/td-es-app517/Welcome.do?language=zh
        # task 1: wait the page to be loaded
        try:
            WebDriverWait(driver, 3).until(
                EC.presence_of_element_located((By.NAME, "instructionForm"))
            )
        except:
            # driver.quit()
            return False

        print("[Start]: " + driver.current_url + "\n")

        # task 2: check agree radio
        radioChoice = driver.find_element_by_xpath('//input[@name="agree"]')
        radioChoice.click()

        # task 3: check captcha
        # captcha = driver.find_element_by_id('jcaptcha_response')
        driver.execute_script(
            "window.scrollTo(0,document.body.scrollHeight);" +
            "document.getElementById('jcaptcha_response').focus();")

        # captcha = driver.find_element_by_xpath('//*[@id="contentPanel"]/form/div[3]/div/table/tbody/tr[2]/td/div/table/tbody/tr[1]/td/img')
        # location = element.location
        # size = element.size
        # im = Image.open(StringIO(base64.decodestring(driver.get_screenshot_as_base64())))
        # left = location['x']
        # top = location['y']
        # right = location['x'] + size['width']
        # bottom = location['y'] + size['height']
        # im = im.crop((left, top, right, bottom))
        # im.save('screenshot.png')

        # Check if the captcha has been filled
        if not waitForInput():
            return False

        driver.find_element_by_xpath('//a[@class="redbutton"]').click()

        print(">> Jumping to next page..\n")
        return True

    def processForthPage(self):
        driver = self.driver
        client = self.client
        # check if the page is loaded
        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.NAME, "main"))
            )
        except:
            print(">> Can't load first input page\n")
            driver.quit()
            return False

        finally:
            driver.switch_to_default_content
            driver.switch_to_frame('main')

        print("[Start]: First input page\n")

        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located(
                    (By.ID, "testFormNumForLastAttempt"))
            )
        finally:
            print(">> Ready to input..\n")

        # Inputs the client's data:
        try:
            driver.find_element_by_id(
                'testFormNumForLastAttempt').send_keys(client['code'])
            driver.find_element_by_id(
                'testFormBirthYear').send_keys(client['b-year'])
            driver.find_element_by_id(
                'testFormBirthMonth').send_keys(client['b-month'])
            driver.find_element_by_id(
                'testFormBirthDay').send_keys(client['b-date'])

            driver.find_element_by_xpath('//a[@class="redbutton"]').click()
        except:
            print('>> Cant assign inputs..\n')
            driver.quit()
            return False

        print(">> Jumping to next page..\n")
        return True

    def processFifthPage(self):

        driver = self.driver
        client = self.client

        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located(
                    (By.XPATH, '//input[@name="telephoneNo"]'))
            )
            print("[Start]: Second input page\n")
        except:
            print(">> Can't load second inputendar page\n")
            driver.quit()
            return False

        # Inputs the client's data:
        print(">> Ready to input..\n")
        try:
            driver.find_element_by_xpath(
                '//input[@name="telephoneNo"]').send_keys(client['mobile'])

            if client["lens"] == "n":
                driver.find_elements_by_xpath(
                    '//input[@name="wearLensesInd"]')[1].click()
            else:
                driver.find_elements_by_xpath(
                    '//input[@name="wearLensesInd"]')[0].click()

            driver.find_elements_by_xpath(
                '//input[@name="wearAidsInd"]')[1].click()
            driver.find_elements_by_xpath(
                '//input[@name="physicalHandicapInd"]')[1].click()

            driver.find_element_by_xpath('//a[@class="redbutton"]').click()
        except:
            raise
            driver.quit()
            return False

        print(">> Jumping to next page..\n")
        return True

    def processLastPage(self):

        # This method aims to located the available's link and
        # click on it if possible

        driver = self.driver
        client = self.client
        timeslot = None
        nextMonth = None
        testAreaCodeOption = None

        # check if the page is loaded
        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.XPATH, "/html"))
            )
            print("[Start]: Calendar page\n")
        except:
            print(">> Can't load calendar page\n")
            driver.quit()
            return False
        else:
            driver.switch_to_default_content()
            driver.switch_to_frame('main')

        # If user are from kowloon, change to kowloon then
        if client["district"] == 'kowloon':
            # Try find the availale link and click it
            try:
                testAreaCodeOption = select = Select(driver.find_element_by_id('testAreaCode'))
            except:
                print(">> trouble selecting areaCode..\n")
            else:
                testAreaCodeOption.select_by_value('2')

        # check if the page is loaded
        try:
            WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.XPATH, "/html"))
            )
            print("[Start]: Calendar page\n")
        except:
            print(">> Can't load calendar page\n")
            driver.quit()
            return False
        else:
            driver.switch_to_default_content()
            driver.switch_to_frame('main')

        # Try find the availale link and click it
        try:
            timeslot = driver.find_element_by_xpath(
                '//td[@class="inner-table-cell"]/a')
        except:
            print(">> Couldn't find available timeslot..\n")
        else:
            timeslot.click()

        # If no available link if find,
        # try find the next month's link and click it
        if timeslot is None:
            try:
                nextMonth = driver.find_element_by_xpath(
                    '//td[@class="calendar-table-header"][3]/a')
            except:
                print(">> Next Month is not available as well..\n")
            else:
                nextMonth.click()

                # Try find the availale link and click it
                try:
                    timeslot = driver.find_element_by_xpath(
                        '//td[@class="inner-table-cell"]/a')
                except:
                    print(">> Couldn't find available timeslot in next month too..\n")
                else:
                    timeslot.click()

        if timeslot is not None:
            try:
                WebDriverWait(driver, 10).until(
                    EC.presence_of_element_located((By.XPATH, "/html"))
                )
                print("Almost there!!! Agree to print the booking\n")
            except:
                print(">> Can't load printing page\n")
                # driver.quit()
                return False
            else:
                # task 1: check agree radio
                radioChoice = driver.find_element_by_xpath('//input[@name="agree"]')
                radioChoice.click()
                driver.find_element_by_xpath('//a[@class="redbutton"]').click()

        print(">> I have done my job!\n")
        print("< Input 'q' to quit the program >")

        return True


# -*- coding: utf-8 -*-
"""Select server (this or subserver) for detection.
"""

import os
import sys
import time
import json
import glob
import pickle
import shutil
import requests

import numpy as np # type: ignore
import cv2

from multiprocessing import Process#, Queue, Value # pylint: disable=W0611
from threading import Thread, Semaphore, get_ident, Lock
from queue import Queue
#import threading, queue
#from multiprocessing import shared_memory#, Lock
import shared_memory
#from NamedAtomicLock import NamedAtomicLock
from named_atomic_lock import NamedAtomicLock
from constants import SHM_PRF

from pipesworker import PipesWorker
from images_bytes_conversions import get_np_image_from_base64, get_base64_from_np_image, get_base64_from_np_array
sys.path.append('../System')
from conversions import int_to_bytes, int_from_bytes # pylint: disable=E0401, C0413
from load_config import load_config

from constants import RUNNED, NEED_STOP_PROC, NEED_STOP_THRD, STOPPED, WAIT_DET_PROC_RESP

#from zmq_wrapper import ZmqWrapper, MON_HOST, DET_HOST, EMB_HOST, SRV_HOST
from shm_wrapper_t import ShmWrapperT

SENTINEL = -1
QUARTER_QUEUE_TEST_CID = 9900
pipe_in_name = './pipes/pipe_detector_main_'
pipe_out_name = './pipes/pipe_main_detector_'

################################################################################

import logging

logger = logging.getLogger('RunnerDetect')
logger.setLevel(logging.INFO)

# Create file handler
logger_handler = logging.FileHandler('./logs/runner_detect.log')
logger_handler.setLevel(logging.INFO)
# Create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

#logger_formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
logger_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
logger_handler.setFormatter(logger_formatter)

logger.addHandler(logger_handler)
#logger.addHandler(ch)

################################################################################

"""class Lock():
    def __init__(self):
        self.flag = False

    def locked(self):
        return self.flag

    def release(self):
        self.flag = False

    def acquire(self):
        while self.flag:
            time.sleep(0.1 / 1000)
        self.flag = True
"""

class RunnerDetect(PipesWorker):
    def __init__(self, json_name):
        PipesWorker.__init__(self, '', '', blocked=False)

        self.json_name = json_name
        CONF_DCT = load_config(json_name)
        self.CONF_DCT = CONF_DCT
        self.DET_PROCS = CONF_DCT['DET_PROCS']
        self.EMBEDDER_BATCH = CONF_DCT['EMBEDDER_BATCH']
        self.IMAGE_SIZE = CONF_DCT['IMAGE_SIZE']
        self.N_GPUS = CONF_DCT['N_GPUS']

        self.last_loaded_id = 0

        self.imgs_dt_lst = []
        self.imgs_dt_len = 0
        self.imgs_dt_lock = Lock()

        self.shm_stop = shared_memory.SharedMemory2(name=SHM_PRF+'stop') # last_handled_id, last_saved_id, reserve
        self.shm_a = shared_memory.SharedMemory2(name=SHM_PRF+'a') # last_handled_id, last_saved_id, reserve
        self.shm_b = shared_memory.SharedMemory2(name=SHM_PRF+'b') # 4*EMBEDDER_BATCH of 160*160*3 images + 13 rect+keypoints+agegender
        self.shm_c = shared_memory.SharedMemory2(name=SHM_PRF+'c') # 4*EMBEDDER_BATCH of 512 embeddeings
        #self.shm_a_lock = NamedAtomicLock(SHM_PRF+'a_lock')
        self.shm_b_lock = Lock()  # NamedAtomicLock(SHM_PRF+'b_lock')
        #self.shm_c_lock = NamedAtomicLock(SHM_PRF+'c_lock')
        self.start_lock = Lock()
        self.stop_resp_thrd = False

        self.zmq_mon = ShmWrapperT('runner_v3_detect', 'monitor', 4096)
        self.zmq_dets = []
        for detector_id in range(self.DET_PROCS):
            zmq_detector = ShmWrapperT(f'detect_thread_{detector_id}', f'detector_{detector_id}', 4*1024*1024)
            self.zmq_dets.append(zmq_detector)
        self.last_worked_detector_id = self.DET_PROCS - 1
        self.start_string = 'python3 src/runner_v3_detect.py %s' % json_name

    def push_quarter_queue_for_all_detectors_test(self):
        fname = './saved_for_internal_tests/000000000000000000000001_9900.pkl'
        f = open(fname, 'rb')
        (req_id, client_id, img_id, nparr, status, dict_stat) = pickle.load(f)
        req_id, client_id, img_id = 0, QUARTER_QUEUE_TEST_CID, ''
        dict_stat['det_load_from_saved_tm'] = time.time()
        dict_stat['saved_queue_len'] = 100500
        #dict_stat['qsize'] = self.q.qsize()
        dict_stat['det_put_to_internal_queue_tm'] = time.time()
        data = (req_id, client_id, img_id, nparr, status, dict_stat)
        identifier, tm, msg_type = 'server3_mt', time.time(), 'send_image_test'
        dt = (identifier, tm, msg_type, data)
        for i in range(self.DET_PROCS * 2):
            self.zmq_callback_func(dt)

    def zmq_callback_func(self, dt):
        #print(f'runner_v3_detect: Runner_V3_Detect: zmq_callback_func')
        (identifier, tm, msg_type, data) = dt
        if data:
            (req_id, client_id, img_id, nparr, status, dict_stat) = data
            print(f'runner_detect: RunnerDetect: zmq_callback_func: req_id={req_id}  self.imgs_dt_len={self.imgs_dt_len}')
            self.imgs_dt_lock.acquire()
            self.imgs_dt_lst.append((req_id, client_id, img_id, nparr, status, dict_stat))
            self.imgs_dt_len += 1
            self.shm_a.buf[3*8:3*8+8] = self.imgs_dt_len.to_bytes(8, 'big')
            self.imgs_dt_lock.release()
        else:
            print(f'runner_detect: RunnerDetect: zmq_callback_func: req_id={None}  self.imgs_dt_len={self.imgs_dt_len}')

    def run(self):
        # Main thread
        print('RunnerDetect main thread started')
        id_to_load = 1 # first id to load
        last_quarter_filled_queue_tm = time.time()
        while True:
            if self.need_stop_all_processes():
                print('RunnerDetect main thread exited by need_stop_all_processes')
                break

            self.zmq_mon.send({'act': 'running', 'start_string': self.start_string}, msg_type='run')
            if self.imgs_dt_len >= 1:
                last_quarter_filled_queue_tm = time.time()
            if time.time() - last_quarter_filled_queue_tm >= 30:  # All detectors test once in 30 seconds
                self.push_quarter_queue_for_all_detectors_test()

            start_tm = time.time()
            # Load images and detect
            # # load images
            last_svd_req_id = int.from_bytes(self.shm_a.buf[0:8], 'big')

            if self.imgs_dt_len == 0:
                time.sleep(0.05)
                continue

            gogogo = False
            for _ in range(self.DET_PROCS):
                self.last_worked_detector_id += 1
                if self.last_worked_detector_id >= self.DET_PROCS:
                    self.last_worked_detector_id = 0
                shft = 8 * (6 + self.last_worked_detector_id)
                is_detector_busy_flag = int.from_bytes(self.shm_a.buf[shft:shft+8], 'big')
                if is_detector_busy_flag == 0:
                    one = 1
                    self.shm_a.buf[shft:shft+8] = one.to_bytes(8, 'big')
                    gogogo = True
                    break
            if not gogogo:
                time.sleep(0.025)
                continue

            self.imgs_dt_lock.acquire()
            (req_id, client_id, img_id, nparr, status, dict_stat) = self.imgs_dt_lst[0]
            dict_stat['det_load_from_saved_tm'] = time.time()
            dict_stat['saved_queue_len'] = self.imgs_dt_len
            self.imgs_dt_lst = self.imgs_dt_lst[1:]
            self.imgs_dt_len -= 1
            self.imgs_dt_lock.release()

            #print('Runner_V3_Detect  shm_b_empty=', shm_b_empty, '  shm_b_filled=', shm_b_filled, '  self.q.qsize()=', self.q.qsize())
            np_image = nparr #cv2.imdecode(nparr, -1)[..., ::-1]
            precisedetection = False
            dict_stat['qsize'] = 0  # self.q.qsize()
            dict_stat['det_put_to_internal_queue_tm'] = time.time()
            dict_stat['det_get_from_internal_queue_tm'] = time.time()
            #self.q.put((req_id, client_id, img_id, np_image, status, dict_stat), block=False)
            self.zmq_dets[self.last_worked_detector_id].send((req_id, client_id, img_id, np_image, dict_stat, precisedetection), msg_type='run')

        #self.stop_resp_thrd = True
        logger.error('Main thread stopped')
        logger.error('RunnerDetect: main thread stopped')
        # Join resp thread
        #resp_thr.join()

def main():
    if len(sys.argv) != 2:
        print("""USAGE: runner_detect.py cnf_file_name
EXAMPLE: runner_detect.py config_all.txt""")
        exit(1)

    r = RunnerDetect(sys.argv[1])
    r.run()

################################################################################

if __name__ == "__main__":
    main()

################################################################################


from flask import Flask, render_template, request
import cv2
import numpy as np
from tensorflow.keras.models import load_model

app = Flask(__name__)

# 감정 분석을 위한 딥 러닝 모델 로드
emotion_model = load_model('emotion_model.h5')

# 웹페이지 렌더링
@app.route('/')
def index():
    return render_template('index.html')

# 이미지 업로드 및 처리
@app.route('/upload', methods=['POST'])
def upload():
    # 업로드된 이미지를 받아옵니다.
    uploaded_image = request.files['image']

    # OpenCV를 사용하여 이미지를 읽고 얼굴을 인식합니다.
    image = cv2.imdecode(np.fromstring(uploaded_image.read(), np.uint8), cv2.IMREAD_COLOR)

    # 얼굴 인식을 위한 OpenCV 코드 (예를 들어, Haar Cascade Classifier 사용)

    # 이미지를 모델에 맞게 전처리합니다.
    resized_image = cv2.resize(face_image, (48, 48))
    grayscale_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
    image_data = np.array(grayscale_image) / 255.0
    image_data = np.expand_dims(image_data, axis=0)
    image_data = image_data.reshape((1, 48, 48, 1))

    # 감정 분석 모델로 감정을 예측합니다.
    emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
    emotion_prediction = emotion_model.predict(image_data)
    emotion_index = np.argmax(emotion_prediction)
    predicted_emotion = emotion_labels[emotion_index]

    # 결과를 웹페이지로 반환합니다.
    return f"감정: {predicted_emotion}"

if __name__ == '__main__':
    app.run(debug=True)