import asyncio
from datetime import datetime
from typing import Any

import httpx
from loguru import logger

from app.database.models import Post, Comment, Group
from app.database.services import PostService, CommentService, GroupService
from app.libs.ml_lib import SentimentalAnalysisModel
from app.loader import session
from app.settings.config import VK_TOKEN

class VkParser:
    def __init__(self) -> None:
        self.url = "https://api.vk.com/method/"
        self.wall_get = self.url + "wall.get"
        self.wall_getComments = self.url + "wall.getComments"
        self.groups_getGroup = self.url + "groups.getById"
        self.groups_getMembers = self.url + "groups.getMembers"
        self.vk_version = 5.131

        # Инициализация модели нейросети для анализа тональности текста
        self.sentiment_model = SentimentalAnalysisModel()

    async def get_group_byid(self, group: str) -> bool:
        """
        Получение данных группы
        """
        logger.info(f"Начался сбор групп")

        self.group_metadata: list[dict[str, Any]] = []
        self.group_update_metadata: list[dict[str, Any]] = []
        params_get_group = {
            "group_id": group,
            "access_token": VK_TOKEN,
            "count": 0,
            "v": self.vk_version,
        }

        try:
            get_group = httpx.get(self.groups_getGroup, params=params_get_group).json()["response"][0]
        except KeyError:
            logger.warning("Ошибка парсинга группы (Ошибка get-запроса: невозможно собрать данные)")
            return False

        params_group_members = {
            "group_id": get_group["id"],
            "access_token": VK_TOKEN,
            "count": 0,
            "v": self.vk_version,
        }

        get_group_members = httpx.get(self.groups_getMembers, params=params_group_members).json()["response"]

        group_data = {
            "group_id": get_group["id"],
            "group_name": get_group["name"],
            "screen_name": get_group["screen_name"],
            "group_members": get_group_members["count"],
        }
        await self.insert_group_data_db(group_data)
        return True

    async def insert_group_data_db(self, group_data: dict[str, Any]) -> None:
        """
        Вставляет полученные данные в таблицу groups
        """
        service_group = GroupService(Group)
        if session.query(Group).filter(Group.group_id == group_data["group_id"]).first() is None:
            self.group_metadata.append(group_data)
            service_group.add_all(self.group_metadata)
        else:
            self.group_update_metadata.append(group_data)
            service_group.update_all(self.group_update_metadata)

    async def get_posts(self) -> None:
        """
        Получение постов по группе
        """
        logger.info(f"Начался сбор постов")

        self.posts_metadata: list[dict[str, Any]] = []
        self.posts_update_metadata: list[dict[str, Any]] = []
        groups = self.group_metadata + self.group_update_metadata

        for group in groups:
            params = {
                "domain": group["screen_name"],
                "count": 60,
                "access_token": VK_TOKEN,
                "v": self.vk_version,
            }
            response = httpx.get(self.wall_get, params=params).json()

            for post in response["response"]["items"]:
                post_data = {
                    "post_id": post["id"],
                    "owner_id": post["owner_id"],
                    "group_id": group["group_id"],
                    "likes": post["likes"]["count"],
                    "quantity_comments": post["comments"]["count"],
                    "reposts": post["reposts"]["count"],
                    "views": post["views"]["count"],
                    "photo": True if "attachments" in post else False,
                    "post_text": post["text"],
                    "date": datetime.fromtimestamp(post["date"]),
                }
                if session.query(Post).filter(Post.post_id == post_data["post_id"]).first() is None:
                    self.posts_metadata.append(post_data)
                else:
                    self.posts_update_metadata.append(post_data)
        await self.insert_posts_data_db()

    async def insert_posts_data_db(self):
        """
        Вставляет полученные данные в таблицу posts
        """
        service_post = PostService(Post)
        service_post.add_all(self.posts_metadata)
        service_post.update_all(self.posts_update_metadata)

    async def get_wall_comments(self) -> None:
        """
        Парсинг комментариев поста
        И занесение данных в бд
        """
        logger.info(f"Начался сбор комментариев")

        self.comments_metadata: list[dict[str, Any]] = []
        self.comments_update_metadata: list[dict[str, Any]] = []
        posts = self.posts_metadata + self.posts_update_metadata
        codes = await self.getcomments_methods_vkscript(posts)
        service_post = PostService(Post)

        for num, code in enumerate(codes, start=1):
            params = {
                "count": 100,
                "code": f"return [{code}];",
                "sort": "desc",
                "access_token": VK_TOKEN,
                "v": self.vk_version,
            }

            # Обход ограничение на 3 запроса в секунду
            # Лучше не убирать, если вы хотите собирать больше постов
            if num % 3 == 0:
                await asyncio.sleep(1)

            response = httpx.get(self.url + "execute/", params=params).json()

            for post in response["response"]:
                tones_post = {
                    "post_id": None,
                    "positive_comments": 0,
                    "negative_comments": 0,
                }
                for comment in post["items"]:
                    if len(comment["text"]) > 0:
                        tone = self.sentiment_model.set_tone_comment([comment["text"]])
                        comment_data = {
                            "comment_id": comment["id"],
                            "post_id": comment["post_id"],
                            "text": comment["text"],
                            "tone": tone,
                        }
                        tones_post["post_id"] = comment_data["post_id"]

                        if (
                            session.query(Comment).filter(Comment.comment_id == comment_data["comment_id"]).first()
                        ) is None:
                            if tone == "positive":
                                tones_post["positive_comments"] += 1
                            elif tone == "negative":
                                tones_post["negative_comments"] += 1
                            self.comments_metadata.append(comment_data)
                        else:
                            self.comments_update_metadata.append(comment_data)

                # Обновление количества позитивных/негативных комментариев поста
                if tones_post["post_id"] is not None:
                    service_post.update_tonal_comments(tones_post)
        await self.insert_comments_data_db()

    async def insert_comments_data_db(self):
        """
        Вставляет полученные данные в таблицу comments
        """
        service_comment = CommentService(Comment)
        service_comment.add_all(self.comments_metadata)
        service_comment.update_all(self.comments_update_metadata)

    async def getcomments_methods_vkscript(self, posts):
        """
        Функция возвращает список с методами для получения комментариев
        Методы пишутся на языке VKScript (Документация VK)
        """
        execute_methods = ""
        methods = []
        for number, post in enumerate(posts, start=1):
            if number % 25 != 0:
                execute_methods += f"""API.wall.getComments({{"owner_id": {post['owner_id']}, count: {100}, "post_id": {post['post_id']}}}), """
            else:
                execute_methods += f"""API.wall.getComments({{"owner_id": {post['owner_id']}, count: {100}, "post_id": {post['post_id']}}}), """
                methods.append(execute_methods)
                execute_methods = ""
        methods.append(execute_methods)
        return methods

    async def run_vk_parser(self, group: str) -> None | bool:
        tasks = [self.get_group_byid(group), self.get_posts(), self.get_wall_comments()]
        result_tasks = await asyncio.gather(*tasks)
        if False in result_tasks:
            return False
        else:
            return True


#ohaohaoha

import json
import socket
from base64 import b64decode
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Util.Padding import unpad
from time import sleep

HOST = '0.0.0.0'
PORT = 50013

TICK = 0.1
c = ''

def rsa(q):
    #print(q)

    try:
        key = RSA.importKey(q['key'])
        cipher = PKCS1_OAEP.new(key)
        s = cipher.decrypt(b64decode(q['data']))
        print(json.loads(s)['extra'])

    except Exception as e:
        print('RSA_PKCS1_OAEP_2048 error', e)

def aes(q):
    #print(q)

    try:
        key = b64decode(q['key'])
        iv = b64decode(q['iv'])
        cipher = AES.new(key, AES.MODE_CBC, iv)
        s = unpad(cipher.decrypt(b64decode(q['data'])), cipher.block_size)
        s = s.decode('utf-8').strip()
        print(json.loads(s)['extra'])

    except Exception as e:
        print('AES_CBC error', e)

def b64(q):
    #print(q)

    try:
        data = b64decode(q['data'])
        print(json.loads(data)['extra'])

    except Exception as e:
        print('b64 error', e)

def plain(q):
    #print(q)

    try:
        print(q['data']['extra'])

    except Exception as e:
        print('plain', e)

def decipher(j):
    try:
        q = json.loads(j)
        m = q['method']
    except:
        print('invalid json:', j)
        return

    if m == 'RSA_PKCS1_OAEP_2048':
        rsa(q)
    elif m == 'AES_CBC':
        aes(q)
    elif m == 'b64':
        b64(q)
    elif m == 'plain':
        plain(q)
    else:
        print('unsupported method')

def work():
    global c
    i = c.find('\n')

    if i == -1:
        try:
            c += s.recv(1024).decode('utf-8')
        except:
            pass # no data yet
    else:
        nextQuery = ''

        if i != len(c) - 1:
            nextQuery = c[i + 1:]

        decipher(c[0:i])
        c = nextQuery

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
s.connect((HOST, PORT))
s.setblocking(0)

try:
    while True:
        work()
        sleep(TICK)

except KeyboardInterrupt:
    print('finished')

except Exception as e:
    print('error:', e)

s.close()


"""

"""
import datetime

import requests
import json
from requests.models import Response

from Constants import Constants

"""
def build_simple_api_call( url: str, id: int ) -> str:
    return url + str(id)

def get_team_info( id: int ) -> str:
    team_info_url: str = build_simple_api_call( Constants.URL_TEAM_INFO, id )
    response: Response = requests.request( Constants.HTTP_GET, team_info_url, headers=Constants.HEADERS )
    return json.dumps( response.json() )
"""

# ROUTINES TO READ GAME DATA FROM API AND SAVE IT TO DISK
def build_date_api_call( url: str, year: int, month: int, day: int ) -> str:
    # ASSERTIONS ABOUT INPUT
    assert( year < 2100 )
    assert( year > 0 )
    assert( year < 100 or year > 1948 )

    assert( month > 0 )
    assert( month < 13 )

    assert( day > 0 )
    assert( day < 32 )

    # TURN YEAR INTO A STRING
    year_str: str = str(year)
    if ( year < 99 ):
        year_str = "20" + year_str

    # TURN MONTH INTO A STRING
    month_str: str = str(month)
    if ( month < 10 ):
        month_str = "0" + month_str

    # TURN DAY INTO A STRING
    day_str: str = str(day)
    if ( day < 10 ):
        day_str = "0" + day_str

    date_str: str = year_str + "-" + month_str + "-" + day_str
    return url + date_str

def get_games_for( year: int, month: int, day: int ):
    games_by_day_url: str = build_date_api_call( Constants.URL_GAMES_ON_DAY, year, month, day )
    response: Response = requests.request( Constants.HTTP_GET, games_by_day_url, headers=Constants.HEADERS)
    return json.dumps( response.json() )

def get_yesterdays_games():
    yes: datetime = datetime.datetime.today() - datetime.timedelta(days=1)
    return get_games_for( yes.year, yes.month, yes.day )

