#!/usr/bin/env python
# Interface to sounds for Ava

from __future__ import print_function, division

import os
import psutil
import argparse
import requests
import Queue
import numpy as np

from pydub import AudioSegment

import rospy
import rospkg
import actionlib

from sound_play.msg import SoundRequestAction, SoundRequestGoal, SoundRequest

# Helper functions

FILENAME_CHARS = list("abcdefghijklmnopqrstuvwxyz0123456789_")
FILENAME_NUM_RANDOM_CHARS = 10

def create_temp_filename(prefix, suffix):
    """A temporary filename creation helper. It's a bit of a hack"""
    global FILENAME_CHARS
    global FILENAME_NUM_RANDOM_CHARS
    name = "{}{}{}".format(
        prefix,
        "".join(np.random.choice(FILENAME_CHARS, FILENAME_NUM_RANDOM_CHARS)),
        suffix
    )
    filename = "/tmp"
    while os.path.exists(filename):
        filename = os.path.join("/tmp", name)
    return filename

# The actual sound client

class SoundClient(object):
    """
    A reimplementation of ROS's SoundClient to help with speech and sound
    tasks. We force the use of an action client in this class.

    To play a beep, you must refer to one of the constant keys in this
    package that begins with `SOUND_*`. By default, the sound files in `sounds`
    map to one of these keys.

    To speak, you can use SSML syntax to specify text. The TTS interface
    expects a MaryTTS server running in the background. The TTS could be a long
    running process and is currently blocking.
    """

    # Template SSML
    SSML_TEMPLATE = \
    """<?xml version="1.0" encoding="UTF-8" ?>
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.w3.org/2001/10/synthesis http://www.w3.org/TR/speech-synthesis/synthesis.xsd"
  xml:lang="en-US">
{speech}
</speak>
    """

    # Keys for the different beeps
    BEEP_CHEERFUL = "CHEERFUL"
    BEEP_CONCERNED = "CONCERNED"
    BEEP_EXCITED = "EXCITED"
    BEEP_HAPPY = "HAPPY"
    BEEP_PLAYFUL = "PLAYFUL"
    BEEP_PROUD = "PROUD"
    BEEP_SAD = "SAD"
    BEEP_SHOCKED = "SHOCKED"
    BEEP_SURPRISED = "SURPRISED"
    BEEP_UNSURE = "UNSURE"

    # Keys for the different affects
    AFFECT_SAD = "SAD"
    AFFECT_HAPPY = "HAPPY"
    AFFECT_ANGRY = "ANGRY"
    AFFECT_CALM = "CALM"
    AFFECT_NERVOUS = "NERVOUS"

    # Mary TTS server URL
    MARY_SERVER_URL = "http://localhost:59125/process"
    MARY_SERVER_TIMEOUT = 30  # Number of seconds to wait before timing out

    # Speech params
    SPEECH_GAIN_DB = 15

    # ROS Constants
    SOUND_PLAY_SERVER = "/sound_server"

    @staticmethod
    def make_happy(text):
        """Make the text happy!"""
        # return ('<emotion><category name="happy" />{}</emotion>'.format(text))
        return text

    @staticmethod
    def make_sad(text):
        """Make the text sad :("""
        # return ('<emotion><category name="sad" />{}</emotion>'.format(text))
        return text

    @staticmethod
    def make_angry(text):
        """Make the text angry"""
        # return ('<emotion><category name="angry" />{}</emotion>'.format(text))
        return text

    @staticmethod
    def make_calm(text):
        """Make the text calm"""
        # return (
        #     """
        # <emotion dimension-set="http://www.w3.org/TR/emotion-voc/xml#pad-dimensions">
        #     {}
        #     <dimension name="arousal" value="0.3"/><!-- lower arousal -->
        #     <dimension name="pleasure" value="0.9"/><!-- high positive valence -->
        #     <dimension name="dominance" value="0.8"/><!-- high potency    -->
        # </emotion>
        #     """.format(text)
        # )
        return text

    @staticmethod
    def make_nervous(text):
        """Make the text nervous"""
        # return (
        #     """
        # <emotion dimension-set="http://www.w3.org/TR/emotion-voc/xml#pad-dimensions">
        #     {}
        #     <dimension name="arousal" value="0.9"/><!-- high arousal -->
        #     <dimension name="pleasure" value="0.2"/><!-- negative valence -->
        #     <dimension name="dominance" value="0.2"/><!-- low potency    -->
        # </emotion>
        #     """.format(text)
        # )
        return text

    @staticmethod
    def change_audio_speed(sound, speed=1.0):
        # Manually override the frame_rate. This tells the computer how many
        # samples to play per second
        sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
            "frame_rate": int(sound.frame_rate * speed)
        })

        # convert the sound with altered frame rate to a standard frame rate
        # so that regular playback programs will work right. They often only
        # know how to play audio at standard frame rate (like 44.1k)
        return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)

    def __init__(self, beeps=None, affects=None):
        # Want to reimplement SoundClient so that we are always using the action
        # interface to the sound_play_node.
        self.sound_client = actionlib.SimpleActionClient(
            SoundClient.SOUND_PLAY_SERVER, SoundRequestAction
        )

        # Background thread to clear out speech files when they're done
        self._tmp_speech_files = Queue.Queue()
        self._tmp_speech_cleanup_thread = rospy.Timer(rospy.Duration(60), callback=self._cleanup)

        # Load the beeps
        self.beeps = beeps
        if self.beeps is None:
            default_sound_path = os.path.join(
                rospkg.RosPack().get_path('sound_interface'),
                'sounds'
            )
            self.beeps = {
                SoundClient.BEEP_CHEERFUL: os.path.join(default_sound_path, 'R2D2_cheerful.wav'),
                SoundClient.BEEP_CONCERNED: os.path.join(default_sound_path, 'R2D2_concerned.wav'),
                SoundClient.BEEP_EXCITED: os.path.join(default_sound_path, 'R2D2_excited.wav'),
                SoundClient.BEEP_HAPPY: os.path.join(default_sound_path, 'R2D2_happy.wav'),
                SoundClient.BEEP_PLAYFUL: os.path.join(default_sound_path, 'R2D2_playful.wav'),
                SoundClient.BEEP_PROUD: os.path.join(default_sound_path, 'R2D2_proud.wav'),
                SoundClient.BEEP_SAD: os.path.join(default_sound_path, 'R2D2_sad.wav'),
                SoundClient.BEEP_SHOCKED: os.path.join(default_sound_path, 'R2D2_shocked.wav'),
                SoundClient.BEEP_SURPRISED: os.path.join(default_sound_path, 'R2D2_surprised.wav'),
                SoundClient.BEEP_UNSURE: os.path.join(default_sound_path, 'R2D2_unsure.wav'),
            }

        # Load the affects
        self.affects = {
            SoundClient.AFFECT_SAD: SoundClient.make_sad,
            SoundClient.AFFECT_HAPPY: SoundClient.make_happy,
            SoundClient.AFFECT_NERVOUS: SoundClient.make_nervous,
            SoundClient.AFFECT_CALM: SoundClient.make_calm,
            SoundClient.AFFECT_ANGRY: SoundClient.make_angry,
        }

        # Need to connect to the server
        rospy.loginfo("Connecting to {}...".format(SoundClient.SOUND_PLAY_SERVER))
        self.sound_client.wait_for_server()
        rospy.loginfo("...{} connected".format(SoundClient.SOUND_PLAY_SERVER))

    def get_state(self):
        """Returns the state of the action client"""
        return self.sound_client.get_state()

    def get_result(self, blocking=False):
        """Returns the result of the last sound action. Blocks for a result"""
        if blocking:
            self.sound_client.wait_for_result()
        return self.sound_client.get_result()

    def get_beep_names(self):
        """Get the keys to the different beep types that are available"""
        return self.beeps.keys()

    def get_affect_names(self):
        """Get the keys to the different affects that are available"""
        return self.affects.keys()

    def say(self, text, affect="", blocking=False, **kwargs):
        """Perform TTS using SSML"""

        # Transform the text if the affect argument calls for it
        if affect and affect.upper() in self.affects.keys():
            text = self.affects[affect.upper()](text)

        # Create the vars for the SSML query
        text = SoundClient.SSML_TEMPLATE.format(speech=text)
        query_dict = {
            'INPUT_TEXT': text,
            'INPUT_TYPE': 'SSML',
            'LOCALE': 'en_GB',
            'VOICE': 'dfki-prudence-hsmm',
            'OUTPUT_TYPE': 'AUDIO',
            'AUDIO': 'WAVE',
            # 'effect_Robot_selected': 'on',
            # 'effect_Robot_parameters': 'amount:60.0',
        }

        # Send a request to MARY and check the response type
        r = requests.post(
            SoundClient.MARY_SERVER_URL,
            params=query_dict,
            timeout=SoundClient.MARY_SERVER_TIMEOUT
        )
        if r.headers['content-type'] != 'audio/x-wav':
            rospy.logerr("Response Error Code: {}. Content-Type: {}"
                         .format(r.status_code, r.headers['content-type']))
            raise ValueError("Incorrect Content Type", r.headers['content-type'], r.status_code)

        # Increase the volume on the temp file
        speech = AudioSegment(data=r.content)
        speech = speech + SoundClient.SPEECH_GAIN_DB
        speech = SoundClient.change_audio_speed(speech, 0.95)
        speech = speech.set_frame_rate(int(speech.frame_rate*2.0))

        # Write the wav data to a temp file
        speech_filename = create_temp_filename(prefix='marytts', suffix='.wav')
        with open(speech_filename, 'wb') as fd:
            speech.export(speech_filename, format='wav')

        # Now send the file's name over to sound play
        sound = SoundRequest()
        sound.sound = SoundRequest.PLAY_FILE
        sound.command = SoundRequest.PLAY_ONCE
        sound.arg = speech_filename
        self._play(sound, blocking=blocking, **kwargs)

        # Send the file to the cleanup thread now
        self._tmp_speech_files.put(speech_filename)

    def beep(self, key, blocking=False, **kwargs):
        """Play one of the beeps and boops that we know of"""
        if not key or key.upper() not in self.beeps:
            return

        sound = SoundRequest()
        sound.sound = SoundRequest.PLAY_FILE
        sound.command = SoundRequest.PLAY_ONCE
        sound.arg = self.beeps[key.upper()]
        self._play(sound, blocking=blocking, **kwargs)

    def stop(self):
        """Stop all sounds"""
        self.sound_client.cancel_all_goals()

        # Send a stop request
        sound = SoundRequest()
        sound.sound = SoundRequest.ALL
        sound.command = SoundRequest.PLAY_STOP
        sound.arg = ""

        self._play(sound, blocking=True)

    def _play(self, sound, blocking, **kwargs):
        # Send the command
        rospy.logdebug("Sending sound action with (sound, command, arg): {}, {}, {}"
                       .format(sound.sound, sound.command, sound.arg))
        goal = SoundRequestGoal(sound_request=sound)
        self.sound_client.send_goal(goal)

        # If blocking, wait until the sound is done
        if blocking:
            self.sound_client.wait_for_result()
            rospy.logdebug('Response to sound action received')

    def _cleanup(self, evt):
        # Get the files that we want to check are safe to delete or not
        files_to_check = set()
        while not self._tmp_speech_files.empty():
            files_to_check.add(self._tmp_speech_files.get(block=False))
            self._tmp_speech_files.task_done()

        # Iterate through all the processes and check if they are using
        # any of the files
        files_to_leave = set()
        for proc in psutil.process_iter():
            try:
                for filename in files_to_check:
                    if filename in [x.path for x in proc.open_files()]:
                        rospy.logdebug("File is used by: {}".format(proc.info))
                        files_to_leave.add(filename)
                        self._tmp_speech_files.put(filename)
            except Exception as e:
                pass

        # Finally delete only the files that we know we should leave
        for filename in (files_to_check - files_to_leave):
            if os.path.exists(filename):
                rospy.logdebug("Removing temp speech file: {}".format(filename))
                os.remove(filename)

