from unittest import mock

from fastapi.responses import Response
from fastapi.testclient import TestClient

from apps.main import app

client = TestClient(app)

def test_read_main_success():
    response = client.get("/")
    assert response.status_code == 200

def test_read_main_template_not_found():
    with mock.patch('apps.translator.service.get_initial_template', side_effect=FileNotFoundError("Template not Found")):
        response = client.get("/")
        assert response.status_code == 404
        assert response.json() == {'detail': 'Template not found'}

def test_read_main_template_internal_error():
    with mock.patch('apps.translator.service.get_initial_template', side_effect=Exception("Error")):
        response = client.get("/")
        assert response.status_code == 500
        assert response.json() == {
            'detail': 'Internal Erro with Template: Error'}

def test_translate_audio_not_send_file():

    source_language = "en"
    target_language = "es"

    response = client.post(
        f"/translate?source_language={source_language}&target_language={target_language}",
    )
    assert response.status_code == 422
    assert response.text == '{"detail":[{"type":"missing","loc":["body","audio_file"],"msg":"Field required","input":null,"url":"https://errors.pydantic.dev/2.5/v/missing"}]}'

def test_translate_audio_not_allowed():

    response = client.get(
        f"/translate",
    )
    assert response.status_code == 405
    assert response.text == '{"detail":"Method Not Allowed"}'

def test_download_audio_not_allowed():

    response = client.post(f"/download")

    assert response.status_code == 405
    assert response.text == '{"detail":"Method Not Allowed"}'

def test_download_audio_file_not_found():
    with mock.patch('pathlib.Path.exists', return_value=False):
        response = client.get(f"/download")
        assert response.status_code == 500
        assert response.json() == {
            'detail': 'Internal Erro Download audio: File at path audio.mp3 does not exist.'
        }

def test_download_audio_success():

    with mock.patch('apps.translator.service.download_audio') as mock_file_response:
        mock_file_response.return_value = Response(
            content="Mocked File Response", status_code=200)
        response = client.get("/download")
        assert response.status_code == 200
        assert response.content == b"Mocked File Response"

def test_download_audio_raises_exception():

    with mock.patch('pathlib.Path.exists', return_value=True):
        with mock.patch('apps.translator.service.__get_audio_file', side_effect=Exception("File at path audio.mp3 does not exist.")):
            response = client.get("/download")
            assert response.status_code == 500
            assert response.json() == {
                'detail': 'Internal Erro Download audio: Error in download audio: File at path audio.mp3 does not exist.'
            }


import pandas as pd

data = pd.read_csv('C:/Users/Rakesh/Desktop/DataScience/Lohith code/Naive Bayes/spam.csv', encoding='latin-1')
data.head()
# Drop column and name change
data = data.drop(["V3", "Unnamed: 3", "Unnamed: 4"], axis=1)

#data2 = data.drop([3,4],axis=0)
#print(data2)

data = data.rename(columns={"v1": "label", "v2": "text"})
data.tail()
data.label.value_counts()
# convert label to a numerical variable
data['label_num'] = data.label.map({'ham':0, 'spam':1})
data.head()
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(data["text"], data["label"], test_size=0.2, random_state=10)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
from sklearn.feature_extraction.text import CountVectorizer

vect = CountVectorizer()
vect.fit(X_train)
print(vect.get_feature_names()[0:20])
print(vect.get_feature_names()[-20:])
X_train_df = vect.transform(X_train)
X_test_df = vect.transform(X_test)
type(X_test_df)
ham_words = ''
spam_words = ''
spam = data[data.label_num == 1]
ham = data[data.label_num == 0]
import nltk
import matplotlib.pyplot as plt

for val in spam.text:
    text = val.lower()
    tokens = nltk.word_tokenize(text)
    # tokens = [word for word in tokens if word not in stopwords.words('english')]
    for words in tokens:
        spam_words = spam_words + words + ' '

for val in ham.text:
    text = val.lower()
    tokens = nltk.word_tokenize(text)
    for words in tokens:
        ham_words = ham_words + words + ' '
from wordcloud import WordCloud