def save_yesterdays_games_to_disk( file_contents: str ):
    yes: datetime = datetime.datetime.today() - datetime.timedelta(days=1)
    filename: str = build_date_api_call( Constants.GAME_DATA_DIR, yes.year, yes.month, yes.day ) + ".json"
    file = open( filename, "w+" )
    file.write(file_contents)
    file.close()

# ROUTINES TO READ AN RSS FILE OFF THE WEB AND SAVE IT TO DISK
def read_rss_url( url: str ):
    response: Response = requests.request( Constants.HTTP_GET, url )
    return response.text

def save_rss_to_disk( info: str, url: str, idx: int ):
    yes: datetime = datetime.datetime.today()
    filename: str = build_date_api_call( Constants.RSS_DATA_DIR, yes.year, yes.month, yes.day ) +\
                                    "_" + str(idx) +\
                                    ".rss"

    file = open( filename, "w+" )
    file.write( info )
    file.close()

count: int = 0
for url in Constants.RSS_URLS:
    count += 1
    text: str = read_rss_url( url )
    save_rss_to_disk( text, url, count )

# GET DATA FOR YESTERDAY'S GAMES
output: str = get_yesterdays_games()
save_yesterdays_games_to_disk(output)

# READ FROM RSS FEEDS


# Functions
import choices
# To work with MySQL
import pymysql
# For the "press any key" in option 1
from getkey import getkey
# To work with months
import datetime
# To work with Neo4j
from neo4j import exceptions

# Main function
def main():
    display_menu()

    while True:
        choice = input("\nChoice: ")

        # Choice ONE
        if (choice == "1"):
            # Set the offset for MySQL cypher (next 2 employees)
            offset = 0

            print ("EMPLOYEES AND DEPARTMENTS")
            print ("--------------------------")

            x = choices.one(offset)
            for i in x:
                print (i["name"], "|", i["d.name"])
            print("-- Quit (q) --")

            # Loop when pressing any key other then "q"
            while True:
                key = getkey()
                # If any key is pressed
                if key != "q":
                    offset += 2
                    x = choices.one(offset)
                    for i in x:
                        print (i["name"], "|", i["d.name"])
                    print("-- Quit (q) --")
                # If "q" is pressed
                else:
                    offset = 0
                    display_menu()
                    break

        # Choice TWO
        elif (choice == "2"):
            print ("SALARY DETAILS")
            print ("--------------------------")

            # Prompt the input
            eid = str(input("Enter Employee ID (EID): "))

            x = choices.two(eid)
            # If non-existing EID is entered
            for i in x:
                if i["eid"] == None:
                    print("Minimum", "|", "Avarage ", "|", "Maximum")
                    print("Non existing EID entered. Please try again.")
            # If existign EID is entered
                else:
                    for i in x:
                        print("\nSalary Details For Employee: " + eid)
                        print("Employee name: " + i["name"])
                        print("----------------------------")
                        print("Minimum", "|", "Avarage ", "|", "Maximum")
                        print("----------------------------")
                        print(i["Minimum"], "|", i["Avarage"], "|", i["Maximum"],"\n")

        # Choice THREE
        elif (choice == "3"):
            print ("EMPLOYEES BY MONTH OF BIRTH")
            print ("---------------------------")

            # Create a list of month names
            month_names = []
            for i in range(1,13):
                month_names.append (((datetime.date(2022, i, 1).strftime('%b'))).upper())
            # Create a list of month numbers
            month_num = []
            for i in range(1,13):
	            month_num.append(str(i)) # Convert to string (because input will be str)
            # Prompt for input
            month = (input("Enter Month of Birth: ")).upper()

            # Loop while the input isn't in either of 2 created lists
            while (month not in month_num) and (month not in month_names):
               month = (input("Enter Month of Birth: ")).upper() # Upper to match lower and upper case
            # If input is a number
            if month in month_num:
                x = choices.three(month)
                for i in x:
                        print(i["eid"], "|", i["name"], "|", i["dob"])
            # If input is a month name
            elif month in month_names:
                x = choices.three(month)
                for i in x:
                        print(i["eid"], "|", i["name"], "|", i["dob"])

        # Choice FOUR
        elif (choice == "4"):

            print ("ADD NEW EMPLOYEE")
            print ("----------------")

            # Prompt for inputs
            employeeID = input ("EID: ")
            name = input ("Name: ")
            employeeDob = input ("DOB (YYYY-MM-DD): ")
            deptID = (input ("Dept ID: ")).upper()

            # If all input is valid run the function
            try:
                choices.four(employeeID, name, employeeDob, deptID)
            # ERROR handling
                # If EID or Dept ID input is invalid
            except pymysql.err.IntegrityError as e:
                if e.args[1] == ("Duplicate entry 'e01' for key 'employee.PRIMARY'"):
                    print("*** ERROR ***:", employeeID, " already exists")
                else:
                    print ("*** ERROR ***: ", deptID, "does not exist")
                # If DOB inout is invalid
            except pymysql.err.OperationalError as e:
                print ("*** ERROR ***: Invalid DOB")
                # If EID or Dept ID inputs are too long (limited in SQL database)
            except pymysql.err.DataError as e:
                if e.args[1] == ("Data too long for column 'did' at row 1"):
                    print("*** ERROR ***: ", deptID, "does not exist")
                else:
                    print("*** ERROR ***: EID too long. Use 3 characters max.")
                # Any other errors
            except Exception as e:
                print ("***ERROR***")

        # Choice FIVE
        elif (choice == "5"):
            print ("DEPARTMENTS MANAGED BY EMPLOYEE")
            print ("-------------------------------")

            # Neo4j function
            n = choices.five()
            print ("\nDepartment   |   Budget")

            # Loop through Neo4j results
            for i in n:
                # MySQL function - pass the results from Neo4j to MySQL
                x = choices.fiveSQL(i)
                for a in x:
                    print (a["did"], " | ", a["budget"])

        # Choice SIX
        elif (choice == "6"):
            print ("ADD MANAGER TO DEPARTMENT")
            print ("-------------------------")
            # Prompt for inputs
            eid = input("Enter EID: ")
            did = input ("Enter DID: ")

            x1 = choices.sqlEID(eid)
            x2 = choices.sqlDID(did)

            # Loop if inputs are not in SQL db
            while not x1 or not x2:
                if not x1 and not x2:
                    print ("EID does not exist.")
                    print ("DID does not exist.")
                elif not x1:
                    print ("EID does not exist.")
                elif not x2:
                    print ("DID does not exist.")

                eid = input("\nEnter EID: ")
                did = input ("Enter DID: ")

                x1 = choices.sqlEID(eid)
                x2 = choices.sqlDID(did)
            # If inputs are valid
            else:
                for i in x1:
                    eid = (i["eid"])
                for i in x2:
                    did = (i["did"])

            # Create Constraint
                # avoids creating a dept managed by multiple managers
            choices.NeoConst()

            # If inputs are valid, call the function
            try:
                choices.six(eid,did)
                print ("Employee", eid, "now manages Department", did,".")
            # If Dept already exists in Neo4j db (constraint)
            except exceptions.ConstraintError as e:
                x = choices.exception(did)
                print ("Department", did, "is already managed by Employee", x, ".")

        # Choice SEVEN
        elif (choice == "7"):
            print ("VIEW DEPARTMENTS")
            print ("----------------")

            # Append the SQL db to an array to save it in memory
                # If array is empty, append results from SQL and output it
                # If it isn't empty, print out the array

            # Output from array (won't run first time when option is chosen)
            try:
                print ("Did    |    Name    |    Location    |    Budget")
                for i in db:
                    print(i["did"], " | ", i["name"], " | ", i["lid"], " | ", i["budget"] )
            # Create an array and append the SQL db (runs when option chosen first time)
            except:
                db = []
                x = choices.seven()
                for i in x:
                    db.append(i)
                for i in db:
                    print(i["did"], " | ", i["name"], " | ", i["lid"], " | ", i["budget"] )

        # Choice EIGHT
        elif (choice == "x"):
            break
        else:
            display_menu()

# Displaying the menu - Menu function
def display_menu():
    print("Employees")
    print("---------")
    print("")
    print("MENU")
    print("====")
    print("1 - View Employees & Departments")
    print("2 - View Salary Details")
    print("3 - View by Month of Birth")
    print("4 - Add New Employee")
    print("5 - View Departments managed by Employee")
    print("6 - Add Manager to Department")
    print("7 - View Departments")
    print("x - Exit application\n")

if __name__ == "__main__":
	main()


low = int(input('Нижняя граница: '))
high = int(input('Верхняя граница: '))
step = int(input('Шаг: '))
print('Вывод:')
celsium = [k for k in range(low, high+1, step)]
if (high - low) % step != 0:
    celsium.append(high)
fahrenheit = [int(round(i * 1.8 + 32, 0)) if i >= 0 else int(round(i*1.8 - 32, 0)) for i in (celsium)]
print('C', '\t', 'F', '\t')
for i in range(len(celsium)):
    print(celsium[i], '\t', fahrenheit[i], '\t')


# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

class BookscarperPipeline:
    def process_item(self, item, spider):
        adapter = ItemAdapter(item)

        # Strip all whitespoaces from strings
        field_names = adapter.field_names()
        for field_name in field_names:
            if field_name != 'description':
                value = adapter.get(field_name)
                adapter[field_name] = value.strip()

        # Category & Product Type into lowercase
        lowercase_keys = ['category', 'product_type']
        for lowercase_key in lowercase_keys:
            value = adapter.get(lowercase_key)
            adapter[lowercase_key] = value.lower()

        # Convert price to float from string
        price_fields = ['price', 'price_excl_tax', 'price_incl_tax', 'tax']
        for price_field in price_fields:
            value = adapter.get(price_field)
            value = value.replace('£', '')
            adapter[price_field] = float(value)

        # Availability: Extract only number from string
        availability_string = adapter.get('availability')
        split_availability = availability_string.split('(')
        if len(split_availability) < 2:
            adapter['availability'] = 0
        else:
            availability_arr = split_availability[1].split(' ')
            adapter['availability'] = float(availability_arr[0])

        # Reviews: Convert number of reviews from string to int
        num_reviews = adapter.get('num_reviews')
        adapter['num_reviews'] = int(num_reviews)

        # Stars: Convert text to number
        stars = adapter.get('stars')
        star_text_value = stars.split(' ')[1]
        star_text_value = star_text_value.lower()
        if star_text_value == "zero":
            adapter['stars'] = 0
        elif star_text_value == "one":
            adapter['stars'] = 1
        elif star_text_value == "two":
            adapter['stars'] = 2
        elif star_text_value == "three":
            adapter['stars'] = 3
        elif star_text_value == "four":
            adapter['stars'] = 4
        elif star_text_value == "five":
            adapter['stars'] = 5

        return item


# Import the Necessary Packages 🤔🤔🤔
import matplotlib.pyplot as plt
import base64
from io import BytesIO
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import os
import pandas as pd

# the plotly package used in most of the dashboard visualization 😊😊
import plotly_express as px
import plotly.graph_objects as go
import plotly.offline as plt1

# -------------------------------------------------------------------------------------------