if __name__ == '__main__':
    # For testing purposes
    rospy.init_node('speaker_test')
    SoundClient.SOUND_PLAY_SERVER = "sound_play"
    client = SoundClient()

    def on_speak(args):
        text = args.text
        affect = args.affect
        client.say(text, affect, blocking=True)

    def on_beep(args):
        client.beep(args.key, blocking=True)

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()

    speak_parser = subparsers.add_parser("speak", help="get the robot to speak")
    speak_parser.add_argument("text", help="text to speak")
    speak_parser.add_argument("--affect", choices=client.get_affect_names(),
                              help="say things in an affected voice")
    speak_parser.set_defaults(func=on_speak)

    beep_parser = subparsers.add_parser("beep", help="get the robot to beep")
    beep_parser.add_argument("key", help="type of beep to produce",
                             choices=client.get_beep_names())
    beep_parser.set_defaults(func=on_beep)

    args = parser.parse_args()
    args.func(args)


import cv2
import datetime

cap = cv2.VideoCapture(0)

width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

writer = cv2.VideoWriter((str(datetime.datetime.now()) + '.mp4'), cv2.VideoWriter_fourcc(*'DIVX') ,20 ,(width,height))

while True:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('s'):
        print("Start Recording")
        while True:
            ret, frame = cap.read()
            cv2.imshow('frame', frame)
            writer.write(frame)
            if cv2.waitKey(1) & 0xFF == ord('p'):
                print("End Recording")
                writer.release()
                cap.release()
                cv2.destroyAllWindows()


"""Neural Network class"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
import numpy as np

from utils import *
#import matplotlib.pyplot as plt

"""
the neural network class inherits from nn.Module
the default functions
init (constructor) and forward
have to be defined manually
"""

class GAN(nn.Module):
    """
    class constructor
    typically the neural networks variables and structure are initialized here
    super().__init__() is necessary to call the mother class constructor
    """

    def __init__(self, generator_struct, discriminator_struct, data_size, latent_size, device):
        super(GAN, self).__init__()
        self.name = "GenerativeAdversarialNetwork"
        self.generator_struct = generator_struct
        self.discriminator_struct = discriminator_struct
        self.data_size = data_size
        self.latent_size = latent_size
        self.batch_size = 1
        self.device = device

        """
        Construct neural network layers from subclasses
        """

        self.generator = Generator("Generator", self.generator_struct, self.latent_size, self.data_size, device)
        #self.discriminator = Discriminator("Discriminator", self.discriminator_struct, self.data_size, 2, device)
        self.discriminator = Discriminator("Discriminator", self.discriminator_struct, self.data_size, 1, device)

    """
    the forward function is called each the the network is propagating forward
    takes in input data and spits out the predicted output
    """

    #def forward(self, batch_size):
    def forward(self, x):

        #gen_sample = self.generator(batch_size)
        gen_sample = self.generator(x)

        prob = self.discriminator(gen_sample)

        return (gen_sample, prob)

    """
    initialize weights
    Using initialization routine from the torch.nn.init package
    """

    def init_weights(self, init_routine):
        print(f"Initializing weights of {self.name} with method {init_routine}\n")
        for i, layer in enumerate(self.layers):
            #if type(layer) == nn.Linear:
            if type(layer) in [nn.Linear, nn.Conv2d]:
                #torch.nn.init.xavier_normal_(layer.weight)
                init_routine(layer.weight)
                #if self.net_struct[i]["bias"] == True:
                    #layer.bias.data.fill_(0.01)

    """
    Basic method(s) to quickly display network structure, input, output
    """

    def show_net_struct(self):
        print(f"Network Architecture of {self.name}:\n")
        for layer in self.net_struct:
            print( "Layer: {}\n".format(layer))

    def get_net_struct(self):
        return self.net_struct

    def get_input_size(self):
        return self.input_size

    def get_output_size(self):
        return self.output_size

    def set_layer_sizes(self, layer_sizes):
        self.layer_sizes = layer_sizes

    def set_layers(self, net_struct):
        self.net_struct = net_struct
        self.layer_sizes = calc_layer_sizes(self.input_size, self.net_struct) #inherit from utils
        self.layers = nn.ModuleList()

        print(f"Initializing {self.name}:\n")
        for layer in self.net_struct:
            print(f"Adding {layer}\n")
            self.layers.append(layer["type"](**layer["layer_pars"]))

    def set_batch_size(self, batch_size):
        self.batch_size = batch_size

    def get_latent_variables(self):
        return (self.latent_mu, self.log_latent_sigma)

"""
Encoder and Decoder have similar structure
so that one can reutilize the same class for both!
"""
#class Coder(nn.Module, AutoEncoder):
class Generator(GAN):

    def __init__(self, name, net_struct, latent_size, output_size, device):
        super(Generator, self).__init__()
        self.name = name
        #self.net_struct = net_struct
        self.input_size = latent_size
        self.output_size = output_size
        self.batch_size = 1
        self.device = device
        self.set_layers(net_struct)

        """
        self.layer_sizes = calc_layer_sizes(self.input_size, self.net_struct) #inherit from utils
        self.layers = nn.ModuleList()

        print(f"Initializing {self.name}:\n")
        for layer in self.net_struct:
            print(f"Adding {layer}\n")
            self.layers.append(layer["type"](**layer["layer_pars"]))
        """
        #self.init_weights(torch.nn.init.xavier_normal_)  #inherit from AutoEncoder

        #self.set_latent_dist(func, kwargs)
        std_no_kwargs = {"loc" : 0.0, "scale" : 1.0}
        self.set_latent_dist(torch.distributions.Normal, std_no_kwargs)

    #def forward(self, batch_size):
    def forward(self, x):

        """
        iterate through all layers and perform calculation
        """

        for layer_i in range(len(self.layers)):
            #print(layer_i)
            #print(x.shape)

            z = self.layers[layer_i](x)
            if "act_func" in self.net_struct[layer_i]:
                x = self.net_struct[layer_i]["act_func"](z)
            else:
                x = z

            #print(x)
            #print(x.shape)

        return x

    def set_latent_dist(self, method, method_kwargs):
        self.dist = method(**method_kwargs)

class Discriminator(GAN):

    def __init__(self, name, net_struct, input_size, output_size, device):
        super(Discriminator, self).__init__()
        self.name = name
        #self.net_struct = net_struct
        self.input_size = input_size
        self.output_size = output_size
        self.batch_size = 1
        self.device = device

        self.set_layers(net_struct)

        """
        self.layer_sizes = calc_layer_sizes(self.input_size, self.net_struct) #inherit from utils
        self.layers = nn.ModuleList()

        print(f"Initializing {self.name}:\n")
        for layer in self.net_struct:
            print(f"Adding {layer}\n")
            self.layers.append(layer["type"](**layer["layer_pars"]))
        """
        #self.init_weights(torch.nn.init.xavier_normal_)  #inherit from AutoEncoder

    def forward(self, input_data):

        x = input_data
        #print(x.shape)

        """
        iterate through all layers and perform calculation
        """

        for layer_i in range(len(self.layers)):
            #print(layer_i)
            #print(x.shape)

            z = self.layers[layer_i](x)
            if "act_func" in self.net_struct[layer_i]:
                x = self.net_struct[layer_i]["act_func"](z)
            else:
                x = z

            #print(x)
            #print(x.shape)

        return x


from flask import Flask, render_template, request
import os
import random
import numpy as np
import random
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input
app = Flask(__name__)
app.config['UPLOAD_FOLDER']=os.path.join('static','pics')
#rendering home page for initial GET request
@app.route("/",methods=['GET', 'POST'])
def home():
    return render_template('page.html')
#rendering prediction page for POST request
@app.route("/imgfile", methods = ['GET','POST'])
def predict():
    if request.method=="POST":
        imagefile=request.files['file']
        image_path=os.path.join(r'static/pics/',imagefile.filename)
        imagefile.save(image_path)
        # initialising variable for visualising the image uploaded in ./imgfile site
        filename1=os.path.join(app.config['UPLOAD_FOLDER'],imagefile.filename)

        model = load_model('FER_model.h5', compile = False)
        # Preprocess Input Image and Make Prediction
        image_dir = image_path
        #load the image
        my_image = load_img(image_dir, target_size=(48, 48))
        #preprocess the image
        my_image = img_to_array(my_image)
        my_image = my_image.reshape((1, my_image.shape[0], my_image.shape[1], my_image.shape[2]))
        my_image = preprocess_input(my_image)

        #make the prediction
        prediction = model.predict(my_image)
        print(prediction)
        out = np.argmax(prediction)
        print(out)
        emote={0:'Angry',1:'Disgust',2:'Fear',3:'Happy',4:'Neutral',5:'Sad',6:'Surprise'}

        #lists of videos
        happy=os.listdir('static/videos/Happy/Cleaned')
        energetic=os.listdir('static/videos/Energetic/Cleaned')
        neutral=os.listdir('static/videos/Neutral/Cleaned')
        sad=os.listdir('static/videos/Sad/Cleaned')

        #mapping videos to prediction
        if emote[out]=='Angry':
            user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
        if emote[out]=='Disgust':
            user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
        if emote[out]=='Fear':
            k=random.randint(0,1)
            if k==0:
                user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
            if k==1:
                user_video=os.path.join('static/videos/Energetic/Cleaned/',energetic[random.randint(0,len(energetic)-1)])
        if emote[out]=='Happy':
            k=random.randint(0,3)
            if k==0:
                user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
            if k==1:
                user_video=os.path.join('static/videos/Energetic/Cleaned/',energetic[random.randint(0,len(energetic)-1)])
            if k==2:
                user_video=os.path.join('static/videos/Neutral/Cleaned/',neutral[random.randint(0,len(neutral)-1)])
            if k==3:
                user_video=os.path.join('static/videos/Sad/Cleaned/',sad[random.randint(0,len(sad)-1)])
        if emote[out]=='Neutral':
            k=random.randint(0,2)
            if k==0:
                user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
            if k==1:
                user_video=os.path.join('static/videos/Energetic/Cleaned/',energetic[random.randint(0,len(energetic)-1)])
            if k==2:
                user_video=os.path.join('static/videos/Neutral/Cleaned/',neutral[random.randint(0,len(neutral)-1)])
        if emote[out]=='Sad':
            k=random.randint(0,1)
            if k==0:
                user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
            if k==1:
                user_video=os.path.join('static/videos/Energetic/Cleaned/',energetic[random.randint(0,len(energetic)-1)])
        if emote[out]=='Surprise':
            k=random.randint(0,1)
            if k==0:
                user_video=os.path.join('static/videos/Happy/Cleaned/',happy[random.randint(0,len(happy)-1)])
            if k==1:
                user_video=os.path.join('static/videos/Energetic/Cleaned/',energetic[random.randint(0,len(energetic)-1)])
    #returning template at end of function
    return render_template('page1.html', prediction=emote[out], user_image = filename1, user_video=user_video) #filepath can be passed to render images or videos
#main code
if __name__=='__main__':
    app.run(debug=True)


from collections import defaultdict
import sys
input = sys.stdin.readline

N, d, k, c = map(int, input().split())
sushi = [int(input()) for _ in range(N)]

choose = defaultdict(int)
cnt_species = 0

for i in range(k):
    if choose[sushi[i]] == 0:
        cnt_species += 1

    choose[sushi[i]] += 1

max_value = cnt_species + 1 if choose[c] == 0 else cnt_species

for i in range(1, N-1):
    left, right = (i-1) % N, (i+k-1) % N

    choose[sushi[left]] -= 1
    if choose[sushi[left]] == 0:
        cnt_species -= 1

    choose[sushi[right]] += 1
    if choose[sushi[right]] == 1:
        cnt_species += 1

    max_value = max(max_value, cnt_species + 1 if choose[c] == 0 else cnt_species)

print(max_value)


from django.conf.urls import patterns, url

from nodeconductor.iaas import views

def register_in(router):
    router.register(r'instances', views.InstanceViewSet)
    router.register(r'iaas-templates', views.TemplateViewSet, base_name='iaastemplate')
    router.register(r'template-licenses', views.TemplateLicenseViewSet)
    router.register(r'iaas-resources', views.ResourceViewSet, base_name='iaas-resource')
    router.register(r'clouds', views.CloudViewSet)
    router.register(r'flavors', views.FlavorViewSet)
    router.register(r'project-cloud-memberships', views.CloudProjectMembershipViewSet, base_name='cloudproject_membership')
    router.register(r'security-groups', views.SecurityGroupViewSet, base_name='security_group')
    router.register(r'ip-mappings', views.IpMappingViewSet, base_name='ip_mapping')
    router.register(r'floating-ips', views.FloatingIPViewSet, base_name='floating_ip')

urlpatterns = patterns(
    '',
    url(r'^stats/customer/$', views.CustomerStatsView.as_view(), name='stats_customer'),
    url(r'^stats/usage/$', views.UsageStatsView.as_view(), name='stats_usage'),
    url(r'^stats/resource/$', views.ResourceStatsView.as_view(), name='stats_resource'),
)


# !/usr/bin/env python
# -*- encoding: utf-8 -*-
from setuptools import setup
import codecs
import io
import re
from glob import glob
from os import path
from os.path import basename
from os.path import dirname
from os.path import join
from os.path import splitext

from setuptools import find_packages
from setuptools import setup

wrkDir = path.abspath(path.dirname(__file__))

with open(path.join(wrkDir, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

def read_file(filename, cb):
    with codecs.open(filename, 'r', 'utf8') as f:
        return cb(f)

setup(
    name='geotime_classify',
    version='0.5.0',
    license='LGPL-3.0-or-later',
    description='Categorizes spatial and temporal columns for csv files. Standardizes date columns for transformations',
    long_description=long_description,
    long_description_content_type='text/markdown',
    author='Kyle Marsh',
    author_email='kyle@jataware.com',
    url='https://github.com/jataware/geotime_classify',
    packages=find_packages(),
    package_dir={'geotime_classify': 'geotime_classify'},
    package_data={'geotime_classify': ['models/*','datasets/*']},
    # include_package_data=True,
    zip_safe=False,

    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)',
        'Operating System :: Unix',
        'Operating System :: POSIX',
        'Operating System :: POSIX :: Linux',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: Implementation :: PyPy',
        'Topic :: Utilities',
    ],
    project_urls={

        'Changelog': 'https://github.com/jataware/geotime_classify/blob/master/CHANGELOG.md',
        'Issue Tracker': 'https://github.com/jataware/geotime_classify/issues',
    },
    keywords=['LSTM', 'RNN', 'Classification', 'Date','Datetime', 'Coordinates', 'Latitude', 'Longitude'],

    python_requires='>=3.6',

    install_requires=read_file('requirements.txt', lambda f: list(
            filter(bool, map(str.strip, f)))),

    extras_require={"dev": ["pytest>=3.7", "twine>=3.3.0"]},
    setup_requires=[
        'pytest-runner',
    ],

)


import os
import urllib.request
from flask import Flask, flash, render_template, request, redirect
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = 'uploads'

app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

CONFIG = {
    "active_menu": "dashboard"
}

ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/")
def dashboard():
    CONFIG["active_menu"] = "dashboard"
    return render_template("dashboard.html", CONFIG=CONFIG)

@app.route("/processing")
def data_processing():
    CONFIG["active_menu"] = "processing"
    return render_template("processing.html", CONFIG=CONFIG)

@app.route("/analysis")
def data_analysis():
    CONFIG["active_menu"] = "analysis"

    data = [
                {"date": "2019/09/04", "message": "processing...", "details": "upload done", "status": 4, "type": 1},
                {"date": "2019/09/04", "message": "processing...", "details": "Preprocessing started", "status": 4, "type": 1},
                {"date": "2019/09/04", "message": "processing...", "details": "Rotation", "status": 7, "type": 3},
                {"date": "2019/09/04", "message": "processing...", "details": "Cropping", "status": 1, "type": 3},
                {"date": "2019/09/04", "message": "processing...", "details": "Text Extraction", "status": 4, "type": 1}
            ]

    return render_template("analysis.html", CONFIG=CONFIG, data=data)

@app.route("/upload", methods=['POST'])
def upload():
    print('********* IN upload method ********* ')
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No file selected for uploading')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            flash('File successfully uploaded')
            return redirect('/')
        else:
            flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
            return redirect(request.url)

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


import matplotlib.pyplot as plt
from constants import RPI_0_COORDINATES, RPI_1_COORDINATES, RPI_2_COORDINATES, PHONE_COORDINATES

def plot_circles(radius_0, radius_1, radius_2):
    figure, axes = plt.subplots()
    rectangle = plt.Rectangle((0,0), 29, 34)
    axes.add_artist(rectangle)
    rectangle.set_facecolor('none')
    rectangle.set_edgecolor('black')

    phone = plt.Rectangle(PHONE_COORDINATES, 1, 1)
    axes.add_artist(phone)
    phone.set_facecolor('cyan')
    phone.set_edgecolor('cyan')

    circle_0 = plt.Circle(RPI_0_COORDINATES, radius_0)
    axes.add_artist( circle_0 )
    circle_0.set_facecolor('none')
    circle_0.set_edgecolor('blue')
    plt.plot(0, 0, 'o', color='blue')

    circle_1 = plt.Circle(RPI_1_COORDINATES, radius_1)
    axes.add_artist( circle_1 )
    circle_1.set_facecolor('none')
    circle_1.set_edgecolor('green')
    plt.plot(29, 22, 'o', color='green')

    circle_2 = plt.Circle(RPI_2_COORDINATES, radius_2)
    axes.add_artist( circle_2 )
    circle_2.set_facecolor('none')
    circle_2.set_edgecolor('red')
    plt.plot(0, 34, 'o', color='red')

    axes.set_aspect(1)
    plt.xlim(-20, 50)
    plt.ylim(-10, 50)

    axes.legend([circle_0, circle_1, circle_2, phone, rectangle], ["rpi_node_0", "rpi_node_1", "rpi_node_2", "phone position", "apartment outline"])
    axes.set_xlabel('Distance (feet)')
    axes.set_ylabel('Distance (feet)')

    plt.title('Phone Trilateration')
    plt.show()

plot_circles(7.943282347242816, 26.6072505979881, 22.3872113856834)


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 23 12:29:03 2021

@author: ELECTROBOT1
"""