# Generate a word cloud image
spam_wordcloud = WordCloud(width=600, height=400).generate(spam_words)
ham_wordcloud = WordCloud(width=600, height=400).generate(ham_words)
# Spam Word cloud
plt.figure(figsize=(10, 8), facecolor='k')
plt.imshow(spam_wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
# Ham word cloud
plt.figure(figsize=(10, 8), facecolor='k')
plt.imshow(ham_wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
prediction = dict()
from sklearn.naive_bayes import MultinomialNB

model = MultinomialNB()
model.fit(X_train_df, y_train)
MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)
prediction["Multinomial"] = model.predict(X_test_df)
from sklearn.metrics import accuracy_score

accuracy_score(y_test, prediction["Multinomial"])


from osgeo import ogr
import warnings
from typing import Union
from gdalhelpers.checks import layer_checks, values_checks
from gdalhelpers.classes.DEM import DEM
import losanalyst.functions.los_field_names as field_names
from losanalyst.functions import helpers

def check_los_layer(layer: ogr.Layer) -> None:
    """
    Checks that the `layer` is LoS layer.

    Parameters
    ----------
    layer : ogr.Layer
        Layer to perform the check on.

    Raises
    ------
    ValueError
        If the `layer` is not LoS.
    """
    if not is_los_layer(layer):
        raise ValueError("The provided LoS layer does not provide the necessary attributes. "
                         "It cannot be correctly processed.")

def is_los_layer(layer: ogr.Layer) -> bool:
    """
    Verify, based on presence of specific fields, that the given `layer` contains LoS data.

    Parameters
    ----------
    layer : ogr.Layer
        Layer to perform the check on.

    Returns
    -------
    bool
    """
    result1 = layer_checks.does_field_exist(layer, field_names.observer_id_field_name) and \
              layer_checks.does_field_exist(layer, field_names.target_id_field_name)
    result2 = layer_checks.does_field_exist(layer, field_names.observer_offset_field_name) and \
              layer_checks.does_field_exist(layer, field_names.target_offset_field_name)
    return result1 and result2

def is_global_los_layer(layer: ogr.Layer) -> bool:
    """
    Verify that the given `layer` contain global LoS data.

    Parameters
    ----------
    layer : ogr.Layer
        Layer to perform the check on.

    Returns
    -------
    bool
    """
    is_global = helpers.get_los_type(layer) == "global"

    result = layer_checks.does_field_exist(layer, field_names.tp_x_field_name) and \
             layer_checks.does_field_exist(layer, field_names.tp_y_field_name) and \
             is_global

    return result

def is_los_without_target(layer: ogr.Layer) -> bool:
    """
    Verify that the given `layer` contain LoS without target data.

    Parameters
    ----------
    layer : ogr.Layer
        Layer to perform the check on.

    Returns
    -------
    bool
    """
    if helpers.get_los_type(layer) == "without target":
        return True
    else:
        return False

def check_return_sampling_distance(sampling_distance: Union[int, float], dsm: DEM) -> float:
    """
    Checks that the provided sampling distance value is valid one, if it is `None` then estimates it based on pixel size
    of `dsm`.

    Parameters
    ----------
    sampling_distance : int or float
        Value of LoS sampling distance. Should be positive number.

    dsm : DEM
        If the `sampling_distance` is not provided, it is estimated as pixel size of `dsm`.

    Returns
    -------
    float
        Sampling distance.
    """

    if sampling_distance is not None:
        values_checks.check_value_is_zero_or_positive(sampling_distance, "sampling_distance")
        return float(sampling_distance)
    else:
        sampling_distance = dsm.get_min_pixel_size()
        return sampling_distance

def check_return_id_field(layer: ogr.Layer,
                          layer_name: str,
                          field_name: str) -> str:
    """
    Checks if the provided field (`field_name`) exists in `layer`. If it does not or it is not `Integer` data type
    outputs warning and sets return value as `None`.

    Parameters
    ----------
    layer : ogr.Layer
        Layer to verify.

    layer_name : str
        Layer name for warning messages.

    field_name : str
        Name of the field to look for.

    Returns
    -------
    str
        Either the name of the field or value `None` if does not exists or if it is of incorrect type.

    Warns
    -------
    UserWarning
        If the field does not exist or if it is not Integer type.
    """

    if field_name is not None:
        if not layer_checks.does_field_exist(layer, field_name):
            warnings.warn(
                "The field `{0}` does not exist in `{1}`, the default gdal FID will be used instead."
                    .format(field_name, layer_name),
                UserWarning
            )
            field_name = None
        elif not layer_checks.is_field_of_type(layer, field_name, ogr.OFTInteger):
            warnings.warn(
                "The field `{0}` in observers is not an `Integer` type, the default gdal FID will be used instead."
                    .format(field_name),
                UserWarning
            )
            field_name = None

    return field_name

def check_return_set_offset(offset: Union[str, int, float],
                            offset_name: str,
                            layer: ogr.Layer,
                            layer_name: str,
                            default_offset: float = 1.75):
    """
    Checks that `offset` is usable. The `offset` is either `str`, which specifies field name in `layer` from which the
    value is obtained, or `float` and `int` which are used directly.

    Parameters
    ----------
    offset : str or int or float
        Either numerical value or string specifying field name of `layer`.

    offset_name : str
        Name of the offset for warning messages.

    layer : ogr.Layer
        Layer to work with.

    layer_name : str
        Name of the layer for warning messages.

    default_offset : float, optional
        Offset value to use if the field does not exist. Default value is `1.75`.

    Returns
    -------
    str or float
        Either verified name of the field from `layer` or numerical value.

    Raises
    ------
    ValueError
        If some of the values is not valid.

    Warns
    -------
    UserWarning
        If the field does not exist or if it is of non-numerical type.
    """

    if not isinstance(default_offset, (float, int)):
        raise ValueError("The `default_offset` variable is not either `int` or `float`. It is: `{0}`."
                         .format(type(default_offset).__name__))

    if isinstance(offset, str):
        if not layer_checks.does_field_exist(layer, offset):
            warnings.warn(
                "`{0}` `{1}` does not exist in `{2}` layer, the default value of offset `{3}` will be used."
                    .format(offset_name, offset, layer_name, default_offset),
                UserWarning
            )
            return default_offset

        elif not (layer_checks.is_field_of_type(layer, offset, ogr.OFTInteger) or
                  layer_checks.is_field_of_type(layer, offset, ogr.OFTReal)):
            warnings.warn(
                "`{0}` field `{1}` is not either `Integer` or `Real` in `{2}` layer, "
                "the default value of offset `{3}` will be used."
                    .format(offset_name, offset, layer_name, default_offset),
                UserWarning
            )
            return default_offset
        else:
            return offset

    elif isinstance(offset, (int, float)):
        return offset

    else:
        raise ValueError("The `{0}` is not either string or number.".format(offset_name))


"""
Taken from pyro tutorial
"""

import argparse
from os.path import exists

import numpy as np
import torch
import torch.nn as nn

import pyro
import pyro.distributions as dist
import pyro.poutine as poutine
from pyro.distributions import TransformedDistribution
from pyro.distributions.transforms import affine_autoregressive
from pyro.infer import SVI, Trace_ELBO
from pyro.optim import ClippedAdam

from models.convlstm import ConvLSTM

from tqdm import tqdm

scale_clamper = 1e-4
dependent_event_dim = 3

class Emitter(nn.Module):
    """
    Parameterizes the bernoulli observation likelihood `p(x_t | z_t)`
    """

    def __init__(self, input_channels, z_channels, emission_channels, kernel_size, bottleneck, n_layers):
        super().__init__()
        padding = int((kernel_size - 1) / 2)
        self.deconv_layers = []

        self.lin_z_to_hidden = nn.Conv2d(
            z_channels, emission_channels, kernel_size, 1, padding, bias=True)
        if bottleneck:
            stride = 2
            kernel_size = 4
            padding = int((kernel_size - 1) / 2)
            for layer in range(n_layers - 1):
                next_emission_channels = emission_channels // 2
                self.deconv_layers.append(
                    nn.ConvTranspose2d(
                        emission_channels, next_emission_channels, kernel_size, stride, padding, bias=True).cuda()
                )
                emission_channels = next_emission_channels

            self.lin_hidden_to_input_loc = nn.ConvTranspose2d(
                emission_channels, input_channels, kernel_size, stride, padding, bias=True)
            self.lin_hidden_to_input_scale = nn.ConvTranspose2d(
                emission_channels, input_channels, kernel_size, stride, padding, bias=True)
        else:
            # initialize the three conv transformations used in the neural network
            for layer in range(n_layers - 1):
                self.deconv_layers.append(
                    nn.Conv2d(emission_channels, emission_channels,
                              kernel_size, 1, padding, bias=True).cuda()
                )
            self.lin_hidden_to_input_loc = nn.Conv2d(
                emission_channels, input_channels, kernel_size, 1, padding, bias=True)
            self.lin_hidden_to_input_scale = nn.Conv2d(
                emission_channels, input_channels, kernel_size, 1, padding, bias=True)
        # initialize the two non-linearities used in the neural network
        self.relu = nn.ReLU()
        self.softplus = nn.Softplus()
        self.tanh = nn.Tanh()

    def forward(self, z_t):
        """
        Given the latent z at a particular time step t we return the vector of
        probabilities `ps` that parameterizes the bernoulli distribution `p(x_t|z_t)`
        """
        h1 = self.relu(self.lin_z_to_hidden(z_t))
        for layer in self.deconv_layers:
            h1 = self.relu(layer(h1))
        loc = self.tanh(self.lin_hidden_to_input_loc(h1))
        scale = self.softplus(
            self.lin_hidden_to_input_scale(h1)).clamp(min=scale_clamper)
        return loc, scale

class GatedTransition(nn.Module):
    """
    Parameterizes the gaussian latent transition probability `p(z_t | z_{t-1})`
    See section 5 in the reference for comparison.
    """

    def __init__(self, z_channels, transition_channels, kernel_size, width, height):
        super().__init__()
        padding = int((kernel_size - 1) / 2)

        # initialize the six conv transformations used in the neural network
        self.lin_gate_z_to_hidden = nn.Conv2d(
            z_channels, transition_channels, kernel_size, 1, padding, bias=True)
        self.lin_gate_hidden_to_z = nn.Conv2d(
            transition_channels, z_channels, kernel_size, 1, padding, bias=True)
        self.lin_proposed_mean_z_to_hidden = nn.Conv2d(
            z_channels, transition_channels, kernel_size, 1, padding, bias=True)
        self.lin_proposed_mean_hidden_to_z = nn.Conv2d(
            transition_channels, z_channels, kernel_size, 1, padding, bias=True)
        self.lin_sig = nn.Conv2d(
            z_channels, z_channels, kernel_size, 1, padding, bias=True)
        self.lin_z_to_loc = nn.Conv2d(
            z_channels, z_channels, kernel_size, 1, padding, bias=True)
        # modify the default initialization of lin_z_to_loc
        # # so that it's starts out as the identity function
        # self.lin_z_to_loc.weight.data = torch.ones(z_channels, z_channels, kernel_size, kernel_size)
        self.lin_z_to_loc.bias.data = torch.zeros(z_channels)
        # initialize the three non-linearities used in the neural network
        self.relu = nn.ReLU()
        self.softplus = nn.Softplus()

    def forward(self, z_t_1):
        """
        Given the latent `z_{t-1}` corresponding to the time step t-1
        we return the mean and scale vectors that parameterize the
        (diagonal) gaussian distribution `p(z_t | z_{t-1})`
        """
        # compute the gating function
        _gate = self.relu(self.lin_gate_z_to_hidden(z_t_1))
        gate = torch.sigmoid(self.lin_gate_hidden_to_z(_gate))
        # compute the 'proposed mean'
        _proposed_mean = self.relu(self.lin_proposed_mean_z_to_hidden(z_t_1))
        proposed_mean = self.lin_proposed_mean_hidden_to_z(_proposed_mean)
        # assemble the actual mean used to sample z_t, which mixes a linear transformation
        # of z_{t-1} with the proposed mean modulated by the gating function
        loc = (1 - gate) * self.lin_z_to_loc(z_t_1) + gate * proposed_mean
        # compute the scale used to sample z_t, using the proposed mean from
        # above as input the softplus ensures that scale is positive
        scale = self.softplus(self.lin_sig(
            self.relu(proposed_mean))).clamp(min=scale_clamper)
        # return loc, scale which can be fed into Normal
        return loc, scale

class Combiner(nn.Module):
    """
    Parameterizes `q(z_t | z_{t-1}, x_{t:T})`, which is the basic building block
    of the guide (i.e. the variational distribution). The dependence on `x_{t:T}` is
    through the hidden state of the RNN (see the PyTorch module `rnn` below)
    """

    def __init__(self, z_channels, rnn_channels, kernel_size):
        super().__init__()
        padding = int((kernel_size - 1) / 2)
        stride = 1

        # initialize the three linear transformations used in the neural network
        self.lin_z_to_hidden = nn.Conv2d(
            z_channels, rnn_channels, kernel_size, stride, padding, bias=True)
        self.lin_hidden_to_loc = nn.Conv2d(
            rnn_channels, z_channels, kernel_size, stride, padding, bias=True)
        self.lin_hidden_to_scale = nn.Conv2d(
            rnn_channels, z_channels, kernel_size, stride, padding, bias=True)
        # initialize the two non-linearities used in the neural network
        self.tanh = nn.Tanh()
        self.softplus = nn.Softplus()

    def forward(self, z_t_1, h_rnn):
        """
        Given the latent z at at a particular time step t-1 as well as the hidden
        state of the RNN `h(x_{t:T})` we return the mean and scale vectors that
        parameterize the (diagonal) gaussian distribution `q(z_t | z_{t-1}, x_{t:T})`
        """
        # combine the rnn hidden state with a transformed version of z_t_1
        h_combined = 0.5 * (self.tanh(self.lin_z_to_hidden(z_t_1)) + h_rnn)
        # use the combined hidden state to compute the mean used to sample z_t
        loc = self.lin_hidden_to_loc(h_combined)
        # use the combined hidden state to compute the scale used to sample z_t
        scale = self.softplus(
            self.lin_hidden_to_scale(h_combined)).clamp(min=scale_clamper)
        # return loc, scale which can be fed into Normal
        return loc, scale

class ObservationEncoder(nn.Module):
    '''
    Encoder for encoding 2d data to be feed as RNN's input

    Args:

    '''

    def __init__(self, width, height, input_channels, encoder_channels, kernel_size, n_layers):
        super().__init__()
        # Parameter initialization
        padding = int((kernel_size - 1) / 2)
        stride = 2
        self.conv_layers = []

        # Initialize the three conv transformations used in the neural network
        for layer in range(n_layers):
            if layer == 0:
                temp_encoder_channels = encoder_channels
                encoder_channels = input_channels
                next_encoder_channels = temp_encoder_channels
            else:
                next_encoder_channels = encoder_channels * 2
            self.conv_layers.append(nn.Conv2d(
                encoder_channels, next_encoder_channels, kernel_size, stride, padding, bias=True).cuda())
            encoder_channels = next_encoder_channels

        # Initialize the two non-linearities used in the neural network
        self.relu = nn.ReLU()

    def forward(self, z_t):
        batch_size = z_t.shape[0]
        h1 = z_t
        for layer in self.conv_layers:
            h1 = self.relu(layer(h1))

        return h1

class DMM(nn.Module):
    """
    This PyTorch Module encapsulates the model as well as the
    variational distribution (the guide) for the Deep Markov Model
    """

    def __init__(self, input_channels=1, z_channels=16, emission_channels=32,
                 transition_channels=32, rnn_channels=32, encoder_channels=32, kernel_size=3, height=100, width=100, pred_input_dim=5, encoder_layer=2, num_layers=2, rnn_dropout_rate=0.0,
                 num_iafs=0, iaf_dim=50, use_cuda=False):
        super().__init__()
        self.input_channels = input_channels
        self.bottleneck = True
        self.encoder_channels = encoder_channels

        # instantiate PyTorch modules used in the model and guide below
        self.emitter = Emitter(input_channels, z_channels,
                               emission_channels, kernel_size, self.bottleneck, encoder_layer)
        self.trans = GatedTransition(
            z_channels, transition_channels, kernel_size, width, height)
        self.combiner = Combiner(z_channels, rnn_channels[0], kernel_size)
        if self.bottleneck:
            self.obs_encoder = ObservationEncoder(
                width, height, input_channels, self.encoder_channels, kernel_size, encoder_layer)

        # Instantiate ConvLSTM
        if use_cuda:
            self.device = 'cuda'
        else:
            self.device = 'cpu'

        if self.bottleneck:
            if encoder_layer == 1:
                self.rnn_input_channels = self.encoder_channels
            else:
                self.rnn_input_channels = self.encoder_channels * \
                    (2 ** (encoder_layer - 1))
        self.convlstm = ConvLSTM(
            self.rnn_input_channels, rnn_channels, kernel_size, pred_input_dim, self.device)

        # define a (trainable) parameters z_0 and z_q_0 that help define the probability
        # distributions p(z_1) and q(z_1)
        # (since for t = 1 there are no previous latents to condition on)
        if self.bottleneck:
            height = height // (2 ** encoder_layer)
            width = width // (2 ** encoder_layer)
        self.z_0 = nn.Parameter(torch.zeros(z_channels, height, width))
        self.z_q_0 = nn.Parameter(torch.zeros(z_channels, height, width))
        self.h_0 = nn.Parameter(torch.zeros(rnn_channels[0], height, width))
        self.c_0 = nn.Parameter(torch.zeros(rnn_channels[0], height, width))

        self.use_cuda = use_cuda
        # if on gpu cuda-ize all PyTorch (sub)modules
        if use_cuda:
            self.cuda()

    # the model p(x_{1:T} | z_{1:T}) p(z_{1:T})
    def model(self, mini_batch, mini_batch_reversed, mini_batch_mask, annealing_factor=1.0):

        # this is the number of time steps we need to process in the mini-batch
        T_max = mini_batch.size(1)

        # register all PyTorch (sub)modules with pyro
        # this needs to happen in both the model and guide
        pyro.module("dmm", self)

        # set z_prev = z_0 to setup the recursive conditioning in p(z_t | z_{t-1})
        z_prev = self.z_0.expand(mini_batch.size(0), self.z_0.size(
            0), self.z_0.size(1), self.z_0.size(2))

        # we enclose all the sample statements in the model in a plate.
        # this marks that each datapoint is conditionally independent of the others
        # Dim=-3 counted from the right -> (-3, -2, -1, 0) : (bs, dim, w, h)
        with pyro.plate("z_minibatch", len(mini_batch)):
            # sample the latents z and observed x's one time step at a time
            # we wrap this loop in pyro.markov so that TraceEnum_ELBO can use multiple samples from the guide at each z
            for t in pyro.markov(range(1, T_max + 1)):
                # the next chunk of code samples z_t ~ p(z_t | z_{t-1})
                # note that (both here and elsewhere) we use poutine.scale to take care
                # of KL annealing. we use the mask() method to deal with raggedness
                # in the observed data (i.e. different sequences in the mini-batch
                # have different lengths)

                # first compute the parameters of the diagonal gaussian distribution p(z_t | z_{t-1})
                z_loc, z_scale = self.trans(z_prev)

                # then sample z_t according to dist.Normal(z_loc, z_scale)
                # note that we use the reshape method so that the univariate Normal distribution
                # is treated as a multivariate Normal distribution with a diagonal covariance.
                # TODO: Might give error because the distribution is an image. Same with other distribution based sampling.
                with poutine.scale(scale=annealing_factor):
                    z_t = pyro.sample("z_%d" % t,
                                      dist.Normal(z_loc, z_scale).to_event(dependent_event_dim))

                # compute the probabilities that parameterize the bernoulli likelihood
                emission_loc_t, emission_scale_t = self.emitter(z_t)
                # the next statement instructs pyro to observe x_t according to the
                # bernoulli distribution p(x_t|z_t)
                pyro.sample("obs_x_%d" % t,
                            dist.Normal(emission_loc_t, emission_scale_t)
                            .to_event(dependent_event_dim),
                            obs=mini_batch[:, t - 1, :, :, :])
                # the latent sampled at this time step will be conditioned upon
                # in the next time step so keep track of it
                z_prev = z_t

    # the guide q(z_{1:T} | x_{1:T}) (i.e. the variational distribution)
    def guide(self, mini_batch, mini_batch_reversed, mini_batch_mask, annealing_factor=1.0):

        # this is the number of time steps we need to process in the mini-batch
        T_max = mini_batch.size(1)
        # register all PyTorch (sub)modules with pyro
        pyro.module("dmm", self)

        h_0 = self.h_0.expand(mini_batch.size(0), self.h_0.size(
            0), self.h_0.size(1), self.h_0.size(2))
        c_0 = self.c_0.expand(mini_batch.size(0), self.c_0.size(
            0), self.c_0.size(1), self.c_0.size(2))

        # encode every observed x
        if self.bottleneck:
            batch_size = mini_batch_reversed.shape[0]
            seq_len = mini_batch_reversed.shape[1]
            enc_mini_batch_reversed = torch.zeros(
                batch_size, seq_len, self.rnn_input_channels, self.h_0.size(1), self.h_0.size(2)).to(self.device)
            for t in range(seq_len):
                enc_mini_batch_reversed[:, t, :, :, :] = self.obs_encoder(
                    mini_batch_reversed[:, t, :, :, :])
            mini_batch_reversed = enc_mini_batch_reversed

        # push the observed x's through the rnn;
        # rnn_output contains the hidden state at each time step
        _, rnn_output = self.convlstm(mini_batch_reversed, 0, h_0, c_0)
        # reverse the time-ordering in the hidden state and un-pack it
        rnn_output = reverse_sequences(rnn_output)
        # set z_prev = z_q_0 to setup the recursive conditioning in q(z_t |...)
        z_prev = self.z_q_0.expand(mini_batch.size(0), self.z_q_0.size(
            0), self.z_q_0.size(1), self.z_q_0.size(2))

        # we enclose all the sample statements in the guide in a plate.
        # this marks that each datapoint is conditionally independent of the others.
        with pyro.plate("z_minibatch", len(mini_batch)):
            # sample the latents z one time step at a time
            # we wrap this loop in pyro.markov so that TraceEnum_ELBO can use multiple samples from the guide at each z
            for t in pyro.markov(range(1, T_max + 1)):
                # the next two lines assemble the distribution q(z_t | z_{t-1}, x_{t:T})
                z_loc, z_scale = self.combiner(
                    z_prev, rnn_output[:, t - 1, :, :, :])

                # if we are using normalizing flows, we apply the sequence of transformations
                # parameterized by self.iafs to the base distribution defined in the previous line
                # to yield a transformed distribution that we use for q(z_t|...)
                z_dist = dist.Normal(z_loc, z_scale)

                assert z_dist.event_shape == ()
                assert z_dist.batch_shape == (
                    len(mini_batch), self.z_q_0.size(0), self.z_q_0.size(1), self.z_q_0.size(2))

                # sample z_t from the distribution z_dist
                with pyro.poutine.scale(scale=annealing_factor):
                    # when no normalizing flow used, ".to_event(1)" indicates latent dimensions are independent
                    z_t = pyro.sample("z_%d" % t,
                                      z_dist.to_event(dependent_event_dim))

                    assert z_t.shape == (len(mini_batch), self.z_q_0.size(
                        0), self.z_q_0.size(1), self.z_q_0.size(2))
                # the latent sampled at this time step will be conditioned upon in the next time step
                # so keep track of it
                z_prev = z_t

        return z_t

def reverse_sequences(rnn_output):
    T = rnn_output.size(1)
    time_slices = torch.arange(T - 1, -1, -1, device=rnn_output.device)

    return rnn_output.index_select(1, time_slices)

def do_prediction(dmm, pred_batch, pred_batch_reversed, pred_batch_mask, pred_length, ground_truth):
    # Do prediction from previous observations
    with torch.no_grad():
        # Initialization
        bsize, input_seq_len, input_channels, width, height = pred_batch.shape
        # pred_latents_loc = torch.Tensor(bsize, pred_length, input_channels, width, height)
        # pred_latents_scale = torch.Tensor(bsize, pred_length, input_channels, width, height)
        pred_observations_loc = torch.Tensor(
            bsize, pred_length, input_channels, width, height).to(pred_batch.device)
        pred_observations_scale = torch.Tensor(
            bsize, pred_length, input_channels, width, height).to(pred_batch.device)

        # Use guide to calculate latents from observations
        z_prev = dmm.guide(pred_batch, pred_batch_reversed, pred_batch_mask)

        # Use model to predict next latents and generate observation from them
        for i in range(0, pred_length):
            z_pred_loc, z_pred_scale = dmm.trans(z_prev)
            z_t = pyro.sample("z_pred_%d" % i, dist.Normal(
                z_pred_loc, z_pred_scale).to_event(dependent_event_dim))

            emission_loc_t, emission_scale_t = dmm.emitter(z_t)
            x_t = pyro.sample("x_pred_%d" % i,
                              dist.Normal(emission_loc_t, emission_scale_t)
                              .to_event(dependent_event_dim))

            # Insert into tensors
            # pred_latents_loc[:, i, :, :, :] = z_pred_loc
            # pred_latents_scale[:, i, :, :, :] = z_pred_scale
            pred_observations_loc[:, i, :, :, :] = emission_loc_t
            pred_observations_scale[:, i, :, :, :] = emission_scale_t

            z_prev = z_t

        observations_loss_loc = torch.sum(
            (pred_observations_loc - ground_truth) ** 2).detach().cpu().numpy()
        observations_loss_scale = torch.sum((pred_observations_scale - torch.ones(
            ground_truth.shape).to(pred_batch.device)) ** 2).detach().cpu().numpy()

    return pred_observations_loc, pred_observations_scale, observations_loss_loc, observations_loss_scale

def do_prediction_rep_inference(dmm, pred_batch_mask, pred_length, input_pred_length, ground_truth, summed=True):
    # Do prediction from previous observations
    with torch.no_grad():
        # Initialization
        bsize, input_seq_len, input_channels, width, height = ground_truth.shape
        # pred_latents_loc = torch.Tensor(bsize, pred_length, input_channels, width, height)
        # pred_latents_scale = torch.Tensor(bsize, pred_length, input_channels, width, height)
        pred_observations_loc = torch.Tensor(
            bsize, pred_length, input_channels, width, height).to(ground_truth.device)
        pred_observations_scale = torch.Tensor(
            bsize, pred_length, input_channels, width, height).to(ground_truth.device)

        for i in range(pred_length):
            input_pred_start = input_seq_len - input_pred_length - pred_length + i
            if input_pred_start < 0:
                input_pred_start = 0
            input_pred_end = input_pred_start + input_pred_length
            if input_pred_end > input_seq_len:
                input_pred_end = input_seq_len
            pred_batch = ground_truth[:,
                                      input_pred_start:input_pred_end, :, :, :]
            pred_batch_reversed = reverse_sequences(pred_batch)
            assert pred_batch.shape[1] == input_pred_length

            # Use guide to calculate latents from observations
            z_prev = dmm.guide(
                pred_batch, pred_batch_reversed, pred_batch_mask)

            # Use model to predict next latents and generate observation from them
            z_pred_loc, z_pred_scale = dmm.trans(z_prev)
            z_t = pyro.sample("z_pred_%d" % i, dist.Normal(
                z_pred_loc, z_pred_scale).to_event(dependent_event_dim))

            emission_loc_t, emission_scale_t = dmm.emitter(z_t)
            x_t = pyro.sample("x_pred_%d" % i,
                              dist.Normal(emission_loc_t, emission_scale_t)
                              .to_event(dependent_event_dim))

            # Insert into tensors
            # pred_latents_loc[:, i, :, :, :] = z_pred_loc
            # pred_latents_scale[:, i, :, :, :] = z_pred_scale
            pred_observations_loc[:, i, :, :, :] = emission_loc_t
            pred_observations_scale[:, i, :, :, :] = emission_scale_t

            z_prev = z_t

        if summed:
            observations_loss_loc = torch.sum(
                (pred_observations_loc - ground_truth[:, input_seq_len - pred_length:, :, :, :]) ** 2).detach().cpu().numpy()
            observations_loss_scale = torch.sum((pred_observations_scale - torch.ones(
                ground_truth[:, input_seq_len - pred_length:, :, :, :].shape).to(pred_batch.device)) ** 2).detach().cpu().numpy()
        else:
            observations_loss_loc = (
                (pred_observations_loc - ground_truth[:, input_seq_len - pred_length:, :, :, :]) ** 2).detach().cpu().numpy()
            observations_loss_scale = ((pred_observations_scale - torch.ones(
                ground_truth[:, input_seq_len - pred_length:, :, :, :].shape).to(pred_batch.device)) ** 2).detach().cpu().numpy()

    return pred_observations_loc, pred_observations_scale, observations_loss_loc, observations_loss_scale

if __name__ == '__main__':
    # Test
    input_channels = 1
    z_channels = 1
    emission_channels = 16
    transition_channels = 64
    rnn_channels = [64, 64]
    kernel_size = 3
    pred_length = 0
    input_length = 30
    width = 100
    height = 100

    input_tensor = torch.zeros(
        16, input_length, input_channels, width, height).cuda()
    input_tensor_mask = torch.ones(
        16, input_length, input_channels, width, height).cuda()
    input_tensor_reversed = reverse_sequences(input_tensor).cuda()

    pred_tensor = input_tensor[:, :25, :, :, :]
    pred_tensor_mask = input_tensor_mask[:, :25, :, :, :]
    pred_tensor_reversed = reverse_sequences(pred_tensor).cuda()

    ground_truth = input_tensor[:, 25:, :, :, :]

    pred_input_dim = 5

    dmm = DMM(input_channels=input_channels, z_channels=z_channels, emission_channels=emission_channels,
              transition_channels=transition_channels, rnn_channels=rnn_channels, kernel_size=kernel_size, height=height, width=width, pred_input_dim=5, num_layers=2, rnn_dropout_rate=0.0,
              num_iafs=0, iaf_dim=50, use_cuda=True)

    learning_rate = 0.01
    beta1 = 0.9
    beta2 = 0.999
    clip_norm = 10.0
    lr_decay = 1.0
    weight_decay = 0
    adam_params = {"lr": learning_rate, "betas": (beta1, beta2),
                   "clip_norm": clip_norm, "lrd": lr_decay,
                   "weight_decay": weight_decay}
    adam = ClippedAdam(adam_params)

    elbo = Trace_ELBO()
    svi = SVI(dmm.model, dmm.guide, adam, loss=elbo)
    for i in range(100):
        loss = svi.step(input_tensor, input_tensor_reversed, input_tensor_mask)
        val_nll = svi.evaluate_loss(
            input_tensor, input_tensor_reversed, input_tensor_mask)
        print(val_nll)
        _, _, loss_loc, loss_scale = do_prediction(
            dmm, pred_tensor, pred_tensor_reversed, pred_tensor_mask, 5, ground_truth)
        print(loss_loc, loss_scale)


import pyautogui, time

amount = input("How many comments to do you want to make?")
amount = int(amount)
comment = input("What do you want to comment?")

print("The program is starting in 5 seconds.")
time.sleep(5)

pyautogui.scroll(100)
pyautogui.scroll(-7)
time.sleep(2)

for i in range(amount):
    pyautogui.moveTo(200, 490)
    pyautogui.click()
    pyautogui.write(comment)
    pyautogui.moveTo(1300, 530)
    pyautogui.click()
    time.sleep(3)


import colour
from IPython.display import Video
from datetime import datetime

class ishow_config():
    def __init__(self,
                 # file_writer_config
                 write_to_movie=True,
                 break_into_partial_movies=False,
                 save_last_frame=False,
                 save_pngs=False,
                 png_mode="RGB",
                 movie_file_extension='.mp4',
                 mirror_module_path=False,
                 output_directory=".",
                 file_name="",
                 open_file_upon_completion=False,
                 show_file_location_upon_completion=False,
                 quiet=True,
                 ##
                 write_all=False,
                 start_at_animation_number=None,
                 preview=False,
                 end_at_animation_number=None,
                 leave_progress_bars=False,
                 # class camera_config:,
                 pixel_width=1920,
                 pixel_height=1080,
                 frame_rate=30,
                 background_color="#333333",
                 # quality="default_quality",
                 # class window_config:
                 size=(960, 540),
                 skip_animations=False
                 ):
        self.default = {
            "file_writer_config": {
                "write_to_movie": write_to_movie,
                "break_into_partial_movies": break_into_partial_movies,
                "save_last_frame": save_last_frame,
                "save_pngs": save_pngs,
                "png_mode": png_mode,
                "movie_file_extension": movie_file_extension,
                "mirror_module_path": mirror_module_path,
                "output_directory": output_directory,
                "file_name": file_name,
                "open_file_upon_completion": open_file_upon_completion,
                "show_file_location_upon_completion": show_file_location_upon_completion,
                "quiet": quiet
            },
            "write_all": write_all,
            "start_at_animation_number": start_at_animation_number,
            "preview": preview,
            "end_at_animation_number": end_at_animation_number,
            "leave_progress_bars": leave_progress_bars,
            "camera_config": {
                "pixel_width": pixel_width,
                "pixel_height": pixel_height,
                "frame_rate": frame_rate,
                "background_color": colour.Color(background_color),
            },
            "window_config": {
                "size": size,
                "skip_animations": skip_animations
            },
        }

    @property
    def low(self):
        self.default["camera_config"]["pixel_width"] = 854
        self.default["camera_config"]["pixel_height"] = 480
        self.default["camera_config"]["frame_rate"] = 15
        return self.default

    @property
    def medium(self):
        self.default["camera_config"]["pixel_width"] = 1280
        self.default["camera_config"]["pixel_height"] = 720
        self.default["camera_config"]["frame_rate"] = 30
        return self.default

    @property
    def high(self):
        self.default["camera_config"]["pixel_width"] = 1920
        self.default["camera_config"]["pixel_height"] = 1080
        self.default["camera_config"]["frame_rate"] = 30
        return self.default

    @property
    def ultra_high(self):
        self.default["camera_config"]["pixel_width"] = 3840
        self.default["camera_config"]["pixel_height"] = 2160
        self.default["camera_config"]["frame_rate"] = 60
        return self.default

def ishow(scene, filename=None, config=None):
    if filename == None:
        fname = scene.__name__ + datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
    else:
        fname = filename
    if config == None:
        cfg = ishow_config().medium
    else:
        cfg = config
    cfg["file_writer_config"]["file_name"] = fname
    scene(**cfg).run()
    return Video("videos/" + fname + ".mp4", html_attributes="width=\"100%\" controls autoplay")

if __name__ == "__main__":
    from manimlib import *
    from imanim import ishow
    class SquaremaToCircle(Scene):
        def construct(self):
            circle = Circle()
            square = Square()
            square.flip(RIGHT)
            square.rotate(-3 * TAU / 8)
            circle.set_fill(PINK, opacity=0.5)

            self.play(ShowCreation(square))
            self.play(Transform(square, circle))
            self.play(FadeOut(square))

    ishow(SquaremaToCircle)


import cv2
import requests
import sys
import os
from PIL import Image
import json
import numpy as np
from . import error_score
from . import face_pp

URL = 'https://api-us.faceplusplus.com/facepp/v1/face/thousandlandmark'
API_KEY = ['-D9lXEJg0Z0MMuSHKtmKxetMLqYkZp2c', 'eORdjUCXMFgW-RahCeWAzZM-huNDakIi', '5AkMaXLVTFXwCCJNEwpCOttuHeAa-wf3', 'hyQuigXn3o3g4A73OI3inYxOqnm_G6W_', '_M-34D9kPT66uMXCjTOXetY1A_7MbnXV']
API_SECRET = ['2At_EM1UjwJHSzE_j325L2KxbXjDrefE', 'KpWC-gpiLp1WuMwfm-LGN1UtvnsUYa1M', 'QEvQvEeoiRXkknw01pXtF0Cd1D9ZfjxK', '6jxwxwTdC-wvDbKIaiCH_dXWk2GqGqPD', 'XzgKDBV5MEmp_CkerpPshpXZIDU1xMeW']
api_idx = 0

def get_facepp_api_res(filepath):
    global api_idx
    img_cv = cv2.imread(filepath)
    binary_cv = cv2.imencode('.PNG', img_cv)[1].tobytes()
    nofAPI = len(API_KEY)
    parameters = dict(api_key=API_KEY[api_idx], api_secret=API_SECRET[api_idx], return_landmark='all')
    api_idx = (api_idx + 1) % nofAPI
    response = requests.post(URL, data=parameters, files={'image_file': binary_cv})

    try:
        res = response.json()
    except:
        tmp = dict()
        tmp["errors"] = "facepp error : face not detected"
        res = tmp
    with open('/work/urs-server/myapp/log/facepp_result.json', 'w', encoding='utf-8') as make_file:
        json.dump(res, make_file, indent="\t")

    ####test####
    # srcname = filepath.split('/')[-1]
    # ref_rectangle = dict(width = 131, top = 93, height = 131, left = 62)
    # src_rectangle = res['face']['face_rectangle']
    # dx_scale = ref_rectangle['width'] / src_rectangle['width']
    # dy_scale = ref_rectangle['height'] / src_rectangle['height']
    # img = cv2.imread(filepath)
    # scaleimg = cv2.resize(img, None,  None, dx_scale, dy_scale, cv2.INTER_CUBIC)
    # dx = ref_rectangle['left'] - src_rectangle['left']
    # dy = ref_rectangle['top'] - src_rectangle['top']
    # mtrx = np.float32([[1, 0, dx],
    #                 [0, 1, dy]])
    # rows,cols = 256, 256
    # gan_src = cv2.warpAffine(scaleimg, mtrx, (cols+dx, rows+dy), None, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT, (255,255,255) )
    # crop_src = gan_src[0:256, 0:256]
    # pil_image=Image.fromarray(crop_src)
    # pil_image.save('/work/urs-server/media/media/gan_src_image/' + srcname)
   # cv2.write('/work/urs-server/media/media/gan_src_img/' + srcname, gan_src)

    ############
    return res

def get_shape_possibilities(h_w_ratio, lw_ratio, chin_ratio, forehead_ratio):
    error_scores = error_score.get_error_score(h_w_ratio, lw_ratio, chin_ratio, forehead_ratio)
    error_score_sum = sum(error_scores.values())

    possibilities = {}
    for k, v in error_scores.items():
        p = 1.0 - (v / error_score_sum)
        possibilities[k] = p

    return possibilities

def get_shape(face_json, face_shape_table):
    fd = face_pp.get_face_data(face_json)

    # calculate possibilities
    sp = get_shape_possibilities(h_w_ratio=fd["h_w_ratio"],
                                 lw_ratio=fd["lw_ratio"],
                                 chin_ratio=fd['chin_ratio'],
                                 forehead_ratio=fd['forehead_ratio'])
    print(f'possibilities : {sp}')
    return get_face_shape_detail(face_json, sp, face_shape_table)

def get_face_shape_detail(face_json, sp, face_shape_table):
    rev_dict = dict()
    rev_dict[str(sp['oblong'])] = 'oblong'
    rev_dict[str(sp['oval'])] = 'oval'
    rev_dict[str(sp['round'])] = 'round'
    rev_dict[str(sp['square'])] = 'square'
    faceshape = rev_dict[str(max(sp['oblong'], sp['oval'], sp['round'], sp['square']))]

    # with open('/work/urs-server/data/face_shape.json', 'r') as f:
    #     face_shape_table = json.load(f)
    face_golden_ratio = 1.3
    tmp_faceshape = faceshape
    islong = False
    isshort = False
    diff_garosero = 1.15
    face_info_tmp = dict()
    face_top = (face_json['face']['landmark']['face']['face_hairline_72']["y"]+face_json['face']['landmark']['face']['face_hairline_74']["y"])/2
    face_jaw =  (face_json['face']['landmark']['face']['face_contour_left_0']["y"]+face_json['face']['landmark']['face']['face_contour_right_0']["y"])/2
    face_height = face_jaw-face_top
    face_width = 0
    for i in range(0, 64):
        tmp = face_json['face']['landmark']['face']["face_contour_right_" + str(i)]["x"] - face_json['face']['landmark']['face']["face_contour_left_" + str(i)]["x"]
        if face_width < tmp:
            face_width = tmp
    face_user_ratio = face_height/face_width
    face_info_tmp['width_height_ratio']=face_user_ratio
    if face_user_ratio >  face_golden_ratio * diff_garosero :
        islong = True
    elif face_user_ratio * diff_garosero < face_golden_ratio :
        isshort = True
    if isshort and tmp_faceshape != "oblog":
        tmp_faceshape = "short_" + tmp_faceshape
    elif islong and tmp_faceshape == "square":
        tmp_faceshape = "square_oblog"
    face_info_tmp['face_shape'] = tmp_faceshape
    face_info_tmp['face_shape_detail'] = face_shape_table[tmp_faceshape]
    print(tmp_faceshape)
    return face_info_tmp

def get_face_info(face_json, face_ratio_table, face_shape_table):
    face_info_tmp = dict()
    face_info_tmp.update(get_face_landmark(face_json))
    face_info_tmp.update(get_face_ratio(face_json, face_ratio_table))
    face_info_tmp.update(get_shape(face_json, face_shape_table))
    return face_info_tmp
def get_face_landmark(face_json):
    face_info_tmp = dict()
    face_landmark=dict()
    face_landmark['top']={'x': (face_json['face']['landmark']['face']['face_hairline_72']["x"]+face_json['face']['landmark']['face']['face_hairline_74']["x"])/2, 'y': (face_json['face']['landmark']['face']['face_hairline_72']["y"]+face_json['face']['landmark']['face']['face_hairline_74']["y"])/2}
    face_landmark['upper_mid']={'x': (face_json['face']['landmark']['left_eyebrow']['left_eyebrow_31']["x"]+face_json['face']['landmark']['right_eyebrow']['right_eyebrow_31']["x"])/2, 'y': (face_json['face']['landmark']['left_eyebrow']['left_eyebrow_31']["y"]+face_json['face']['landmark']['right_eyebrow']['right_eyebrow_31']["y"])/2}
    face_landmark['lower_mid']={'x':(face_json['face']['landmark']['nose']['nose_left_47']["x"]+face_json['face']['landmark']['nose']['nose_right_47']["x"])/2, "y": (face_json['face']['landmark']['nose']['nose_left_47']["y"]+face_json['face']['landmark']['nose']['nose_right_47']["y"])/2}
    face_landmark['bottom']={"x":(face_json['face']['landmark']['face']['face_contour_left_0']["x"]+face_json['face']['landmark']['face']['face_contour_right_0']["x"])/2, "y": (face_json['face']['landmark']['face']['face_contour_left_0']["y"]+face_json['face']['landmark']['face']['face_contour_right_0']["y"])/2}
    face_info_tmp['landmark']=face_landmark
    return face_info_tmp
def get_face_ratio(face_json, face_ratio_table):
    # with open('/work/urs-server/data/face_ratio.json', 'r') as f:
    #     face_ratio_table = json.load(f)
    face_info_tmp = dict()
    face_top = (face_json['face']['landmark']['face']['face_hairline_72']["y"]+face_json['face']['landmark']['face']['face_hairline_74']["y"])/2
    face_eyebrow = (face_json['face']['landmark']['left_eyebrow']['left_eyebrow_31']["y"]+face_json['face']['landmark']['right_eyebrow']['right_eyebrow_31']["y"])/2
    face_noseend = (face_json['face']['landmark']['nose']['nose_left_47']["y"]+face_json['face']['landmark']['nose']['nose_right_47']["y"])/2
    face_jaw =  (face_json['face']['landmark']['face']['face_contour_left_0']["y"]+face_json['face']['landmark']['face']['face_contour_right_0']["y"])/2
    sanganbu = face_eyebrow-face_top
    junganbu = face_noseend-face_eyebrow
    haanbu = face_jaw-face_noseend

    face_ratio=dict()
    face_ratio['top']=round(sanganbu/(sanganbu+junganbu+haanbu)*100)
    face_ratio['mid']=round(junganbu/(sanganbu+junganbu+haanbu)*100)
    face_ratio['bottom']=round(haanbu/(sanganbu+junganbu+haanbu)*100)

    face_info_tmp['ratio']=face_ratio

    face_ratio_type = "tmp"
    diff_sangjungha = 1.05
    print(face_jaw-face_top)
    print(face_json['face']['face_rectangle']['height'])
    print(face_json['face']['face_rectangle']['width'])
    print(face_json['face']['landmark']['face']['face_hairline_0']["x"] - face_json['face']['landmark']['face']['face_hairline_144']["x"])
    if sanganbu > junganbu * diff_sangjungha and sanganbu > haanbu * diff_sangjungha :
        face_ratio_type = "long_sanganbu"
    elif junganbu > sanganbu * diff_sangjungha and junganbu > haanbu * diff_sangjungha :
        face_ratio_type = "long_junganbu"
    elif haanbu > sanganbu * diff_sangjungha and haanbu > junganbu * diff_sangjungha :
        face_ratio_type = "long_haanbu"
    elif sanganbu * diff_sangjungha < junganbu and sanganbu * diff_sangjungha < haanbu :
        face_ratio_type = "short_sanganbu"
    elif junganbu * diff_sangjungha < sanganbu and junganbu * diff_sangjungha < haanbu :
        face_ratio_type = "short_junganbu"
    elif haanbu * diff_sangjungha < junganbu and haanbu * diff_sangjungha < sanganbu :
        face_ratio_type = "short_haanbu"
    else :
        face_ratio_type = "golden_face"
    face_info_tmp['ratio_detail'] = face_ratio_table[str(face_ratio_type)]
    return face_info_tmp


from django.urls import path
from django.contrib.auth import views as auth_views
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    # path('login/', auth_views.LoginView.as_view(template_name='pybo/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('login/', views.login_user, name='login'),
    path('signup/', views.signup, name='signup'),
    path('mypill/', views.mypill, name='mypill'),
    path('friend/', views.friend, name='friend'),
    path('searchFriend/', views.search, name='searchFriend'),
    path('addpill/', views.addpill, name='addpill'),
    path('addpillList/', views.addpillList, name='addpillList'),
    path('friend/<str:username>/', views.friendpill, name='friendpill')
]