# draft template of all the plots to show the watermark
draft_template = go.layout.Template()
# Take the format of each plotting watermark and layout and will use in each plot
draft_template.layout.annotations = [
    dict(
        name="draft watermark",
        text="MYN",
        textangle=-30,
        opacity=0.5,
        font=dict(color="lightblue", size=50),
        xref="paper",
        yref="paper",
        showarrow=False,
    )
]
global fig1
fig1=go.Figure()

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽
# system task to set the graph
def get_graph():
    buffer=BytesIO()
    plt.savefig(buffer,format='png')
    buffer.seek(0)
    image_png=buffer.getvalue()
    graph=base64.b64encode(image_png)
    graph=graph.decode('utf-8')
    buffer.close()
    return graph

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽

# Function to show the barplot of the total stars in a movie
def stars(data,g,y):
    # take the value_count of the top 15 stars in year and genre basis
    bar = data["first_name"].value_counts()[1:16]
    # taking the bar-plot
    fig = px.bar(bar,
       x = bar.index,
       y = bar.values,
       title="<b> Top 15 Stars Acted in movie in {} genre, {} year</b>".format(g, y),
       text_auto='.2s',
       color = "first_name",
       width=800,
       height=600,
       )
    # update the layout and add the watermark
    fig.update_layout(template = draft_template, paper_bgcolor = "black")
    fig.update_traces(textfont_size=12, textangle=0, textposition="outside", cliponaxis=False)
    fig.update_layout(
        font_color="white",
        title_font_color="white",
        xaxis_title="Name of Stars",
        yaxis_title="Total no of Movies"
    )

    gantt_plt = plt1.plot(fig, output_type='div')
    return gantt_plt

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽

# the wordcloud to show the frequency of the directors in movies
def wordcloud(d):
    # taking the required format from system
    mask=np.array(Image.open(r'E:\movies\wordlogo.png'))
    # finding the wordcloud
    wc = WordCloud(mask=mask,min_font_size=10, background_color='black',contour_color='white', contour_width=10, colormap='Paired',width=mask.shape[1],height=mask.shape[0],random_state=42)
    df_wc = wc.generate(d['director'].str.cat(sep=' '))
    figx=plt.figure()
    figx.patch.set_facecolor('black')
    plt.imshow(df_wc,interpolation='None')
    plt.axis('off')
    plt.tight_layout()
    graph=get_graph()
    return graph

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽

# The function to show the histogram of total certificates in movies
def certificate(data,g,y):
    # plotting the histogram
    fig = px.histogram(data, x = "certificate", title="<b> Year wise Certificates (HistPlot with Boxplot)\t <total count : {}> </b>".format(len(data)), marginal="box", template="ggplot2", color_discrete_sequence=["#AB63FA"])
    fig.update_layout(template = draft_template, paper_bgcolor = "black")
    fig.update_layout(
        font_color="white",
        title_font_color="white",
        legend_title_font_color="white"
    )
    gantt_plt=plt1.plot(fig,output_type='div')
    return gantt_plt

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽

# The function to show the pie-plot of the status/tv-shows
def status(df,g,y):
    # taking the values of the tv-shows in 'pie'
    pie = df["status"].value_counts()
    fig = go.Figure(data=[go.Pie(labels=pie.index, values=pie, pull=[0, 0.2], hole = 0.3, title = "<b> MYN </b>")])
    fig.update_traces(textposition="inside", textinfo="percent+label")
    # updating the layout according to the 'MYN' requirments
    fig.update_layout(title="<b> Tv-Shows in {} year and {} genre </b>".format(y, g), height=450, width=450, paper_bgcolor = "black")
    fig.update_layout(
        font_color="white",
        title_font_color="white",
        xaxis_title="Name of Stars",
        yaxis_title="Total no of Movies"
    )
    gantt_plt=plt1.plot(fig,output_type='div')
    return gantt_plt

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽

# The function to show the scatterplot of the avarage rating
def rating(data,g, y):
    # taking the avarage rating of each year in 'rg'
    rg = data.groupby("start_year")["rating"].mean().reset_index()
    # doing the scatterplot
    fig = px.scatter(rg, x="start_year", y="rating", color="start_year", size='rating')
    # altering the layout according to the requiments
    fig.update_layout(template = draft_template, paper_bgcolor = "black", title="<b> Avarage Rating of Movies in {} year, {} Genre </b>".format(y,g))
    fig.update_layout(
        font_color="white",
        title_font_color="white",
        legend_title_font_color="white"
    )
    gantt_plt = plt1.plot(fig, output_type='div')
    return gantt_plt

# 😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊😊🍿🍿📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽📽

# The function to show the lineplot of the total time of the movies
def line_time(data,g,y):
    # taking the total time of movies in each year basis
    line = data.groupby("start_year")["time"].agg("sum").reset_index()
    #  some condition to satisfy the requirmrnts
    if y=='all':
        # doing the lineplot
        fig = px.line(line, x="start_year", y="time")
        fig.update_layout(template = draft_template, paper_bgcolor = "black", title="<b> Total Time of Movies in {} year, {} Genre </b>".format(y, g))
        # altering the layout according to the requiments
        fig.update_layout(
            font_color="white",
            title_font_color="white",
            legend_title_font_color="white"
        )
    else:
        # otherwise do the barplot
        fig = px.bar(line, x="start_year", y="time")
        fig.update_layout(template=draft_template, paper_bgcolor="black",
                          title="<b> Total Time of Movies in {} year, {} Genre </b>".format(y, g))
        # altering the layout according to the requiments
        fig.update_layout(
            font_color="white",
            title_font_color="white",
            legend_title_font_color="white"
        )

    gantt_plt = plt1.plot(fig, output_type='div')
    return gantt_plt

# -----------------------------------------------------------------------------------------------------------------


from keras import backend as K

from .WrappedGRU import WrappedGRU
from .helpers import softmax

class SelfAttnGRU(WrappedGRU):
    """
    Class implementing self-attention mechanism on the input tensor.
    """
    def build(self, input_shape):
        """Creates the layer weights.
        Must be implemented on all layers that have weights.
        # Arguments
            input_shape: Keras tensor (future input to layer)
                or list/tuple of Keras tensors to reference
                for weight shape computations
        """
        H = self.units
        assert(isinstance(input_shape, list))

        nb_inputs = len(input_shape)
        assert(nb_inputs >= 2)

        assert(len(input_shape[0]) == 3)
        B, P, H_ = input_shape[0]
        assert(H_ == H)

        assert(len(input_shape[1]) == 3)
        B, P_, H_ = input_shape[1]
        assert(P_ == P)
        assert(H_ == H)

        self.input_spec = [None]
        super(SelfAttnGRU, self).build(input_shape=(B, P, 2 * H))
        self.GRU_input_spec = self.input_spec
        self.input_spec = [None] * nb_inputs

    def step(self, inputs, states):
        """
        Additional operations to be carried out before passing the input to the GRU cell.
        """
        vP_t = inputs
        hP_tm1 = states[0]
        _ = states[1:3] # ignore internal dropout/masks
        vP, WP_v, WPP_v, v, W_g2 = states[3:8]
        vP_mask, = states[8:]

        WP_v_Dot = K.dot(vP, WP_v)
        WPP_v_Dot = K.dot(K.expand_dims(vP_t, axis=1), WPP_v)

        s_t_hat = K.tanh(WPP_v_Dot + WP_v_Dot)
        s_t = K.dot(s_t_hat, v)
        s_t = K.batch_flatten(s_t)

        a_t = softmax(s_t, mask=vP_mask, axis=1)

        c_t = K.batch_dot(a_t, vP, axes=[1, 1])

        GRU_inputs = K.concatenate([vP_t, c_t])
        g = K.sigmoid(K.dot(GRU_inputs, W_g2))
        GRU_inputs = g * GRU_inputs

        hP_t, s = super(SelfAttnGRU, self).step(GRU_inputs, states)

        return hP_t, s


import sys
sys.stdin = open("input.txt", "r")

for tc in range(1, int(input())+1):
    N, M = map(int, input().split())
    pad = [list(map(int, input().split())) for _ in range(N)]
    flies = []

    for i in range(N-M+1):
        for j in range(N-M+1):
            fly = 0
            for r in range(i, i+M):
                for c in range(j, j+M):
                    fly += pad[r][c]
            flies.append(fly)

    print('# {} {}'.format(tc, max(flies)))


"""This module contains functions for ImageNet Similarity Search."""

import ipoly
import numpy as np
import pinecone
import tensorflow as tf
import streamlit as st

def text_embedding(text, processor, model):
    """Computes the text embedding for a given text input.

    Args:
        text (str): The text input to be embedded.
        processor (transformers.PreTrainedTokenizer): The tokenizer to process the input text.
        model (transformers.PreTrainedModel): The pretrained model to compute the embeddings.

    Returns:
        tf.Tensor: The normalized text embedding tensor.
    """
    tokens = processor(text=text, padding=True, images=None, return_tensors="tf")
    text_emb = model.get_text_features(**tokens)
    norm_factor = np.linalg.norm(text_emb, axis=1)
    text_emb = tf.transpose(text_emb) / norm_factor
    text_emb = tf.transpose(text_emb)
    return text_emb

def image_embedding(image_batch, processor, model):
    """Computes image embeddings for a batch of images.

    Args:
        image_batch (List[Union[PIL.Image.Image, np.ndarray]]): A list of images to be embedded.
        processor (transformers.PreTrainedTokenizer): The tokenizer to process the input images.
        model (transformers.PreTrainedModel): The pretrained model to compute the embeddings.

    Returns:
        tf.Tensor: The normalized image embedding tensor.
    """
    images = processor(text=None, images=image_batch, return_tensors="tf")[
        "pixel_values"
    ]
    img_emb = model.get_image_features(images)
    norm_factor = np.linalg.norm(img_emb, axis=1)
    img_emb = tf.transpose(img_emb) / norm_factor
    img_emb = tf.transpose(img_emb)
    return img_emb

def connect_pinecone(processor, model, api_key, environment="eu-west1-gcp"):
    """Connects to Pinecone and initializes the index with image embeddings if it doesn't exist.

    Args:
        processor (transformers.PreTrainedTokenizer): The tokenizer to process the input images.
        model (transformers.PreTrainedModel): The pretrained model to compute the embeddings.
        api_key (str): The API key for Pinecone.
        environment (str, optional): The Pinecone environment to connect to. Defaults to "us-central1-gcp".

    Returns:
        pinecone.Index: The Pinecone index containing the image embeddings.
    """
    pinecone.init(api_key=api_key, environment=environment)
    index_name = "imagenet-index"
    if not (index_name in pinecone.list_indexes()):
        dataset = ipoly.load("imagenet-sample/data", keep_3D=True, file_names=True)
        np_dataset = np.array(dataset)
        img_embd = image_embedding(np_dataset[:, 0].tolist(), processor, model)
        index_vectors = list(zip(np_dataset[:, 1].tolist(), img_embd.numpy().tolist()))
        pinecone.create_index(index_name, dimension=512)
        index = pinecone.Index(index_name)
        index.upsert(index_vectors)
    else:
        index = pinecone.Index(index_name)
    return index

