# Databricks notebook source
#Importación de modulos

from pyspark.sql import SparkSession

#Michael Moriano Cruz
#Creamos las session de apache spark en una variable

spark = SparkSession.builder.getOrCreate()

# COMMAND ----------

#variable de la sesión
#Michael Moriano Cruz
spark

# COMMAND ----------

spark.stop()

# COMMAND ----------

#creamos una lista
lista = ['Apache Spark', 'Curso de Introducción', 'Big data', 'Cloud']

# COMMAND ----------

#Crear RDD utilizando parallelize

rdd=spark.sparkContext.parallelize(lista)

# COMMAND ----------

rdd.collect()

# COMMAND ----------

rddempty = spark.sparkContext.emptyRDD

# COMMAND ----------

rddempty.collect()

# COMMAND ----------

rdd3 = spark.sparkContext.wholeTextFiles("/path/textFile.txt")

# COMMAND ----------

rdd = spark.sparkContext.textFile("gs://curso-introduccion-apache-spark/datalake/persona.data")

# COMMAND ----------

rdd2 = rdd.flatMap(lambda x: x.split("|"))

# COMMAND ----------

rdd3 = rdd2.map(lambda x: (x,1))

# COMMAND ----------

data = [("Java", "20000"), ("Python", "100000"), ("Scala", "3000")]

rdd = spark.sparkContext.parallelize(data)

#convetir RDD A DATAFRAME
dfFromRDD1 = rdd.toDF()

#printschema () produce la siguiente salida.
dfFromRDD1.printSchema()

# COMMAND ----------

columns = ["language","users_count"]
dfFromRDD1 = rdd.toDF(columns)

#Esto produce un esquema del DataFrame con nombres de columna.
dfFromRDD1.printSchema()

# COMMAND ----------

dfFromData2 = spark.createDataFrame(data).toDF(*columns)

# COMMAND ----------

dfFromData2.show()

# COMMAND ----------

from pyspark.sql.types import StructType,StructField, StringType, IntegerType

data2 = [("James","","Smith","36636","M",3000),
    ("Michael","Rose","","40288","M",4000),
    ("Robert","","Williams","42114","M",4000),
    ("Maria","Anne","Jones","39192","F",4000),
    ("Jen","Mary","Brown","","F",-1)
  ]

schema = StructType([ \
    StructField("firstname",StringType(),True), \
    StructField("middlename",StringType(),True), \
    StructField("lastname",StringType(),True), \
    StructField("id", StringType(), True), \
    StructField("gender", StringType(), True), \
    StructField("salary", IntegerType(), True) \
  ])

df = spark.createDataFrame(data=data2,schema=schema)
df.printSchema()
df.show(truncate=False)

# COMMAND ----------

from pyspark.sql.types import *
ruta = 'gs://curso-introduccion-apache-spark/datalake/persona.data'

df_schema = StructType([
StructField("ID", StringType(),True),
StructField("NOMBRE", StringType(),True),
StructField("TELEFONO", StringType(),True),
StructField("CORREO", StringType(),True),
StructField("FECHA_INGRESO", StringType(),True),
StructField("EDAD", IntegerType(),True),
StructField("SALARIO", DoubleType(),True),
StructField("ID_EMPRESA", StringType(),True),
])

df_with_schema = spark.read.format("CSV").option("header","true").option("delimiter","|").schema(df_schema).load(ruta)

# COMMAND ----------

df_with_schema.show()

# COMMAND ----------

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType,StructField, StringType, IntegerType

spark = SparkSession.builder.master("local[1]") \
                    .appName('SparkByExamples.com') \
                    .getOrCreate()

data = [("James","","Smith","36636","M",3000),
    ("Michael","Rose","","40288","M",4000),
    ("Robert","","Williams","42114","M",4000),
    ("Maria","Anne","Jones","39192","F",4000),
    ("Jen","Mary","Brown","","F",-1)
  ]

schema = StructType([ \
    StructField("firstname",StringType(),True), \
    StructField("middlename",StringType(),True), \
    StructField("lastname",StringType(),True), \
    StructField("id", StringType(), True), \
    StructField("gender", StringType(), True), \
    StructField("salary", IntegerType(), True) \
  ])

df = spark.createDataFrame(data=data,schema=schema)
df.printSchema()
df.show(truncate=False)

# COMMAND ----------

import pyspark
from pyspark.sql import SparkSession

spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()
data = [("James","Smith","USA","CA"),
    ("Michael","Rose","USA","NY"),
    ("Robert","Williams","USA","CA"),
    ("Maria","Jones","USA","FL")
  ]
columns = ["firstname","lastname","country","state"]
df = spark.createDataFrame(data = data, schema = columns)
df.show(truncate=False)

# COMMAND ----------

df.select("firstname","lastname").show()

df.select(df.firstname,df.lastname).show()

df.select(df["firstname"],df["lastname"]).show()

#Usando Función col()
from pyspark.sql.functions import col
df.select(col("firstname"),col("lastname")).show()

#seleccionando  columnas  con   expresion regular
df.select(df.colRegex("`^.*name*`")).show()

# COMMAND ----------

data = [('James','','Smith','1991-04-01','M',3000),
  ('Michael','Rose','','2000-05-19','M',4000),
  ('Robert','','Williams','1978-09-05','M',4000),
  ('Maria','Anne','Jones','1967-12-01','F',4000),
  ('Jen','Mary','Brown','1980-02-17','F',-1)
]

columns = ["firstname","middlename","lastname","dob","gender","salary"]
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()
df = spark.createDataFrame(data=data, schema = columns)

# COMMAND ----------

df.withColumn("salary",col("salary").cast("Integer")).show()

#Actualizar el valor existente

df.withColumn("salary",col("salary")*100).show()

#Crear una nueva columna a partir de una existente

df.withColumn("CopiedColumn",col("salary")* -1).show()

#Renombrar una columna
df.withColumnRenamed("gender","sex") \
  .show(truncate=False)

#Eliminar una columna

df.drop("salary").show()

# COMMAND ----------

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, lit
from pyspark.sql.types import StructType, StructField, StringType,IntegerType

spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()

data = [('James','','Smith','1991-04-01','M',3000),
  ('Michael','Rose','','2000-05-19','M',4000),
  ('Robert','','Williams','1978-09-05','M',4000),
  ('Maria','Anne','Jones','1967-12-01','F',4000),
  ('Jen','Mary','Brown','1980-02-17','F',-1)
]

columns = ["firstname","middlename","lastname","dob","gender","salary"]
df = spark.createDataFrame(data=data, schema = columns)
df.printSchema()
df.show(truncate=False)

df2 = df.withColumn("salary",col("salary").cast("Integer"))
df2.printSchema()
df2.show(truncate=False)

df3 = df.withColumn("salary",col("salary")*100)
df3.printSchema()
df3.show(truncate=False)

df4 = df.withColumn("CopiedColumn",col("salary")* -1)
df4.printSchema()

df5 = df.withColumn("Country", lit("USA"))
df5.printSchema()

df6 = df.withColumn("Country", lit("USA")) \
   .withColumn("anotherColumn",lit("anotherValue"))
df6.printSchema()

df.withColumnRenamed("gender","sex") \
  .show(truncate=False)

df4.drop("CopiedColumn") \
.show(truncate=False)

# COMMAND ----------

from pyspark.sql.types import StructType,StructField
from pyspark.sql.types import StringType, IntegerType, ArrayType
data = [
    (("James","","Smith"),["Java","Scala","C++"],"OH","M"),
    (("Anna","Rose",""),["Spark","Java","C++"],"NY","F"),
    (("Julia","","Williams"),["CSharp","VB"],"OH","F"),
    (("Maria","Anne","Jones"),["CSharp","VB"],"NY","M"),
    (("Jen","Mary","Brown"),["CSharp","VB"],"NY","M"),
    (("Mike","Mary","Williams"),["Python","VB"],"OH","M")
 ]

schema = StructType([
     StructField('name', StructType([
        StructField('firstname', StringType(), True),
        StructField('middlename', StringType(), True),
         StructField('lastname', StringType(), True)
     ])),
     StructField('languages', ArrayType(StringType()), True),
     StructField('state', StringType(), True),
     StructField('gender', StringType(), True)
 ])

df = spark.createDataFrame(data = data, schema = schema)
df.printSchema()
df.show(truncate=False)

# COMMAND ----------

# Use igual condicion
df.filter(df.state == "OH").show(truncate=False)

# not equals condition
df.filter(df.state != "OH").show(truncate=False)
df.filter(~(df.state == "OH")).show(truncate=False)

#Using SQL col() function
from pyspark.sql.functions import col
df.filter(col("state") == "OH") \
    .show(truncate=False)

#Using SQL Expression
df.filter("gender == 'M'").show()

#For not equal
df.filter("gender != 'M'").show()
df.filter("gender <> 'M'").show()

## Filter multiple condition
df.filter( (df.state  == "OH") & (df.gender  == "M") ) \
    .show(truncate=False)

#Filter IS IN List values
li=["OH","CA","DE"]
df.filter(df.state.isin(li)).show()

# like - SQL LIKE pattern
df2.filter(df2.name.like("%rose%")).show()

# COMMAND ----------

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import expr
spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()

data = [("James", "Sales", 3000), \
    ("Michael", "Sales", 4600), \
    ("Robert", "Sales", 4100), \
    ("Maria", "Finance", 3000), \
    ("James", "Sales", 3000), \
    ("Scott", "Finance", 3300), \
    ("Jen", "Finance", 3900), \
    ("Jeff", "Marketing", 3000), \
    ("Kumar", "Marketing", 2000), \
    ("Saif", "Sales", 4100) \
  ]
columns= ["employee_name", "department", "salary"]
df = spark.createDataFrame(data = data, schema = columns)
df.printSchema()
df.show(truncate=False)