from flask import Flask, request
from flask.templating import render_template
from model import summariser

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/results', methods=["POST"])
def summary():
    if request.method == "POST":
        data = request.form.to_dict()
        text = data['review_text']
        finalResult = summariser(text)
        #finalResult = finalResult.append()
    return render_template("results.html", summary = finalResult)
if __name__ == "__main__":
    app.run(debug=True, port=8000)


from django.utils.translation import gettext_lazy

try:
    from pretix.base.plugins import PluginConfig
except ImportError:
    raise RuntimeError("Please use pretix 2.7 or above to run this plugin!")

__version__ = "1.0.1"

class PluginApp(PluginConfig):
    name = "pretix_batch_emailer"
    verbose_name = "Batch Emailer"

    class PretixPluginMeta:
        name = gettext_lazy("Batch Emailer")
        author = "Lukas Bockstaller"
        description = gettext_lazy(
            "Send a Batch Email to all currently displayed orders. "
        )
        visible = True
        version = __version__
        category = "FEATURE"
        compatibility = "pretix>=3.0"

    def ready(self):
        from . import signals  # NOQA

default_app_config = "pretix_batch_emailer.PluginApp"


import os
import warnings
from setuptools import setup, find_packages

def read(fname):
    with open(os.path.join(os.path.dirname(__file__), fname), "rb") as f:
        reqs = f.read().decode("utf-8")
    return reqs