def get_images(text_input, processor, model, index):
    """Finds the top 5 most similar images to the input text from the Pinecone index.

    Args:
        text_input (str): The input text to find related images.
        processor (transformers.PreTrainedTokenizer): The tokenizer to process the input text and images.
        model (transformers.PreTrainedModel): The pretrained model to compute the embeddings.
        index (pinecone.Index): The Pinecone index containing the image embeddings.

    Returns:
        List[Tuple[str, float]]: A list of tuples with image names and their similarity scores.
    """
    test_embd = text_embedding(text_input, processor, model).numpy().tolist()
    return index.query(test_embd, top_k=5, include_metadata=True)

if __name__ == "__main__":
    st.title("Image seeker")

    api_token = st.text_input("Enter your API token", type="password")
    text_environment=st.text_input("Enter your cloud environment ex: eu-west1-gcp")
    text_input = st.text_input("Enter a small text")

    model, processor = ipoly.load_transformers("openai/clip-vit-base-patch32")

    if st.button("Validate"):
        if not api_token:
            st.error("Please enter a valid API token.")
        else:
            index = connect_pinecone(processor, model, api_token,text_environment)
            images = get_images(text_input, processor, model, index)
            images = [name["id"] for name in images["matches"]]
            if images:
                for img in images:
                    st.image(img, caption="Image", use_column_width=True)
            else:
                st.error("No images were found.")


"""Metrics to assess performance on classification task given scores

Functions named as ``*_score`` return a scalar value to maximize: the higher
the better

Function named as ``*_error`` or ``*_loss`` return a scalar value to minimize:
the lower the better
"""

# Authors: Alexandre Gramfort <alexandre.gramfort@inria.fr>
#          Mathieu Blondel <mathieu@mblondel.org>
#          Olivier Grisel <olivier.grisel@ensta.org>
#          Arnaud Joly <a.joly@ulg.ac.be>
#          Jochen Wersdorfer <jochen@wersdoerfer.de>
#          Lars Buitinck
#          Joel Nothman <joel.nothman@gmail.com>
#          Noel Dawe <noel@dawe.me>
#          Xing David Wang
# License: BSD 3 clause

from functools import partial

import numpy as np

from sklearn.utils import assert_all_finite
from sklearn.utils import check_consistent_length
from sklearn.utils import column_or_1d
from sklearn.utils.multiclass import type_of_target
from sklearn.utils.extmath import stable_cumsum
from sklearn.utils.validation import _deprecate_positional_args

from sklearn.metrics._base import _average_binary_score