#Distinct
distinctDF = df.distinct()
print("Distinct count: "+str(distinctDF.count()))
distinctDF.show(truncate=False)

#Drop duplicates
df2 = df.dropDuplicates()
print("Distinct count: "+str(df2.count()))
df2.show(truncate=False)

#Drop duplicates on selected columns
dropDisDF = df.dropDuplicates(["department","salary"])
print("Distinct count of department salary : "+str(dropDisDF.count()))
dropDisDF.show(truncate=False)

# COMMAND ----------

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, asc,desc

spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()

simpleData = [("James","Sales","NY",90000,34,10000), \
    ("Michael","Sales","NY",86000,56,20000), \
    ("Robert","Sales","CA",81000,30,23000), \
    ("Maria","Finance","CA",90000,24,23000), \
    ("Raman","Finance","CA",99000,40,24000), \
    ("Scott","Finance","NY",83000,36,19000), \
    ("Jen","Finance","NY",79000,53,15000), \
    ("Jeff","Marketing","CA",80000,25,18000), \
    ("Kumar","Marketing","NY",91000,50,21000) \
  ]
columns= ["employee_name","department","state","salary","age","bonus"]

df = spark.createDataFrame(data = simpleData, schema = columns)

df.printSchema()
df.show(truncate=False)

df.sort("department","state").show(truncate=False)
df.sort(col("department"),col("state")).show(truncate=False)

df.orderBy("department","state").show(truncate=False)
df.orderBy(col("department"),col("state")).show(truncate=False)

df.sort(df.department.asc(),df.state.asc()).show(truncate=False)
df.sort(col("department").asc(),col("state").asc()).show(truncate=False)
df.orderBy(col("department").asc(),col("state").asc()).show(truncate=False)

df.sort(df.department.asc(),df.state.desc()).show(truncate=False)
df.sort(col("department").asc(),col("state").desc()).show(truncate=False)
df.orderBy(col("department").asc(),col("state").desc()).show(truncate=False)

df.createOrReplaceTempView("EMP")
spark.sql("select employee_name,department,state,salary,age,bonus from EMP ORDER BY department asc").show(truncate=False)

# COMMAND ----------

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import col,sum,avg,max

spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()

simpleData = [("James","Sales","NY",90000,34,10000),
    ("Michael","Sales","NY",86000,56,20000),
    ("Robert","Sales","CA",81000,30,23000),
    ("Maria","Finance","CA",90000,24,23000),
    ("Raman","Finance","CA",99000,40,24000),
    ("Scott","Finance","NY",83000,36,19000),
    ("Jen","Finance","NY",79000,53,15000),
    ("Jeff","Marketing","CA",80000,25,18000),
    ("Kumar","Marketing","NY",91000,50,21000)
  ]

schema = ["employee_name","department","state","salary","age","bonus"]
df = spark.createDataFrame(data=simpleData, schema = schema)
df.printSchema()
df.show(truncate=False)

df.groupBy("department").sum("salary").show(truncate=False)

df.groupBy("department").count().show(truncate=False)

df.groupBy("department","state") \
    .sum("salary","bonus") \
   .show(truncate=False)

df.groupBy("department") \
    .agg(sum("salary").alias("sum_salary"), \
         avg("salary").alias("avg_salary"), \
         sum("bonus").alias("sum_bonus"), \
         max("bonus").alias("max_bonus") \
     ) \
    .show(truncate=False)

df.groupBy("department") \
    .agg(sum("salary").alias("sum_salary"), \
      avg("salary").alias("avg_salary"), \
      sum("bonus").alias("sum_bonus"), \
      max("bonus").alias("max_bonus")) \
    .where(col("sum_bonus") >= 50000) \
    .show(truncate=False)

# COMMAND ----------

df=spark.read.option("header",True).csv("/tmp/resources/simple-zipcodes.csv")

df.printSchema()

#partitionBy()
df.write.option("header",True) \
        .partitionBy("state") \
        .mode("overwrite") \
        .csv("/tmp/zipcodes-state")

# COMMAND ----------


""""
Task 1 - Social Distribution
"""
from sys import maxsize
population = list(map(int,input().split(", ")))
minw = int(input())
if sum(population) < minw * len(population):
    print("No equal distribution possible")
else:
    while True:
        poorest = (maxsize, -1)
        richest = (-maxsize, -1)
        for ind, person in enumerate(population):
            if person < poorest[0]:
                poorest = (person, ind)
            if person > richest[0]:
                richest = (person, ind)
        if poorest[0] < minw:
            diff = minw - poorest[0]
            population[poorest[1]] += diff
            population[richest[1]] -= diff
        else:
            break
    print(population)


from django.urls import path, include
from rest_framework.authtoken import views
from rest_framework.routers import SimpleRouter
from .views import PostViewSet, GroupViewSet, CommentViewSet, FollowViewSet

router = SimpleRouter()
router_comments = SimpleRouter()

router.register('posts', PostViewSet, basename='posts')
router.register('groups', GroupViewSet, basename='groups')
router.register('follow', FollowViewSet, basename='follow')
router_comments.register('comments', CommentViewSet, basename='comments')

urlpatterns = [
    path('v1/', include(router.urls)),
    path('v1/api-token-auth/', views.obtain_auth_token),
    path('v1/posts/<int:post_pk>/', include(router_comments.urls)),
    path('v1/', include('djoser.urls')),
    path('v1/', include('djoser.urls.jwt')),
]


"""
    Methods that help in scraping the data from debian wiki

"""

import os

import requests
from bs4 import BeautifulSoup

def response_status(response):
    """
    checks the response status of the request url
    """
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as http_error:
        print(f"HTTP error occurred: {http_error}")
    except requests.exceptions.Timeout as timeout_error:
        print(f"Request timed out: {timeout_error}")
    except requests.exceptions.RequestException as request_error:
        print(f"An error occurred during the HTTP request: {request_error}")

def all_links_modifier(link, default_url, url):
    """
    Modifies all the links in the page as expected
    """
    other_links = ["news", "press/", "project/", "awards"]

    if link.startswith("../"):
        return default_url + link[3:]
    if (
        link.startswith("index")
        or any(link.startswith(str(year) + "/") for year in range(2023, 1996, -1))
        or link in other_links
    ):
        return f" {url + link} "
    if link == "/":
        return f" {default_url} "
    return f" {link} "

def extract_languages(soup2, default_url, url):
    """
    Generate the Markdown language content with embedded links
    """
    language_links = soup2.find("div", {"id": "langContainer"}).find_all("a")

    markdown_content = (
        "***\n\n This page is also available in the following languages:\n"
    )
    default_language_link = soup2.find("a", href="../intro/cn")["href"]
    default_language_link = default_url + default_language_link[3:]
    markdown_content += (
        f"\n\nHow to set [the default document language]({default_language_link})"
    )

    for link in language_links:
        language_name = link.get_text()
        language_url = link["href"]
        language_url = all_links_modifier(language_url, default_url, url)
        markdown_content += f" \n\n [{language_name}]({language_url})\n"

    return markdown_content

def extract_footer(soup2, defaul_url, url):
    """
    Extract and format the footermap content
    """
    footermap = soup2.find("div", id="footermap")
    footer_links = footermap.find_all("a")

    formatted_footer = ""
    for link in footer_links:
        link_text = link.get_text(strip=True)
        href = link["href"]
        href = all_links_modifier(href, defaul_url, url)
        formatted_footer += f" \n\n [{link_text}]({href})\n"

    return formatted_footer

def extract_text_with_links(element, default_url, url):
    """
    extracts texts that have links
    """
    text = ""

    for item in element:
        if isinstance(item, str):
            text += item.strip()

            if text == "Latest News":
                text = ""
        elif item.name == "a":
            link_text = item.get_text(strip=True)
            href = item["href"]
            href = all_links_modifier(href, default_url, url)
            text += f"[{link_text}]({href})"
            if text == "[Skip Quicknav]( #content )":
                text = ""

    return text

def extract_paragraphs(languages, footers, soup, default_url, url):
    """
    Find all <p> elements
    Initialize an empty list to store the formatted text
    """

    paragraphs = soup.find_all("p")

    formatted_text = []

    executed = False
    for paragraph in paragraphs:
        text = extract_text_with_links(paragraph, default_url, url)

        if (
            text.startswith("Back to the[Debian Project homepage]")
            and executed is False
        ):
            text = f"*** \n Back to the[Debian Project homepage]({default_url})."
            text += f"\n {languages} \n *** \n\n {footers}"
            executed = True

        formatted_text.append(text)

    formatted_output = "\n\n".join(formatted_text)

    return formatted_output

def save_to_markdown_file(paragraph_output, soup, default_url, url, file_path):
    """
    save the content into a markdown file
    """
    tt_elements = soup.find_all("tt")
    strong_elements = soup.find_all("strong")

    title = soup.find(href="2023/")
    with open(file_path, "w", encoding="UTF-8") as file:
        file.write(f"# {title.get_text(strip=True)}\n\n")
        for tt_item, strong in zip(tt_elements, strong_elements):
            tt_content = tt_item.get_text(strip=True)
            strong_content = strong.a.get_text(strip=True)
            href = strong.a["href"]
            href = all_links_modifier(href, default_url, url)
            file.write(f"{tt_content} [{strong_content}]({href})\n<br>\n")
        file.write("***")

        file.write(f"{paragraph_output}")
        print("Content saved to 'debian_news.md'")
        file.close()

def process_debian_news(url, default_url):
    """
    Processes the whole debian news content page
    """

    response = requests.get(url, timeout=(3, 5))
    # print(response)

    response_status(response)

    soup = BeautifulSoup(response.content, "html.parser")

    soup2 = BeautifulSoup(response.content, "html.parser")

    parent_dir_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

    file_path = os.path.join(parent_dir_path, "debian_news.md")

    try:
        languages_content = extract_languages(soup2, default_url, url)
        footer_content = extract_footer(soup2, default_url, url)
        formatted_content = extract_paragraphs(
            languages_content, footer_content, soup, default_url, url
        )

        save_to_markdown_file(formatted_content, soup, default_url, url, file_path)
    except Exception as error:  # pylint: disable=broad-except
        error_message = str(error)
        print(f"An unexpected error occurred: {error_message}")