from pkg_resources import require, DistributionNotFound, parse_version

def check_provided(distribution, min_version, max_version=None, optional=False):
    # taken from https://github.com/BD2KGenomics/toil-scripts/blob/master/setup.py
    min_version = parse_version(min_version)
    if isinstance(min_version, tuple):
        raise RuntimeError("Setuptools version 8.0 or newer required. Update by running "
                           "'pip install setuptools --upgrade'")
    if max_version is not None:
        max_version = parse_version(max_version)

    messages = []

    toil_missing = 'Cannot find a valid installation of Toil.'
    dist_missing = 'Cannot find an installed copy of the %s distribution, typically provided by Toil.' % distribution
    version_too_low = 'The installed copy of %s is out of date.' % distribution
    version_too_high = 'The installed copy of %s is too new.' % distribution
    required_version = 'Setup requires version %s or higher' % min_version
    required_version += '.' if max_version is None else ', up to but not including %s.' % max_version
    install_toil = 'Installing Toil should fix this problem.'
    upgrade_toil = 'Upgrading Toil should fix this problem.'
    reinstall_dist = 'Uninstalling %s and reinstalling Toil should fix this problem.' % distribution
    reinstall_toil = 'Uninstalling Toil and reinstalling it should fix this problem.'
    footer = ("Setup doesn't install Toil automatically to give you a chance to choose any of the optional extras "
              "that Toil provides. More on installing Toil at http://toil.readthedocs.io/en/latest/installation.html.")
    try:
        # This check will fail if the distribution or any of its dependencies are missing.
        installed_version = parse_version(require(distribution)[0].version)
    except DistributionNotFound:
        installed_version = None
        if not optional:
            messages.extend([toil_missing if distribution == 'toil' else dist_missing, install_toil])
    else:
        if installed_version < min_version:
            messages.extend([version_too_low, required_version,
                             upgrade_toil if distribution == 'toil' else reinstall_dist])
        elif max_version is not None and max_version < installed_version:
            messages.extend([version_too_high, required_version,
                             reinstall_toil if distribution == 'toil' else reinstall_dist])
    if messages:
        messages.append(footer)
        raise RuntimeError(' '.join(messages))
    else:
        return str(installed_version)