# =============================================================================
# get data
# =============================================================================
from sklearn.datasets import fetch_20newsgroups
import pandas as pd
import re

dataset = fetch_20newsgroups(shuffle=True, random_state=1, remove=('headers', 'footers', 'quotes'))
documents = dataset.data
target= dataset.target

df= pd.DataFrame({'sentence':documents, 'label':target})

df['sentence']= [re.sub(r'[^A-Za-z /.]'," ",i) for i in df['sentence']]
df['sentence']= [re.sub(r'\s+'," ",i) for i in df['sentence']]

# Get the lists of sentences and their labels.
sentences = df.sentence.values
labels = df.label.values

#helper accuracy function
import numpy as np

# Function to calculate the accuracy of our predictions vs labels
def flat_accuracy(preds, labels):
    pred_flat = np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    return np.sum(pred_flat == labels_flat) / len(labels_flat)

# =============================================================================
# load bert
# =============================================================================

from transformers import BertTokenizer
bert_path=r"C:\Users\ELECTROBOT\Desktop\bert_base_uncased"

# Load the BERT tokenizer.

tokenizer = BertTokenizer.from_pretrained(bert_path, do_lower_case=True)

# =============================================================================
# create data loader
# =============================================================================

import torch
class loader(torch.utils.data.Dataset):
    def __init__(self, tokenizer,documents, labels=None, max_len=512):
        super().__init__()
        self.documents= documents
        if labels:
            self.labels=torch.tensor(labels)
        else:
            self.labels=None
        self.encoded= tokenizer.batch_encode_plus(self.documents, padding="max_length", max_length= max_len, truncation=True, return_attention_mask=True)

    def __len__(self):
        return len(self.documents)

    def __getitem__(self, index):
        input_ids= torch.tensor(self.encoded['input_ids'][index])
        attention_mask= torch.tensor(self.encoded['attention_mask'][index])

        if self.labels!=None:
            labels= self.labels[index]
            return input_ids, attention_mask, labels
        else:
            return input_ids, attention_mask

#load= loader(tokenizer=tokenizer, documents=sentences, labels= labels.tolist())

# kl=torch.utils.data.DataLoader(load, batch_size=4, shuffle=True)
# for i in kl:
#     break

# =============================================================================
# create train and val dataloader
# =============================================================================

from sklearn.model_selection import train_test_split

train,val, y_train, y_val= train_test_split(sentences, labels, test_size=.3, stratify= labels, shuffle=True)

train_loader= loader(tokenizer=tokenizer, documents=train, labels= y_train.tolist())
train_loader= torch.utils.data.DataLoader(train_loader, batch_size=4, shuffle=True)

val_loader= loader(tokenizer=tokenizer, documents=val, labels= y_val.tolist())
val_loader= torch.utils.data.DataLoader(val_loader, batch_size=4, shuffle=False)

# =============================================================================
# Define model
# =============================================================================

from transformers import BertForSequenceClassification, AdamW, BertConfig
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

epochs=5

model = BertForSequenceClassification.from_pretrained(bert_path, num_labels=len(set(target)),
    output_attentions = False, # Whether the model returns attentions weights.
    output_hidden_states = False, # Whether the model returns all hidden-states.
)

model.to(device)

params = list(model.named_parameters())

optimizer = AdamW(model.parameters(),
                  lr = 2e-5, # args.learning_rate - default is 5e-5
                  eps = 1e-8 # args.adam_epsilon  - default is 1e-8.
                )

#set warm-up
from transformers import get_linear_schedule_with_warmup
total_steps = len(train_loader) * epochs
scheduler = get_linear_schedule_with_warmup(optimizer,
                                            num_warmup_steps = 0, # Default value in run_glue.py
                                            num_training_steps = total_steps)

# =============================================================================
# train model
# =============================================================================

import time
t0=time.time()

best_valid_loss = float('inf')
train_losses=[]
val_losses=[]
val_accuracy=[]