if __name__ == "__main__":
    DEFAULT_URL = "https://www.debian.org/"
    URL = "https://www.debian.org/News/"

    process_debian_news(URL, DEFAULT_URL)


'''
Created on 02-Sep-2018

@author: DX
'''

# We will start with importing the libraries
import logging
import time

# Of course grpc will be rquired for communication
import grpc
from grpc import RpcError

# predict_client will help us to use the client services
# without having tensorflow on the client system
from predict_client.pbs.prediction_service_pb2 import PredictionServiceStub
from predict_client.pbs.predict_pb2 import PredictRequest
from predict_client.util import predict_response_to_dict, make_tensor_proto

# Let's write a clas ProdClient
class ProdClient:
    def __init__(self, host, model_name, model_version):

        # Input to the class will be host id
        # model's name and its version

        # Load the value in self so that it will be
        # visible to each defination in the package
        self.logger = logging.getLogger(self.__class__.__name__)
        self.host = host
        self.model_name = model_name
        self.model_version = model_version

    # Here will be the predict method which will load the model and
    # make prediction for us whenever get called
    def predict(self, request_data, request_timeout=10):
        # SELF: is the class variable
        # REQUEST_DATA: is the input to the model
        # REQUEST_TIMEOUT: Time in seconds after which request will end

        # Start with logger informations
        self.logger.info('Sending request to tfserving model')
        self.logger.info('Host: {}'.format(self.host))
        self.logger.info('Model name: {}'.format(self.model_name))
        self.logger.info('Model version: {}'.format(self.model_version))

        # Create gRPC client and request
        t = time.time()
        channel = grpc.insecure_channel(self.host)
        self.logger.debug('Establishing insecure channel took: {}'.format(time.time() - t))

        # We will check the time taken for each operation
        t = time.time()
        stub = PredictionServiceStub(channel)
        self.logger.debug('Creating stub took: {}'.format(time.time() - t))

        t = time.time()
        request = PredictRequest()
        self.logger.debug('Creating request object took: {}'.format(time.time() - t))

        # Here we will define the model name
        request.model_spec.name = self.model_name

        # Specific version of the model
        if self.model_version > 0:
            request.model_spec.version.value = self.model_version

        t = time.time()
        # Put inputs in the request
        for d in request_data:
            tensor_proto = make_tensor_proto(d['data'],
                                    d['in_tensor_dtype'])
            request.inputs[\
                d['in_tensor_name']].CopyFrom(tensor_proto)

        self.logger.debug(\
            'Making tensor protos took: {}'.format(time.time() - t))

        # Now we are ready for prediction following line will help us in that
        try:
            t = time.time()
            predict_response = stub.Predict(request,
                                timeout=request_timeout)

            self.logger.debug(\
           'Actual request took: {} seconds'.format(time.time() - t))

            predict_response_dict = \
            predict_response_to_dict(predict_response)

            keys = [k for k in predict_response_dict]
            self.logger.info('\
            Got predict_response with keys: {}'.format(keys))

            return predict_response_dict

        except RpcError as e:
            self.logger.error(e)
            self.logger.error('Prediction failed!')

        return {}


"""Pytest fixtures to keep the test database clean each time tests are run."""
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator

import alembic.config
import sqlalchemy
from _pytest.config.argparsing import Parser
from _pytest.fixtures import FixtureRequest, fixture
from alembic import command
from sqlalchemy.orm import Session

def pytest_addoption(parser: Parser) -> None:
    """Add CLI overrides for where to run alembic commands.

    Args:
        parser: pytest's command line argument parser to add options to.
    """
    parser.addoption(
        "--alembic-folder",
        default=Path("./migrations"),
        type=Path,
        help="folder (from the test root) where alembic commands should be run from",
    )

@fixture(scope="session")
def _with_test_db(
    database_engine: sqlalchemy.engine.Engine, request: FixtureRequest
) -> None:
    """Fixture for database setup before running tests.

    Args:
        database_engine: SQLAlchemy engine to use for cleaning up the database.
        request: Pytest request to get configuration from.
    """
    with _real_db_session(database_engine) as session:
        _wipe_db_schema(session)
    alembic_folder = Path(request.config.rootdir) / request.config.getoption(
        "--alembic-folder"
    )
    _run_db_schema_migrations(str(database_engine.url), str(alembic_folder))

@fixture(scope="function")
def test_db_session(
    database_engine: sqlalchemy.engine.Engine, _with_test_db
) -> Iterator[Session]:
    """Create a database session that will rollback even if commits are issued during the test.

    Args:
        database_engine: SQLAlchemy engine to use to connect to the database.

    Returns:
        Test-ready automatically rolled back database session. Should be used to override any
            production sessions used in the application.
    """
    # get a new database transaction to wrap any transactions used in the test or tested code
    connection = database_engine.connect()
    transaction = connection.begin()
    # bind the database session to this outer transaction
    session = Session(bind=connection)
    yield session
    # rollback everything the test committed and clean up
    transaction.rollback()
    connection.close()
    session.close()

def _wipe_db_schema(session: Session, schema: str = "public") -> None:
    """Wipes everything in a database's schema.

    Args:
        session: SQLAlchemy database session to use.
        schema: Name of the database schema which needs to be wiped.
    """
    session.execute(f"DROP SCHEMA IF EXISTS {schema} CASCADE;")
    session.commit()
    session.execute(f"CREATE SCHEMA {schema};")
    session.commit()

def _run_db_schema_migrations(sqlalchemy_url: str, script_location: str) -> None:
    """Runs Alembic migrations to build the database schema.

    Args:
        sqlalchemy_url: Connection string of the database to migrate.
        script_location: Path to the folder where alembic scripts can be run.
    """
    config = alembic.config.Config()
    config.set_main_option("script_location", script_location)
    config.set_main_option("sqlalchemy.url", sqlalchemy_url)
    # heads means all migrations from all branches (in case there are split branches)
    command.upgrade(config, "heads")

@contextmanager
def _real_db_session(engine: sqlalchemy.engine.Engine) -> Iterator[Session]:
    """Context manager for a normal SQLAlchemy session to be used during before test setup.

    Args:
        engine: SQLAlchemy engine to use to connect to the database.

    Returns:
        Self-closing SQLAlchemy session.
    """
    connection = engine.connect()
    session = Session(bind=connection)
    yield session
    connection.close()
    session.close()


import argparse
import sys
import tensorflow as tf
import numpy as np
import multiprocessing
from tensorflow.python.ops import rnn
from collections import deque
from models_tf import RnnCategorial, CnnCategorial
from reader import read_datasets, make_dense

parser = argparse.ArgumentParser(
    description="train a demographics predictor. File formats: CoNLL, plus one line with CSV demographic values")
parser.add_argument('--train', help='train files', required=True)
parser.add_argument('--dev', help='dev files', required=True)
parser.add_argument('--test', help='test files', required=True)
parser.add_argument('--architecture', '-a', help="Model architecture", choices=['lstm', 'cnn'])
# parser.add_argument('--embeddings', help='embeddings files (word2vec format)', default=None)
parser.add_argument('--target', help='predict age, gender, both of them, or the joint cross-product',
                    choices=('age', 'gender', 'both', 'joint'), default='both')
# parser.add_argument('--trainer', help='which training algorithm to use',
#                     choices=('adagrad', 'sgd', 'adam', 'adadelta', 'momentum'), default='adam')
parser.add_argument('--num-epochs', help='Number of epochs', default=50)
# parser.add_argument('--status', help='number of processed instances between status updates', default=10000, type=int)
# parser.add_argument('--noise', help='amount of noise added to embeddings', default=0.1, type=float)
parser.add_argument('--dim-rnn', help='dimensionality of hidden RNN layer', default=50, type=int)
parser.add_argument('--dim-emb', help='dimensionality of word embeddings', default=100, type=int)
parser.add_argument('--dim-out', help='dimensionality of output layer', default=32, type=int)
# parser.add_argument('--dropout', help='dropout probability for final sentence representation', default=0.0, type=float)
parser.add_argument('--batch-size', help='batch size', default=1, type=int)
parser.add_argument('--max-len', help='Maximum length of input', default=100, type=int)
args = parser.parse_args()

# Read in data, mapping words to integers
datasets = read_datasets(args.train, args.test, args.dev, args.target)
train_dataset = datasets['train']
dev_dataset = datasets['dev']
test_dataset = datasets['test']
word_mapper = datasets['word_mapper']
label_mapper = datasets['label_mapper']

word_embs = tf.Variable(tf.random_uniform([len(word_mapper), args.dim_emb], -1, 1, tf.float32),
                        name='word_embs')

# Create dense representations of datasets
X_train, y_train = make_dense(train_dataset['sentences'], args.max_len,
                              train_dataset['labels'], len(label_mapper))
train_sent_lens = np.array([len(sent) for sent in train_dataset['sentences']])
X_dev, y_dev = make_dense(dev_dataset['sentences'], args.max_len,
                          dev_dataset['labels'], len(label_mapper))
dev_sent_lens = np.array([len(sent) for sent in dev_dataset['sentences']])
X_test, y_test= make_dense(test_dataset['sentences'], args.max_len,
                          test_dataset['labels'], len(label_mapper))
test_sent_lens = np.array([len(sent) for sent in test_dataset['sentences']])

assert len(X_train) == len(y_train)
assert len(X_train) == len(train_sent_lens)

# Setup model
if args.architecture == 'lstm':
    rnn_cell_type = rnn.rnn_cell.LSTMCell(args.dim_rnn, args.dim_emb)
    model = RnnCategorial(word_embs=word_embs, cell=rnn_cell_type,
                          num_labels=len(label_mapper),
                          max_seq_len=args.max_len)