try:
    toil_version = check_provided('toil', min_version='3.7.0a1.dev392', max_version='3.7.0a1.dev392')
except RuntimeError as e:
    warnings.warn(f"Toil is not installed, you will not ne able to create datasets with AtomicToil. \n\n {e}")

packages = [
    "Prop3D",
    "Prop3D.pdb_tools",
    "Prop3D.common",
    "Prop3D.custom_featurizers",
    "Prop3D.generate_data",
    "Prop3D.parsers",
    "Prop3D.parsers.CNS-Templates",
    "Prop3D.parsers.superpose",
    "Prop3D.util",
    "Prop3D.visualize",
    "Prop3D.ml",
    "Prop3D.ml.datasets",
    "Prop3D.ml.examples"
]

setup(
    name = "Prop3D",
    version = "0.0.1",
    author = "Eli Draizen",
    author_email = "edraizen@gmail.com",
    packages=packages,
    long_description=read('README.md'),
    install_requires=read("requirements.txt").splitlines(),
    extras_require={"s3": ["boto3", "awscli"]},
    include_package_data=True,
    zip_safe=False
)


import cv2
import numpy as np
import matplotlib.pyplot as plt

def conv2d(ori,kernal):
    dimension = ori.shape
    # ori = np.pad(ori,1,'constant',constant_values = 0)
    result = np.zeros((dimension[0],dimension[1]))
    for i in range(0,dimension[0]-2):
        for j in range(0,dimension[1]-2):
            ori_1 = ori[0+i:3+i,0+j:3+j]
            ori_2 = ori_1*kernal
            result[i,j] = np.abs(ori_2.sum())
    return result

def p1():
    img = cv2.imread("Q3_Image\Chihiro.jpg")
    # filter
    sigma = 1
    x,y = np.mgrid[-1:2, -1:2]
    gaussian_kernal = np.exp(-(x**2 + y**2))/sigma**2
    gaussian_kernal = gaussian_kernal / gaussian_kernal.sum()
    # tranfer to gray scale
    gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    # convolution
    gaussian = conv2d(gray,gaussian_kernal)
    # transform to uint8
    gray = np.asarray(gray,dtype=np.uint8)
    gaussian = np.asarray(gaussian,dtype=np.uint8)

    # plot figure
    cv2.imwrite("Gaussian.png",gaussian)
    cv2.imshow("gray",gray)
    cv2.imshow("gaussian",gaussian)