@_deprecate_positional_args
def average_precision_score(y_true, y_score, *, average="macro", pos_label=1,
                            sample_weight=None):
    """Compute average precision (AP) from prediction scores

    AP summarizes a precision-recall curve as the weighted mean of precisions
    achieved at each threshold, with the increase in recall from the previous
    threshold used as the weight:

    .. math::
        \\text{AP} = \\sum_n (R_n - R_{n-1}) P_n

    where :math:`P_n` and :math:`R_n` are the precision and recall at the nth
    threshold [1]_. This implementation is not interpolated and is different
    from computing the area under the precision-recall curve with the
    trapezoidal rule, which uses linear interpolation and can be too
    optimistic.

    Note: this implementation is restricted to the binary classification task
    or multilabel classification task.

    Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.

    Parameters
    ----------
    y_true : array, shape = [n_samples] or [n_samples, n_classes]
        True binary labels or binary label indicators.

    y_score : array, shape = [n_samples] or [n_samples, n_classes]
        Target scores, can either be probability estimates of the positive
        class, confidence values, or non-thresholded measure of decisions
        (as returned by "decision_function" on some classifiers).

    average : string, [None, 'micro', 'macro' (default), 'samples', 'weighted']
        If ``None``, the scores for each class are returned. Otherwise,
        this determines the type of averaging performed on the data:

        ``'micro'``:
            Calculate metrics globally by considering each element of the label
            indicator matrix as a label.
        ``'macro'``:
            Calculate metrics for each label, and find their unweighted
            mean.  This does not take label imbalance into account.
        ``'weighted'``:
            Calculate metrics for each label, and find their average, weighted
            by support (the number of true instances for each label).
        ``'samples'``:
            Calculate metrics for each instance, and find their average.

        Will be ignored when ``y_true`` is binary.

    pos_label : int or str (default=1)
        The label of the positive class. Only applied to binary ``y_true``.
        For multilabel-indicator ``y_true``, ``pos_label`` is fixed to 1.

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    average_precision : float

    References
    ----------
    .. [1] `Wikipedia entry for the Average precision
           <https://en.wikipedia.org/w/index.php?title=Information_retrieval&
           oldid=793358396#Average_precision>`_

    See also
    --------
    roc_auc_score : Compute the area under the ROC curve

    precision_recall_curve :
        Compute precision-recall pairs for different probability thresholds

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.metrics import average_precision_score
    >>> y_true = np.array([0, 0, 1, 1])
    >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
    >>> average_precision_score(y_true, y_scores)
    0.83...

    Notes
    -----
    .. versionchanged:: 0.19
      Instead of linearly interpolating between operating points, precisions
      are weighted by the change in recall since the last operating point.
    """
    def _binary_uninterpolated_average_precision(
            y_true, y_score, pos_label=1, sample_weight=None):
        precision, recall, _ = precision_recall_curve(
            y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
        # Return the step function integral
        # The following works because the last entry of precision is
        # guaranteed to be 1, as returned by precision_recall_curve
        return -np.sum(np.diff(recall) * np.array(precision)[:-1])

    y_type = type_of_target(y_true)
    if y_type == "multilabel-indicator" and pos_label != 1:
        raise ValueError("Parameter pos_label is fixed to 1 for "
                         "multilabel-indicator y_true. Do not set "
                         "pos_label or set pos_label to 1.")
    elif y_type == "binary":
        present_labels = np.unique(y_true)
        if len(present_labels) == 2 and pos_label not in present_labels:
            raise ValueError("pos_label=%r is invalid. Set it to a label in "
                             "y_true." % pos_label)
    average_precision = partial(_binary_uninterpolated_average_precision,
                                pos_label=pos_label)
    return _average_binary_score(average_precision, y_true, y_score,
                                 average, sample_weight=sample_weight)

@_deprecate_positional_args
def precision_recall_curve(y_true, probas_pred, *, pos_label=None,
                           sample_weight=None):
    """Compute precision-recall pairs for different probability thresholds

    Note: this implementation is restricted to the binary classification task.

    The precision is the ratio ``tp / (tp + fp)`` where ``tp`` is the number of
    true positives and ``fp`` the number of false positives. The precision is
    intuitively the ability of the classifier not to label as positive a sample
    that is negative.

    The recall is the ratio ``tp / (tp + fn)`` where ``tp`` is the number of
    true positives and ``fn`` the number of false negatives. The recall is
    intuitively the ability of the classifier to find all the positive samples.

    The last precision and recall values are 1. and 0. respectively and do not
    have a corresponding threshold.  This ensures that the graph starts on the
    y axis.

    Read more in the :ref:`User Guide <precision_recall_f_measure_metrics>`.

    Parameters
    ----------
    y_true : array, shape = [n_samples]
        True binary labels. If labels are not either {-1, 1} or {0, 1}, then
        pos_label should be explicitly given.

    probas_pred : array, shape = [n_samples]
        Estimated probabilities or decision function.

    pos_label : int or str, default=None
        The label of the positive class.
        When ``pos_label=None``, if y_true is in {-1, 1} or {0, 1},
        ``pos_label`` is set to 1, otherwise an error will be raised.

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    precision : array, shape = [n_thresholds + 1]
        Precision values such that element i is the precision of
        predictions with score >= thresholds[i] and the last element is 1.

    recall : array, shape = [n_thresholds + 1]
        Decreasing recall values such that element i is the recall of
        predictions with score >= thresholds[i] and the last element is 0.

    thresholds : array, shape = [n_thresholds <= len(np.unique(probas_pred))]
        Increasing thresholds on the decision function used to compute
        precision and recall.

    See also
    --------
    average_precision_score : Compute average precision from prediction scores

    roc_curve : Compute Receiver operating characteristic (ROC) curve

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.metrics import precision_recall_curve
    >>> y_true = np.array([0, 0, 1, 1])
    >>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
    >>> precision, recall, thresholds = precision_recall_curve(
    ...     y_true, y_scores)
    >>> precision
    array([0.66666667, 0.5       , 1.        , 1.        ])
    >>> recall
    array([1. , 0.5, 0.5, 0. ])
    >>> thresholds
    array([0.35, 0.4 , 0.8 ])

    """
    fps, tps, thresholds = _binary_clf_curve(y_true, probas_pred,
                                             pos_label=pos_label,
                                             sample_weight=sample_weight)
    # print(len(fps))
    # print(fps)
    # print(len(tps))
    # print(tps)
    # print(len(thresholds))
    # print(thresholds)
    # fps = fps[:-1]
    # tps[-1] = tps[-2]
    # thresholds = thresholds[:-1]
    tps[-1] = np.count_nonzero(y_true)

    precision = tps / (tps + fps)
    precision[np.isnan(precision)] = 0
    precision[-1] = 0
    recall = tps / tps[-1]
    # print(list(zip(precision, recall, tps, fps, thresholds)))
    # print(tps[-1])
    # stop when full recall attained
    # and reverse the outputs so recall is decreasing
    last_ind = tps.searchsorted(tps[-1])
    sl = slice(last_ind, None, -1)
    return np.r_[precision[sl], 1], np.r_[recall[sl], 0], thresholds[sl]

def _binary_clf_curve(y_true, y_score, pos_label=None, sample_weight=None):
    """Calculate true and false positives per binary classification threshold.

    Parameters
    ----------
    y_true : array, shape = [n_samples]
        True targets of binary classification

    y_score : array, shape = [n_samples]
        Estimated probabilities or decision function

    pos_label : int or str, default=None
        The label of the positive class

    sample_weight : array-like of shape (n_samples,), default=None
        Sample weights.

    Returns
    -------
    fps : array, shape = [n_thresholds]
        A count of false positives, at index i being the number of negative
        samples assigned a score >= thresholds[i]. The total number of
        negative samples is equal to fps[-1] (thus true negatives are given by
        fps[-1] - fps).

    tps : array, shape = [n_thresholds <= len(np.unique(y_score))]
        An increasing count of true positives, at index i being the number
        of positive samples assigned a score >= thresholds[i]. The total
        number of positive samples is equal to tps[-1] (thus false negatives
        are given by tps[-1] - tps).

    thresholds : array, shape = [n_thresholds]
        Decreasing score values.
    """
    # Check to make sure y_true is valid
    y_type = type_of_target(y_true)
    if not (y_type == "binary" or (y_type == "multiclass" and pos_label is not None)):
        raise ValueError("{0} format is not supported".format(y_type))

    check_consistent_length(y_true, y_score, sample_weight)
    y_true = column_or_1d(y_true)
    y_score = column_or_1d(y_score)
    assert_all_finite(y_true)
    # assert_all_finite(y_score)

    if sample_weight is not None:
        sample_weight = column_or_1d(sample_weight)

    # ensure binary classification if pos_label is not specified
    # classes.dtype.kind in ('O', 'U', 'S') is required to avoid
    # triggering a FutureWarning by calling np.array_equal(a, b)
    # when elements in the two arrays are not comparable.
    classes = np.unique(y_true)
    if (pos_label is None and (classes.dtype.kind in ('O', 'U', 'S')
       or not (np.array_equal(classes, [0, 1])
       or np.array_equal(classes, [-1, 1])
       or np.array_equal(classes, [0])
       or np.array_equal(classes, [-1])
       or np.array_equal(classes, [1])))):
        classes_repr = ", ".join(repr(c) for c in classes)
        raise ValueError("y_true takes value in {{{classes_repr}}} and "
                         "pos_label is not specified: either make y_true "
                         "take value in {{0, 1}} or {{-1, 1}} or "
                         "pass pos_label explicitly.".format(
                             classes_repr=classes_repr))
    elif pos_label is None:
        pos_label = 1.

    # make y_true a boolean vector
    y_true = (y_true == pos_label)

    # sort scores and corresponding truth values
    desc_score_indices = np.argsort(y_score, kind="mergesort")[::-1]
    y_score = y_score[desc_score_indices]
    y_true = y_true[desc_score_indices]
    # print(list(zip(desc_score_indices, y_score, y_true)))
    if sample_weight is not None:
        weight = sample_weight[desc_score_indices]
    else:
        weight = 1.

    # y_score typically has many tied values. Here we extract
    # the indices associated with the distinct values. We also
    # concatenate a value for the end of the curve.
    distinct_value_indices = np.where(np.diff(y_score))[0]
    threshold_idxs = np.r_[distinct_value_indices, y_true.size - 1]

    # accumulate the true positives with decreasing threshold
    tps = stable_cumsum(y_true * weight)[threshold_idxs]
    if sample_weight is not None:
        # express fps as a cumsum to ensure fps is increasing even in
        # the presence of floating point errors
        fps = stable_cumsum((1 - y_true) * weight)[threshold_idxs]
    else:
        fps = 1 + threshold_idxs - tps
    return fps, tps, y_score[threshold_idxs]


import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from lists import solutions, queries, dependencies, greetings

def process(message):
	keywords = word_tokenize(message)

	not_flag = 0
	working_flag = 0
	submit_flag = 0
	bid_flag = 0
	confrm_flag = 0
	status_flag = 0
	edit_flag = 0
	one_flag = 0
	two_flag = 0
	price_flag = 0
	attachment_flag = 0
	technical_flag = 0

	for word in keywords:
		word = word.lower() #convert to lowercase

		for key in greetings:
			if key == word:
				return solutions['hi']

		for key in dependencies:
			if key == word:
				return solutions[key]

		for key in queries:
			if key == word:
				if key in solutions.keys():
					return solutions[key]
				if key == 'webpage' or key == 'website':
					return solutions['website']
				if key == 'tenders':
					return solutions['all tenders']
				if key == 'tender':
					return solutions['particular tender']
				if key == 'bid' or key == 'bidding':
					bid_flag = 1
				if key == 'submit' or key == 'submitted' or key == 'submitting':
					submit_flag = 1
				if key == 'confirmation' or key == 'confirm' or key == 'confirmed':
					confrm_flag = 1
				if key == 'status':
					status_flag = 1
				if key == 'not':
					not_flag = 1
				if key == 'working' or key == 'worked' or key == 'work':
					working_flag = 1
				if key == 'edit' or key == 'change':
					edit_flag = 1
				if key == '1' or key == 'one' or key == 'single':
					one_flag = 1
				if key == '2' or key == 'two' or key == 'double':
					two_flag = 1
				if key == 'attachment' or key == 'attach':
					attachment_flag = 1
				if key == 'price' or key == 'cost':
					price_flag = 1
				if key == 'technical' or key == 'technological':
					technical_flag = 1

		if not_flag == 1 and working_flag == 1 :
			return solutions['working']

		if bid_flag == 1 and submit_flag == 1:
			return solutions['submit bid']

		if bid_flag == 1 and confrm_flag == 1 and status_flag == 1:
			return solutions['confirm bid status']

		if bid_flag == 1:
			if one_flag == 1:
				return solutions['1 stage bid']
			elif two_flag == 1:
				return solutions['2 stage bid']
			else:
				return "What type of bid, 2 stage or 1 stage?"

		if bid_flag == 1 and edit_flag == 1:
			return solutions['edit bid']

		if attachment_flag == 1 :
			if price_flag == 1:
				return solutions['price attachment']
			elif technical_flag == 1:
				return solutions['technical attachment']
			else:
				return 'What type of attachment, price of technical?'

		'''for key in vendor_list:
			if key == word:
				return solutions['vendor']

		for key in tender_list:
			if key == word:
				return solutions['tender']'''

	return "Keyword not found!"


from utility.api import get_input

'''

t...012..
...H..
......
......
......

......
...H...
HT..t..
......
......

......
.......
..T..HT..
.H.......
......

......
.....
..T..H..
..H...
......

......H.
...H..T.
..T......
......
......

'''

def move_up(head,tail):
    head=list(head)
    tail=list(tail)

    if head[0]==tail[0]:
        head[0]+=1
        return tuple(head),tuple(tail)

    head[0]+=1

    if head==tail:
        return tuple(head),tuple(tail)

    if abs(head[0] -tail[0])>1:
        tail[0]+=1
        tail[1]=head[1]

    return tuple(head),tuple(tail)

def move_down(head,tail):
    head=list(head)
    tail=list(tail)

    if head[0]==tail[0]:
        head[0]-=1
        return tuple(head),tuple(tail)

    head[0]-=1

    if head==tail:
        return tuple(head),tuple(tail)

    if abs(head[0]-tail[0])>1:
        tail[0]-=1
        tail[1]=head[1]

    return tuple(head),tuple(tail)

def move_left(head,tail):
    head=list(head)
    tail=list(tail)

    if head[1]==tail[1]:
        head[1]-=1
        return tuple(head),tuple(tail)

    head[1]-=1

    if head==tail:
        return tuple(head),tuple(tail)

    if abs(head[1]-tail[1])>1:
        tail[1]-=1
        tail[0]=head[0]

    return tuple(head),tuple(tail)

def move_right(head,tail):
    head=list(head)
    tail=list(tail)

    if head[1]==tail[1]:
        head[1]+=1
        return tuple(head),tuple(tail)

    head[1]+=1

    if head==tail:
        return tuple(head),tuple(tail)

    if abs(head[1]-tail[1])>1:
        tail[1]+=1
        tail[0]=head[0]

    return tuple(head),tuple(tail)

def part_1(input):
    head=(0,0)
    tail=(0,0)
    sett=set([tail])
    dictt={"U":move_up,"D":move_down,"L":move_left,"R":move_right}
    count=0
    for move in input:
        for _ in range(move[1]):
            head1,tail1=head,tail
            head,tail=  dictt[move[0]](head,tail)
            sett.add(tail)
        count+=1

    print(len(sett))

def move_up(list_tuple):
    head=list(head)
    tail=list(tail)

    if head[0]==tail[0]:
        head[0]+=1
        return tuple(head),tuple(tail)

    head[0]+=1

    if head==tail:
        return tuple(head),tuple(tail)

    if abs(head[0] -tail[0])>1:
        tail[0]+=1
        tail[1]=head[1]

    return tuple(head),tuple(tail)

def move_tail(head,tail):
    head=list(head)
    tail=list(tail)

    if abs(head[1]-tail[1])>1 and abs(head[0]-tail[0])>1:
        if abs(head[1]-(tail[1]+1))==1 and abs(head[0]-(tail[0]+1))==1:
            tail[1]+=1
            tail[0]+=1
        else:
            tail[1]-=1
            tail[0]-=1
        return tuple(tail)

    if abs(head[1]-tail[1])>1:
        if abs(head[1]-(tail[1]+1))==1:
            tail[1]+=1
        else:
            tail[1]-=1

        tail[0]=head[0]
        return tuple(tail)

    if abs(head[0]-tail[0])>1:
        if abs(head[0]-(tail[0]+1))==1:
            tail[0]+=1
        else:
            tail[0]-=1
        tail[1]=head[1]

    return tuple(tail)

def part_2(input):
    snake=[(0,0) for i in range(10)]

    sett=set([(0,0)])
    m={"U":(1,0),"D":(-1,0),"L":(0,-1),"R":(0,1)}
    for move in input:
        for _ in range(move[1]):

            snake[0]=(snake[0][0]+m[move[0]][0],snake[0][1]+m[move[0]][1])

            for i in range(len(snake)-1):
                head=snake[i]
                tail=snake[i+1]

                snake[i+1]=  move_tail(head,tail)

            '''
            k=[["." for i in range(60)] for _ in range(60)]

            k[snake[0][0]][snake[0][1]]=f"H"

            for j,i in enumerate(snake[1:]):
                k[i[0]][i[1]]=str(j+1)
            for i in reversed(k):
                print("".join(i))
            print(f"----------- moveee {move}")
            '''

            sett.add(snake[-1])

    print(len(sett))

def parse_input(input):
    input=input.split("\n")[:-1]
    li=[]
    for el in input:
        s=el.split(" ")
        li.append([s[0], int(s[1])])
    return li

if __name__=="__main__":
    input=get_input(year=2022, day=9)
    input=parse_input(input)
    part_2(input)


# Import streamlit
import streamlit as st
import os
from neuralintents import GenericAssistant
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as pdr
import mplfinance as mpf
import pickle
import datetime as dt

st.title("Financial Assistant Chatbot")

portfolio_path = './data/portfolio.pkl'
portfolio = {}
if os.path.exists(portfolio_path):
    with open(portfolio_path, 'rb') as f:
        portfolio = pickle.load(f)

def greeting():
    st.write("Suuuuuuh Dude!")

def save_portfolio():
    with open('portfolio.pkl', 'wb') as f:
        pickle.dump(portfolio, f)

def add_portfolio(ticker, amount):
    if ticker in portfolio.keys():
        portfolio[ticker] += int(amount)
    else:
        portfolio[ticker] = int(amount)

    save_portfolio()

def remove_portfolio(ticker, amount):
    if ticker in portfolio.keys():
        if int(amount) <= portfolio[ticker]:
            portfolio[ticker] -= int(amount)
            save_portfolio()
        else:
            st.write("You don't have the facilities for that big man!")
    else:
        st.write(f"You don't own any shares of {ticker}")

def show_portfolio():
    st.write("Your Portfolio:")
    for ticker in portfolio.keys():
        st.write(f"You own {portfolio[ticker]} shares of {ticker}")

def portfolio_worth():
    sum = 0
    for ticker in portfolio.keys():
        data = pdr.Datareader(ticker, 'yahoo')
        price = data['Close'].iloc[-1]
        sum += price

    st.write(f"Your portfolio is worth {sum} USD")

def portfolio_gains(starting_date):
    sum_now = 0
    sum_then = 0

    try:
        for ticker in portfolio.keys():
            data = pdr.DataReader(ticker, 'yahoo')
            price_now = data['Close'].iloc[-1]
            price_then = data.loc[data.index == starting_date]['Close'].values[0]
            sum_now += price_now
            sum_then += price_then

        st.write(f"Relative Gains: {((sum_now-sum_then)/sum_then)* 100}%")
        st.write(f"Absolute Gains: {sum_now-sum_then} USD")
    except IndexError:
        st.write("No Trading On This Day")

import yfinance as yf

def plot_chart(ticker, starting_string):
    plt.style.use('dark_background')

    start = dt.datetime.strptime(starting_string, "%d/%m/%Y")
    end = dt.datetime.now()

    data = yf.download(ticker, start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'))

    colors = mpf.make_marketcolors(up = '#00ff00', down = '#ff0000', wick = 'inherit', edge = 'inherit', volume = 'in')
    mpf_style = mpf.make_mpf_style(base_mpf_style = 'nightclouds', marketcolors = colors)
    mpf.plot(data, type = 'candle', style = mpf_style, volume = True)
    st.pyplot(plt)

mappings = {
    'greeting': greeting,
    'plot_chart' : plot_chart,
    'add_portfolio': add_portfolio,
    'remove_portfolio': remove_portfolio,
    'show_portfolio': show_portfolio,
    'portfolio_worth': portfolio_worth,
    'portfolio_gains': portfolio_gains,
}
assistant = GenericAssistant('intents.json', mappings, "Jafire")

assistant.load_model()

user_message = st.text_input("Your message:")
assistant.request(user_message)


from datetime import datetime
from dateutil.relativedelta import relativedelta

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions
from tasks.models import Task

class CountTaskReport(APIView):
    """
    Формируем статистики по месяцам
    """

    permission_classes = [permissions.IsAuthenticated]

    def get(self, request):

        start = request.query_params['start'].rsplit('-', 1)[0]
        end = request.query_params['end'].rsplit('-', 1)[0]

        start_date = datetime.strptime(start, "%Y-%m")
        end_date = datetime.strptime(end, "%Y-%m")

        labels = []

        count_all_tasks = []
        count_executed_tasks = []
        count_expired_tasks = []

        while start_date <= end_date:

            date_str = start_date.strftime("%Y-%m")
            labels.append(date_str)

            count_all_tasks.append(Task.objects.filter(createDate__gte=start_date).filter(
                createDate__lte=start_date + relativedelta(months=1)).count())

            count_executed_tasks.append(Task.objects.filter(createDate__gte=start_date).
                                        filter(createDate__lte=start_date+relativedelta(months=1)).
                                        filter(is_done=True).count())

            count_expired_tasks.append(Task.objects.filter(createDate__gte=start_date).
                                       filter(createDate__lte=start_date + relativedelta(months=1)).
                                       filter(expired=True).filter(is_done=False).count())

            start_date += relativedelta(months=1)

        date_list = {
            'labels': labels,
            'datasets': {
                'count_all_tasks': count_all_tasks,
                'count_executed_tasks': count_executed_tasks,
                'count_expired_tasks': count_expired_tasks,
            }
        }

        return Response(date_list)


name = input("Enter player name: ")
start = False

while True:
    input1 = input("> ").capitalize()
    if input1 in ('Start','Stop','Turn','Park','Help','Quit'):

        if input1 == "Start":
            if start:
                print("The car is already started! It can't be started again.")
            else:
                start = True
                print("The car is started!!!")

        elif input1 == "Stop":
            if not start:
                print("The car is already stopped! It can't be stopped again.")
            else:
                start = False
                print("The car is stopped!!!")

        elif input1 == "Turn":
            turn = " "

            if not start:
                print("The car isn't started yet! Please start your car to turn!")

            else:
                start = True
                print("Which side do you want to turn your car? (Left/Right) :")
                turn = input("> ")

                if turn == "left":
                    print("The car turns towards the left side!")
                elif turn == "right":
                    print("The car turns towards the right side!")

        elif input1 == "Park":
            print("The car has been parked!!!")

        elif input1 == "Help":
            print('''
Instructions:

>>> Start => To start the car...
>>> Stop => To stop the car...
>>> Turn => To turn left or right...
>>> Park => To park the car...
>>> Quit => To quit the game...
            ''')

        elif input1 == "Quit":
              print("Thank You",name,"for participating!!!")
              break

    else:
        print("Sorry! I don't understand!")


from __future__ import print_function
from os.path import join, dirname, abspath
import datetime, xlrd, numpy as np
from xlrd.sheet import ctype_text
import sqlite3

conn = sqlite3.connect('sample_db.sqlite3')
cur = conn.cursor()

xl_workbook = xlrd.open_workbook('2015 ROOMS SEGMENTATION.xlsx')  # location and name of the file
sheet_names = xl_workbook.sheet_names()
xl_sheet = xl_workbook.sheet_by_index(4)
row = xl_sheet.row(4)
ind_or_grp = xl_sheet.row(3)
for iog, cell_obj in enumerate(ind_or_grp):
    if cell_obj.value == 'GROUP':
        group_start = iog

ind_actual = np.zeros((13, 12),
                      dtype=[('subsegment', 'S40'), ('month', 'S40'), ('rns', float), ('arr', float), ('rev', float)])

ind_actual_last_year = np.zeros((13, 12),
                                dtype=[('subsegment', 'S40'), ('month', 'S40'), ('rns', float), ('arr', float),
                                       ('rev', float)])

month_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
              'November', 'December']
unneeded_columns = ['', 'Barter', 'GRAND TOTAL', 'TOTAL GROUP', 'TOTAL INDIVIDUAL', 'SEGMENT NAME',
                    'Qualified Discount', 'Long Staying']

year = xl_sheet.cell(1, 1).value.split()[1]
last_year = int(year) - 1

subsegment_column = 0
m = 0
actual_row = 7
actual_row_last_year = 9
for idx, cell_obj in enumerate(row):
    # only gets the subsegment
    subsegment = cell_obj.value  # use .value to get the value (duh)

    if subsegment not in unneeded_columns:  # get the Subsegment (rack, group, etc)
        mon = 5
        monx = 0
        for month in month_list:  # get the month (January, February etc)
            ind_actual[subsegment_column, m]['subsegment'] = subsegment
            ind_actual_last_year[subsegment_column, m]['subsegment'] = subsegment
            ind_actual[subsegment_column, m]['month'] = month
            ind_actual_last_year[subsegment_column, m]['month'] = month
            mon = mon + monx
            for x in range(0, 3):  # get the column headers (Room Nights Sold, Average Rm Rate(PHP), Revenue (PHP'000)
                ecolumn = idx + x
                if x == 0:
                    val = 'rns'
                elif x == 1:
                    val = 'arr'
                else:
                    val = 'rev'
                our = xl_sheet.cell(actual_row, ecolumn).value
                our2 = xl_sheet.cell(actual_row_last_year, ecolumn).value
                if isinstance(our, str):
                    our = 0.0
                if isinstance(our2, str):
                    our2 = 0.0
                ind_actual[subsegment_column, m][val] = our
                ind_actual_last_year[subsegment_column, m][val] = our2
            actual_row += 4
            actual_row_last_year += 4
            m += 1
        subsegment_column += 1
        actual_row = 7
        actual_row_last_year = 9
        m = 0

def getDate(month, year):
    thirty_ones = ["January", "March", "May", "July", "August", "October", "December"]
    # thirties = ["February","April","June","September","November"]
    monthMap = {"January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8,
                "September": 9, "October": 10, "November": 11,
                "December": 12}
    if month in thirty_ones:
        day = 31
    else:
        day = 30
    month = monthMap.get(month)
    date = "%s-%s-%s" % (year, month, day)
    return date

for main in ind_actual:
    for sub in main:
        segment = sub[0].upper().decode('utf-8').strip()
        month = sub[1].decode('utf-8')
        rns = sub[2]
        arr = sub[3]
        rev = sub[4]
        date = getDate(month, year)
        try:
            seg_id_get_query = "select id from seg_list where name like  '%%%s' limit 1" % segment
            cur.execute(seg_id_get_query)
            seg_id = cur.fetchone()[0]
            insert_query = "insert into actual (date,rns,arr,rev,seg_id) values('%s',%s,%s,%s,%s)" % (
                date, rns, arr, rev, seg_id)
            cur.execute(insert_query)
            conn.commit()
        except(Exception):
            pass
conn.close()


#Ejemplo 1 de bucle for anidado
dibujo=''
anchura=int(input("Ingrese el valor del ancho = "))
altura=int(input("Ingrese el valor de la altura = "))

for i in range(0,altura):

    for j in range(0,anchura):
        if i ==0 or i == altura-1:
            dibujo+='*'
        elif j ==0 or j==anchura-1:
            dibujo+='*'
        else:
            dibujo +='-'
    dibujo+='\n'

print ("\n"*2)
print (dibujo)


import copy
import numpy # needed (don't change it)
import importlib
import os
import sys
import socket
mammoth_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(mammoth_path)
sys.path.append(mammoth_path + '/datasets')
sys.path.append(mammoth_path + '/backbone')
sys.path.append(mammoth_path + '/models')

from datasets import NAMES as DATASET_NAMES
from models import get_all_models
from argparse import ArgumentParser
from utils.args import add_management_args
from datasets import ContinualDataset
from datasets import get_dataset
from models import get_model
from utils.training import run_epoch, evaluate, print_mean_accuracy
from utils.best_args import best_args
from utils.conf import set_random_seed, base_path, set_base_path
from utils.callbacks import FintuneCallback, CheckpointCallback
from utils.wandb_logger import WandbLogger
from backbone import get_classifier
from backbone.MNISTMLP import MNISTMLP
from backbone.ConvNet import ConvNet

import numpy as np
import torch
import uuid
import datetime
import wandb

time_suffix = datetime.datetime.now().strftime('%y%m%d%H%M%S')

def lecun_fix():
    # Yann moved his website to CloudFlare. You need this now
    from six.moves import urllib
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib.request.install_opener(opener)

def parse_args(to_parse=None):
    parser = ArgumentParser(description='mammoth', allow_abbrev=False)
    parser.add_argument('--model', type=str, required=True,
                        help='Model name.', choices=get_all_models())
    parser.add_argument('--dataset', type=str, required=True,
                        choices=DATASET_NAMES,
                        help='Which dataset to perform experiments on.')
    parser.add_argument('--load_best_args', action='store_true',
                        help='Loads the best arguments for each method, '
                             'dataset and memory buffer.')
    torch.set_num_threads(4)
    add_management_args(parser)
    args = parser.parse_known_args(to_parse)[0]
    mod = importlib.import_module('models.' + args.model)

    if args.load_best_args:

        if hasattr(mod, 'Buffer'):
            parser.add_argument('--buffer_size', type=int, required=True,
                                help='The size of the memory buffer.')

        # Up to this point, the parsed arguments are only basic ones and used for get the best args
        args = parser.parse_known_args(to_parse)[0]

        # get the best args for the given model, dataset and buffer size
        best = best_args[args.dataset][args.model]

        if hasattr(mod, 'Buffer'):
            best = best[args.buffer_size]
        else:
            best = best[-1]

        # Get the parser from the specific model and dataset
        get_parser = getattr(mod, 'get_parser')
        parser = get_parser()

        # Modified the input arguments according to best_args
        if to_parse is None:
            to_parse = sys.argv[1:]

        to_parse = to_parse + ['--' + k + '=' + str(v)
                               for k, v in best.items()
                               if '--' + k not in to_parse]

        to_parse.remove('--load_best_args')

        # Parse the arguments
        args = parser.parse_args(to_parse)
    else:
        get_parser = getattr(mod, 'get_parser')
        parser = get_parser()
        args = parser.parse_args(to_parse)

    if args.model == 'joint' and args.dataset == 'mnist-360':
        args.model = 'joint_gcl'

    if args.seed is not None:
        set_random_seed(args.seed)

    exp_name = f'{args.model}-{args.dataset}'
    if 'buffer_size' in vars(args).keys():
        exp_name += f'-buf_{args.buffer_size}'
    if args.exp_suffix is not None:
        exp_name += f'-{args.exp_suffix}'

    if args.time_suffix is not None:
        exp_name += "-" + args.time_suffix
    else:
        exp_name += "-" + time_suffix

    args.exp_name = exp_name

    return args

def process_args(args):
    # Set environment variables for ComputeCanada
    if "SLURM_TMPDIR" in os.environ:
        args.base_path = os.path.join(os.environ["SLURM_TMPDIR"], 'data/')
        args.ckpt_folder = os.path.join(os.environ["SLURM_TMPDIR"], 'ckpt/')

    set_base_path(args.base_path)

    return args

def get_backbone(args, dataset):
    if args.backbone == 'convnet':
        im_size = get_dataset(args).IMG_SIZE
        channel = get_dataset(args).N_CHANNELS
        num_classes = get_dataset(args).N_CLASSES
        backbone = ConvNet(channel=channel,
                        num_classes=num_classes,
                        net_width=args.net_width,
                        net_depth=args.net_depth,
                        net_act='relu',
                        net_norm=args.net_norm,
                        net_pooling=args.net_pooling,
                        im_size=im_size,
                        )

    elif args.backbone == 'mnistmlp':
        backbone = MNISTMLP(28 * 28, 10)
    else:
        backbone = dataset.get_backbone(args)

    # Change the classification layer of the backbone according to the arguments
    if args.classifier_type is not None:
        backbone.classifier = get_classifier(args.classifier_type, backbone.feat_dim, backbone.num_classes)

    # Used for loading pretrained models
    if args.load_backbone_weights is not None:
        print("Loading weights from", args.load_backbone_weights)
        ckpt = torch.load(args.load_backbone_weights)
        backbone.load_state_dict(ckpt['state_dict'])

    print("Using backbone", type(backbone).__name__)

    return backbone

def create_or_restore_training_state(args, ckpt_path=None):
    if ckpt_path is not None:
        ckpt = torch.load(ckpt_path)
        args.exp_name = ckpt['args'].exp_name
    else:
        ckpt = None

    # Build dataset class (for meta data only)
    dataset = get_dataset(args)

    # Build backbone network and continual model
    backbone = get_backbone(args, dataset)
    model = get_model(args, backbone, dataset.get_loss(), dataset.get_transform())
    model.reset_optimizers(dataset)

    # Set some arguments to default ones if still not set at this point
    if args.n_epochs is None and isinstance(dataset, ContinualDataset):
        args.n_epochs = dataset.get_epochs()
        assert args.n_epochs is not None, 'Could not get number of epochs'
    if args.batch_size is None:
        args.batch_size = dataset.get_batch_size()
        assert args.batch_size is not None, 'Could not get batch size'
    if hasattr(importlib.import_module('models.' + args.model), 'Buffer') and (not hasattr(args, "minibatch_size") or args.minibatch_size is None):
        args.minibatch_size = dataset.get_minibatch_size()
        assert args.minibatch_size is not None, 'Could not get minibatch size'

    # Re-create the dataset to reflect some argument changes and build dataloaders
    dataset = get_dataset(args)
    dataset.build_data_loaders()
    if args.cum_loaders:
        dataset.build_data_loaders_up_to()

    # Initialize logger and callbacks
    # Always using wandb for the logger
    logger = WandbLogger(args, dataset.SETTING, args.project)

    # build callbacks
    callbacks = []
    if args.save_weights:
        callbacks.append(CheckpointCallback(args, model, dataset))

    # Finetune callback is always used
    callbacks.append(FintuneCallback(args, model, dataset))

    if ckpt is not None:
        ckpt = torch.load(ckpt_path)

        if hasattr(model, "buffer"):
            # initialize the variables in buffer
            for attr_str in model.buffer.attributes:
                key = "buffer." + attr_str
                if key in ckpt['state_dict']:
                    model.buffer.register_buffer(attr_str, ckpt['state_dict'][key])

        if "old_net.classifier.weight" in ckpt['state_dict']:
            model.old_net = copy.deepcopy(model.net)

        # Load model
        model.load_state_dict(ckpt['state_dict'])
        model.opt.load_state_dict(ckpt['opt_state_dict'])
        if model.scheduler is not None:
            model.scheduler.load_state_dict(ckpt['scheduler_state_dict'])

        start_run = ckpt['run']
        start_task = ckpt['task']
        start_epoch = ckpt['epoch']
        wandb_id = ckpt['wandb_id']

        wandb.init(
            project=args.project,
            id=wandb_id,
            resume="must",
            dir=args.ckpt_folder,
            settings=wandb.Settings(start_method="fork")
        )
        print("Experiment resumed from", ckpt_path)
    else:
        wandb_id = wandb.util.generate_id()
        wandb.init(
            project=args.project,
            name=args.exp_name,
            id=wandb_id,
            config=args,
            dir=args.ckpt_folder,
            reinit=True,
            settings=wandb.Settings(start_method="fork")
        )
        start_run = 0
        start_task = 0
        start_epoch = 0

    return model, dataset, logger, callbacks, start_run, start_task, start_epoch, wandb_id

def save_training_state(args, model, dataset, run_id, task_id, epoch, wandb_id):
    temp_path = os.path.join(args.ckpt_folder, "temp.ckpt")
    ckpt_path = os.path.join(args.ckpt_folder, "latest.ckpt")

    state_dict = model.state_dict()

    ckpt = {
        'state_dict': state_dict,
        "opt_state_dict": model.opt.state_dict(),
        'run': run_id,
        'task': task_id,
        'epoch': epoch,
        'wandb_id': wandb_id,
        'args': args,
    }

    if model.scheduler is not None:
        ckpt["scheduler_state_dict"] = model.scheduler.state_dict()

    torch.save(ckpt, open(temp_path, 'wb'))

    os.replace(temp_path, ckpt_path)

def main(args):
    # whether to resume training or start a new one
    if args.preemptive:
        if args.resume_path is None:
            args.resume_path = os.path.join(args.ckpt_folder, "latest.ckpt")
        if not os.path.exists(args.resume_path):
            args.resume_path = None

    model, dataset, logger, callbacks, start_run, start_task, start_epoch, wandb_id = \
        create_or_restore_training_state(args, args.resume_path)

    print("Starting training from run", start_run, "task", start_task, "epoch", start_epoch)

    # If not running on a preemptive cluster, append the ckpt_folder with the experiment name
    if not args.preemptive:
        args.ckpt_folder = os.path.join(args.ckpt_folder, args.exp_name)

    if not os.path.exists(args.ckpt_folder):
        os.makedirs(args.ckpt_folder)

    train_loader_all = dataset.train_loader_all
    test_loader_all = dataset.test_loader_all

    for run_id in range(start_run, args.n_runs):
        set_random_seed(args.seed + run_id)
        if start_task == 0 and start_epoch == 0  and run_id > start_run:
            # Reset the state at the beginning of each run
            model, dataset, logger, callbacks, _, start_task, start_epoch, wandb_id = \
                create_or_restore_training_state(args, None)

        # Move the model to the proper device
        model.net.to(model.device)
        if hasattr(model, "old_net") and model.old_net is not None:
            model.old_net.to(model.device)

        for task_id in range(start_task, dataset.N_TASKS):
            model.net.train()
            if args.cum_loaders:
                train_loader, _ = dataset.get_data_loaders_up_to_task(task_id)
            else:
                train_loader, _ = dataset.get_data_loaders_by_task(task_id)
            model.begin_task(dataset, task_id, start_epoch)

            for epoch in range(start_epoch, model.args.n_epochs):
                # Save checkpoint at the beginning of each epoch
                if args.preemptive:
                    save_training_state(args, model, dataset, run_id, task_id, epoch, wandb_id)

                model.begin_epoch(dataset, task_id, epoch)

                run_epoch(model, train_loader, logger, callbacks, task_id, epoch)

                model.end_epoch(dataset, task_id, epoch)

                # callbacks for epoch end
                for callback in callbacks:
                    callback.on_train_epoch_end(
                        epoch=epoch,
                        model=model,
                        task_train_loader=train_loader,
                    )

                # Evaluate the model every a few epochs
                if epoch % 5 == 0:
                    accs, mean_acc = evaluate(model, dataset, task_id=task_id)
                    logger.log_accuracy(np.array(accs), mean_acc)

            # At the end of a task
            if hasattr(model, 'end_task'):
                model.end_task(dataset)

            accs, mean_acc = evaluate(model, dataset, task_id=task_id)
            print_mean_accuracy(mean_acc, task_id + 1, dataset.SETTING)
            logger.log_accuracy(np.array(accs), mean_acc, type="end_task")

            for callback in callbacks:
                callback.on_task_end(
                    task_id=task_id,
                    model=model,
                    task_train_loader=train_loader,
                )

            start_epoch = 0

        # At the end of the experiment
        # Log the final accuracy
        logger.log_accuracy(np.array(accs), mean_acc, type="final")

        for callback in callbacks:
            callback.on_train_end(
                model=model,
                train_loader_all=train_loader_all,
                test_loader_all=test_loader_all
            )

        logger.close()

        start_task = 0

if __name__ == '__main__':
    lecun_fix()
    args = parse_args()
    args = process_args(args)
    main(args)


from pythonish_validator.common import validate, Validator

def test_repr_one():
    validator = Validator(str)

    validator.is_valid("asd")
    validator.is_valid([1])
    assert validator.repr_errors() == ["[[1]]"]

    validator = validate({
        'name': str,
        'age': int,
        'skills': [str]
    }, {
        'name': 'Georgy',
        'age': None,
        'skills': ['Python', 'Perl', 'C']
    })
    assert validator.repr_errors() == ["{'age'}->NoneType(None)"]

def test_repr_may():
    validator = validate({
        'name': str,
        'age': int,
        'skills': [str]
    }, {
        'name': 'Georgy',
        'age': None,
        'skills': ['Python', 'Perl', 42]
    })
    assert set(validator.repr_errors()) == {
        "{'age'}->NoneType(None)",
        "{'skills'}->[2]->int(42)",
    }


import os

from FiguresClassifier.Classifier.classifier import Classifier
from FiguresClassifier.Classifier.classifier import ClassifierConfiguration
from FiguresClassifier.Dataset.generator import DatasetGeneratorConfiguration
from FiguresClassifier.Figures.generator import FiguresEnum
from NN.neural_network import NeuralNetworkConfiguration
from NN.optimizer import OptimizerConfiguration
from NN.optimizer import OptimizerEnum

class ClassifierFactory:
    def __init__(self, dimensions_size, root_path):
        self.dimensions_size = dimensions_size
        self.root_path = root_path

    def get_classifier(self, target_name: FiguresEnum,
                       min_scale=0.5, max_scale=3.5, scale_precision=0.5, angle_precision=30, distortion_percentage=5,
                       save_plots=False,
                       optimizer_name = OptimizerEnum.ADAMOptimizer, optimizer_alpha=0.01, optimizer_beta1=0.9, optimizer_beta2=0.9,
                       nn_h1=100, nn_h2=10, nn_l1=0.01, nn_l2=0.01):
        train_min_scale = min_scale
        train_max_scale = max_scale
        train_scale_precision = scale_precision
        train_min_angle = 0
        train_max_angle = 360
        train_angle_precision = angle_precision
        train_distortion_percentage = distortion_percentage
        train_save_plots = save_plots
        self.train_dataset_config = DatasetGeneratorConfiguration(f'{self.dimensions_size}_{train_min_scale}_{train_max_scale}_{train_scale_precision}_{train_min_angle}_{train_max_angle}_{train_angle_precision}_{train_distortion_percentage}',
                                                                  os.path.join(self.root_path, 'datasets'),
                                                                  self.dimensions_size, train_min_scale, train_max_scale, train_scale_precision, train_min_angle, train_max_angle, train_angle_precision, train_distortion_percentage, train_save_plots)

        test_min_scale = train_min_scale
        test_max_scale = train_max_scale
        test_scale_precision = train_scale_precision * 1.5
        test_min_angle = train_min_angle
        test_max_angle = train_max_angle
        test_angle_precision = train_angle_precision * 1.5
        test_distortion_percentage = distortion_percentage
        test_save_plots = save_plots
        self.test_dataset_config = DatasetGeneratorConfiguration(f'{self.dimensions_size}_{test_min_scale}_{test_max_scale}_{test_scale_precision}_{test_min_angle}_{test_max_angle}_{test_angle_precision}_{test_distortion_percentage}',
                                                                 os.path.join(self.root_path, 'datasets'),
                                                                 self.dimensions_size, test_min_scale, test_max_scale, test_scale_precision, test_min_angle, test_max_angle, test_angle_precision, test_distortion_percentage, test_save_plots)

        optimizer_record = True

        self.optimizer_configuration = OptimizerConfiguration(optimizer_name, alpha=optimizer_alpha, beta1=optimizer_beta1, beta2=optimizer_beta2, record=optimizer_record)

        nn_h1 = nn_h1
        nn_h2 = nn_h2
        nn_l1 = nn_l1
        nn_l2 = nn_l2
        nn_mini_batch_size = 64
        nn_output = True

        self.nn_config = NeuralNetworkConfiguration(sizes=[self.dimensions_size**2, nn_h1, nn_h2, 1], lambda_l1=nn_l1, lambda_l2=nn_l2,
                                                    optimizer_configuration=self.optimizer_configuration, mini_batch_size=nn_mini_batch_size, output=nn_output)

        c_config = ClassifierConfiguration(f'{self.dimensions_size}_{target_name.name}_{self.nn_config.sizes[1]}_{self.nn_config.sizes[2]}_{self.nn_config.lambda_l1}_{self.nn_config.lambda_l2}_{self.nn_config.mini_batch_size}_{self.optimizer_configuration.name}_{self.optimizer_configuration.alpha}_{self.optimizer_configuration.beta1}_{self.optimizer_configuration.beta2}',
                                           os.path.join(self.root_path, 'parameters'),
                                           target_name, self.dimensions_size, self.train_dataset_config, self.test_dataset_config, self.nn_config)

        return Classifier(c_config)


from abc import ABC, abstractmethod
from typing import List, Any

class Tokenizer(ABC):

    @abstractmethod
    async def encode(self, text: str) -> List[Any]:
        raise NotImplementedError

    @abstractmethod
    def decode(self, raw_text: str, tokens: List[Any]) -> str:
        raise NotImplementedError


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 18-8-18 上午12:26
# @Author : lijia
# @File : base_views.py
# @Software: PyCharm

from django.http import HttpResponse
from django.shortcuts import render
from snow_ticket.models import *

def index(request):
    return render(request, 'index.html')

def snow_pack_views(request):
    snow_pack_id = request.GET.get('id', 1)
    # 雪票类型
    snow_ticket_type_map = dict()
    all_snow_ticket_type = SnowTicketType.objects.all()
    for one_type in all_snow_ticket_type:
        snow_ticket_type_map[one_type.id] = one_type.type_name
    # 雪场
    snow_pack_desc = SnowPack.objects.get(id=snow_pack_id).name

    # 雪票
    snow_ticket = list()
    all_ticket = SnowTicket.objects.filter(snow_pack_id=snow_pack_id)
    for one_snow_ticket in all_ticket:
        one_snow_ticket.product_desc = snow_ticket_type_map.get(one_snow_ticket.product_type, '')

    data = dict()
    data['snow_pack_desc'] = snow_pack_desc
    data['all_ticket'] = all_ticket

    return render(request, 'table_static.html', context=data)


# -*- coding: utf-8 -*-
"""
Created on Sat Feb 24 15:14:41 2018

@author: stefa
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Feb 21 17:48:19 2018

@author: stefa
"""

import numpy as np

def swap(mat, i, j):
    mat[i], mat[j] = mat[j], mat[i]
#eliminare swap

def find_pivot(mat, start, col):
    m = len(mat)  # m è il numero delle righe
    for i in range(start, m):
        if mat[i][col] != 0:
            return i
    return -1

def eliminazione(mat):
    nrighe=len(mat)
    ncolonne=len(mat[0])
    riga_pivot = 0
    col = 0
    minimo=min(nrighe,ncolonne)
    while riga_pivot < minimo:
        p = find_pivot(mat, riga_pivot, col) # p è l'indice di riga del pivot trovato
        while  p==-1 and col<ncolonne-1:
            col += 1
            p = find_pivot(mat, riga_pivot, col)
        if p != -1:
            swap(mat, riga_pivot, p)
            #divide
            for i in range(col,ncolonne):
                mat[riga_pivot][i] /= mat[riga_pivot][col]
            #sottrae
            for i in range(riga_pivot+1,nrighe):
                a=mat[i][col]
                mat[i] =[ x-a*mat[riga_pivot][j] if j>=col else 0  for j,x in enumerate(mat[i])]
        riga_pivot +=1

if __name__ == '__main__':
    a = [[0,3,0,6,0],[0,0,0,8,7],[0,0,0,9,6]]
    print(np.matrix(a))
    print()
    eliminazione(a)
    print(np.matrix(a))

    print()
    print()
    print()
    b = [[10, 0, 7, 9], [12,23,45, 34], [60, 55, 7, 5], [7,3,65,7]]
    c = [[1,2,3,4],[1,2,3,4],[4,5,6,10],[7,8,9,10]]
    d = [[0,0,0],[0,0,1]]
    eliminazione(a)
    eliminazione(b)
    eliminazione(c)
    eliminazione(d)
    print(np.matrix(a))
    print(np.matrix(b))
    print(np.matrix(c))
    print(np.matrix(d))


# source https://www.youtube.com/playlist?list=PLEsfXFp6DpzRyxnU-vfs3vk-61Wpt7bOS

import cv2
import os

# source: https://stackoverflow.com/a/44659589
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    (h, w) = image.shape[:2]
    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        return image
    # check to see if the width is None
    if width is None:
        # calculate the ratio of the height and construct the
        # dimensions
        r = height / float(h)
        dim = (int(w * r), height)
    # otherwise, the height is None
    else:
        # calculate the ratio of the width and construct the
        # dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # resize the image
    resized = cv2.resize(image, dim, interpolation = inter)
    # return the resized image
    return resized
def alpha_blend(frame_1, frame_2, mask):
    alpha = mask/255.0
    blended = cv2.convertScaleAbs(frame_1*(1-alpha) + frame_2*alpha)
    return blended

def apply_circle_focus_blur(frame, intensity=0.2):
    frame_h, frame_w, frame_c = frame.shape
    y = int(frame_h/2)
    x = int(frame_w/2)

    mask = np.zeros((frame_h, frame_w, 4), dtype='uint8')
    cv2.circle(mask, (x, y), int(y/2), (255,255,255), -1, cv2.LINE_AA)
    mask = cv2.GaussianBlur(mask, (21,21),11 )

    blured = cv2.GaussianBlur(frame, (21,21), 11)
    blended = alpha_blend(frame, blured, 255-mask)
    frame = cv2.cvtColor(blended, cv2.COLOR_BGRA2BGR)
    return frame

def portrait_mode(frame):
#    cv2.imshow('frame', frame)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    _, mask = cv2.threshold(gray, 120,255,cv2.THRESH_BINARY)
    mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGRA)
    blured = cv2.GaussianBlur(frame, (21,21), 11)
    blended = alpha_blend(frame, blured, mask)
    frame = cv2.cvtColor(blended, cv2.COLOR_BGRA2BGR)
    return frame