for epoch in range(0,epochs):

    print("epoch---{}".format(epoch))

    total_train_loss=0

    model.train()

    for step, batch in enumerate(train_loader):
        print(",", end="", flush=True)
        #break

        batch=[r.to(device) for r in batch]

        model.zero_grad()

        outputs= model(input_ids=batch[0], attention_mask= batch[1], labels=batch[2])

        loss= outputs[0]

        total_train_loss+=loss.item()

        loss.backward()

        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)

        optimizer.step()

        scheduler.step()

    avg_train_loss = total_train_loss / len(train_loader)
    print("avg loss {} in epoch {}".format(avg_train_loss, epoch))
    train_losses.append(avg_train_loss)

    total_eval_loss=0
    total_eval_accuracy=0
    model.eval()

    for batch in val_loader:
        print(",", end="", flush=True)
        #break
        batch= [r.to(device) for r in batch]

        with torch.no_grad():

            outputs= model(input_ids=batch[0], attention_mask= batch[1], labels=batch[2])

        loss= outputs[0]
        total_eval_loss+=loss.item()

        logits= outputs[1]
        logits = logits.detach().cpu().numpy()
        label_ids = batch[2].to('cpu').numpy()

        total_eval_accuracy += flat_accuracy(logits, label_ids)

    avg_val_accuracy = total_eval_accuracy / len(val_loader)
    print("avg val accuracy {}".format(avg_val_accuracy))
    val_accuracy.append(avg_val_accuracy)

    avg_val_loss = total_eval_loss / len(val_loader)
    print("avg val loss {}".format(avg_val_loss))

    val_losses.append(avg_val_loss)

    if avg_val_loss<best_valid_loss:
        best_valid_loss=avg_val_loss
        torch.save(model.state_dict(), r'C:\Users\ELECTROBOT\Desktop\saved_model\model_saved.pt')

    print("time for epoch {} is {} minutes".format(epoch,round(time.time()-t0)/60))

#can train the model for a couple of epochs on the val data as well!!

# =============================================================================
# save full model and tokenizer
# =============================================================================

model.save_pretrained(r'C:\Users\ELECTROBOT\Desktop\saved_model\full')
tokenizer.save_pretrained(r'C:\Users\ELECTROBOT\Desktop\saved_model\full')

# =============================================================================
# predict on new data, load keys in bert
# =============================================================================

from transformers import BertForSequenceClassification,BertTokenizer
import torch
import numpy as np

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

bert_path=r"C:\Users\ELECTROBOT\Desktop\bert_base_uncased"
model = BertForSequenceClassification.from_pretrained(bert_path, num_labels=20,
    output_attentions = False, # Whether the model returns attentions weights.
    output_hidden_states = False, # Whether the model returns all hidden-states.
)

model.to(device)

path=r'C:\Users\ELECTROBOT\Desktop\saved_model\model_saved.pt'
model.load_state_dict(torch.load(path))

tokenizer = BertTokenizer.from_pretrained(bert_path, do_lower_case=True)

doc= 'Although I realize that principle is not one of your strongest points I would still like to know why do do not ask any question of this sort about the Arab countries. If you want to continue this think tank charade of yours your fixation on Israel must stop. You might have to start asking the same sort of questions of Arab countries as well. You realize it would not work as the Arab countries treatment of Jews over the last several decades is so bad that your fixation on Israel would begin to look like the biased attack that it is. Everyone in this group recognizes that your stupid Center for Policy Research is nothing more than a fancy name for some bigot who hates Israel.'

#17

encoded= tokenizer.encode_plus(doc, padding="max_length", max_length= 512, truncation=True, return_attention_mask=True)

ids=torch.tensor(encoded['input_ids']).to(device)

#encoded['token_type_ids']
masks=torch.tensor(encoded['attention_mask']).to(device)

with torch.no_grad():

        # model predictions
        preds = model(input_ids=ids.unsqueeze(0), attention_mask= masks.unsqueeze(0))

pred=np.argmax(preds['logits'].detach().cpu().numpy(), axis=1)

# =============================================================================
# load full saved model
# =============================================================================

from transformers import BertForSequenceClassification,BertTokenizer
import torch
import numpy as np
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

mdl= BertForSequenceClassification.from_pretrained(r'C:\Users\ELECTROBOT\Desktop\saved_model\full')
mdl.to(device)
tok= BertTokenizer.from_pretrained(r'C:\Users\ELECTROBOT\Desktop\saved_model\full', do_lower_case=True)

doc= 'Although I realize that principle is not one of your strongest points I would still like to know why do do not ask any question of this sort about the Arab countries. If you want to continue this think tank charade of yours your fixation on Israel must stop. You might have to start asking the same sort of questions of Arab countries as well. You realize it would not work as the Arab countries treatment of Jews over the last several decades is so bad that your fixation on Israel would begin to look like the biased attack that it is. Everyone in this group recognizes that your stupid Center for Policy Research is nothing more than a fancy name for some bigot who hates Israel.'

#17

encoded= tok.encode_plus(doc, padding="max_length", max_length= 512, truncation=True, return_attention_mask=True)

ids=torch.tensor(encoded['input_ids']).to(device)

#encoded['token_type_ids']
masks=torch.tensor(encoded['attention_mask']).to(device)

with torch.no_grad():
        # model predictions
        preds = mdl(input_ids=ids.unsqueeze(0), attention_mask= masks.unsqueeze(0))

pred=np.argmax(preds['logits'].detach().cpu().numpy(), axis=1).item()

# =============================================================================
# end
# =============================================================================

# =============================================================================
# predict on val dataset
# =============================================================================

#for a list of inputs, not using the loader
predicted=[]

for va in val:
    print(".", end="", flush=True)
    encoded= tokenizer.encode_plus(va, padding="max_length", max_length= 512, truncation=True, return_attention_mask=True)
    ids=torch.tensor(encoded['input_ids']).to(device)

    #encoded['token_type_ids']
    masks=torch.tensor(encoded['attention_mask']).to(device)

    with torch.no_grad():

            # model predictions
            preds = model(input_ids=ids.unsqueeze(0), attention_mask= masks.unsqueeze(0))

    pred=np.argmax(preds['logits'].detach().cpu().numpy(), axis=1)
    predicted.append(pred)

from sklearn import metrics

cr= metrics.classification_report(y_val, predicted)

#               precision    recall  f1-score   support

#            0       0.67      0.56      0.61       144
#            1       0.73      0.70      0.72       175
#            2       0.75      0.71      0.73       177
#            3       0.65      0.72      0.68       177
#            4       0.79      0.70      0.74       173
#            5       0.83      0.85      0.84       178
#            6       0.80      0.83      0.82       176
#            7       0.52      0.76      0.62       178
#            8       0.82      0.72      0.77       180
#            9       0.91      0.86      0.88       179
#           10       0.92      0.89      0.90       180
#           11       0.89      0.82      0.85       179
#           12       0.74      0.70      0.72       177
#           13       0.86      0.85      0.86       178
#           14       0.86      0.81      0.84       178
#           15       0.75      0.66      0.70       180
#           16       0.78      0.74      0.76       164
#           17       0.82      0.82      0.82       169
#           18       0.73      0.75      0.74       140
#           19       0.30      0.42      0.35       113

#     accuracy                           0.75      3395
#    macro avg       0.76      0.74      0.75      3395
# weighted avg       0.77      0.75      0.76      3395


import streamlit as sl
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers

button = 14
GPIO.setup(button, GPIO.OUT) # GPIO Assign mode

def press():
    sl.write('Switching...')
    GPIO.output(button, GPIO.LOW) # switch
    sleep(1)

def main():
    sl.title("USB Swithcer")
    switch = sl.button('Switch')
    while(switch):
        press()
        GPIO.output(button, GPIO.HIGH)
        switch = False
        # print(switch)

if __name__ == '__main__':
    main()


import yaml
import os
from pathlib import Path
from typing import Dict

PROJECT_ROOT = Path(__file__).parent.parent.absolute()

def load_yaml(source: str) -> Dict:
    with open(os.path.join(PROJECT_ROOT, source)) as resource:
        return yaml.safe_load(resource)

class Resources:
    def __init__(self, *paths: str):
        self.data = {}

        for path in paths:
            self.data.update(load_yaml(path))

    def __getitem__(self, key):
        return self.data[key]


import warnings
warnings.filterwarnings('ignore')
import pandas as pd
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import cohen_kappa_score

def process_data_for_model():
    data = pd.read_csv("data/processed/data.csv")

    y = data["AdoptionSpeed"]
    data = data.drop(
        columns=["AdoptionSpeed"])

    X_train, X_test, Y_train, Y_test = train_test_split(data, y, test_size=0.2, random_state=1)

    return X_train, X_test, Y_train, Y_test

def train_and_test_models(X_train, X_test, Y_train, Y_test):
    model1 = XGBClassifier(learning_rate=0.02, objective='multi:softmax',
                        silent=True, nthread=1, num_class=5,
                        max_depth=15, gamma=0.5, colsample_bytree=1.0, min_child_weight=5,
                         subsample=0.8, n_estimators=200)
    model = OneVsRestClassifier(model1)
    model = model.fit(X_train, Y_train)

    acc = model.score(X_test, Y_test)
    pred = model.predict(X_test)

    return acc, pred

X_train, X_test, Y_train, Y_test = process_data_for_model()
acc, pred = train_and_test_models(X_train, X_test, Y_train, Y_test)

print(acc)

Y_test = Y_test.values

kappa = cohen_kappa_score(Y_test, pred)
print(kappa)


import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread("C:\\Users\\megan\\Desktop\\Curso Udemy - Visao computacional com Python e OpenCV\\Parte 3\\Imagens\\colorida.jpg")
azul, verde, vermelho = cv.split(img)

plt.hist(azul.ravel(), 256, [0,256])
plt.figure()
plt.hist(verde.ravel(), 256, [0,256])
plt.figure()
plt.hist(vermelho.ravel(), 256,[0,256])
plt.show()


import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix, save_npz, load_npz
import sys

# Use python3
if sys.version_info[0]==2:
    sys.exit("Use python3")
file_name = "../data/lastfm-dataset-1K/userid-timestamp-artid-artname-traid-traname.tsv" # whole
file_name = "../data/halfid_20%_train.tsv"
df = pd.read_csv(file_name, sep='\t',header=None, quoting=3,
                 names=['userid', 'timestamp', 'artid', 'artname', 'traid', 'traname'],
                 dtype=object)

sys.version_info
cnt_df = pd.crosstab(df.userid,df.traid) # use traid or traname
del df
print ("release df")

np.save('sp_info_usr.npy', cnt_df.index ) # Save info
np.save('sp_info_tra.npy', cnt_df.columns ) # Save info
print ("save sp_info")

save_npz('sparse_matrix.npz',csr_matrix(cnt_df.values)) # Save sp
print ("save sparse_matrix.npz")


import requests
import csv
from bs4 import BeautifulSoup
from datetime import datetime

url = "https://www.baseball-reference.com/leagues/majors/2021-schedule.shtml"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")

games = soup.select("p.game")

with open("team_names.csv", "w", newline="") as csv_file:
    writer = csv.writer(csv_file)
    writer.writerow(["Date", "Away team", "Home team"])
    current_date = ""
    for game in games:
        date_element = game.find_previous("h3")
        date_string = date_element.text.strip() if date_element else current_date
        current_date = date_string
        date_object = datetime.strptime(date_string, "%A, %B %d, %Y")
        date_formatted = date_object.strftime("%Y-%m-%d")
        away_team = game.find_all("a")[0].text
        home_team = game.find_all("a")[1].text
        writer.writerow([date_formatted, away_team, home_team])