def p2():
    img = cv2.imread("Gaussian.png")
    # img = np.asarray(img, dtype=np.int64)
    # filter
    sobelX_filter = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
    # tranfer to gray scale
    gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    # convolution
    sobelX = conv2d(gray,sobelX_filter)
    sobelX = np.asarray(sobelX,dtype = np.uint8)
    # plot figure
    cv2.imshow("SobelX",sobelX)

def p3():
    img = cv2.imread("Gaussian.png")
    # filter
    sobelY_filter = np.array([[1,2,1],[0,0,0],[-1,-2,-1]])
    # tranfer to gray scale
    gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    # convolution
    sobelY = conv2d(gray,sobelY_filter)
    sobelY = np.asarray(sobelY,dtype=np.uint8)
    # plot figure
    cv2.imshow("SobelY",sobelY)

def p4():
    img = cv2.imread("Gaussian.png")
    # filter
    sobelX_filter = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
    sobelY_filter = np.array([[1,2,1],[0,0,0],[-1,-2,-1]])
    # tranfer to gray scale
    gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
    # convolution
    sobelX = conv2d(gray,sobelX_filter)
    sobelY = conv2d(gray,sobelY_filter)
    magnitude = np.asarray(np.sqrt(sobelX**2 + sobelY**2),np.uint8)
    # plot figure
    cv2.imshow("magnitude",magnitude)


'''
A simple python calculation question generator
usg: python calculator.py -d "+,-" -c 3 -l 200 -t 30
usg: python calculator.py --op_type="+,-" --op_count=3 --limit=20 --total=30
'''
import sys, getopt, random, json

def auto_cal_generator(limit=100, op_count=1, op_type=["+"], total=100):
    res = {}
    if limit>999 or op_count>9 or total>99:
        res["status"] = "failed"
        res["msg"] = "exceed max input limit"
        return json.dumps(res)

    questions = []
    l = len(op_type)-1
    for j in range(0, total):
        up = limit
        question = ""
        for i in range(0, op_count+1):
            num = 0
            if i == 0:
                num = random.randint(1,max(1,min(limit,up)))
                question = "%s%d" % (question, num)
                up -= num
                continue
            op = "+"
            if limit - up > 0:
                op_i = random.randint(0,l)
                op = op_type[op_i]
            question = "%s%s" % (question, op)
            if op =="+":
                num = random.randint(1,max(1,min(limit,up)))
                up -= num
            elif op == "-":
                num = random.randint(1,max(limit-up, 1))
                up += num
            else:
                res["status"] = "failed"
                res["msg"] = "operator error: %s" % op
                return json.dumps(res)
            question = "%s%d" % (question, num)

        questions.append("%d: %s=" % (j+1, question))

    res["msg"] = "Here are today's %d works, good luck!" % total
    res["questions"] = questions
    res["status"] = "success"
    return json.dumps(res)


from __future__ import absolute_import, division, unicode_literals

import logging
import uuid

from flask_restful.reqparse import RequestParser

from sqlalchemy.orm import subqueryload_all
from changes.utils.diff_parser import DiffParser
from werkzeug.datastructures import FileStorage
from changes.api.base import APIView, error
from changes.api.build_index import (
    create_build, get_build_plans, identify_revision, MissingRevision
)
from changes.api.validators.author import AuthorValidator
from changes.config import db
from changes.db.utils import try_create
from changes.models import (
    ItemOption, Patch, PhabricatorDiff, Project, ProjectOption, ProjectOptionsHelper, ProjectStatus,
    Repository, RepositoryStatus, Source
)
from changes.utils.whitelist import in_project_files_whitelist

def get_repository_by_callsign(callsign):
    # It's possible to have multiple repositories with the same callsign due
    # to us not enforcing a unique constraint (via options). Given that it is
    # complex and shouldn't actually happen we make an assumption that there's
    # only a single repo
    item_id_list = db.session.query(ItemOption.item_id).filter(
        ItemOption.name == 'phabricator.callsign',
        ItemOption.value == callsign,
    )
    repo_list = list(Repository.query.filter(
        Repository.id.in_(item_id_list),
        Repository.status == RepositoryStatus.active,
    ))
    if len(repo_list) > 1:
        logging.warning('Multiple repositories found matching phabricator.callsign=%s', callsign)
    elif not repo_list:
        return None  # Match behavior of project and repository parameters
    return repo_list[0]

class PhabricatorNotifyDiffAPIView(APIView):
    parser = RequestParser()
    parser.add_argument('sha', type=str, required=True)
    parser.add_argument('author', type=AuthorValidator(), required=True)
    parser.add_argument('label', type=unicode, required=True)
    parser.add_argument('message', type=unicode, required=True)
    parser.add_argument('patch', type=FileStorage, dest='patch_file',
                        location='files', required=True)

    parser.add_argument('phabricator.callsign', type=get_repository_by_callsign,
                        required=True, dest='repository')
    parser.add_argument('phabricator.buildTargetPHID', required=False)
    parser.add_argument('phabricator.diffID', required=True)
    parser.add_argument('phabricator.revisionID', required=True)
    parser.add_argument('phabricator.revisionURL', required=True)

    def post(self):
        """
        Notify Changes of a newly created diff.

        Depending on system configuration, this may create 0 or more new builds,
        and the resulting response will be a list of those build objects.
        """
        args = self.parser.parse_args()

        repository = args.repository
        if not args.repository:
            return error("Repository not found")

        projects = list(Project.query.options(
            subqueryload_all('plans'),
        ).filter(
            Project.status == ProjectStatus.active,
            Project.repository_id == repository.id,
        ))

        # no projects bound to repository
        if not projects:
            return self.respond([])

        options = dict(
            db.session.query(
                ProjectOption.project_id, ProjectOption.value
            ).filter(
                ProjectOption.project_id.in_([p.id for p in projects]),
                ProjectOption.name.in_([
                    'phabricator.diff-trigger',
                ])
            )
        )

        projects = [
            p for p in projects
            if options.get(p.id, '1') == '1'
        ]

        if not projects:
            return self.respond([])

        label = args.label[:128]
        author = args.author
        message = args.message
        sha = args.sha
        target = 'D{}'.format(args['phabricator.revisionID'])

        try:
            identify_revision(repository, sha)
        except MissingRevision:
            return error("Unable to find commit %s in %s." % (
                sha, repository.url), problems=['sha', 'repository'])

        source_data = {
            'phabricator.buildTargetPHID': args['phabricator.buildTargetPHID'],
            'phabricator.diffID': args['phabricator.diffID'],
            'phabricator.revisionID': args['phabricator.revisionID'],
            'phabricator.revisionURL': args['phabricator.revisionURL'],
        }

        patch = Patch(
            repository=repository,
            parent_revision_sha=sha,
            diff=''.join(args.patch_file),
        )
        db.session.add(patch)

        source = Source(
            patch=patch,
            repository=repository,
            revision_sha=sha,
            data=source_data,
        )
        db.session.add(source)

        phabricatordiff = try_create(PhabricatorDiff, {
            'diff_id': args['phabricator.diffID'],
            'revision_id': args['phabricator.revisionID'],
            'url': args['phabricator.revisionURL'],
            'source': source,
        })
        if phabricatordiff is None:
            logging.error("Diff %s, Revision %s already exists", args['phabricator.diffID'], args['phabricator.revisionID'])
            return error("Diff already exists within Changes")

        project_options = ProjectOptionsHelper.get_options(projects, ['build.file-whitelist'])
        diff_parser = DiffParser(patch.diff)
        files_changed = diff_parser.get_changed_files()

        collection_id = uuid.uuid4()
        builds = []
        for project in projects:
            plan_list = get_build_plans(project)
            if not plan_list:
                logging.warning('No plans defined for project %s', project.slug)
                continue

            if not in_project_files_whitelist(project_options[project.id], files_changed):
                logging.info('No changed files matched build.file-whitelist for project %s', project.slug)
                continue

            builds.append(create_build(
                project=project,
                collection_id=collection_id,
                sha=sha,
                target=target,
                label=label,
                message=message,
                author=author,
                patch=patch,
            ))

        return self.respond(builds)


from typing import Callable, Tuple, Union

from scipy.integrate import quad  # type: ignore

from .context import Context
from .tree import BaseValue, Resolveable
from .utils import aslist

quad: Callable[..., Tuple[float, float]]

class Integral(Resolveable):
    def __init__(
        self,
        integrand: Callable[[Union[float, BaseValue]], Union[float, BaseValue]],
        low: float,
        high: float,
    ):
        self.integrand = integrand
        self.low = low
        self.high = high

    def _integrand_wrapper(self, x: float):
        result = self.integrand(x)
        if isinstance(result, BaseValue):
            return ~result
        return result

    @aslist
    def _resolve(self):
        with Context() as context:
            for _ in range(context.sample_count):
                with Context(cache={}, sample_count=1) as context:
                    integral, _ = quad(self._integrand_wrapper, self.low, self.high)
                    yield integral


import os
import re
import ast
import pandas as pd
import numpy as np
import datetime
from typing import List, Union, Any, Tuple
import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials

CREDENTIALS_FILE = os.getenv("CREDENTIALS_FILE")
gsheetId = os.getenv("gsheetId")
API_SERVICE_NAME = os.getenv("API_SERVICE_NAME")
API_VERSION = os.getenv("API_VERSION")
SCOPES = ast.literal_eval(os.environ.get("SCOPES"))
PERIOD_DAYS = int(os.environ.get("PERIOD_DAYS"))
TODAY = datetime.datetime.today().strftime("%d%m")
RAW_DATA = f"raw_tansactions{PERIOD_DAYS}_{TODAY}.csv"

credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, SCOPES)

httpAuth = credentials.authorize(httplib2.Http())
service = apiclient.discovery.build(API_SERVICE_NAME, API_VERSION, http=httpAuth)

def export_data_to_sheet(df: pd.DataFrame, listName: str) -> None:

    response_date = (
        service.spreadsheets()
        .values()
        .update(
            spreadsheetId=gsheetId,
            valueInputOption="USER_ENTERED",
            range=f"{listName}!a1",
            body=dict(
                majorDimension="ROWS", values=df.T.reset_index().T.values.tolist()
            ),
        )
        .execute()
    )

def add_sheet(title: str, nrows: int, ncols: int) -> None:

    results = (
        service.spreadsheets()
        .batchUpdate(
            spreadsheetId=gsheetId,
            body={
                "requests": [
                    {
                        "addSheet": {
                            "properties": {
                                "title": title,
                                "gridProperties": {
                                    "rowCount": nrows,
                                    "columnCount": ncols,
                                },
                            }
                        }
                    }
                ]
            },
        )
        .execute()
    )

def clear_sheet(sheetName: str) -> None:

    rangeAll = "{0}!A1:Z".format(sheetName)
    resultClear = (
        service.spreadsheets()
        .values()
        .clear(spreadsheetId=gsheetId, range=rangeAll, body={})
        .execute()
    )