class CFEVideoConf(object):
    # Standard Video Dimensions Sizes
    STD_DIMENSIONS =  {
        "360p": (480, 360),
        "480p": (640, 480),
        "720p": (1280, 720),
        "1080p": (1920, 1080),
        "4k": (3840, 2160),
    }
    # Video Encoding, might require additional installs
    # Types of Codes: http://www.fourcc.org/codecs.php
    VIDEO_TYPE = {
        'avi': cv2.VideoWriter_fourcc(*'XVID'),
        #'mp4': cv2.VideoWriter_fourcc(*'H264'),
        'mp4': cv2.VideoWriter_fourcc(*'XVID'),
    }

    width           = 640
    height          = 480
    dims            = (640, 480)
    capture         = None
    video_type      = None
    def __init__(self, capture, filepath, res="480p", *args, **kwargs):
        self.capture = capture
        self.filepath = filepath
        self.width, self.height = self.get_dims(res=res)
        self.video_type = self.get_video_type()

    # Set resolution for the video capture
    # Function adapted from https://kirr.co/0l6qmh
    def change_res(self, width, height):
        self.capture.set(3, width)
        self.capture.set(4, height)

    def get_dims(self, res='480p'):
        width, height = self.STD_DIMENSIONS['480p']
        if res in self.STD_DIMENSIONS:
            width, height = self.STD_DIMENSIONS[res]
        self.change_res(width, height)
        self.dims = (width, height)
        return width, height

    def get_video_type(self):
        filename, ext = os.path.splitext(self.filepath)
        if ext in self.VIDEO_TYPE:
          return  self.VIDEO_TYPE[ext]
        return self.VIDEO_TYPE['avi']