else:
    model = CnnCategorial(word_embs=word_embs,
                          num_labels=len(label_mapper),
                          max_seq_len=args.max_len)

NUM_THREADS = min(10, multiprocessing.cpu_count())
with tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=NUM_THREADS, inter_op_parallelism_threads=NUM_THREADS)) as sess:
# with tf.Session() as sess:
    # Try ADAM out for training
    global_step = tf.Variable(0, name="global_step", trainable=False)

    optimizer = tf.train.AdamOptimizer(1e-4)
    grads_and_vars = optimizer.compute_gradients(model.loss)
    train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)

    sess.run(tf.initialize_all_variables())

    # Batches
    indices = np.arange(len(X_train))
    last_index = len(indices) - (len(indices) % args.batch_size)
    indices = indices[:last_index]

    # Validation set performance
    dev_feed = {model.y_indicator: y_dev,
                model.y: dev_dataset['labels'],
                model.input_lengths: dev_sent_lens,
                model.input: X_dev,
                model.dropout_keep_p: 1}

    # Test set performance
    test_feed = {model.y_indicator: y_test,
                model.y: test_dataset['labels'],
                model.input_lengths: test_sent_lens,
                model.input: X_test,
                model.dropout_keep_p: 1}

    stat_size = 50
    num_correct_list = deque(maxlen=stat_size)
    losses = deque(maxlen=stat_size)
    # num_total = deque(50)

    for epoch in range(1, args.num_epochs + 1):
        print("ITERATION {}".format(epoch), file=sys.stderr)
        np.random.shuffle(indices)

        # Train loop
        for i in range(0, len(indices), args.batch_size):
            batch_indices = indices[i:i+args.batch_size]

            feed_dict = {model.y_indicator: y_train[batch_indices],
                         model.y: [train_dataset['labels'][i] for i in batch_indices],
                         model.input_lengths: train_sent_lens[batch_indices],
                         model.input: X_train[batch_indices],
                         model.dropout_keep_p: 0.5}
            #
            _, loss_batch, num_correct_batch = sess.run([train_op, model.loss, model.num_correct], feed_dict)

            # num_total += len(batch_indices)
            num_correct_list.append(num_correct_batch)
            losses.append(loss_batch)
            if i % 500 == 0:
                moving_avg = sum(num_correct_list) / (len(num_correct_list) * args.batch_size)
                print("Epoch {}, instance {} (Batch size {}). Loss {} / Cumulative training accuracy {}".format(epoch, i, args.batch_size, sum(losses), moving_avg), file=sys.stderr)

        preds_dev, num_correct_dev = sess.run([model.preds, model.num_correct], dev_feed)
        preds_test, num_correct_test = sess.run([model.preds, model.num_correct], test_feed)
        print("Epoch {}: Development set performance: {}".format(epoch, num_correct_dev / len(X_dev)), file=sys.stdout, flush=True)
        print("Epoch {}: Test set performance: {}".format(epoch, num_correct_test / len(X_test)), file=sys.stdout, flush=True)


# import line
#################################
import sys
import math
import copy
import ast
import re
import statistics as st
from decimal import *
from collections import *
from itertools import *
from heapq import *
#################################

def sol(pl, pr):
    arr = []
    if pr == 1:
        print(-1)
    else:
        for i in range(pl, pr+1):
            if i == 1:
                continue
            if i == 2:
                arr.append(i)
                continue
            if i == 3:
                arr.append(i)
                continue
            for j in range(2, int(math.sqrt(i))+1):
                if i % j == 0:
                    break
                if j+1 == int(math.sqrt(i))+1:
                    arr.append(i)
        if len(arr) == 0:
            print(-1)
        else:
            print(sum(arr))
            print(min(arr))

if __name__ == '__main__':
    input = sys.stdin.readline

    # Input
    pl, pr = int(input()), int(input())

    # Output
    sol(pl, pr)


import numpy as np
import vg

def test_principal_components():
    # Example from http://www.iro.umontreal.ca/~pift6080/H09/documents/papers/pca_tutorial.pdf
    coords = np.array(
        [
            [2.5, 2.4],
            [0.5, 0.7],
            [2.2, 2.9],
            [1.9, 2.2],
            [3.1, 3.0],
            [2.3, 2.7],
            [2, 1.6],
            [1, 1.1],
            [1.5, 1.6],
            [1.1, 0.9],
        ]
    )

    pcs = vg.principal_components(coords)

    assert pcs.shape == (2, 2)
    np.testing.assert_array_almost_equal(pcs[0], np.array([-0.677873, -0.735179]))

    first_pc = vg.major_axis(coords)

    np.testing.assert_array_almost_equal(first_pc, np.array([-0.677873, -0.735179]))


"""
pip install streamlit redis

streamlit run --server.port 7860 --server.address 0.0.0.0 app.py
http://203.253.25.176:35010 으로 접속
"""
import streamlit as st
import pymongo, json
from streamlit_sortables import sort_items

import random
from dataclasses import dataclass

st.set_page_config(layout="wide")

def count_works(db, annotator):
    return db.count_documents({"annotator": annotator})
    # return db.estimated_document_count({"annotator": annotator})

def sample_random(db):
    print("sample")
    item = db.aggregate([
        { "$match": { "state": None } },
        { "$sample": { "size": 1 } }
    ])
    item = list(item)
    if len(item) > 0:
        item = item[0]
        db.update_one({"_id": item["_id"]}, {"$set":{"state":"annotating"}})
        return item
    else:
        return None

def ack_item(db, item, annotator, selection):
    db.update_one(
        {"_id": item["_id"]},
        {"$set": {
            "state": "finish",
            "annotator": annotator,
            "selection": selection
            }
        })

@st.cache_resource(hash_funcs={pymongo.MongoClient: id})
def connect_to_database():
    return pymongo.MongoClient("localhost", 27017)

@dataclass
class RankingOption:
    label: str
    value: str

    def __str__(self) -> str:
        return label

def prompt_binary(user, outputs):

    if isinstance(user, str):
        with st.chat_message("user"):
            st.write("Human:")
            st.write(user.strip())
    elif isinstance(user, list):
        for conv in user[:-1]:
            if conv["from"] == "human":
                icon, speaker = "user", "Human"
            else:
                icon, speaker = "assistant", "Assistant"

            with st.chat_message(icon):
                st.write(speaker + ":")
                st.markdown(conv["value"])

    random.shuffle(outputs)
    labels = ["A", "B", "C", "D", "E"]
    tabs = st.tabs(labels)

    # outputs.insert(0,)
    for tab, label, option in zip(tabs, labels, outputs):
        with tab, st.chat_message("assistant"):
            st.write(f"{label}:")
            st.markdown(option)

    original_items = [
        {'header': '1',  'items': labels},
        {'header': '2', 'items': []},
        {'header': '3', 'items': []},
        {'header': '4', 'items': []},
        {'header': '5', 'items': []},
    ]

    sorted_items = sort_items(original_items, multi_containers=True, direction="vertical")

    st.write(f'sorted_items: {sorted_items}')

    # options = [
    #     "A를 확실하게 더 선호합니다.",
    #     "A를 약간 더 선호합니다.",
    #     "둘이 비슷합니다.",
    #     "B를 약간 더 선호합니다.",
    #     "B를 확실하게 더 선호합니다.",
    #     "둘 다 별로입니다."
    #     ]

    # selection = st.radio(
    #     "A와 B의 답변 중 어떠한 것을 더 선호하시나요?",
    #     options,
    #     index=2
    # )

    return {
        "ranking": sorted_items,
        "options": outputs
    }

def main(annotator):
    if "item" not in st.session_state:
        st.session_state.item = sample_random(db_pref)

    item = st.session_state.item

    with st.form('hi', False):
        selection = prompt_binary(
            item["conversations"],
            [item["conversations"][-1]["value"]] + item["samples"]
        )
        submit = st.form_submit_button("제출")
    skip = st.button("skip")

    if submit:
        ack_item(db_pref, item, annotator, selection)
        st.session_state.count += 1
        print("submit")
        st.session_state.item = sample_random(db_pref)
        st.experimental_rerun()

    if skip:
        st.session_state.item = sample_random(db_pref)
        st.experimental_rerun()

db_client = connect_to_database()
db_pref = db_client.humaneval.pref

human_name = st.text_input("평가자명 입력")

if human_name:
    if "count" not in st.session_state:
        st.session_state.count = count_works(db_pref, human_name)
    st.write(f"{st.session_state.count}개 작업하셨습니다")
    main(human_name)
else:
    st.info("평가자명을 입력하세요.")