def get_sheets_properties() -> dict:
    spreadsheet = service.spreadsheets().get(spreadsheetId=gsheetId).execute()
    sheetList = spreadsheet.get("sheets")
    dct_sheets = {}
    for sheet in sheetList:
        dct_sheets[sheet["properties"]["title"]] = sheet["properties"]["sheetId"]

    return dct_sheets

def clean_categories1(row: str) -> str:
    return re.sub(r"[^a-zA-Z0-9_\s,]+", "", row).split(", ")[0]

def clean_categories2(row: Union[str, list]) -> Union[str, list]:

    res = re.sub(r"[^a-zA-Z0-9_\s,]+", "", row).split(", ")[1:]
    category2 = res if res else "null"
    return category2

def clean_data_trans() -> pd.DataFrame:

    df = pd.read_csv(
        RAW_DATA, parse_dates=["date"], usecols=["amount", "category", "date"]
    )

    split_categories = lambda x: x.split(",")
    df["category 1"] = df.category.apply(clean_categories1)
    df["category 2"] = df.category.apply(clean_categories2)
    df.drop("category", axis=1, inplace=True)
    df = df.explode("category 2")

    df["Income/Expense"] = df.amount.map(lambda x: "Income" if x > 0 else "Expense")
    df.replace(np.nan, "", inplace=True)

    global month_count
    month_count = df.date.nunique()

    global dct_cols
    dct_cols = dict(zip(df.columns.tolist(), range(len(df.columns))))
    df["date"] = df.date.apply(lambda x: x.strftime("%m/%Y")).astype(str)
    return df

def make_pivotTbl(
    source_sheet_id: int, target_sheet_id: int, end_row: int, end_col: int
) -> None:

    requests = []
    requests.append(
        {
            "updateCells": {
                "rows": {
                    "values": [
                        {
                            "pivotTable": {
                                "source": {
                                    "sheetId": source_sheet_id,
                                    "startRowIndex": 0,
                                    "startColumnIndex": 0,
                                    "endRowIndex": end_row,
                                    "endColumnIndex": end_col,
                                },
                                "rows": [
                                    {
                                        "sourceColumnOffset": dct_cols[
                                            "Income/Expense"
                                        ],
                                        "showTotals": True,
                                        "sortOrder": "DESCENDING",
                                    },
                                    {
                                        "sourceColumnOffset": dct_cols["category 1"],
                                        "showTotals": True,
                                        "sortOrder": "ASCENDING",
                                    },
                                    {
                                        "sourceColumnOffset": dct_cols["category 2"],
                                        "showTotals": True,
                                        "sortOrder": "ASCENDING",
                                    },
                                ],
                                "columns": [
                                    {
                                        "sourceColumnOffset": dct_cols["date"],
                                        "sortOrder": "ASCENDING",
                                        "showTotals": True,
                                    }
                                ],
                                "values": [
                                    {
                                        "summarizeFunction": "SUM",
                                        "sourceColumnOffset": dct_cols["amount"],
                                    }
                                ],
                                "valueLayout": "HORIZONTAL",
                            }
                        }
                    ]
                },
                "start": {"sheetId": target_sheet_id, "rowIndex": 0, "columnIndex": 0},
                "fields": "pivotTable",
            }
        }
    )

    # setting autoresize columns
    requests.append(
        {
            "autoResizeDimensions": {
                "dimensions": {
                    "sheetId": target_sheet_id,
                    "dimension": "COLUMNS",
                    "startIndex": 0,
                    "endIndex": 4,
                }
            }
        }
    )

    # setting number formart
    requests.append(
        {
            "repeatCell": {
                "range": {
                    "sheetId": target_sheet_id,
                    "startRowIndex": 2,
                    "startColumnIndex": 3,
                    "endRowIndex": 26,
                    "endColumnIndex": month_count + 4,
                },
                "cell": {
                    "userEnteredFormat": {
                        "numberFormat": {"type": "CURRENCY", "pattern": '""#,###.##'}
                    }
                },
                "fields": "userEnteredFormat.numberFormat",
            }
        }
    )

    # bold total
    requests.append(
        {
            "repeatCell": {
                "range": {
                    "sheetId": target_sheet_id,
                    "startColumnIndex": month_count + 3,
                    "endColumnIndex": month_count + 4,
                },
                "cell": {"userEnteredFormat": {"textFormat": {"bold": True}}},
                "fields": "userEnteredFormat.textFormat.bold",
            }
        }
    )

    body = {"requests": requests}
    response = (
        service.spreadsheets().batchUpdate(spreadsheetId=gsheetId, body=body).execute()
    )

def update_properties(request: Tuple[dict, ...]) -> None:

    body = {"requests": request}
    response = (
        service.spreadsheets().batchUpdate(spreadsheetId=gsheetId, body=body).execute()
    )

def del_sheet(sheet_id):
    result = (
        service.spreadsheets()
        .batchUpdate(
            spreadsheetId=gsheetId,
            body={"requests": [{"deleteSheet": {"sheetId": sheet_id}}]},
        )
        .execute()
    )

if __name__ == "__main__":

    raw_data = pd.read_csv(RAW_DATA)
    raw_data.replace(np.nan, "", inplace=True)
    export_data_to_sheet(raw_data, "raw_data")

    df = clean_data_trans()
    # dim clean_data
    nrows, ncols = df.shape

    # dim of cashflow_statement nr x nc
    nr = df.groupby(["category 1", "category 2"]).size().shape[0] * 3
    nc = len(df) + month_count

    add_sheet("cleaned_data", nrows, ncols)
    add_sheet("cashflow_statement", nr, nc)
    export_data_to_sheet(df, "cleaned_data")

    dct_sheets = get_sheets_properties()
    source_sheet_id = dct_sheets["cleaned_data"]
    target_sheet_id = dct_sheets["cashflow_statement"]

    date_format = [
        {
            "repeatCell": {
                "range": {
                    "sheetId": source_sheet_id,
                    "startRowIndex": 1,
                    "startColumnIndex": 1,
                    "endColumnIndex": 2,
                },
                "cell": {
                    "userEnteredFormat": {
                        "numberFormat": {"type": "DATE", "pattern": "mmm yyyy"}
                    }
                },
                "fields": "userEnteredFormat.numberFormat",
            }
        }
    ]
    # update date format in cleaned_data
    update_properties(date_format)
    make_pivotTbl(source_sheet_id, target_sheet_id, nrows, ncols)


import os
import requests
import csv
import tqdm

def download_img(url, name):
    if not os.path.exists("./images/"):
        os.mkdir("./images/")
    name = name.replace("/", "_").replace('"', '“')
    if os.path.exists("./images/" + name + '.jpg'):
        return
    req = requests.get(url=url)
    try:
        with open("./images/" + name + '.jpg', 'wb') as f:
            f.write(req.content)
    except Exception as e:
        print(e)

def read_csv(address):
    with open(address, "r", encoding="utf-8") as f:
        reader = csv.reader(f)
        rows = list(reader)
        for num in tqdm.trange(1, len(rows)):
            download_img(rows[num][2], rows[num][1])

if __name__ == '__main__':
    read_csv("./data/arsenal.csv")


# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np
import torch
import torchvision as tv
from torchvision import transforms, datasets
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
print(tf.__version__)

# 3 models to implement various models
class ShallowSimNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(1, 434)
        self.fc2 = nn.Linear(434, 1)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = self.fc2(val)
        return val

class MiddleSimNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(1, 10)
        self.fc2 = nn.Linear(10, 40)
        self.fc3 = nn.Linear(40,20)
        self.fc4 = nn.Linear(20, 1)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = F.relu(self.fc2(val))
        val = F.relu(self.fc3(val))
        val = self.fc4(val)
        return val

class DeepSimNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(1, 32)
        self.fc2 = nn.Linear(32, 20)
        self.fc3 = nn.Linear(20, 18)
        self.fc4 = nn.Linear(18, 9)
        self.fc5 = nn.Linear(9, 3)
        self.fc6 = nn.Linear(3, 1)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = F.relu(self.fc2(val))
        val = F.relu(self.fc3(val))
        val = F.relu(self.fc4(val))
        val = F.relu(self.fc5(val))
        val = self.fc6(val)
        return val

np.random.seed(10)
simulatedInput = 10 * torch.rand((1000, 1)) - 5
x=simulatedInput
groundTruthSin = (np.sin(1.5*x))
groundTruthCos = np.cos(0.5*x)

def calcParams(inputModel):
    val = sum(params.numel() for params in inputModel.parameters() if params.requires_grad)
    return val
model1 = ShallowSimNN()
print(calcParams(MiddleSimNN()))
print(calcParams(DeepSimNN()))
print(calcParams(ShallowSimNN()))

shallowCosNN = ShallowSimNN()
middleCosNN = MiddleSimNN()
deepCosNN = DeepSimNN()
shallowSinNN = ShallowSimNN()
middleSinNN = MiddleSimNN()
deepSinNN = DeepSimNN()
costFunc = nn.MSELoss()
shallowCosOpt = optim.Adam(shallowCosNN.parameters(), lr=0.001)
middleCosOpt = optim.Adam(middleCosNN.parameters(), lr=0.001)
deepCosOpt = optim.Adam(deepCosNN.parameters(), lr=0.001)
shallowSinOpt = optim.Adam(shallowSinNN.parameters(), lr=0.001)
middleSinOpt = optim.Adam(middleSinNN.parameters(), lr=0.001)
deepSinOpt = optim.Adam(deepSinNN.parameters(), lr=0.001)

EPOCHS = 5000
counter = 0
counterList = []
shallowCosCostList = []
for index in range(EPOCHS):
    counterList.append(counter)
    counter += 1
    shallowCosNN.zero_grad()
    output = shallowCosNN(simulatedInput)
    cost = costFunc(output, groundTruthCos)
    shallowCosCostList.append(cost)
    cost.backward()
    shallowCosOpt.step()

middleCosCostList = []
for index in range(EPOCHS):
    middleCosNN.zero_grad()
    output = middleCosNN(simulatedInput)
    cost = costFunc(output, groundTruthCos)
    middleCosCostList.append(cost)
    cost.backward()
    middleCosOpt.step()

deepCosCostList = []
for index in range(EPOCHS):
    deepCosNN.zero_grad()
    output = deepCosNN(simulatedInput)
    cost = costFunc(output, groundTruthCos)
    deepCosCostList.append(cost)
    cost.backward()
    deepCosOpt.step()

shallowSinCostList = []
for index in range(EPOCHS):
    shallowSinNN.zero_grad()
    output = shallowSinNN(simulatedInput)
    cost = costFunc(output, groundTruthSin)
    shallowSinCostList.append(cost)
    cost.backward()
    shallowSinOpt.step()

middleSinCostList = []
for index in range(EPOCHS):
    middleSinNN.zero_grad()
    output = middleSinNN(simulatedInput)
    cost = costFunc(output, groundTruthSin)
    middleSinCostList.append(cost)
    cost.backward()
    middleSinOpt.step()

deepSinCostList = []
for index in range(EPOCHS):
    deepSinNN.zero_grad()
    output = deepSinNN(simulatedInput)
    cost = costFunc(output, groundTruthSin)
    deepSinCostList.append(cost)
    cost.backward()
    deepSinOpt.step()