import torch
from torchvision.models import resnet50
from DeformableConv import DeformableConv2d
from plugplay import *
from PEG import *

def testDeformableConv():
    print("start of test LeFF")
    testx = torch.randn(8, 32, 123, 25)
    print("input shape ", testx.shape)
    deformConv = DeformableConv2d(in_channels=32, out_channels=64, kernel_size=1, stride=1, padding=0)
    output = deformConv(testx)
    print("output shape ", output.shape)

def testProjAttention():
    testx = torch.randn(8, 3, 25, 25)
    print("input shape ", testx.shape)
    #s3_proj_kernel = 3 s3_kv_proj_stride = 2, s3_heads = 4, s3_depth = 10,
    projAttention = ProjAttention(dim = 3, proj_kernel = 3, kv_proj_stride = 2, heads = 4, dim_head = 64, dropout = 0.)
    print("proj attention created ")
    output = projAttention(testx)
    print("output shape ", output.shape)

def testLeFF():
    print("start of test LeFF")
    testx = torch.randn(8, 196, 3)
    print("input shape ", testx.shape)
    leFF = LeFF(dim = 3, scale = 4, depth_kernel = 3, h =14,w=14)
    print("leFF created ")
    output = leFF(testx)
    print("output shape ", output.shape)

def testPEG():
    print("start of test PEG")
    testx = torch.randn(8, 400, 256)
    print("input shape ", testx.shape)
    peg = PEG(256)
    output = peg(testx, 20, 20)
    print("output shape ", output.shape)

if __name__ == "__main__":
    print("This is the start of main program")
    testDeformableConv()
    print("=============================================")
    testProjAttention()
    print("=============================================")
    testLeFF()
    print("=============================================")
    testPEG()

    testX = torch.randn(400, 2, 256)  #=> 2, 256, 20,20
    testX = testX.permute(1, 2, 0)   #Now 2,256,400
    B, C, HW = testX.shape
    H = 20
    W = 20
    testX = testX.view(B, C, H, W)
    print("testX shape:", testX.shape)
    testX = testX.view(B, C, -1)
    print("testX shape:", testX.shape)


import pygame, sys
from pygame.math import Vector2 as vec
import time
import random
import numpy as np

from map import *
import map as m

# CONFIGURATION FOR DEBUGGING PURPOSE
FORCE_TO_MOVE = False # Bắt con pacman phải đi mặc dù thức ăn quá xa