from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(642, 750)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setStyleSheet("background-color:#22222e\n"
                                         "")
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(-10, -10, 651, 271))
        self.frame.setStyleSheet("background-color: #483D8B")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(180, 10, 311, 111))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setStyleSheet("color: white")
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.frame)
        self.label_2.setGeometry(QtCore.QRect(300, 110, 241, 121))
        self.label_2.setText("")
        self.label_2.setPixmap(
            QtGui.QPixmap("C:/Users/letin/OneDrive/Рабочий стол/icons8-получить-доллары-наличными-64.png"))
        self.label_2.setObjectName("label_2")
        self.input_amount = QtWidgets.QLineEdit(self.centralwidget)
        self.input_amount.setGeometry(QtCore.QRect(60, 360, 341, 61))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(26)
        font.setBold(False)
        font.setWeight(50)
        self.input_amount.setFont(font)
        self.input_amount.setStyleSheet("background-color:#22222e;\n"
                                        "border:2px solid#483D8B;\n"
                                        "border-radius:30;\n"
                                        "color:white;")
        self.input_amount.setAlignment(QtCore.Qt.AlignCenter)
        self.input_amount.setObjectName("input_amount")
        self.output_currency = QtWidgets.QLineEdit(self.centralwidget)
        self.output_currency.setGeometry(QtCore.QRect(60, 540, 340, 61))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(26)
        font.setBold(False)
        font.setWeight(50)
        self.output_currency.setFont(font)
        self.output_currency.setStyleSheet("background-color:#22222e;\n"
                                           "border:2px solid#483D8B;\n"
                                           "border-radius:30;\n"
                                           "color:white;")
        self.output_currency.setText("")
        self.output_currency.setAlignment(QtCore.Qt.AlignCenter)
        self.output_currency.setObjectName("output_currency")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(64, 632, 331, 61))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(17)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("QPushButton{\n"
                                      "    background-color:#483D8B;\n"
                                      "    border:2px solid#483D8B;\n"
                                      "    border-radius:30;\n"
                                      "    color:white;\n"
                                      "}\n"
                                      "\n"
                                      "QPushButton:pressed{\n"
                                      "    background-color:#683F8B\n"
                                      "}")
        self.pushButton.setObjectName("pushButton")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(68, 281, 321, 51))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.comboBox.setFont(font)
        self.comboBox.setStyleSheet("background-color:#22222e;\n"
                                    "border:2px solid#483D8B;\n"
                                    "border-radius:30;\n"
                                    "color:white;")
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox_2.setGeometry(QtCore.QRect(70, 450, 321, 51))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.comboBox_2.setFont(font)
        self.comboBox_2.setStyleSheet("background-color:#22222e;\n"
                                      "border:2px solid#483D8B;\n"
                                      "border-radius:30;\n"
                                      "color:white;")
        self.comboBox_2.setObjectName("comboBox_2")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.comboBox_2.addItem("")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(490, 280, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(540, 280, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(590, 280, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(490, 330, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_5.setFont(font)
        self.pushButton_5.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(540, 330, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_6.setFont(font)
        self.pushButton_6.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_7.setGeometry(QtCore.QRect(590, 330, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_7.setFont(font)
        self.pushButton_7.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_8.setGeometry(QtCore.QRect(490, 380, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_8.setFont(font)
        self.pushButton_8.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_9.setGeometry(QtCore.QRect(540, 380, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_9.setFont(font)
        self.pushButton_9.setStyleSheet("QPushButton{\n"
                                        "    background-color:#22222e;\n"
                                        "    border:2px solid#483D8B;\n"
                                        "    border-radius:30;\n"
                                        "    color:white;\n"
                                        "}\n"
                                        "QPushButton:pressed{\n"
                                        "    background-color:#683F8B\n"
                                        "}\n"
                                        "")
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_10.setGeometry(QtCore.QRect(590, 380, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_10.setFont(font)
        self.pushButton_10.setStyleSheet("QPushButton{\n"
                                         "    background-color:#22222e;\n"
                                         "    border:2px solid#483D8B;\n"
                                         "    border-radius:30;\n"
                                         "    color:white;\n"
                                         "}\n"
                                         "QPushButton:pressed{\n"
                                         "    background-color:#683F8B\n"
                                         "}\n"
                                         "")
        self.pushButton_10.setObjectName("pushButton_10")
        self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_11.setGeometry(QtCore.QRect(540, 430, 50, 50))
        font = QtGui.QFont()
        font.setFamily("MS Serif")
        font.setPointSize(14)
        self.pushButton_11.setFont(font)
        self.pushButton_11.setStyleSheet("QPushButton{\n"
                                         "    background-color:#22222e;\n"
                                         "    border:2px solid#483D8B;\n"
                                         "    border-radius:30;\n"
                                         "    color:white;\n"
                                         "}\n"
                                         "QPushButton:pressed{\n"
                                         "    background-color:#683F8B\n"
                                         "}\n"
                                         "")
        self.pushButton_11.setObjectName("pushButton_11")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 642, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "КОНВЕРТЕР ВАЛЮТ"))
        self.input_amount.setText(_translate("MainWindow", ""))
        self.pushButton.setText(_translate("MainWindow", "КОНВЕРТИРУЙ"))
        self.comboBox.setItemText(0, _translate("MainWindow", "EUR"))
        self.comboBox.setItemText(1, _translate("MainWindow", "RUR"))
        self.comboBox.setItemText(2, _translate("MainWindow", "GPB"))
        self.comboBox.setItemText(3, _translate("MainWindow", "USD"))
        self.comboBox.setItemText(4, _translate("MainWindow", "BYN"))
        self.comboBox.setItemText(5, _translate("MainWindow", "CAD"))
        self.comboBox.setItemText(6, _translate("MainWindow", "AMD"))
        self.comboBox_2.setItemText(0, _translate("MainWindow", "EUR"))
        self.comboBox_2.setItemText(1, _translate("MainWindow", "RUR"))
        self.comboBox_2.setItemText(2, _translate("MainWindow", "GBP"))
        self.comboBox_2.setItemText(3, _translate("MainWindow", "USD"))
        self.comboBox_2.setItemText(4, _translate("MainWindow", "BYN"))
        self.comboBox_2.setItemText(5, _translate("MainWindow", "CAD"))
        self.comboBox_2.setItemText(6, _translate("MainWindow", "AMD"))
        self.pushButton_2.setText(_translate("MainWindow", "7"))
        self.pushButton_3.setText(_translate("MainWindow", "8"))
        self.pushButton_4.setText(_translate("MainWindow", "9"))
        self.pushButton_5.setText(_translate("MainWindow", "4"))
        self.pushButton_6.setText(_translate("MainWindow", "5"))
        self.pushButton_7.setText(_translate("MainWindow", "6"))
        self.pushButton_8.setText(_translate("MainWindow", "1"))
        self.pushButton_9.setText(_translate("MainWindow", "2"))
        self.pushButton_10.setText(_translate("MainWindow", "3"))
        self.pushButton_11.setText(_translate("MainWindow", "0"))

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


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

from biocluster.workflow import Workflow
import pandas as pd
from biocluster.core.exceptions import OptionError
from mbio.packages.tool_lab.get_box import get_box_value_row
import os
from mbio.packages.tool_lab.common_function import rename_name,rename_name_back
from biocluster.file import download,exists
import HTMLParser
from mbio.packages.tool_lab.common_function import meta_get_file
import shutil

class AnovaWorkflow(Workflow):
    """
    单因素方差小工具
    """
    def __init__(self, wsheet_object):
        self._sheet = wsheet_object
        super(AnovaWorkflow, self).__init__(wsheet_object)
        options = [
            {'name': 'table', 'type': 'infile', 'format': 'tool_lab.table2'},  # 表达矩阵文件
            {"name": "group_file", "type": "infile", "format": "tool_lab.simple"},  # 分组文件
            {'name': 'group_name', 'type': 'string', 'default': ''},  # 用于检测的组名，eg："A|B|C"  或 A,B,C
            {'name': 'mul_test', 'type': 'string', 'default': 'none'},
            # ["holm", "hochberg", "hommel", "bonferroni", "BH", "BY","fdr", "none"]
            {"name": "update_info", "type": "string"},
            {"name": "main_id", "type": "string"},
            {"name": "method", "type": "string", "default": "none"},  # 数据转换
            {"name": "methor", "type": "string", "default": "tukeykramer"},  # Post-hoc检验方法
            {"name": "coverage", "type": "float", "default": 0.95},  # Post-hoc检验值
            {'name': 'project_data', 'type': 'string'},
            {'name': 'source', 'type': 'string', 'default': 'tool_lab'},
            {'name': 'project_task_id', 'type': 'string'},
        ]
        self.add_option(options)
        self.revise_infiles()
        self.set_options(self._sheet.options())

    def check_file(self):
        if self.option("method") == "T":
            with open(self.data_table_raw) as f:
                data = f.readlines()
                num = 0
                for i in data:
                    if i.strip():
                        num +=1
                if num == 2:
                    raise OptionError('1行数据无法进行relative数据转换!')
        if '|' in self.option('group_name'):
            self.groups = self.option('group_name').split('|')
        elif ',' in self.option('group_name'):
            self.groups = self.option('group_name').split(',')
        elif '，' in self.option('group_name'):
            self.groups = self.option('group_name').split('，')
        else:
            self.set_error("组名之间要用，或|分割")
        if len(self.groups) < 3:
            self.set_error("分组数小于3组")

        self.groups = [g.strip() for g in self.groups]

    def group_check(self):
        if os.path.getsize(self.group_table) == 0:
            self.set_error('分组文件为空')

        if os.path.getsize(self.data_table_raw) == 0:
            self.set_error('数据表文件为空')

        group_data = pd.read_table(self.group_table, sep='\t',header=0)
        table =  pd.read_table(self.data_table_raw, sep='\t',header=0)
        if len(group_data)==0 :
            self.set_error("分组文件为空")
        if len(table)==0 :
            self.set_error("table文件为空")

        for sample in group_data[group_data.columns[0]]:
            if sample not in table.columns:
                self.set_error('分组文件的样本%s 不在另一文件中'%sample)
        for g in self.groups:
            if g not in group_data[group_data.columns[1]].tolist():
                self.set_error('输入的组名没有在分组文件中')

        self.data_table = self.work_dir + "/input_table.txt"
        if os.path.exists(self.data_table):
            os.remove(self.data_table)
        self.name_dict = rename_name(self.data_table_raw, self.data_table)

    def run(self):
        self.IMPORT_REPORT_DATA = True
        self.IMPORT_REPORT_DATA_AFTER_END = False
        self.logger.info("start run workflow")
        if self.option("project_data"):
            self.data_table_raw, self.group_table = meta_get_file(self.option('project_data'), self.work_dir)
        else:
            self.data_table_raw = self.option('table').path
            self.group_table = self.option('group_file').path
        self.check_file()
        self.group_check()
        self.run_diff()

        super(AnovaWorkflow, self).run()

    def run_diff(self):
        group_data = pd.read_table(self.group_table, sep='\t')
        group_data.columns = ['#sample', 'group']
        self.group = self.work_dir + '/pick_group.txt'
        new_group = group_data[group_data['group'].map(lambda x: x in self.groups)]
        new_group.to_csv(self.group, sep='\t', header=True, index=False)

        self.this_tool = self.add_tool("tool_lab.metastat2")

        mul_test = self.option("mul_test")
        if mul_test == 'FDR':
            mul_test = 'fdr'

        options = {
                "anova_input": self.data_table,
                "anova_group": self.group,
                "anova_correction": mul_test,
                "test": 'anova',
                "anova_gname": 'group',
                "anova_methor": self.option("methor"),
                "anova_coverage": self.option("coverage"),
                "anova_method": self.option("method"),
            }
        self.this_tool.set_options(options)
        self.this_tool.on('end',self.set_db)
        self.this_tool.run()

    def set_db(self):
        """
        保存结果到mongo数据库中
        """
        if os.path.exists(self.this_tool.output_dir + '/anova_result.xls'):
            os.rename(self.this_tool.output_dir + '/anova_result.xls',self.this_tool.output_dir + '/anova_result_bak1.xls')
            with open(self.this_tool.output_dir + '/anova_result_bak1.xls') as f,open(self.this_tool.output_dir + '/anova_result.xls',"w") as t:
                data = f.readlines()
                if data[0].strip().split("\t")[0] != "name":
                    t.write("name"+"\t"+data[0].strip()+"\n")
                else:
                    t.write(data[0])
                for i in data[1:]:
                    t.write(i)
                os.remove(self.this_tool.output_dir + '/anova_result_bak1.xls')
        if os.path.exists(self.this_tool.output_dir + '/anova_boxfile.xls'):
            os.rename(self.this_tool.output_dir + '/anova_boxfile.xls',self.this_tool.output_dir + '/anova_boxfile_bak1.xls')
            with open(self.this_tool.output_dir + '/anova_boxfile_bak1.xls') as f,open(self.this_tool.output_dir + '/anova_boxfile.xls',"w") as t:
                data = f.readlines()
                if data[0].strip().split("\t")[0] != "name":
                    t.write("name"+"\t"+data[0].strip()+"\n")
                else:
                    t.write(data[0])
                for i in data[1:]:
                    t.write(i)
                os.remove(self.this_tool.output_dir + '/anova_boxfile_bak1.xls')
        for file in os.listdir(self.this_tool.output_dir):
            rename_name_back(self.this_tool.output_dir + '/' + file,self.name_dict)
        box_file = self.work_dir + '/box_result.xls'
        get_box_value_row(self.data_table_raw, self.group, box_file,self.option('method'))
        api_name = self.api.api("tool_lab.anova")
        self.logger.info("正在写入mongo数据库")
        main_id = self.option("main_id")
        if self.option("group_name").find(";") >= 1:
            group_names = self.option("group_name").split(';')
        elif self.option("group_name").find("|") >= 1:
            group_names = self.option("group_name").split('|')
        else:
            group_names = self.option("group_name").split(',')
        api_name.add_detail_box(main_id,self.this_tool.output_dir,box_file,self.option('methor'),self.option('method'),self.option('mul_test'),group_names=group_names)
        if self.option("project_data"):
            if os.path.exists(self.this_tool.output_dir + "/input_data"):
                shutil.rmtree(self.this_tool.output_dir + "/input_data")
            os.mkdir(self.this_tool.output_dir + "/input_data")
            os.link(self.data_table_raw, self.this_tool.output_dir + "/input_data/input_table.txt")
            os.link(self.group_table, self.this_tool.output_dir + "/input_data/input_group.txt")
        self.end()

    def end(self):
        result_dir = self.add_upload_dir(self.this_tool.output_dir)
        relpath_rules = [
            [".", "", "比较结果文件夹", 0, ],
        ]
        regexps = [
            [r".*/.*_result.xls", "xls", "显著性比较结果表，包括均值，标准差，p值", 0],
            [r".*/.*_CI.xls", "xls", "两样本比较的置信区间值", 0]
        ]
        result_dir.add_relpath_rules(relpath_rules)
        result_dir.add_regexp_rules(regexps)
        super(AnovaWorkflow, self).end()

if __name__ == '__main__':
    from biocluster.wsheet import Sheet
    data = {
        'name': 'test_sample_diff',
        'id': 'tsg_36964',
        'type': 'workflow',
        'options': {
            "table" : "/mnt/ilustre/users/sanger-dev/workspace/20200228/Metabolome_tsg_36991/Preprocess/output/pos/metab_abund.txt",
            "group_file": "/mnt/ilustre/users/sanger-dev/workspace/20200228/Metabolome_tsg_36991/remote_input/group_table/group.txt",
            "group_name" : "NG_D1|NG_D3|SMG_D1",
            "main_id" : "5e9e6a6017b2bf2049a81be3",

        }
    }

    wsheet = Sheet(data=data)
    wf = AnovaWorkflow(wsheet)
    wf.run()


import argparse
from glob import glob
import os
import json
import random

import chainer
import chainer.links as L
import chainer.functions as F
import numpy as np

from networks.mobilenetv2 import MobilenetV2
from networks.vgg16 import VGG16
from networks.resnet50 import ResNet50
from dataset import FoodDataset, preprocess

from chainer.links.model.vision import vgg
from chainer.links.model.vision import resnet

def find_latest(model_dir):
    files = glob(os.path.join(model_dir, "model_epoch_*.npz"))
    numbers = []
    for f in files:
        base = os.path.basename(f)
        base = base[len("model_epoch_"):]
        base = base[:-len(".npz")]
        e = int(base)
        numbers.append(e)
    e = max(numbers)
    return os.path.join(model_dir, "model_epoch_{}.npz".format(e))

def prepare_setting(args):
    model_path = find_latest(args.model_path)
    print(model_path)
    jsonpath = os.path.join(os.path.dirname(model_path), "args.json")
    print(jsonpath)
    with open(jsonpath, 'r') as f:
        train_args = json.load(f)

    model_cand = {"mv2": MobilenetV2,
                  "vgg16": VGG16,
                  "resnet50": ResNet50}
    if train_args["model_name"] == "mv2":
        model = MobilenetV2(num_classes=101, depth_multiplier=1.0)
    else:
        model = model_cand[train_args["model_name"]](num_classes=101)

    model = L.Classifier(model)
    chainer.serializers.load_npz(model_path, model)

    test_dataset = FoodDataset(args.dataset,
                               model_name=train_args["model_name"],
                               train=False)

    if args.device >= 0:
        # use GPU
        chainer.backends.cuda.get_device_from_id(args.device).use()
        model.predictor.to_gpu()
        import cupy as xp
    else:
        # use CPU
        xp = np

    preprocess_ = lambda image: preprocess(image, train_args["model_name"])

    return model, preprocess_, xp, test_dataset,

def predict(args):
    classes = np.genfromtxt(os.path.join(args.dataset, "meta", "classes.txt"),
                            str,
                            delimiter="\n")
    model, preprocess, xp, test_dataset = prepare_setting(args)

    top_1_counter = 0
    top_5_counter = 0
    top_10_counter = 0
    indices = list(range(len(test_dataset)))
    num_iteration = len(indices) if args.sample < 0 else args.sample
    random.shuffle(indices)
    with chainer.function.no_backprop_mode(), chainer.using_config('train', False):
        for i in indices[:num_iteration]:
            img, label = test_dataset.get_example(i)
            h = model.predictor(xp.expand_dims(xp.array(img), axis=0))
            prediction = chainer.functions.softmax(h)
            if args.device >= 0:
                prediction = xp.asnumpy(prediction[0].data)
            else:
                prediction = prediction[0].data
            top_ten = np.argsort(-prediction)[:10]
            top_five = top_ten[:5]
            if top_five[0] == label:
                top_1_counter += 1
                top_5_counter += 1
                top_10_counter += 1
                msg = "Bingo!"
            elif label in top_five:
                top_5_counter += 1
                top_10_counter += 1
                msg = "matched top 5"
            elif label in top_ten:
                top_10_counter += 1
                msg = "matched top 10"
            else:
                msg = "Boo, actual {}".format(classes[label])
            print(classes[top_five], prediction[top_five], msg)
        print('top1 accuracy', top_1_counter / num_iteration)
        print('top5 accuracy', top_5_counter / num_iteration)
        print('top10 accuracy', top_10_counter / num_iteration)

def parse_argument():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "model_path", type=str, help="path/to/snapshot e.g. pretrained/model_epoch_100.npz")
    parser.add_argument("--sample", type=int, default=-1,
                        help="select num of --sample from test dataset to evaluate accuracy")
    parser.add_argument("--device", type=int, default=0,
                        help="specify GPU_ID. If negative, use CPU")
    parser.add_argument("--dataset", type=str, default=".")
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_argument()
    predict(args)


# -*- coding:utf-8 -*-
# @Time  : 2019/4/19 17:12
# @Author: lisa.liu
# @Email : 2423844@qq.com
# @File  : testcase_recharge.py

from common import DoExcel
import unittest
import json
from ddt import ddt, data, unpack
from common.HttpRequest import HttpRequest
from common import contents
from common.do_mysql import DoMySQL

@ddt
class TestRecharge(unittest.TestCase):
    myexcel = DoExcel.DoExcel(contents.case_file, 'recharge')
    cases = myexcel.get_cases()

    @classmethod
    def setUpClass(cls):
        cls.http_request = HttpRequest()
        cls.mysql=DoMySQL()

    @data(*cases)
    def test_recharge(self, case):

        expected = str(case.expected)
        if case.sql:
            sql=case.sql
            sql_re = self.mysql.fetch_one(sql)
            print(sql_re['leaveamount'])
            before = sql_re['leaveamount']

        res = self.http_request.http_request(case.method, case.url, eval(case.data))
        print(res.text)
        res_code = res.json()['code']
        print(case.title)
        try:
            self.assertEqual(res_code, expected)
        except AssertionError as e:
            self.myexcel.write_result(case.case_id + 1, res_code, 'FAIL')
            raise e
        else:
            self.myexcel.write_result(case.case_id + 1, res_code, 'PASS')
            if case.sql:
                sql = case.sql
                sql_re = self.mysql.fetch_one(sql)
                print(sql_re['leaveamount'])
                after = sql_re['leaveamount']
                add=int(eval(case.data)['amount'])
                self.assertEqual(before+add,after)

    @classmethod
    def tearDownClass(cls):
        cls.http_request.close()


import milecsa

class TransactionParser:

    def __init__(self, json_data):
        self.transactions = []
        trx_type = json_data['transaction-type']

        if trx_type in ['TransferAssetsTransaction', 'EmissionTransaction']:

            _assets = json_data['asset']
            frm = json_data['from']
            to = json_data['to']
            trxId = json_data['transaction-id']
            description = json_data["description"]

            for a in _assets:
                asset_code = int(a['code'])
                amount = a['amount']

                # if type(asset_code) is str:
                #    asset = self.__chain.asset_code(name=asset)

                if trx_type == 'TransferAssetsTransaction':
                    trx = milecsa.Transfer(src=frm, dest=to, asset_code=asset_code, amount=amount, description=description, trxid=trxId)

                elif trx_type == 'EmissionTransaction':
                    trx = milecsa.Emission(src=frm, dest=to, asset_code=asset_code, amount=amount, description=description, trxid=trxId)

                self.transactions.append(trx)


import argparse
import pandas as pd
from src.utils.all_utils import read_yaml,load_model
import os
from sklearn.metrics import roc_auc_score,roc_curve,confusion_matrix
from sklearn.metrics import precision_score, recall_score, f1_score,accuracy_score,precision_recall_curve
import json
import math
EVAL_PATH = "confusion"

def evaluate(config_path):
    config = read_yaml(config_path)
    artifacts_dir = config['artifacts']['artifacts_dir']
    split_data_dir = config['artifacts']['split_data_dir']
    test_data = config['artifacts']['test']

    model_dir = config['artifacts']['model']['model_dir']
    model_file = config['artifacts']['model']['model_file']

    test_path = os.path.join(artifacts_dir,split_data_dir,test_data)
    model_file_path = os.path.join(artifacts_dir,model_dir,model_file)

    prc_json_path = config['plots']['PRC']
    roc_json_path = config['plots']['ROC']
    scores_json_path = config['metrics']['SCORES']

    df = pd.read_csv(test_path)
    x = df.drop('Outcome',axis=1)
    y = df['Outcome']

    pipe = load_model(model_file_path)
    pred = pipe.predict(x)

    roc_auc = roc_auc_score(y,pred)
    scores = {
        "roc_auc":roc_auc
    }
    with open(scores_json_path,'w') as f:
        json.dump(scores,f)

    precision,recall,prc_th = precision_recall_curve(y,pred)

    nth_point = math.ceil(len(prc_th)/1000)
    prc_points =list(zip(precision,recall,prc_th))[::nth_point]
    prc_data = {
        'prc':[
            {"precision": float(p), "recall": float(r), "thresh": float(t)}
            for p,r,t in prc_points
        ]
    }
    with open(prc_json_path,"w") as f:
        json.dump(prc_data,f)

    fpr,tpr,roc_th = roc_curve(y,pred)

    roc_data = {
        'roc': [
            {'fpr':float(fp), "tpr":float(tp),"th":float(t)}
            for fp,tp,t in zip(fpr,tpr,roc_th)
        ]
    }
    with open(roc_json_path,'w') as f:
        json.dump(roc_data,f)

if __name__ == "__main__":
    args = argparse.ArgumentParser()
    args.add_argument("--config","-c",default="config/config.yaml")

    parsed_arg = args.parse_args()
    evaluate(config_path=parsed_arg.config)


#!/usr/bin/python3.1

import subprocess
import sys
import os
import re

from functions.api import api
from functions.clean import clean
from functions.about import about

blue = '\033[96m'
white = '\033[37m'
red = '\033[31m'
reset = '\033[0m'
green = '\033[32m'
blink = '\033[5m'
yellow = '\033[33m'
cyan = '\033[1;36m'

rgb_75 = '\x1b[38;5;75m'

models = {
    "1": "GPT-4",
    "2": "GPT-3.5"

}

models_names = {
    "GPT-4": "gpt-4",
    "GPT-3.5" : "gpt-3.5-turbo"
}

def options_list():
    print(" {}┌──────────────────────────────────────┐{}".format(rgb_75, reset))
    print(" {}│{}                                      {}│{}".format(rgb_75, reset, rgb_75, reset))
    print(" {}│{}       {}╔══════════════════════╗{}       {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
    print(" {}│{}       {}║  Welcome to MeeGPT!  ║{}       {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
    print(" {}│{}       {}╚══════════════════════╝{}       {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))

    string = "AI Model: {}".format(model)
    if len(string) % 2 != 0:
        string += " "
    lenght = " " * int((38 - len(string)) / 2)
    print(" {}│{}{}{}{}{}{}{}│{}".format(rgb_75, reset, lenght, white, string, reset, lenght, rgb_75, reset))

    print(" {}│{}                                      {}│{}".format(rgb_75, reset, rgb_75, reset))
    print(" {}│{}   {}{}Type your message / select option{}  {}│{}".format(rgb_75, reset, white, blink, reset, rgb_75, reset))
    print(" {}│{}                                      {}│{}".format(rgb_75, reset, rgb_75, reset))
    print(" {}│{}            {}1. Change model{}           {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
    print(" {}│{}            {}2. About{}                  {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
    print(" {}│{}                                      {}│{}".format(rgb_75, reset, rgb_75, reset))
    print(" {}│{}            {}0. Exit{}                   {}│{}".format(rgb_75, reset, white, reset, rgb_75, reset))
    print(" {}│{}                                      {}│{}".format(rgb_75, reset, rgb_75, reset))
    print(" {}└──────────────────────────────────────┘{} \n ".format(rgb_75, reset))

def model_changer():
    while True:
        print(" {}Supported models:{}".format(rgb_75, reset))
        for i, m in models.items():
            print(" {}. {}".format(i, m))
        print(" ")
        global model
        answer = input(" {}Enter desired model{}: ".format(yellow, white))
        if answer == "0":
            break
        if answer not in models.keys():
            model = "gpt-3.5-turbo"
            print(" {}Wrong model!{}".format(red, reset))
            print(" ")
            continue
        model = models_names[models[answer]]
        with open(".config", "w") as f:
            f.write("model = {}".format(model))
            break

def instance_checker():

    output = api(query="Yes")

    if output == "Error":
        return False
    try:
        if "error" in output.keys() or "invalid_api_key" in output.values():
            working = False
        else:
            working = True
    except:
        working = False

    return working

def main():

    clean()

    #folder = "."
    folder = "/opt/MeeGPT"
    if not os.path.isdir(folder):
        if os.path.isfile(folder):
            os.remove(folder)
        os.mkdir(folder)
    os.chdir(folder)

    _ = subprocess.call("ping wunderwungiel.pl -c 2 > /dev/null 2>&1", shell=True)
    if _ != 0:
        print(" {}Failed to connect, please\n check your internet connection.{}".format(red, reset))
        print()
        input(" {}{}Press Enter to continue... {}".format(blink, cyan, reset))
        clean()
        sys.exit(1)

    global model
    if not os.path.isfile(".config"):
        with open(".config", "w") as f:
            f.write("model = gpt-3.5-turbo")

    with open(".config", "r") as f:
        content = f.read()
        model = re.search("model = (.+)", content)
        if model:
            model = model.group(1)
        else:
            with open(".config", "w") as f:
                f.write("model = gpt-3.5-turbo")
                model = "gpt-3.5-turbo"
    instance = instance_checker()
    if instance == False:
        print(" {}I couldn't find working API endpoint...{}".format(red, reset))
        input(" \n {}{}Press Enter to exit... {}".format(blink, cyan, reset))
        sys.exit(0)

    options_list()

    while True:

        try:
            answer = input(" {}Your message:{} ".format(yellow, white))
        except KeyboardInterrupt:
            print(" {}KeyboardInterrupt, exiting...{}".format(red, reset))
            sys.exit(0)

        supported = ["1", "2", "0"]

        print(" ")

        if answer.isnumeric():
            if answer not in supported:
                print(" {}Wrong number, select a correct one!{}".format(red, reset))
                print(" ")
                continue
            if answer == "1":
                model_changer()
                clean()
                options_list()
                continue
            elif answer == "2":
                about()
                clean()
                options_list()
                continue
            elif answer == "0":
                sys.exit(0)

        else:

            result = api(query=answer, model="gpt-3.5-turbo")
            print(" {}══════════════════════════════════════{}\n ".format(white, reset))
            ai_answer = result['choices'][0]['message']
            if isinstance(ai_answer, dict):
                ai_answer = ai_answer['content']
            ai_answer = ai_answer.splitlines()  # Podzielenie ciągu na linie
            ai_answer = [' ' + line for line in ai_answer]
            ai_answer = '\n'.join(ai_answer)
            ai_answer = " {}ChatGPT:{}".format(yellow, reset) + white + ai_answer + reset

            print(ai_answer)
            print("\n {}══════════════════════════════════════{}\n ".format(white, reset))

if __name__ == "__main__":
    main()


import os

import pandas as pd
import numpy as np

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func

from flask import Flask, jsonify, render_template

app = Flask(__name__)

#################################################
# Database Setup
#################################################

engine = create_engine("sqlite:///data/BOTULISM.db")

Base = automap_base()
Base.prepare(engine, reflect=True)

# Save reference to Botulism table
bot_data = Base.classes.BOTULISM

#################################################
# Flask Routes
#################################################

@app.route("/")
def index():
    """Return the homepage."""
    return render_template("index.html")

@app.route("/data")
def datatable():
    """Return the data page."""
    return render_template("data.html")

@app.route("/cleanup")
def cleanup():
    """Return the data cleanup page."""
    return render_template("cleanup.html")

@app.route("/about")
def about():
    """Return the about page."""
    return render_template("about.html")

@app.route("/botulism")
def botulism():
    """Return the botulism page."""
    return render_template("botulism.html")

@app.route("/history")
def history():
    """Return the history page."""
    return render_template("history.html")

@app.route("/types")
def types():
    """Return the history page."""
    return render_template("types.html")

@app.route("/safety")
def safety():
    """Return the history page."""
    return render_template("safety.html")

@app.route("/allresults")
#Route retuns json of all records in dataset
def all():

    session = Session(engine)

    all_results = session.query(bot_data.state_name,
                                bot_data.record_year,
                                bot_data.BotType,
                                bot_data.ToxinType,
                                bot_data.record_count,
                                bot_data.BotId).all()

    session.close()

    all_records = []

    for state, year, botType, toxType, count, botID in all_results:
        results_dict = {}
        results_dict["state"] = state
        results_dict["year"] = year
        results_dict["botType"] = botType
        results_dict["toxType"] = toxType
        results_dict["count"] = count
        results_dict["botID"] = botID
        all_records.append(results_dict)

    return jsonify(all_records)

@app.route("/states")
#Route returns list of unique state names
def statenames():

    session = Session(engine)

    all_states = session.query(bot_data.state_name).distinct().all()

    session.close()

    state_names = []

    for i in all_states:
        for state in i:
            #create dict here if necessary to work in app
            state_names.append(state)

    return jsonify(state_names)

@app.route("/statecharts/<state>")
#Route returns data for two state charts:  bot type vs time, tox type vs time
def stateInfo(state):
    #Returns data for one specific state

    session = Session(engine)

    state_data = session.query(bot_data.state_name,
                        bot_data.record_year,
                        bot_data.BotType,
                        bot_data.ToxinType,
                        bot_data.record_count).filter(bot_data.state_name == state).all()

    session.close()

    years = []

    foodborne_bot = []
    infant_bot = []
    wound_bot = []
    other_bot = []

    unknown_tox = []
    E_tox =[]
    B_tox =[]
    A_tox =[]
    F_tox =[]
    AB_tox =[]
    Ba_tox =[]
    Bf_tox =[]
    EF_tox =[]
    ABE_tox =[]
    Ab_tox =[]
    BF_tox =[]

    for i in state_data:
        years.append(i[1])

    for i in state_data:
        if i[2] == "Foodborne":
            foodborne_bot.append(i[4])
        else:
            foodborne_bot.append('0')

    for i in state_data:
        if i[2] == "Infant":
            infant_bot.append(i[4])
        else:
            infant_bot.append('0')

    for i in state_data:
        if i[2] == "Wound":
            wound_bot.append(i[4])
        else:
            wound_bot.append('0')

    for i in state_data:
        if i[2] == "Other":
            other_bot.append(i[4])
        else:
            other_bot.append('0')

    for i in state_data:
        if i[3] == "Unknown":
            unknown_tox.append(i[4])
        else:
            unknown_tox.append('0')

    for i in state_data:
        if i[3] == "E":
            E_tox.append(i[4])
        else:
            E_tox.append('0')

    for i in state_data:
        if i[3] == "B":
            B_tox.append(i[4])
        else:
            B_tox.append('0')

    for i in state_data:
        if i[3] == "A":
            A_tox.append(i[4])
        else:
            A_tox.append('0')

    for i in state_data:
        if i[3] == "F":
            F_tox.append(i[4])
        else:
            F_tox.append('0')

    for i in state_data:
        if i[3] == "AB":
            AB_tox.append(i[4])
        else:
            AB_tox.append('0')

    for i in state_data:
        if i[3] == "Ba":
            Ba_tox.append(i[4])
        else:
            Ba_tox.append('0')

    for i in state_data:
        if i[3] == "Bf":
            Bf_tox.append(i[4])
        else:
            Bf_tox.append('0')

    for i in state_data:
        if i[3] == "EF":
            EF_tox.append(i[4])
        else:
            EF_tox.append('0')

    for i in state_data:
        if i[3] == "ABE":
            ABE_tox.append(i[4])
        else:
            ABE_tox.append('0')

    for i in state_data:
        if i[3] == "Ab":
            Ab_tox.append(i[4])
        else:
            Ab_tox.append('0')

    for i in state_data:
        if i[3] == "BF":
            BF_tox.append(i[4])
        else:
            BF_tox.append('0')

    state_dict = {
        "years": years,
        "foodborne": foodborne_bot,
        "infant": infant_bot,
        "wound": wound_bot,
        "other": other_bot,
        "unknownTox": unknown_tox,
        "ETox": E_tox,
        "BTox": B_tox,
        "ATox": A_tox,
        "FTox": F_tox,
        "ABTox": AB_tox,
        "BaTox": Ba_tox,
        "BfTox": Bf_tox,
        "EFTox": EF_tox,
        "ABETox": ABE_tox,
        "AbTox": Ab_tox,
        "BFTox": BF_tox
    }

    return jsonify(state_dict)

@app.route("/bottypes")
#Route returns list of unique Botulism types
def botTypes():

    session = Session(engine)

    all_botTypes = session.query(bot_data.BotType).distinct().all()

    session.close()

    bot_types = []

    for i in all_botTypes:
        for kind in i:
            #create dict here if necessary to work in app
            bot_types.append(kind)

    return jsonify(bot_types)

@app.route("/toxtypes")
#Route returns list of unique toxin types
def toxTypes():

    session = Session(engine)

    all_toxTypes = session.query(bot_data.ToxinType).distinct().all()

    session.close()

    tox_types = []

    for i in all_toxTypes:
        for kind in i:
            #create dict here if necessary to work in app
            tox_types.append(kind)

    return jsonify(tox_types)

@app.route("/years")
#Returns list of unique years

def record_years():

    session = Session(engine)

    all_years = session.query(bot_data.record_year).order_by(bot_data.record_year).distinct().all()

    session.close()

    years = []

    for i in all_years:
        for year in i:
            #create dict here if necessary to work in app
            years.append(year)

    return jsonify(years)

@app.route("/circles")
#Returns json for circlechart

def circleData():
    session = Session(engine)

    all_states = session.query(bot_data.state_name).distinct().all()

    state_names = []

    for i in all_states:
        for state in i:
            state_names.append(state)

    food_children = []
    for state in state_names:

        foodborne_data = session.query(bot_data.state_name,
                                bot_data.BotType,
                            bot_data.record_year,
                            func.sum(bot_data.record_count)).\
                            filter(bot_data.BotType == "Foodborne").\
                            filter(bot_data.state_name == state).\
                            group_by(bot_data.record_year).\
                            order_by(bot_data.state_name).all()

        children = []

        for i in foodborne_data:

            children.append({"name": i[2], "value": i[3]})

        one_state = {"name": state, "children": children}
        food_children.append(one_state)

    infant_children = []
    for state in state_names:

        infant_data = session.query(bot_data.state_name,
                                bot_data.BotType,
                            bot_data.record_year,
                            func.sum(bot_data.record_count)).\
                            filter(bot_data.BotType == "Infant").\
                            filter(bot_data.state_name == state).\
                            group_by(bot_data.record_year).\
                            order_by(bot_data.state_name).all()

        children = []

        for i in infant_data:

            children.append({"name": i[2], "value": i[3]})

        one_state = {"name": state, "children": children}
        infant_children.append(one_state)

    wound_children = []
    for state in state_names:

        wound_data = session.query(bot_data.state_name,
                                bot_data.BotType,
                            bot_data.record_year,
                            func.sum(bot_data.record_count)).\
                            filter(bot_data.BotType == "Wound").\
                            filter(bot_data.state_name == state).\
                            group_by(bot_data.record_year).\
                            order_by(bot_data.state_name).all()

        children = []

        for i in wound_data:

            children.append({"name": i[2], "value": i[3]})

        one_state = {"name": state, "children": children}
        wound_children.append(one_state)

    other_children = []
    for state in state_names:

        other_data = session.query(bot_data.state_name,
                                bot_data.BotType,
                            bot_data.record_year,
                            func.sum(bot_data.record_count)).\
                            filter(bot_data.BotType == "Other").\
                            filter(bot_data.state_name == state).\
                            group_by(bot_data.record_year).\
                            order_by(bot_data.state_name).all()

        children = []

        for i in other_data:

            children.append({"name": i[2], "value": i[3]})

        one_state = {"name": state, "children": children}
        other_children.append(one_state)

    session.close()

    json= {"name": "botulism",
       "children":[
           {
           "name": "Foodborne",
            "children": food_children
           },
           {
            "name": "Infant",
            "children": infant_children
           },
           {"name": "Wound",
            "children": wound_children
           },
           {"name": "Other",
           "children": other_children}
       ]}

    return jsonify(json)

@app.route("/stacey")
#Route retuns json of all records in dataset
def f_stacey():
    session = Session(engine)

    all_results = session.query(bot_data.state_name,
                                bot_data.record_year,
                                bot_data.BotType,
                                bot_data.ToxinType,
                                bot_data.record_count,
                                bot_data.BotId).all()

    session.close()

    all_records = []

    for state, year, botType, toxType, count, botID in all_results:
        results_dict = {}
        results_dict["state"] = state
        results_dict["year"] = year
        results_dict["botType"] = botType
        results_dict["toxType"] = toxType
        results_dict["count"] = count
        results_dict["botID"] = botID
        all_records.append(results_dict)

    df = pd.DataFrame(all_records)
    table = pd.pivot_table(df, values='count', index=['toxType'], columns=['botType'], aggfunc=np.sum).fillna(0).to_json()

    return jsonify(table)

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