plt.plot(counterList, deepSinCostList, 'r', label='model1(deep)')
plt.plot(counterList, middleSinCostList, 'g', label='model2(normal)')
plt.plot(counterList, shallowSinCostList, 'b', label='model3(shallow)')
plt.title("sin(1.5*x)")
plt.xlabel("ephocs")
plt.ylabel("loss")
plt.legend(loc="center")
plt.show()

plt.plot(counterList, deepCosCostList, 'r', label='model1(deep)')
plt.plot(counterList, middleCosCostList, 'g', label='model2(normal)')
plt.plot(counterList, shallowCosCostList, 'b', label='model3(shallow)')
plt.title("cos(0.5*x)")
plt.xlabel("ephocs")
plt.ylabel("loss")
plt.legend(loc="center")
plt.show()

simulatedInput = 10 * torch.rand((1000, 1)) - 5
x=simulatedInput
groundTruthSin = (np.sin(1.5*x))
groundTruthCos = (np.cos(0.5*x))

shallowCosOutput = shallowCosNN(simulatedInput)
middleCosOutput = middleCosNN(simulatedInput)
deepCosOutput = deepCosNN(simulatedInput)
shallowSinOutput = shallowSinNN(simulatedInput)
middleSinOutput = middleSinNN(simulatedInput)
deepSinOutput = deepSinNN(simulatedInput)

plt.plot(simulatedInput, deepCosOutput.tolist(), 'go', label='model1(deep)')
plt.plot(simulatedInput, middleCosOutput.tolist(), 'ro', label='model2(normal)')
plt.plot(simulatedInput, shallowCosOutput.tolist(), 'co', label='model3(shalloow)')
plt.plot(simulatedInput, groundTruthCos.tolist(), 'bo', label='Ground-Truth')

plt.title("Predictions curve and Truth Value for cos(0.5x)")
plt.xlabel("input(x)")
plt.ylabel("output(y=cos(0.5x)")
plt.legend(loc="top right")
plt.show()

plt.plot(simulatedInput, deepSinOutput.tolist(), 'go', label='model1(deep)')
plt.plot(simulatedInput, middleSinOutput.tolist(), 'ro', label='model2(normal)')
plt.plot(simulatedInput, shallowSinOutput.tolist(), 'co', label='model3(shallow)')
plt.plot(simulatedInput, groundTruthSin.tolist(), 'bo', label='Ground-Truth')

plt.title("Predictions curve and Truth Value for sin(1.5x)")
plt.xlabel("input(x)")
plt.ylabel("output(y=sin(1.5x)")
plt.legend(loc="upper left")
plt.show()

print(tf.__version__)

class ShallowTrainNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 30)
        self.fc2 = nn.Linear(30, 10)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = self.fc2(val)
        return val

class MiddleTrainNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 25)
        self.fc2 = nn.Linear(25, 60)
        self.fc3 = nn.Linear(60, 38)
        self.fc4 = nn.Linear(38, 10)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = F.relu(self.fc2(val))
        val = F.relu(self.fc3(val))
        val = self.fc4(val)
        return val

class DeepTrainNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 20)
        self.fc2 = nn.Linear(20, 40)
        self.fc3 = nn.Linear(40, 60)
        self.fc4 = nn.Linear(60, 50)
        self.fc5 = nn.Linear(50, 30)
        self.fc6 = nn.Linear(30, 10)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = F.relu(self.fc2(val))
        val = F.relu(self.fc3(val))
        val = F.relu(self.fc4(val))
        val = F.relu(self.fc5(val))
        val = self.fc6(val)
        return val

class GradientNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(1, 8)
        self.fc2 = nn.Linear(8, 15)
        self.fc3 = nn.Linear(15, 8)
        self.fc4 = nn.Linear(8, 1)

    def forward(self, val):
        val = F.relu(self.fc1(val))
        val = F.relu(self.fc2(val))
        val = F.relu(self.fc3(val))
        val = self.fc4(val)
        return val

def calcParams(inputModel):
    val = sum(params.numel() for params in inputModel.parameters() if params.requires_grad)
    return val
print(calcParams(MiddleTrainNN()))
print(calcParams(DeepTrainNN()))
print(calcParams(ShallowTrainNN()))
print(calcParams(GradientNN()))


from scipy.ndimage import label as bwlabel
from computeNsSystem import computeNsSystem
from computeLinesDC import computeLinesDC, compute_lines_data_cost
from computeLinesLabelCost import computeLineLabelCost
import numpy as np
from LineExtraction_GC_MRFminimization import LineExtraction_GC_MRFminimization, line_extraction_GC

# @partial_image(2, "post_process_by_mfr")
def post_process_by_mfr(labeled_raw_components, raw_components_num, lines_mask, char_range):
    cc_sparse_ns = computeNsSystem(labeled_raw_components, raw_components_num)
    [result, labels, num_lines] = post_process_by_mfr_helper(labeled_raw_components,
                                                             raw_components_num,
                                                             lines_mask,
                                                             char_range,
                                                             cc_sparse_ns)
    return [result, labels, num_lines]

def post_process_by_mfr_helper(labeled_raw_components, raw_components_num, line_mask, char_range, cc_sparse_ns=None):
    # line mask should be binarized already
    line_mask_lables, n_masked_lines = bwlabel(line_mask, np.ones((3,3)))
    data_cost = compute_lines_data_cost(line_mask_lables, n_masked_lines, labeled_raw_components, raw_components_num,
                                         char_range[1])

    label_cost = computeLineLabelCost(labeled_raw_components, line_mask_lables, n_masked_lines)

    labels = line_extraction_GC(raw_components_num, n_masked_lines, data_cost, cc_sparse_ns, label_cost)

    result = drawLabels(labeled_raw_components, labels)
    return [result, labels, n_masked_lines]

# L = original image
def PostProcessByMRFHelper(L, num, LineMask, CCsparseNs, charRange):
    # LineLabels, n_label = bwlabel(LineMask.__invert__())
    LineLabels, n_label = bwlabel(LineMask)
    print("got {} labels".format(n_label))
    Lines, numLines = LineLabels, n_label
    # Lines, numLines = permuteLabels(LineLabels)
    Dc = computeLinesDC(Lines, numLines, L, num, charRange[1])
    LabelCost = computeLineLabelCost(L, Lines, numLines)
    Labels = LineExtraction_GC_MRFminimization(numLines, num, CCsparseNs, Dc, LabelCost)
    Labels[Labels == numLines + 1] = 0
    # residualLines = np.isin(Lines, Labels)
    # Lines[residualLines.__invert__()] = 0
    result = drawLabels(L, Labels)
    # RefinedCCs = RefineBinnaryOverlappingComponents(L, num, Lines, numLines)

    # tempMask = RefinedCCs >= 0

    # result[tempMask] = RefinedCCs[tempMask]
    return [result, Labels, numLines]

def drawLabels(L, Labels):
    L = np.uint16(L)
    LUT = np.zeros(65536, np.uint16)
    LUT[1:len(Labels) + 1] = Labels
    result = np.double(LUT[L])
    return result


#!/usr/bin/env python 3.8
# -*- coding: UTF-8 -*-
# @date: 2022.01.14 下午 11:46
# @name: demo2
# @author：Ads-Ryen
# @webside：www.prlrr.com
# @software: PyCharm

import requests
import re

url_1 = "https://www.demo.net/XiuRen/485_4.html"
response = requests.get(url=url_1)
try:
    htmls = response.text.encode('ISO-8859-1').decode(requests.utils.get_encodings_from_content(response.text)[0])
except UnicodeDecodeError:
    htmls = response.text.encode('ISO-8859-1').decode('gbk')
print(htmls)
img_name = re.findall('
(.*?)
', htmls)
img_content = re.findall('
     (.*?)
', htmls)
img_url = re.findall('
',htmls)
img_url32 = re.findall('',htmls)
# img_CoverUrls = re.findall('
', htmls)
# img_times = re.findall('(.*?)', htmls)
# img_urls = re.findall('
', htmls)
#
#
# for (img_url, img_CoverUrl, img_time, Volume_name) in zip(img_urls, img_CoverUrls, img_times, Volume_names):
#     print(Volume_name,img_url, img_CoverUrl, img_time)
#     print()
#     print()

print(img_url)
# try:
#     htmls_status = re.findall('<title>(.*?)</title>', htmls)
# except:
#     print(url_1, '判断当前套图中第',x,'页的状态时，正则表达式执行异常!!')
# htmls_status_404 = str(re.findall("\d+", str(htmls_status))[0])
# if htmls_status_404=='404':
#     print('页面404，跳出循环！')


from flask import request, Flask, jsonify
from search.Searchengine import Searchengine
from flask_cors import *

app = Flask(__name__)

CORS(app, supports_credentials=True)
searchengine = Searchengine()

@app.route("/")
def main():
    return "hello world"

@app.route("/search", methods=["GET","POST"])
def search():
    reponsedic = {}
    data = request.form.get("data", type=str,default=None)
    response = searchengine.search(data)
    reponsedic["data"]  =response
    return jsonify(reponsedic), 200

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=5000)


import sys

from flask import Flask, render_template, request, flash, url_for, redirect

from config import SQLITE_DATABASE_NAME
from model import db, db_init, Post

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + SQLITE_DATABASE_NAME
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SECRET_KEY'] = "anti-amogus"
db.app = app
db.init_app(app)

@app.route('/', methods=['GET', 'POST'])
def index():

    ps = Post.query.order_by(Post.id.desc()).all()

    if request.method == 'POST':
        name = request.form.get('name', type=str, default='')
        text = request.form.get('text', type=str, default='')

        if not name:
            flash('Пожлуйста, укажите своё имя!')
            return render_template('index.html', posts=ps)

        if not text:
            flash('Отзыв не может быть пустым.')
            return render_template('index.html', posts=ps)

        try:
            post = Post(name=name, text=text)
            db.session.add(post)
            db.session.commit()
        except:
            print('Fatall error wile adding new post')

        ps = Post.query.order_by(Post.id.desc()).all()

        return render_template('index.html', posts=ps)

    return render_template('index.html', posts=ps)

@app.route('/guestbook')
def guest_book():

    posts = Post.query.order_by(Post.id.desc()).all()
    return render_template('guestbook.html', posts=posts)

@app.route('/yandex_8565fd849978eca0')
def yandexwebvisor():
    return render_template('yandex_8565fd849978eca0.html')

if __name__ == '__main__':

    if len(sys.argv) > 1:
        if sys.argv[1] == 'init':
            with app.app_context():
                db_init()
                sys.exit(0)

    app.run(port=5000, debug=True)


from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
    name = 'Lisa'
    city_names = ['Paris', 'London', 'Rome', 'Tahiti']
    city = ""
    for i in city_names:
        city += f'
{i}
'

    return f'''

Welcome {name}!

                not google

                    {city}

    '''
#app.run()


'''Crie um pg que leia a idade de 7 pessoas. no final mostre quantas pessoas são maiores de idade e quantas são menores.'''
from datetime import date
maior = 0
menor = 0
ano = date.today().year
for lista in range(1,8):
    nasc = int(input('Digite seu ano de nascimento da {}ª pessoa: '.format(lista)))
    if ano - nasc >= 18:
        maior += 1
    else:
        menor +=1
print('{} Pessoas são maiores de idade e {} são menores.'.format(maior, menor))

exercicios/ex054 repetição for grupo da maioridade.py