class Pacman:
    def __init__(self, map_obj, pos):
        self.map_obj = map_obj
        self.map_nrow = map_obj.grid_2d.shape[0]
        self.map_ncol = map_obj.grid_2d.shape[1]
        self.saveghost = []
        self.grid_pos = pos # tính theo i, j
        self.road = self.map_obj.grid_2d.copy()
        # self.vision = []
        self.pac_location = pos                 # Location in cell index
        self.pix_pos = self.get_pix_pos()       # Absolute location
        self.list_data = self.heuristic_allmap() # adjacency list
        self.save_shadow = self.create_save_shadow()

    def update_pos(self, pos):
        self.pac_location = pos
        self.grid_pos = pos

    def get_pix_pos(self):
        return (vec(((self.grid_pos.x + 1/2)*m.BLOCK_SIZE), \
                ((self.grid_pos.y + 1/2)*m.BLOCK_SIZE)))

    def check_map(self, road):
        self.road = road

    """
    current, food: theo cell index, không phải tọa độ
    """
    def heuristic(self, current, food):
        #mahatan
        return abs(current % self.map_nrow - food % self.map_nrow) + abs(current//self.map_nrow - food//self.map_nrow)

    def isValid(self, row:int, col:int):
        return (row >= 0) and (row < self.map_nrow) and (col >= 0) and (col < self.map_ncol)

    def isValid3(self, row:int, col:int, height, width):
        return (row >= 0) and (row < height) and (col >= 0) and (col < width)

    def isUnblock(self, vision, row:int, col:int):
        return self.map_obj.ghost_map[row, col] != GHOST \
                and (vision[row, col] != WALL)

    def isUnblock3(self, vision, row:int, col:int):
        return vision[row][col] == ROAD or vision[row][col] == FOOD

    def isDestination(self, row:int, col:int, food: vec):
        return (row == int(food.x) and col == int(food.y))

    """
    Trả về một danh sách các cạnh kề nhau
    """
    def heuristic_allmap(self):
        allmap = []
        for col in range(self.map_ncol):
            for row in range(self.map_nrow):

                temp = []
                if self.isValid(row, col) and self.isUnblock(self.road, row, col):
                        up = [row - 1,col]
                        down = [row + 1, col]
                        left = [row, col - 1]
                        right = [row, col + 1]

                        if self.isValid(up[0], up[1]) and self.isUnblock(self.road, up[0], up[1]):
                            temp.append(self.map_nrow * up[1] + up[0])

                        if self.isValid(down[0], down[1]) and self.isUnblock(self.road, down[0], down[1]):
                            temp.append(self.map_nrow * down[1] + down[0])

                        if self.isValid(left[0], left[1]) and self.isUnblock(self.road, left[0], left[1]):
                            temp.append(self.map_nrow * left[1] + left[0])

                        if self.isValid(right[0], right[1]) and self.isUnblock(self.road, right[0], right[1]):
                            temp.append(self.map_nrow * right[1] + right[0])
                        allmap.append(temp)

                else:

                    allmap.append([])
        return allmap

    def heuristic_allmap3(self, map_2d, width, height):
        allmap = []
        for col in range(width):
            for row in range(height):

                temp = []
                if self.isValid3(row, col, height, width) and self.isUnblock3(map_2d, row, col):
                        up = [row - 1,col]
                        down = [row + 1, col]
                        left = [row, col - 1]
                        right = [row, col + 1]

                        if self.isValid3(up[0], up[1], height, width) and self.isUnblock3(map_2d, up[0], up[1]):
                            temp.append(int(height) * up[1] + up[0])

                        if self.isValid3(down[0], down[1], height, width) and self.isUnblock3(map_2d, down[0], down[1]):
                            temp.append(int(height) * down[1] + down[0])

                        if self.isValid3(left[0], left[1], height, width) and self.isUnblock3(map_2d, left[0], left[1]):
                            temp.append(int(height) * left[1] + left[0])

                        if self.isValid3(right[0], right[1], height, width) and self.isUnblock3(map_2d, right[0], right[1]):
                            temp.append(int(height) * right[1] + right[0])
                        allmap.append(temp)

                else:
                    allmap.append([])
        return allmap

    def f_heuristic(self, node):
        return node[1]

    def next_node(self, current_node):
        return current_node[0][-1]

    def Graph_Search_A_Star(self, goal_x, goal_y):
        queue = []

        # Các node trong A_start tính theo số thứ tự của LAB01
        goal = int(goal_y) + self.map_nrow * int(goal_x)
        start = int(self.pac_location.y) + self.map_nrow * int(self.pac_location.x)

        # (node, f, g)
        queue.append(([start], self.heuristic(start,goal), 0))
        explored = []
        visited = []
        timeEscape = 0

        for i in range(0, len(self.list_data)):
            visited.append(False)
        # visited[0] = True # Node 0 không có bị visited # may cho bài này node 0 luôn là WALL
        visited[start] = True

        while len(queue) > 0:
            queue = sorted(queue,key=self.next_node)
            queue = sorted(queue,key=self.f_heuristic)

            catch = queue.pop(0)
            g = catch[2]
            explored.append(catch[0][-1])

            timeEscape += 1
            visited[catch[0][-1]] = True

            if catch[0][-1] == goal:
                # print("!", explored)
                # print(catch[0])
                return catch[0], explored, timeEscape

            else:

                for x in self.list_data[catch[0][-1]]:
                    catch1 = catch[0].copy()
                    catch1.append(x)
                    if visited[x] == False:
                        visited[x] = True
                        queue.append((catch1, self.heuristic(x,goal) + g + 1, g + 1))
        # print(explored)
        return [], explored, timeEscape

    # ---------------------------------------------
    # METHODS FOR LEVEL 3
    def create_save_shadow(self):
        return [[-1 for _ in range(self.map_ncol)]] * self.map_nrow
        # temp = []
        # save_shadow = []
        # for i in range(int(HEIGHT/20)):
        #     for j in range(int(WIDTH/20)):
        #         temp.append(-1)
        #     save_shadow.append(temp)
        # return save_shadow

    def get_shadow(self):
        map_shadow = []
        flag = 0
        check = 0

        for row in range(int(self.pac_location.y) - 3, int(self.pac_location.y) + 4):
            temp = []
            for col in range(int(self.pac_location.x) -3, int(self.pac_location.x) + 4):

                if self.isValid3(row, col, self.map_nrow, self.map_ncol) and col >= (int(self.pac_location.x) - flag) and col <= (int(self.pac_location.x) + flag):
                    if(self.pac_location.y == row and self.pac_location.x == col and self.road[row][col] == FOOD): ### WHY?
                        self.road[row][col] = ROAD
                    temp.append(self.road[row][col])
                    # --------------
                    if self.map_obj.ghost_map[row][col] == GHOST:
                        temp[-1] = GHOST

                    if temp[-1] == PACMAN:
                        temp[-1] = ROAD
                else:
                    temp.append(-1)

            if flag <= 3 and check == 0:
                flag += 1
            if flag > 3:
                flag = flag - 1
                check = 1
            if check == 1:
                flag -= 1
            map_shadow.append(temp)

        return map_shadow

    def get_food(self, map_shadow):
        food = []
        for row in range(len(map_shadow)):
            for col in range(len(map_shadow[0])):
                if(map_shadow[row][col] == FOOD):
                    food.append([row,col])
        return food

    def check_monster(self, map_shadow):
        for row in range(len(map_shadow)):
            for col in range(len(map_shadow[0])):
                check = 0
                if(map_shadow[row][col] == GHOST):
                    if(row == 3 and col == 0):
                        if  map_shadow[row][col + 1] == 3:
                            map_shadow[row][col] = 1
                        else:
                            map_shadow[row][col] = 1
                            map_shadow[row][col + 1] = 1
                    elif(row == 3 and col == len(map_shadow[0]) - 1):
                        if map_shadow[row][col - 1] == 3:
                            map_shadow[row][col] = 1
                        else:
                            map_shadow[row][col] = 1
                            map_shadow[row][col - 1] = 1
                    elif(col == 3 and row == 0):
                        if map_shadow[row + 1][col] == 3:
                            map_shadow[row][col] = 1
                        else:
                            map_shadow[row+1][col] = 1
                            map_shadow[row][col] = 1
                    elif(col == 3 and row == len(map_shadow[0]) - 1):
                        if map_shadow[row - 1][col] == 3:
                            map_shadow[row][col] = 1
                        else:
                            map_shadow[row][col] = 1
                            map_shadow[row - 1][col] == 1
                    else:
                        if row == 3 and col == 2:
                            check = 1
                        elif(row == 2 and col == 3):
                            check = 2
                        elif(row == 3 and col == 4):
                            check = 3
                        elif(row == 4 and col == 3):
                            check = 4
                        map_shadow[row][col] = 1
                        if(map_shadow[row][col+1] != -1 and map_shadow[row][col+1] != 3 and check != 1):
                            map_shadow[row][col+1] = 1
                        if(map_shadow[row][col-1] != -1 and map_shadow[row][col-1] != 3 and check != 3):
                            map_shadow[row][col-1] = 1
                        if(map_shadow[row+1][col] != -1 and map_shadow[row+1][col] != 3 and check != 2):
                            map_shadow[row+1][col] = 1
                        if(map_shadow[row-1][col] != -1 and map_shadow[row-1][col] != 3 and check != 4):
                            map_shadow[row-1][col] = 1
        return map_shadow
    #cap nhap bong pacman
    #cap nhap bong cua ma
    #cap nhap do an
    #bat dau tinh trong bong

    def Breadth_First_Search(self, list_data, start, food):
        queue = []
        queue.append([start])
        explored = []
        timeEscape = 0

        visited = [False] * len(list_data)
        #for i in range(0,len(list_data)):
        #    visited.append(False)

        visited[0] = True
        while len(queue) > 0:

            catch = queue.pop(0)
            explored.append(catch[-1])
            timeEscape += 1
            visited[catch[-1]] = True

            if catch[-1] == food:
                return catch, explored, timeEscape
            else:
                for x in list_data[catch[-1]]:
                    catch1 = catch.copy()
                    catch1.append(x)
                    if visited[x] == False:
                        visited[x] = True
                        queue.append(catch1)

        return [], explored, timeEscape

    def get_w_food(self, set_food):
        data_food = []
        for x in set_food:
            data_food.append(int(x[0]) + int((7)*int(x[1])))
        return data_food

    def check_around(self, map_shadow):
        X = []

        if(map_shadow[3][2] == ROAD):
            X.append([3, 2])
        if(map_shadow[3][4] == ROAD):
            X.append([3, 4])
        if(map_shadow[2][3] == ROAD):
            X.append([2, 3])
        if(map_shadow[4][3] == ROAD):
            X.append([4, 3])

        if len(X) != 0:
            i = random.randint(0,len(X) - 1)
            return X[i]
        return []

    def set_limit(self, map_shadow):
        limit = []
        flag = 0
        check = 0

        for row in range(7):
            for col in range(7):
                if(col == (3 - flag) or col == (3 + flag)):
                    if map_shadow[row][col] != WALL and map_shadow[row][col] != -1:
                        limit.append([row,col])

            if flag <= 3 and check == 0:
                flag += 1
            if flag > 3:
                flag = flag - 1
                check = 1
            if check == 1:
                flag -= 1

        return limit

    def path_level_3(self):

        #self.road = [[self.road[j, i] for j in range(len(self.road))] for i in range(len(self.road[0]))]
        map_shadow = self.check_monster(self.get_shadow()).copy()

        #map_shadow = [[map_shadow1[j][i] for j in range(len(map_shadow1))] for i in range(len(map_shadow1[0]))]

        set_food = self.get_food(map_shadow).copy()
        data_level3 = self.heuristic_allmap3(map_shadow, 7, 7).copy()

        start = 3 + 7 * 3
        data_food = self.get_w_food(set_food).copy()

        Y = []
        check = 0
        set_path_limit = []

        if not data_food:
            check = 1
            s_limit = self.set_limit(map_shadow)
            d_limit = self.get_w_food(s_limit)

            for i in d_limit:
                path_return, explored, timeEscape = self.Breadth_First_Search(data_level3,start, i)
                set_path_limit.append(path_return)

            list2 = [e for e in set_path_limit if e]
            if len(list2) > 1:
                list_path = sorted(list2, key = len)
                return list_path[0]
            else:
                print('end game')
                return []
            #X = self.check_around(map_shadow).copy()
            #goal = int(X[0]) + int((7)*int(X[1]))
            #path_return,explored,timeEscape = self.Breadth_First_Search(data_level3, start, goal)
            return path_return
        else:
            for i in data_food:
                path_return,explored,timeEscape = self.Breadth_First_Search(data_level3, start, i)
                Y.append(path_return)

        queue1 = [e for e in Y if e]
        if len(queue1) > 1:
            queue = sorted(queue1, key = len)
            return queue[0]

        if len(Y[0]) > 0:
            return Y[0]
        elif check != 1:
            return explored

    def level_3(self):
        path_temp = self.path_level_3().copy()
        if len(path_temp) > 1:
            path_temp.pop(0)
        else:
            return Direction.NO_MOVE.value # 5

        for i in path_temp:
            S_x = int(i // 7) - 3
            S_y = int(i % 7) - 3
            X = self.level_return(int(S_x), int(S_y))
            return X

    def level_return(self, S_x, S_y):
        if S_x == -1 and S_y == 0:
            return Direction.LEFT.value # 1
        elif  S_x == 1 and S_y == 0:
            return Direction.RIGHT.value # 2
        elif S_x == 0 and S_y == -1:
            return Direction.UP.value # 3
        elif S_x == 0 and S_y == 1:
            return Direction.DOWN.value # 4
        elif S_x == 0 and S_y == 0:
            return Direction.NO_MOVE.value # 5

# TODO: Chuyển hàm này vào class một class nào đó?
def level1_2(map_obj: map_graphic, pacman_moves: list):

    if map_obj.game_over[0]:
        return

    # Pacman ---------------------------
    if pacman_moves is not None or len(pacman_moves) > 0:
        if not map_obj.pacman_block.is_moving():
            ### XÓA DÒNG NÀY KHI GHÉP THUẬT TOÁN LEVEL 1
            ### pacman_moves.append(map_obj.pacman_block.random_moves(map_obj))
            if len(pacman_moves) > 0:
                has_moves = True
                direction = pacman_moves.pop(0)
                map_obj.pacman_block.turn(direction)

        map_obj.pacman_block.update(map_obj)

    hit_ghost_blocks = pygame.sprite.spritecollide(map_obj.pacman_block, map_obj.ghost_blocks,True)

    if len(hit_ghost_blocks) > 0:
        map_obj.game_over = True, 1

    # Pac đã đi hết đoạn đường cần đi
    if len(pacman_moves) == 0 and not map_obj.pacman_block.is_moving():
        map_obj.game_over = True, 2

    # Ghosts ----------------------------
    # for ghost in map_obj.ghost_objs:
    #    if not ghost.is_moving():
    #        ghost.turn(ghost.random_moves(map_obj))

    # map_obj.ghost_blocks.update(map_obj)

from pygame.math import Vector2 as vec
import winning
import multiprocessing as mp
def run_game1(grid_2d: np.ndarray, pacman_i, pacman_j, init_yet=False):

    # Initialize all imported pygame modules
    print("HELLO")
    if not init_yet:
        pygame.init()

    # get and set the system screen size
    screen_width, screen_height = set_screen_size(grid_2d)
    # print(total_screen_size(grid_2d))
    screen = pygame.display.set_mode((screen_width, screen_height + m.BLOCK_SIZE))

    # Set the current window caption
    pygame.display.set_caption(WINDOW_TITLE)

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    #Loop until the user clicks the close button.
    closed_window = False
    start_y = m.BLOCK_SIZE

    print(f"Block size: {m.BLOCK_SIZE}")
    print(f"start_y: {start_y}")

    map_obj = map_graphic(screen, grid_2d, start_y, pacman_i=pacman_i, pacman_j=pacman_j)
    pacman = Pacman(map_obj, vec(pacman_j, pacman_i))

    # ----------------------
    # Tính toán đường đi của pacman
    foods = np.where(map_obj.grid_2d == FOOD)
    should_go = True
    if not np.size(foods) == 0:
        food_x = foods[1][0]
        food_y = foods[0][0]

        nrow, _ = map_obj.grid_2d.shape
        path, _, _ = pacman.Graph_Search_A_Star(food_x, food_y)
        # Quyết định chọn đi hay không:
        if not FORCE_TO_MOVE:
            if len(path) - 1 <= SCORE_PER_FOOD:
                should_go = True
            if len(path) == 0:
                should_go = False
        pacman_moves = coord_to_direction(cell_to_coord(path, nrow), pacman_i, pacman_j)
    else:
        path = []
        pacman_moves = []
        should_go = False

    print(f"Path: {len(path)} | {path}")
    print(f"Moves: {len(pacman_moves)} | {pacman_moves}")

    # Game Loop -----------
    done = False
    game_over = False # 0: no moves, 1: hitted by ghost, 2:

    while not done:

        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done = True

        if should_go:
            level1_2(map_obj, pacman_moves)

        map_obj.draw_map()

        if map_obj.game_over[0] or not should_go:
            if not should_go:
                map_obj.game_over = True, 0

            game_over = True
            ctx = mp.get_context('spawn')
            p = ctx.Process(target=winning.winning_screen, args=(map_obj.game_over[1],
                            map_obj.pacman_block.score, map_obj.pacman_block.path_length))
            p.start()
            p.join()
            break

        # Tăng/giảm frame speed ở đây
        clock.tick(GAME_FPS)

    print(map_obj)
    print("SCORE: ", map_obj.pacman_block.score)

    if not should_go:
        pygame.time.wait(2000)

    if not init_yet:
        pygame.quit()

def level3(map_obj: map_graphic, pacman: Pacman, store_direction):

    if map_obj.game_over[0]:
        return

    # Pacman ---------------------------
    has_move = False
    if not map_obj.pacman_block.is_moving():
        pacman.road = map_obj.grid_2d.copy()
        cur_i, cur_j = map_obj.pacman_block.current_pos(map_obj)

        pacman.update_pos(vec(cur_j, cur_i))
        direction = pacman.level_3()
        store_direction.append(direction)
        if direction != Direction.NO_MOVE.value:
            map_obj.pacman_block.turn(direction)
            has_move = True
        else:
            map_obj.game_over = True, 2

    map_obj.pacman_block.update(map_obj)

    hit_ghost_blocks = pygame.sprite.spritecollide(map_obj.pacman_block, map_obj.ghost_blocks,True)

    if len(hit_ghost_blocks) > 0:
        map_obj.game_over = True, 1

    # Pac đã đi hết đoạn đường cần đi
    # if len(pacman_moves) == 0 and not map_obj.pacman_block.is_moving():
    #     map_obj.game_over = True, 2

    # Ghosts ----------------------------
    for ghost in map_obj.ghost_objs:
       if not ghost.is_moving() and has_move:
           ghost.turn(ghost.random_moves_around_root(map_obj))

    map_obj.ghost_blocks.update(map_obj)

def check_repeat(store):
    repeat = 0
    if len(store) > 8:
        for i in range(len(store)-4):
            if store[i] != store[i+1]:
                if (store[i], store[i+1]) == (store[i+2], store[i+3]):
                    repeat += 1
                else: repeat = 0
        if repeat == 6:
            return True
        else: return False
    else: return False

def run_game3(grid_2d: np.ndarray, pacman_i, pacman_j, init_yet=False):
    # Initialize all imported pygame modules
    print("HELLO")
    if not init_yet:
        pygame.init()

    # get and set the system screen size
    screen_width, screen_height = set_screen_size(grid_2d)
    # print(total_screen_size(grid_2d))
    screen = pygame.display.set_mode((screen_width, screen_height + m.BLOCK_SIZE))

    # Set the current window caption
    pygame.display.set_caption(WINDOW_TITLE)

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    #Loop until the user clicks the close button.
    closed_window = False
    start_y = m.BLOCK_SIZE

    print(f"Block size: {m.BLOCK_SIZE}")
    print(f"start_y: {start_y}")

    map_obj = map_graphic(screen, grid_2d, start_y, pacman_i=pacman_i, pacman_j=pacman_j)
    pacman = Pacman(map_obj, vec(pacman_j, pacman_i))

    # Game Loop -----------

    done = False
    game_over = False # 0: no moves, 1: hitted by ghost, 2:
    first_move = pacman.level_3()
    should_go = first_move != Direction.NO_MOVE.value

    store_direction = []

    while not done:

        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done = True

        if should_go:
            level3(map_obj, pacman, store_direction)

        if check_repeat(store_direction):
            map_obj.game_over = True, 0

        map_obj.draw_map()

        if map_obj.game_over[0] or not should_go:
            if not should_go:
                map_obj.game_over = True, 0

            game_over = True
            ctx = mp.get_context('spawn')
            p = ctx.Process(target=winning.winning_screen, args=(map_obj.game_over[1],
                            map_obj.pacman_block.score, map_obj.pacman_block.path_length))
            p.start()
            p.join()
            break

        # Tăng/giảm frame speed ở đây
        clock.tick(GAME_FPS)

    print(map_obj)
    print("SCORE: ", map_obj.pacman_block.score)

    if not should_go:
        pygame.time.wait(2000)

    if not init_yet:
        pygame.quit()

from readfile import *

def test(level,path):
    main(level, path)
    size, grid_2d, start = readfile('input/3/input1.txt')
    size = np.array(size)
    grid_2d = np.array(grid_2d)
    start = np.array(start)

    grid_2d, size, start = check_fence(grid_2d, size, start)
    print("MAP SIZE: ", len(grid_2d))
    run_game1(grid_2d, pacman_i=start[0], pacman_j=start[1])

def main(level, path):

    size, grid_2d, start = readfile(path)
    size = np.array(size)
    grid_2d = np.array(grid_2d)
    start = np.array(start)

    grid_2d, size, start = check_fence(grid_2d, size, start)
    print("MAP SIZE: ", len(grid_2d))
    print(f"LEVEL  : {level}")
    print(f"PATH   : {path}")
    level1_2_str = [1, 2, "1", "2", "LEVEL1", "LEVEL2"]
    level1_3_str = [3, "3", "LEVEL3"]

    if level in level1_2_str:
        print(f"PLAY {level}")
        run_game1(grid_2d, pacman_i=start[0], pacman_j=start[1])
    elif level in level1_3_str:
        print(f"PLAY {level}")
        run_game3(grid_2d, pacman_i=start[0], pacman_j=start[1])

import sys
if __name__ == '__main__':
    ##### VUI LÒNG CHUYỂN THƯ MỤC LÀM VIỆC Project_AI_BooMan_CSC14003 ĐỂ CÓ THỂ ĐỌC FILE ẢNH ĐƯỢC
    # main()
    argv = sys.argv
    if len(argv) <= 1:
        test(level = 3, path = "input/3/input1.txt")
    else:
        main(argv[1], argv[2])


import requests
from pandas import DataFrame
from bs4 import BeautifulSoup
import re
from datetime import datetime
import os
import urllib.request
import pandas as pd
import numpy as np

def main(): # 제목

    total_list_title = []
    total_list_date = []

    start = 11
    while True:

        if start != 1921 :

            # 삼성
            url = 'https://search.naver.com/search.naver?where=news&sm=tab_pge&query=%EC%82%BC%EC%84%B1%EC%A0%84%EC%9E%90&sort=0&photo=3&field=0&pd=3&ds=2021.02.01&de=2021.02.28&cluster_rank=24&mynews=0&office_type=0&office_section_code=0&news_office_checked=&nso=so:r,p:from20210201to20210228,a:all&start={}'

            headers = {'User-Agent': 'Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57'}

            response = requests.get(url.format(start))

            soup = BeautifulSoup(response.text, 'html.parser')

            # 기사 헤드라인 추출
            title_list = [title['title'] for title in soup.find_all('a', attrs={'class' : 'news_tit'})]

            # 날짜 추출
            dates = [date.get_text() for date in soup.find_all('span', attrs={'class':'info'})]
            date_list = dates[1::2] # 다른 부분까지 같이 추출되기 때문에

            total_list_title.append(title_list)
            total_list_date.append(date_list)

            start += 10
            print('크롤링 중.......')

        else:
            print('크롤링 완료!')

            total_list_date = [element for array in total_list_date for element in array]
            total_list_title = [element for array in total_list_title for element in array]
            print(total_list_date)

            df = pd.DataFrame({'title':total_list_title, 'date':total_list_date}, columns=['title', 'date'])

            # DataFrame으로 변환 후 데이터 전처리
            def clean_text(text): # 특수 문자 제거
                cleaned_text = re.sub('[-=+,#/\?:^$.@*\"*~&%`!\\`|\(\)\[\]\<\>`\'...>]', '', text)
                cleaned_text = re.sub("[^가-힣ㄱ-ㅎㅏ-ㅣ\\s]", " ", text)
                return cleaned_text

            df['title'] = df['title'].dropna(axis=0).apply(lambda x : clean_text(x))
            df['date'] = df['date'].dropna(axis=0)

            df = df.sort_values(by=['date'], axis=0) # 날짜 순으로 정렬

            print(df)

            # 엑셀 파일로 데이터 프레임 저장
            date_time = str(datetime.now())
            date_time = date_time[:date_time.rfind(':')].replace(' ', '_')
            date_time = date_time.replace(':','시') + '분'

            folder_path = os.getcwd()
            xlsx_file_name = '네이버뉴스_2월{}.xlsx'.format(date_time)

            df.to_excel(xlsx_file_name)

            print('엑셀 저장 완료 | 경로 : {}\\{}'.format(folder_path, xlsx_file_name))
            os.startfile(folder_path)

            break

main()


import tkinter as tk
from tkinter import messagebox
import pyodbc

class CrudApp:
    def __init__(self, root):
        self.root = root
        self.root.title("CRUD App")

        self.id_var = tk.StringVar()
        self.nome_var = tk.StringVar()
        self.endereco_var = tk.StringVar()
        self.telefone_var = tk.StringVar()
        self.email_var = tk.StringVar()

        # Frames
        frame_form = tk.Frame(self.root)
        frame_form.pack(pady=20)

        frame_listbox = tk.Frame(self.root)
        frame_listbox.pack()

        # Labels e Entry para o formulário
        tk.Label(frame_form, text="ID:").grid(row=0, column=0, sticky="w")
        tk.Entry(frame_form, textvariable=self.id_var, state="readonly").grid(row=0, column=1)

        tk.Label(frame_form, text="Nome:").grid(row=1, column=0, sticky="w")
        tk.Entry(frame_form, textvariable=self.nome_var).grid(row=1, column=1)

        tk.Label(frame_form, text="Endereço:").grid(row=2, column=0, sticky="w")
        tk.Entry(frame_form, textvariable=self.endereco_var).grid(row=2, column=1)

        tk.Label(frame_form, text="Telefone:").grid(row=3, column=0, sticky="w")
        tk.Entry(frame_form, textvariable=self.telefone_var).grid(row=3, column=1)

        tk.Label(frame_form, text="Email:").grid(row=4, column=0, sticky="w")
        tk.Entry(frame_form, textvariable=self.email_var).grid(row=4, column=1)

        # Botões
        tk.Button(frame_form, text="Novo", command=self.novo_registro).grid(row=5, column=0, pady=10)
        tk.Button(frame_form, text="Salvar", command=self.salvar_registro).grid(row=5, column=1, pady=10)
        tk.Button(frame_form, text="Excluir", command=self.excluir_registro).grid(row=5, column=2, pady=10)

        # Listbox para mostrar os registros
        self.listbox = tk.Listbox(frame_listbox, width=50, height=10)
        self.listbox.pack(pady=20)
        self.listbox.bind("<<ListboxSelect>>", self.preencher_campos)

        # Conectar ao SQL Server
        self.conn = pyodbc.connect('Driver={SQL Server};'
                                   'Server=LAPTOP-5M9EP0AR\SQLEXPRESS;'
                                   'Database=PythonSQL;'
                                   'Trusted_Connection=yes;')

        # Carregar dados na Listbox
        self.carregar_dados()

    def carregar_dados(self):
        cursor = self.conn.cursor()
        cursor.execute("SELECT * FROM TblClientes")
        rows = cursor.fetchall()

        self.listbox.delete(0, tk.END)

        for row in rows:
            self.listbox.insert(tk.END, f"{row.Nome} - {row.Telefone}")

    def preencher_campos(self, event):
        selected_index = self.listbox.curselection()
        if selected_index:
            selected_data = self.listbox.get(selected_index[0])
            # Você precisará implementar a lógica para obter o ID do registro selecionado
            # e buscar os detalhes desse registro no banco de dados.

            # Exemplo:
            # id_selecionado = obter_id_do_registro(selected_data)
            # dados_registro = obter_dados_do_banco(id_selecionado)
            # self.id_var.set(dados_registro['id'])
            # self.nome_var.set(dados_registro['nome'])
            # ... e assim por diante.

    def novo_registro(self):
        self.limpar_campos()

    def salvar_registro(self):
        # Lógica para validar os campos (por exemplo, verificar se todos estão preenchidos)
        if not self.campos_validos():
            messagebox.showerror("Erro", "Todos os campos devem ser preenchidos.")
            return

        # Lógica para inserir ou atualizar no banco de dados
        # ...

        # Após salvar, carregue os dados novamente na Listbox
        self.carregar_dados()

        messagebox.showinfo("Sucesso", "Registro salvo com sucesso.")

    def excluir_registro(self):
        # Verificar se o usuário realmente deseja excluir
        resposta = messagebox.askyesno("Confirmação", "Deseja realmente excluir este registro?")
        if resposta == tk.YES:
            # Lógica para excluir no banco de dados
            # ...

            # Após excluir, carregue os dados novamente na Listbox
            self.carregar_dados()

            messagebox.showinfo("Sucesso", "Registro excluído com sucesso.")

    def campos_validos(self):
        # Lógica para validar campos (por exemplo, verificar se estão preenchidos)
        return bool(self.nome_var.get() and self.endereco_var.get() and
                    self.telefone_var.get() and self.email_var.get())

    def limpar_campos(self):
        self.id_var.set("")
        self.nome_var.set("")
        self.endereco_var.set("")
        self.telefone_var.set("")
        self.email_var.set("")

root = tk.Tk()
app = CrudApp(root)
root.mainloop()

Treinando Python 13-10-2023_Sistema crud com base no sql server/app.py


from itertools import combinations
import tensorflow as tf
from base64 import decodebytes
from io import BytesIO
from skimage.measure import regionprops
from skimage.measure import label as mlabel
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.resnet50 import preprocess_input
from skimage.io import imread
import numpy as np

# import segmentation as seg
import matplotlib.pyplot as plt
from gramex.config import random_string, variables
import geopandas as gpd
from shapely.geometry import Polygon

def img_from_b64(s):
    s = s.split(",")[1]
    bytedata = decodebytes(s.encode())
    return imread(BytesIO(bytedata))

def plot_attention(
    image,
    prediction,
    windows_shape,
    prob_threshold=0.5,
    prob_alpha=0.5,
    cmap=plt.cm.Reds,
    show=False,
):
    prob_max = tf.reduce_max(prediction, axis=1).numpy().reshape(*windows_shape)
    fig, ax = plt.subplots()
    ax.imshow(image, extent=[0, image.shape[1], 0, image.shape[0]])
    if prob_threshold > 0:
        mask = prob_max > prob_threshold
    else:
        mask = prob_max
    ax.imshow(
        mask, alpha=prob_alpha, cmap=cmap, extent=[0, image.shape[1], 0, image.shape[0]]
    )
    ax.set_axis_off()
    if show:
        plt.show()
    return mask

def get_bboxes(mask, threshold=0.7, plot=False):
    bbox = []
    for i in range(mask.shape[-1]):
        rp = regionprops(mlabel(mask[..., i].numpy().astype(int)))
        class_box = []
        for region in rp:
            class_box.append(region.bbox)
            if plot:
                minrow, mincol, maxrow, maxcol = region.bbox
                plt.hlines(minrow, mincol, maxcol)
                plt.hlines(maxrow, mincol, maxcol)
                plt.vlines(mincol, minrow, maxrow)
                plt.vlines(maxcol, minrow, maxrow)
        bbox.append(class_box)
        if plot:
            plt.show()
    return bbox

def compare_intersection(x, y):
    i = x.geometry.intersection(y.geometry).area
    smaller = min([x, y], key=lambda g: g.geometry.area)
    if i >= (smaller.geometry.area / 2):
        return smaller.name
    return False

def drop_overlapping_classes(boxes):
    d = []
    for box, label in zip(boxes, variables["LENC"]):
        for top, left, bottom, right in box:
            d.append(
                {
                    "label": label,
                    "geometry": Polygon(
                        [(left, top), (right, top), (right, bottom), (left, bottom)]
                    ),
                }
            )
    d = gpd.GeoDataFrame(d)
    to_drop = []
    for i, j in combinations(d.index, 2):
        smaller = compare_intersection(d.iloc[i], d.iloc[j])
        if smaller:
            to_drop.append(smaller)
    to_drop = set(to_drop)
    d.drop(to_drop, axis=0, inplace=True)
    dropped = []
    for label in variables['LENC']:
        xdf = d[d['label'] == label]
        bound = xdf.geometry.bounds.values
        dropped.append(np.c_[bound[:, :2][:, ::-1], bound[:, 2:][:, ::-1]])
    return dropped

def get_pre_annotations(x, model, bbox_threshold=0.7, plot=False):
    payload = []
    x.shape = (1,) + x.shape
    x = preprocess_input(x)
    height, width, boxes = model.predict(x, batch_size=32)
    boxes = drop_overlapping_classes(boxes)
    for i, _boxes in enumerate(boxes):
        if i != 0:  # Don't predict bars for now
            if len(_boxes) > 0:
                for rowmin, colmin, rowmax, colmax in _boxes:
                    payload.append(
                        {
                            "value": {
                                "x": colmin / width * 100,
                                "y": rowmin / height * 100,
                                "width": (colmax - colmin) / width * 100,
                                "height": (rowmax - rowmin) / height * 100,
                                "rotation": 0,
                                "rectanglelabels": [variables["LENC"][i]],
                            },
                            "id": random_string(6),
                            "from_name": "tag",
                            "to_name": "img",
                            "type": "rectanglelabels",
                        }
                    )
    return payload

if __name__ == "__main__":
    vgg = load_model("vgg16-validated-five-classes.h5")
    x = imread("/tmp/medistrava.png")
    get_bboxes(x, vgg)


'''
Subclass of Solr classes enabling passing of local cert with Solr requests.
Configuration looks like:

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': '${SOME_MODULE}.solr_backend.SecureSolrEngine',
        'URL': ${SOME_URL},
        'CERT': 'ssl/test.pem',
        'KWARGS': {
            'verify': False,  # For self-signed certificates
        },
    },
}
'''
from haystack.backends.solr_backend import SolrSearchBackend, SolrEngine
from pysolr import Solr

class SecureSolr(Solr):
    '''
    https://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification
    '''
    def __init__(self, url, cert, **kwargs):
        self.cert = cert
        super().__init__(url, **kwargs)

    def _send_request(self, method, path="", body=None, headers=None, files=None):
        '''
        Had to override this entire method from source to add the cert kwarg
        to the request. Source: https://github.com/django-haystack/pysolr/blob/560a94bd16c782c6b69af6a461d4b64c38b079a2/pysolr.py#L387-L471
        '''
        url = self._create_full_url(path)
        method = method.lower()
        log_body = body

        if headers is None:
            headers = {}

        if log_body is None:
            log_body = ""
        elif not isinstance(log_body, str):
            log_body = repr(body)

        self.log.debug(
            "Starting request to '%s' (%s) with body '%s'...",
            url,
            method,
            log_body[:10],
        )
        start_time = time.time()

        session = self.get_session()

        try:
            requests_method = getattr(session, method)
        except AttributeError:
            raise SolrError("Unable to use unknown HTTP method '{0}.".format(method))

        # Everything except the body can be Unicode. The body must be
        # encoded to bytes to work properly on Py3.
        bytes_body = body

        if bytes_body is not None:
            bytes_body = force_bytes(body)
        try:
            resp = requests_method(
                url,
                data=bytes_body,
                headers=headers,
                files=files,
                timeout=self.timeout,
                auth=self.auth,
                cert=self.cert
            )
        except requests.exceptions.Timeout as err:
            error_message = "Connection to server '%s' timed out: %s"
            self.log.exception(error_message, url, err)  # NOQA: G200
            raise SolrError(error_message % (url, err))
        except requests.exceptions.ConnectionError as err:
            error_message = "Failed to connect to server at %s: %s"
            self.log.exception(error_message, url, err)  # NOQA: G200
            raise SolrError(error_message % (url, err))
        except HTTPException as err:
            error_message = "Unhandled error: %s %s: %s"
            self.log.exception(error_message, method, url, err)  # NOQA: G200
            raise SolrError(error_message % (method, url, err))

        end_time = time.time()
        self.log.info(
            "Finished '%s' (%s) with body '%s' in %0.3f seconds, with status %s",
            url,
            method,
            log_body[:10],
            end_time - start_time,
            resp.status_code,
        )

        if int(resp.status_code) != 200:
            error_message = "Solr responded with an error (HTTP %s): %s"
            solr_message = self._extract_error(resp)
            self.log.error(
                error_message,
                resp.status_code,
                solr_message,
                extra={
                    "data": {
                        "headers": resp.headers,
                        "response": resp.content,
                        "request_body": bytes_body,
                        "request_headers": headers,
                    }
                },
            )
            raise SolrError(error_message % (resp.status_code, solr_message))

        return force_unicode(resp.content)

class SecureSolrSearchBackend(SolrSearchBackend):
    '''
    Issue secure Solr requests.
    Source: https://github.com/django-haystack/django-haystack/blob/master/haystack/backends/solr_backend.py
    '''

    def __init__(self, connection_alias, **connection_options):
        super().__init__(connection_alias, **connection_options)

        self.conn = SecureSolr(
            connection_options['URL'],
            connection_options['CERT'],
            timeout=self.timeout,
            **connection_options.get("KWARGS", {})
        )

class SecureSolrEngine(SolrEngine):
    backend = SecureSolrSearchBackend


import csv
from difflib import SequenceMatcher
from itertools import chain

import matplotlib.pyplot as plt
from unidecode import unidecode
import networkx as nx

class EstacaoMetro:
    def __init__(self, id, nome, linha, lat, lon):
        self.id = id
        self.nome = nome
        self.linha = linha
        self.lat = float(lat)
        self.lon = float(lon)

    def get_id(self):
        return self.id

    def get_nome(self):
        return self.nome

    def get_linha(self):
        return self.linha

    def get_lat(self):
        return self.lat

    def get_lon(self):
        return self.lon

    def get_coordenadas(self):
        return self.lat, self.lon

    def __str__(self):
        return f"EstacaoMetro(id={self.id}, nome={self.nome}, linha={self.linha}, lat={self.lat}, lon={self.lon})"

def criar_estacoes(lista_estacoes_file="resources/estacoes.csv", estacoes_coordenadas_file='resources/stops.txt'):
    lista_estacoes = []
    estacoes_coordenadas = []
    final_list = []

    with open(lista_estacoes_file, 'r', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile, delimiter=';')
        next(reader)
        for row in reader:
            lista_estacoes.append([row[0], row[1]])

    with open(estacoes_coordenadas_file, 'r', encoding='utf-8') as txfile:
        reader = csv.reader(txfile)
        next(reader)
        for row in reader:
            row[1] = row[1].replace('"', '')
            estacoes_coordenadas.append([row[0], row[1], row[3], row[4]])

        for i in range(len(lista_estacoes)):
            for j in range(len(estacoes_coordenadas)):
                if similar(lista_estacoes[i][1], estacoes_coordenadas[j][1]) > 0.8:
                    final_list.append(
                        EstacaoMetro(estacoes_coordenadas[j][0], lista_estacoes[i][1], lista_estacoes[i][0],
                                     estacoes_coordenadas[j][2],
                                     estacoes_coordenadas[j][3]))
                    break

    return final_list

def print_estacoes(estacoes):
    for i in range(len(estacoes)):
        print(estacoes[i])

def similar(a, b):
    return SequenceMatcher(None, normalize(a), normalize(b)).ratio()

def normalize(text):
    return unidecode(text).lower()

def build_station_network(filename='resources/estacoes_edges_resolved.csv'):
    t_map = nx.Graph()

    with open(filename, 'r', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        next(reader)
        for row in reader:
            (source, destination, time, color) = (row[0], row[1], row[2], row[3])
            color = color.replace('\n', '')
            t_map.add_edge(source, destination, weight=float(time), color=color)

    return t_map

def draw_network(G, edges, color='black', new_plot=True, title='Metro SP', label=None):
    locations = {}
    nodes = list(chain(*edges))
    for station in criar_estacoes():
        locations[station.get_id()] = station.get_coordenadas()

    if new_plot:
        plt.figure()
        plt.title(title)
    if label is not None:
        nx.draw_networkx_labels(G, pos=locations, labels=label, font_size=6)
    nx.draw_networkx_edges(G, pos=locations, edge_color=color, edgelist=edges)
    nx.draw_networkx_nodes(G, pos=locations, node_color='black',
                           node_size=20, nodelist=nodes)
    plt.show()

def all_paths(G, source, target, cutoff=None):
    paths = []
    for path in nx.all_simple_paths(G, source=source, target=target, cutoff=cutoff):
        paths.append(path)
    return paths

def path_length(G, x):
    length = 0
    for i in range(len(x) - 1):
        length += G[x[i]][x[i + 1]]['weight']
    return length

def shortest_paths(G, cutoff=None):
    shortest_paths_list = []

    for source in G.nodes():
        for target in G.nodes():
            if source != target:
                paths = all_paths(G, source, target, cutoff)
                shortest_path = min(paths, key=lambda x: path_length(G, x))
                shortest_paths_list.append(shortest_path)

    with open('resources/shortest_paths.csv', 'w', encoding='utf-8', newline='') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow(['source', 'target', 'path'])
        for path in shortest_paths_list:
            writer.writerow([path[0], path[-1], path])


import requests
import json

port = 5005
url = f'http://127.0.01:{port}/'

def get_body():
    return {'input': 'This Is The sTrInG we want in lowEr CAsE.'}

def main():
    body = get_body()
    print('This is the input to our API.')
    print(body)

    # We have to serialize the body of our post request before we can send.
    body_str = json.dumps(body)

    # Don't believe me? Uncomment the line below this one!
    # res = requests.post(url, data=body)

    # This is us passing the serialized body.
    res = requests.post(url, data=body_str)
    print('This is the output from the API.')
    print(res.json())

if __name__ == '__main__':
    main()