from flask import current_app, json, request, url_for
import config.constants as CONSTANTS
import traceback
import requests

class AddressValidator():
    def validate_street_details(street_json):
        try:
            is_street_valid = True
            if not current_app.config['IS_DEVELOPMENT_MODE'] == 1:
                ss_url = CONSTANTS.SS_SCHEME + CONSTANTS.SS_STREET_HOST_NAME + CONSTANTS.SS_CONTEXT
                ss_request = {}
                ss_json = {}
                ss_request['auth-id'] = current_app.config['SS_API_KEY']
                ss_request['auth-token'] = current_app.config['SS_AUTH_TOKEN']
                ss_json['address1'] = street_json['street_name_1']
                ss_json['address2'] = street_json['street_name_2']
                ss_json['locality'] = street_json['city']
                ss_json['administrative_area'] = street_json['region']
                ss_json['postal_code'] = street_json['postal_code']
                ss_json['country'] = street_json['country_code']
                ss_request = {**ss_request, **ss_json}
                query_string = '&'.join(f'{k}={v}' for k, v in ss_request.items())
                query_string = query_string.replace(" ", "+")
                ss_url += query_string
                print(ss_url)
                #ss_response = None
                ss_response = requests.get(ss_url)
                print(ss_response)
                http_status_code = ss_response.status_code
                if str(http_status_code) == CONSTANTS.HTTP_CODE_200:
                    return ss_response.content
                else:
                    current_app.logger.error("Status code received is " + str(http_status_code) )
                    return None
            else:
                return None
        except Exception:
            print(traceback.format_exc())
            current_app.logger.error("Exception occurred while processing function validate_street_details")
            return None


from ledfx.config import save_config
from ledfx.api import RestEndpoint
from aiohttp import web
import voluptuous as vol
import random
import logging

_LOGGER = logging.getLogger(__name__)

class EffectsEndpoint(RestEndpoint):

    ENDPOINT_PATH = "/api/devices/{device_id}/effects"

    async def get(self, device_id) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = {"not found": 404}
            return web.json_response(data=response, status=404)

        # Get the active effect
        response = {"effect": {}}
        if device.active_effect:
            effect_response = {}
            effect_response["config"] = device.active_effect.config
            effect_response["name"] = device.active_effect.name
            effect_response["type"] = device.active_effect.type
            response = {"effect": effect_response}

        return web.json_response(data=response, status=200)

    async def put(self, device_id, request) -> web.Response:
        """Update the config of the active effect of a device"""
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = {"not found": 404}
            return web.json_response(data=response, status=404)

        if not device.active_effect:
            response = {
                "status": "failed",
                "reason": "Device {} has no active effect to update config".format(
                    device_id
                ),
            }
            return web.json_response(data=response, status=500)

        data = await request.json()
        effect_config = data.get("config")
        effect_type = data.get("type")
        if effect_config is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "config" was not provided',
            }
            return web.json_response(data=response, status=500)

        if effect_config == "RANDOMIZE":
            # Parse and break down schema for effect, in order to generate
            # acceptable random values
            effect_config = {}
            effect_type = device.active_effect.type
            effect = self._ledfx.effects.get_class(effect_type)
            schema = effect.schema().schema
            for setting in schema.keys():
                # Booleans
                if schema[setting] is bool:
                    val = random.choice([True, False])
                # Lists
                elif isinstance(schema[setting], vol.validators.In):
                    val = random.choice(schema[setting].container)
                # All (assuming coerce(float/int), range(min,max))
                # NOTE: vol.coerce(float/int) does not give enough info for a random value to be generated!
                # *** All effects should give a range! ***
                # This is also important for when sliders will be added, slider
                # needs a start and stop
                elif isinstance(schema[setting], vol.validators.All):
                    for validator in schema[setting].validators:
                        if isinstance(validator, vol.validators.Coerce):
                            coerce_type = validator.type
                        elif isinstance(validator, vol.validators.Range):
                            lower = validator.min
                            upper = validator.max
                    if coerce_type is float:
                        val = random.uniform(lower, upper)
                    elif coerce_type is int:
                        val = random.randint(lower, upper)
                effect_config[setting.schema] = val

        # Create the effect and add it to the device
        effect = self._ledfx.effects.create(
            ledfx=self._ledfx, type=effect_type, config=effect_config
        )
        device.set_effect(effect)

        # Update and save the configuration
        for device in self._ledfx.config["devices"]:
            if device["id"] == device_id:
                # if not ('effect' in device):
                device["effect"] = {}
                device["effect"]["type"] = effect_type
                device["effect"]["config"] = effect_config
                break
        save_config(
            config=self._ledfx.config,
            config_dir=self._ledfx.config_dir,
        )

        effect_response = {}
        effect_response["config"] = effect.config
        effect_response["name"] = effect.name
        effect_response["type"] = effect.type

        response = {"status": "success", "effect": effect_response}
        return web.json_response(data=response, status=200)

    async def post(self, device_id, request) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = {"not found": 404}
            return web.json_response(data=response, status=404)

        data = await request.json()
        effect_type = data.get("type")
        if effect_type is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "type" was not provided',
            }
            return web.json_response(data=response, status=500)

        effect_config = data.get("config")
        if effect_config is None:
            effect_config = {}

        # Create the effect and add it to the device
        effect = self._ledfx.effects.create(
            ledfx=self._ledfx, type=effect_type, config=effect_config
        )
        device.set_effect(effect)

        # Update and save the configuration
        for device in self._ledfx.config["devices"]:
            if device["id"] == device_id:
                # if not ('effect' in device):
                device["effect"] = {}
                device["effect"]["type"] = effect_type
                device["effect"]["config"] = effect_config
                break
        save_config(
            config=self._ledfx.config,
            config_dir=self._ledfx.config_dir,
        )

        effect_response = {}
        effect_response["config"] = effect.config
        effect_response["name"] = effect.name
        effect_response["type"] = effect.type

        response = {"status": "success", "effect": effect_response}
        return web.json_response(data=response, status=200)

    async def delete(self, device_id) -> web.Response:
        device = self._ledfx.devices.get(device_id)
        if device is None:
            response = {"not found": 404}
            return web.json_response(data=response, status=404)

        # Clear the effect
        device.clear_effect()

        for device in self._ledfx.config["devices"]:
            if device["id"] == device_id:
                if "effect" in device:
                    del device["effect"]
                    break
        save_config(
            config=self._ledfx.config,
            config_dir=self._ledfx.config_dir,
        )

        response = {"status": "success", "effect": {}}
        return web.json_response(data=response, status=200)


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
#   Author  :   XueWeiHan
#   Date    :   17/6/2 下午4:35
#   Desc    :   管理后台
import uuid
from datetime import datetime

from peewee import IntegrityError
from flask import render_template, redirect, request, flash, abort
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired, URL
from wtforms.widgets import TextArea

from models.blog import Blog
from pyhub import app, logger
from views import login

class BlogForm(FlaskForm):
    name = StringField('name', validators=[InputRequired()])
    url = StringField('url', validators=[URL()])
    description = StringField('description', widget=TextArea())

@app.route('/manage/')
@login
def manage():
    all_blog = Blog.select().order_by(Blog.create_time.desc())
    form = BlogForm()
    logger.info('%s|request manage page' % request.remote_addr)
    return render_template('manage.html', blogs=all_blog, form=form)

@app.route('/manage/create/', methods=['POST'])
@login
def create():
    form = BlogForm()
    if form.validate_on_submit():
        try:
            blog = Blog.create(
                blog_id=uuid.uuid4(), name=form.name.data, url=form.url.data,
                description=form.description.data)
            flash(u'创建 {name} 成功'.format(name=blog.name))
        except IntegrityError:
            flash(u'创建 {name} 失败，该条目已存在'.format(name=form.name.data), 'error')
    else:
        flash(u'创建失败，参数错误', 'error')
    return redirect('/manage')

@app.route('/manage/update/<blog_id>', methods=['GET', 'POST'])
@login
def update(blog_id):
    blog = Blog.get(Blog.blog_id == blog_id)
    if not blog:
        abort(400)
    form = BlogForm(name=blog.name, url=blog.url, description=blog.description)

    if request.method == 'GET':
        return render_template('update.html', blog=blog, form=form)
    else:
        if form.validate_on_submit():
            try:
                blog.name = form.name.data
                blog.url = form.url.data
                blog.description = form.description.data
                blog.update_time = datetime.now()
                blog.save()
                flash(u'更新 {name} 成功'.format(name=form.name.data))
                return redirect('/manage')
            except IntegrityError:
                flash(u'更新 {name} 失败，该条目已存在'.format(name=form.name.data), 'error')
                return render_template('update.html', blog=blog, form=form)
        else:
            flash(u'更新失败，参数错误', 'error')
            return render_template('update.html', blog=blog, form=form)

@app.route('/manage/status/<blog_id>', methods=['GET'])
@login
def status(blog_id):
    blog = Blog.get(Blog.blog_id == blog_id)
    if not blog:
        abort(400)
    if blog.status:
        blog.status = 0
        flash(u'{name}下线成功'.format(name=blog.name))
    else:
        blog.status = 1
        flash(u'{name}上线成功'.format(name=blog.name))
    blog.update_time = datetime.now()
    blog.save()
    return redirect('/manage')


import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)

import xmind
xmind_name="kafka"
w = xmind.load(os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")
s2=w.createSheet()
s2.setTitle("生产者(下)")
r2=s2.getRootTopic()
r2.setTitle("生产者(下)")

content={
# '8.分区器':[
#     {'消息在通过send()方法发往broker':[
#         '拦截器（Interceptor）',
#         '序列化器（Serializer）',
#         '分区器（Partitioner）'
#     ]},
#     {'计算分区':[
#         '1.ProducerRecord中指定partition字段',
#         '2.未指定partition字段，有key,根据key计算partition(先hash，再根据分区数取模)',
#         '3.无key,轮询'
#     ]},
#     {'Partitioner接口':[
#         'partition（）:计算分区号，返回值为int类型',
#         'close（）:关闭分区器的时候用来回收一些资源',
#         'configure（）:获取配置信息及初始化数据(继承自父类)'
#     ]}
# ],
# '9.RecordAccumulator':[
#     '缓存消息以便Sender线程可批量发送，减少网络传输的资源消耗',
#     '缓存大小:通过buffer.memory 配置，默认值32MB',
#     '生产者发送消息的速度>发送到服务器的速度:生产者空间不足，此时send()方法调用要么被阻塞，要么抛出异常',
#     '内部为每个分区都维护了一个双端队列，队列中的内容就是ProducerBatch，即 Deque＜ProducerBatch＞'
# ],
# '10.Sender 线程':[
#     'Sender从RecordAccumulator中获取＜分区，Deque＜ProducerBatch＞＞的缓存消息',
#     '转变为：＜Node，List＜ ProducerBatch＞的形式，其中Node表示Kafka集群的broker节点',
#     '封装为：＜Node，Request＞的形式',
#     'Map＜NodeId，Deque＜Request＞＞:缓存了已经发出去但还没有收到响应的请求'
# ],
'整体架构':[
    '生产者客户端由两个线程协调运行：主线程和Sender线程（发送线程）',
    {'主线程':[
        '由KafkaProducer创建消息，然后通过可能的拦截器、序列化器和分区器的作用之后缓存到消息累加器'
    ]},
    {'Sender线程':[
        '从RecordAccumulator中获取消息并将其发送到Kafka中'
    ]}
],
'重要的生产者参数':[
    {'acks':[
        '指定分区中必须要有多少个副本收到这条消息，之后生产者才会认为这条消息是成功写入的',
        '配置的值是一个字符串类型',
        'acks=0:生产者发送消息后不需等待服务端响应,认为消息发送成功',
        'acks=1:只要分区的leader副本成功写入消息，收到来自服务端的成功响应，认为消息发送成功',
        {'acks=-1':[
            '等待ISR中的所有副本都成功写入消息后，收到来自服务端的成功响应，认为消息发送成功',
            '需配合min.insync.replicas参数使用，其值要大于或者等于2,否则acks=-1作用只是acks=1'
        ]}
    ]},
    {'retries':[
        '生产者重试的次数，默认0,注意消费时实现幂等'
    ]},
    {'retry.backoff.ms':[
        '两次重试之间的时间间隔,默认100'
    ]},
    {'batch.size':[
        '批次大小,默认16K,可提高增加系统吞吐量'
    ]},
    {'linger.ms':[
        '发送时间限制,Batch没满也发送，默0'
    ]},
    {'buffer.memory':[
        '缓冲区大小,默认32M',
        'Sender线程处理缓慢，生产数据的速度快时，中间的缓冲区如容量不够，生产者无法再继续生产数据'
    ]},
    {'max.request.size':[
        '限制生产者客户端能发送的消息的最大值，默认1MB,可根据业务调大些',
    ]},
    {'request.timeout.ms':[
        'Producer等待请求响应的最长时间，默认30s',
        '请求超时之后可以选择进行重试,防止因网络抖动导致的数据丢失，限制等待时间'
    ]}
],
}

#构建xmind
xmind.build(content,r2)
#保存xmind
xmind.save(w,os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")


import re
import socket

from .common import InfoExtractor
from ..utils import (
    compat_http_client,
    compat_parse_qs,
    compat_urllib_error,
    compat_urllib_parse,
    compat_urllib_request,
    compat_str,
    determine_ext,
    ExtractorError,
)

class MetacafeIE(InfoExtractor):
    """Information Extractor for metacafe.com."""

    _VALID_URL = r'(?:http://)?(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
    _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
    _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
    IE_NAME = u'metacafe'
    _TESTS = [{
        u"add_ie": ["Youtube"],
        u"url":  u"http://metacafe.com/watch/yt-_aUehQsCQtM/the_electric_company_short_i_pbs_kids_go/",
        u"file":  u"_aUehQsCQtM.flv",
        u"info_dict": {
            u"upload_date": u"20090102",
            u"title": u"The Electric Company | \"Short I\" | PBS KIDS GO!",
            u"description": u"md5:2439a8ef6d5a70e380c22f5ad323e5a8",
            u"uploader": u"PBS",
            u"uploader_id": u"PBS"
        }
    },
    {
        u"url": u"http://www.metacafe.com/watch/an-dVVXnuY7Jh77J/the_andromeda_strain_1971_stop_the_bomb_part_3/",
        u"file": u"an-dVVXnuY7Jh77J.mp4",
        u"info_dict": {
            u"title": u"The Andromeda Strain (1971): Stop the Bomb Part 3",
            u"uploader": u"anyclip",
            u"description": u"md5:38c711dd98f5bb87acf973d573442e67"
        }
    }]

    def report_disclaimer(self):
        """Report disclaimer retrieval."""
        self.to_screen(u'Retrieving disclaimer')

    def _real_initialize(self):
        # Retrieve disclaimer
        request = compat_urllib_request.Request(self._DISCLAIMER)
        try:
            self.report_disclaimer()
            compat_urllib_request.urlopen(request).read()
        except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
            raise ExtractorError(u'Unable to retrieve disclaimer: %s' % compat_str(err))

        # Confirm age
        disclaimer_form = {
            'filters': '0',
            'submit': "Continue - I'm over 18",
            }
        request = compat_urllib_request.Request(self._FILTER_POST, compat_urllib_parse.urlencode(disclaimer_form))
        try:
            self.report_age_confirmation()
            compat_urllib_request.urlopen(request).read()
        except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
            raise ExtractorError(u'Unable to confirm age: %s' % compat_str(err))

    def _real_extract(self, url):
        # Extract id and simplified title from URL
        mobj = re.match(self._VALID_URL, url)
        if mobj is None:
            raise ExtractorError(u'Invalid URL: %s' % url)

        video_id = mobj.group(1)

        # Check if video comes from YouTube
        mobj2 = re.match(r'^yt-(.*)$', video_id)
        if mobj2 is not None:
            return [self.url_result('http://www.youtube.com/watch?v=%s' % mobj2.group(1), 'Youtube')]

        # Retrieve video webpage to extract further information
        req = compat_urllib_request.Request('http://www.metacafe.com/watch/%s/' % video_id)
        req.headers['Cookie'] = 'flashVersion=0;'
        webpage = self._download_webpage(req, video_id)

        # Extract URL, uploader and title from webpage
        self.report_extraction(video_id)
        mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
        if mobj is not None:
            mediaURL = compat_urllib_parse.unquote(mobj.group(1))
            video_ext = mediaURL[-3:]

            # Extract gdaKey if available
            mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
            if mobj is None:
                video_url = mediaURL
            else:
                gdaKey = mobj.group(1)
                video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
        else:
            mobj = re.search(r'<video src="([^"]+)"', webpage)
            if mobj:
                video_url = mobj.group(1)
                video_ext = 'mp4'
            else:
                mobj = re.search(r' name="flashvars" value="(.*?)"', webpage)
                if mobj is None:
                    raise ExtractorError(u'Unable to extract media URL')
                vardict = compat_parse_qs(mobj.group(1))
                if 'mediaData' not in vardict:
                    raise ExtractorError(u'Unable to extract media URL')
                mobj = re.search(r'"mediaURL":"(?P<mediaURL>http.*?)",(.*?)"key":"(?P<key>.*?)"', vardict['mediaData'][0])
                if mobj is None:
                    raise ExtractorError(u'Unable to extract media URL')
                mediaURL = mobj.group('mediaURL').replace('\\/', '/')
                video_url = '%s?__gda__=%s' % (mediaURL, mobj.group('key'))
                video_ext = determine_ext(video_url)

        video_title = self._html_search_regex(r'(?im)<title>(.*) - Video</title>', webpage, u'title')
        description = self._og_search_description(webpage)
        video_uploader = self._html_search_regex(
                r'submitter=(.*?);|googletag\.pubads\(\)\.setTargeting\("channel","([^"]+)"\);',
                webpage, u'uploader nickname', fatal=False)

        return {
            '_type':    'video',
            'id':       video_id,
            'url':      video_url,
            'description': description,
            'uploader': video_uploader,
            'upload_date':  None,
            'title':    video_title,
            'ext':      video_ext,
        }


import bs4
import re
import requests
from urllib.request import urlopen as uReq

import urllib.request as req
from bs4 import BeautifulSoup as soup

def retriever_soup(my_url):
	uClient = uReq(my_url)
	page_html = uClient.read()
	uClient.close()
	page_soup = soup(page_html, "html.parser")
	return (page_soup)

my_url = 'http://dl.upload8.net/Serial/'

a_href = retriever_soup(my_url).findAll("a")
total = 0
for i in a_href:
	new_link = my_url+i.get('href')
	inner_data = retriever_soup(new_link).findAll("a")
	for j in inner_data:
		main_link = new_link+j.get('href')
		r = requests.get(main_link)
		print(r.status_code)
		if r.status_code == 400:
			print(str(main_link)+ " ERROR")
		else:
			size= req.urlopen(main_link).headers['Content-Length']
			print (main_link)
			print (size)
	# 	size= req.urlopen(main_link).headers['Content-Length']
	# 	size = int(size)/(1024*1024)
	# 	total = total + size
	# 	print (main_link+ " : " + str(size) + " MB")
	# print (str(total)+ " MB")


import pandas as pd
import torch
from collections import defaultdict
import cosypose.utils.tensor_collection as tc
from cosypose.lib3d.transform_ops import invert_T

def parse_obs_data(obs):
    data = defaultdict(list)
    frame_info = obs['frame_info']
    TWC = torch.as_tensor(obs['camera']['TWC']).float()
    for n, obj in enumerate(obs['objects']):
        info = dict(frame_obj_id=n,
                    label=obj['name'],
                    visib_fract=obj.get('visib_fract', 1),
                    scene_id=frame_info['scene_id'],
                    view_id=frame_info['view_id'])
        data['infos'].append(info)
        data['TWO'].append(obj['TWO'])
        data['bboxes'].append(obj['bbox'])

    for k, v in data.items():
        if k != 'infos':
            data[k] = torch.stack([torch.as_tensor(x) .float()for x in v])

    data['infos'] = pd.DataFrame(data['infos'])
    TCO = invert_T(TWC).unsqueeze(0) @ data['TWO']

    data = tc.PandasTensorCollection(
        infos=data['infos'],
        TCO=TCO,
        bboxes=data['bboxes'],
        poses=TCO,
    )
    return data

def data_to_pose_model_inputs(data):
    TXO = data.poses
    obj_infos = []
    for n in range(len(data)):
        obj_info = dict(name=data.infos.loc[n, 'label'])
        obj_infos.append(obj_info)
    return TXO, obj_infos


import logging
import torch
from pathlib import Path

from farm.utils import set_all_seeds, MLFlowLogger, initialize_device_settings
from farm.modeling.tokenization import Tokenizer
from farm.data_handler.processor import SquadProcessor
from farm.data_handler.data_silo import DataSilo
from farm.modeling.adaptive_model import AdaptiveModel
from farm.infer import QAInferencer
from farm.eval import Evaluator
from farm.evaluation.metrics import metrics_per_bin

def question_answering_confidence():
    ##########################
    ########## Logging
    ##########################
    logger = logging.getLogger(__name__)
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO)
    # reduce verbosity from transformers library
    logging.getLogger('transformers').setLevel(logging.WARNING)

    ##########################
    ########## Settings
    ##########################
    set_all_seeds(seed=42)
    device, n_gpu = initialize_device_settings(use_cuda=True)

    lang_model = "deepset/roberta-base-squad2"
    do_lower_case = False
    batch_size = 80

    data_dir = Path("../data/squad20")
    # We use the same file for dev and test set only for demo purposes
    dev_filename = "dev-v2.0.json"
    test_filename = "dev-v2.0.json"
    accuracy_at = 3 # accuracy at n is useful for answers inside long documents

    # 1.Create a tokenizer
    tokenizer = Tokenizer.load(
        pretrained_model_name_or_path=lang_model,
        do_lower_case=do_lower_case)

    # 2. Create a DataProcessor that handles all the conversion from raw text into a pytorch Dataset
    processor = SquadProcessor(
        tokenizer=tokenizer,
        max_seq_len=384,
        label_list=["start_token", "end_token"],
        metric="squad",
        train_filename=None,
        dev_filename=dev_filename,
        test_filename=test_filename,
        data_dir=data_dir,
        doc_stride=192,
    )

    # 3. Create a DataSilo that loads several datasets (train/dev/test), provides DataLoaders for them and calculates a few descriptive statistics of our datasets
    data_silo = DataSilo(
        processor=processor,
        batch_size=batch_size)

    # 4. Load pre-trained question-answering model
    model = AdaptiveModel.convert_from_transformers(lang_model, device=device, task_type="question_answering")
    model.connect_heads_with_processor(data_silo.processor.tasks, require_labels=True)
    # Number of predictions the model will make per Question.
    # The multiple predictions are used for evaluating top n recall.
    model.prediction_heads[0].n_best = accuracy_at

    # 5. The calibration of model confidence scores sets one parameter, which is called temperature and can be accessed through the prediction_head.
    # This temperature is applied to each logit in the forward pass, where each logit is divided by the temperature.
    # A softmax function is applied to the logits afterward to get confidence scores in the range [0,1].
    # A temperature larger than 1 decreases the model’s confidence scores.
    logger.info(f"Parameter used for temperature scaling of model confidence scores: {model.prediction_heads[0].temperature_for_confidence}")

    # 6a. We can either manually set the temperature (default value is 1.0)...
    model.prediction_heads[0].temperature_for_confidence = torch.nn.Parameter((torch.ones(1) * 1.0).to(device=device))

    # 6b. ...or we can run the evaluator on the dev set and use it to calibrate confidence scores with a technique called temperature scaling.
    # It will align the confidence scores with the model's accuracy based on the dev set data by tuning the temperature parameter.
    # During the calibration, this parameter is automatically set internally as an attribute of the prediction head.
    evaluator_dev = Evaluator(
        data_loader=data_silo.get_data_loader("dev"),
        tasks=data_silo.processor.tasks,
        device=device
    )
    result_dev = evaluator_dev.eval(model, return_preds_and_labels=True, calibrate_conf_scores=True)
    # evaluator_dev.log_results(result_dev, "Dev", logging=False, steps=len(data_silo.get_data_loader("dev")))

    # 7. Optionally, run the evaluator on the test set to see how well the confidence scores are aligned with the model's accuracy
    evaluator_test = Evaluator(
        data_loader=data_silo.get_data_loader("test"),
        tasks=data_silo.processor.tasks,
        device=device
    )
    result_test = evaluator_test.eval(model, return_preds_and_labels=True)[0]
    logger.info("Grouping predictions by confidence score and calculating metrics for each bin.")
    em_per_bin, confidence_per_bin, count_per_bin = metrics_per_bin(result_test["preds"], result_test["labels"], num_bins=10)
    for bin_number in range(10):
        logger.info(f"Bin {bin_number} - exact match: {em_per_bin[bin_number]}, average confidence score: {confidence_per_bin[bin_number]}")

    # 8. Hooray! You have a model with calibrated confidence scores.
    # Store the model and the temperature parameter will be stored automatically as an attribute of the prediction head.
    save_dir = Path("../saved_models/qa-confidence-tutorial")
    model.save(save_dir)
    processor.save(save_dir)

    # 9. When making a prediction with the calibrated model, we could filter out predictions where the model is not confident enough
    # To this end, load the stored model, which will automatically load the stored temperature parameter.
    # The confidence scores are automatically adjusted based on this temperature parameter.
    # For each prediction, we can check the model's confidence and decide whether to output the prediction or not.
    inferencer = QAInferencer.load(save_dir, batch_size=40, gpu=True)
    logger.info(f"Loaded model with stored temperature: {inferencer.model.prediction_heads[0].temperature_for_confidence}")

    QA_input = [
        {
            "questions": ["Who counted the game among the best ever made?"],
            "text": "Twilight Princess was released to universal critical acclaim and commercial success. It received perfect scores from major publications such as 1UP.com, Computer and Video Games, Electronic Gaming Monthly, Game Informer, GamesRadar, and GameSpy. On the review aggregators GameRankings and Metacritic, Twilight Princess has average scores of 95% and 95 for the Wii version and scores of 95% and 96 for the GameCube version. GameTrailers in their review called it one of the greatest games ever created."
        }]
    result = inferencer.inference_from_dicts(dicts=QA_input, return_json=False)[0]
    if result.prediction[0].confidence > 0.9:
        print(result.prediction[0].answer)
    else:
        print("The confidence is not high enough to give an answer.")

if __name__ == "__main__":
    question_answering_confidence()


#!/usr/bin/env python
# vim:fileencoding=utf-8

__license__   = 'GPL v3'
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

# Setup code {{{
import codecs
import sys

from calibre.utils.config_base import tweaks
from calibre_extensions import icu as _icu
from polyglot.builtins import cmp, filter, unicode_type

_locale = _collator = _primary_collator = _sort_collator = _numeric_collator = _case_sensitive_collator = None
cmp

_none = ''
_none2 = b''
_cmap = {}

icu_unicode_version = getattr(_icu, 'unicode_version', None)
_nmodes = {m:getattr(_icu, m) for m in ('NFC', 'NFD', 'NFKC', 'NFKD')}

# Ensure that the python internal filesystem and default encodings are not ASCII

def is_ascii(name):
    try:
        return codecs.lookup(name).name == b'ascii'
    except (TypeError, LookupError):
        return True

try:
    if is_ascii(sys.getdefaultencoding()):
        _icu.set_default_encoding(b'utf-8')
except:
    import traceback
    traceback.print_exc()

try:
    if is_ascii(sys.getfilesystemencoding()):
        _icu.set_filesystem_encoding(b'utf-8')
except:
    import traceback
    traceback.print_exc()
del is_ascii

def collator():
    global _collator, _locale
    if _collator is None:
        if _locale is None:
            from calibre.utils.localization import get_lang
            if tweaks['locale_for_sorting']:
                _locale = tweaks['locale_for_sorting']
            else:
                _locale = get_lang()
        try:
            _collator = _icu.Collator(_locale)
        except Exception as e:
            print('Failed to load collator for locale: %r with error %r, using English' % (_locale, e))
            _collator = _icu.Collator('en')
    return _collator

def change_locale(locale=None):
    global _locale, _collator, _primary_collator, _sort_collator, _numeric_collator, _case_sensitive_collator
    _collator = _primary_collator = _sort_collator = _numeric_collator = _case_sensitive_collator = None
    _locale = locale

def primary_collator():
    'Ignores case differences and accented characters'
    global _primary_collator
    if _primary_collator is None:
        _primary_collator = collator().clone()
        _primary_collator.strength = _icu.UCOL_PRIMARY
    return _primary_collator

def sort_collator():
    'Ignores case differences and recognizes numbers in strings (if the tweak is set)'
    global _sort_collator
    if _sort_collator is None:
        _sort_collator = collator().clone()
        _sort_collator.strength = _icu.UCOL_SECONDARY
        _sort_collator.numeric = tweaks['numeric_collation']
    return _sort_collator

def numeric_collator():
    'Uses natural sorting for numbers inside strings so something2 will sort before something10'
    global _numeric_collator
    if _numeric_collator is None:
        _numeric_collator = collator().clone()
        _numeric_collator.strength = _icu.UCOL_SECONDARY
        _numeric_collator.numeric = True
    return _numeric_collator

def case_sensitive_collator():
    'Always sorts upper case letter before lower case'
    global _case_sensitive_collator
    if _case_sensitive_collator is None:
        _case_sensitive_collator = collator().clone()
        _case_sensitive_collator.numeric = sort_collator().numeric
        _case_sensitive_collator.upper_first = True
    return _case_sensitive_collator

def make_sort_key_func(collator_function, func_name='sort_key'):
    func = None

    def sort_key(a):
        nonlocal func
        if func is None:
            func = getattr(collator_function(), func_name)

        try:
            return func(a)
        except TypeError:
            if isinstance(a, bytes):
                try:
                    a = a.decode(sys.getdefaultencoding())
                except ValueError:
                    return a
                return func(a)
        return b''

    return sort_key

def make_two_arg_func(collator_function, func_name='strcmp'):
    func = None

    def two_args(a, b):
        nonlocal func
        if func is None:
            func = getattr(collator_function(), func_name)

        try:
            return func(a, b)
        except TypeError:
            if isinstance(a, bytes):
                try:
                    a = a.decode(sys.getdefaultencoding())
                except Exception:
                    return cmp(a, b)
            elif a is None:
                a = ''
            if isinstance(b, bytes):
                try:
                    b = b.decode(sys.getdefaultencoding())
                except Exception:
                    return cmp(a, b)
            elif b is None:
                b = ''
            return func(a, b)

    return two_args

def make_change_case_func(which):

    def change_case(x):
        try:
            try:
                return _icu.change_case(x, which, _locale)
            except NotImplementedError:
                pass
            collator()  # sets _locale
            return _icu.change_case(x, which, _locale)
        except TypeError:
            if isinstance(x, bytes):
                try:
                    x = x.decode(sys.getdefaultencoding())
                except ValueError:
                    return x
                return _icu.change_case(x, which, _locale)
            raise
    return change_case
# }}}

# ################ The string functions ########################################
sort_key = make_sort_key_func(sort_collator)
numeric_sort_key = make_sort_key_func(numeric_collator)
primary_sort_key = make_sort_key_func(primary_collator)
case_sensitive_sort_key = make_sort_key_func(case_sensitive_collator)
collation_order = make_sort_key_func(sort_collator, 'collation_order')

strcmp = make_two_arg_func(sort_collator)
case_sensitive_strcmp = make_two_arg_func(case_sensitive_collator)
primary_strcmp = make_two_arg_func(primary_collator)
upper = make_change_case_func(_icu.UPPER_CASE)
lower = make_change_case_func(_icu.LOWER_CASE)
title_case = make_change_case_func(_icu.TITLE_CASE)

def capitalize(x):
    try:
        return upper(x[0]) + lower(x[1:])
    except (IndexError, TypeError, AttributeError):
        return x

try:
    swapcase = _icu.swap_case
except AttributeError:  # For people running from source
    swapcase = lambda x:x.swapcase()

find = make_two_arg_func(collator, 'find')
primary_find = make_two_arg_func(primary_collator, 'find')
contains = make_two_arg_func(collator, 'contains')
primary_contains = make_two_arg_func(primary_collator, 'contains')
startswith = make_two_arg_func(collator, 'startswith')
primary_startswith = make_two_arg_func(primary_collator, 'startswith')
safe_chr = _icu.chr
ord_string = _icu.ord_string

def character_name(string):
    try:
        return _icu.character_name(unicode_type(string)) or None
    except (TypeError, ValueError, KeyError):
        pass

def character_name_from_code(code):
    try:
        return _icu.character_name_from_code(code) or ''
    except (TypeError, ValueError, KeyError):
        return ''

def normalize(text, mode='NFC'):
    # This is very slightly slower than using unicodedata.normalize, so stick with
    # that unless you have very good reasons not too. Also, it's speed
    # decreases on wide python builds, where conversion to/from ICU's string
    # representation is slower.
    return _icu.normalize(_nmodes[mode], unicode_type(text))

def contractions(col=None):
    global _cmap
    col = col or _collator
    if col is None:
        col = collator()
    ans = _cmap.get(collator, None)
    if ans is None:
        ans = col.contractions()
        ans = frozenset(filter(None, ans))
        _cmap[col] = ans
    return ans

def partition_by_first_letter(items, reverse=False, key=lambda x:x):
    # Build a list of 'equal' first letters by noticing changes
    # in ICU's 'ordinal' for the first letter.
    from collections import OrderedDict
    items = sorted(items, key=lambda x:sort_key(key(x)), reverse=reverse)
    ans = OrderedDict()
    last_c, last_ordnum = ' ', 0
    for item in items:
        c = icu_upper(key(item) or ' ')
        ordnum, ordlen = collation_order(c)
        if last_ordnum != ordnum:
            last_c = c[0:1]
            last_ordnum = ordnum
        try:
            ans[last_c].append(item)
        except KeyError:
            ans[last_c] = [item]
    return ans

# Return the number of unicode codepoints in a string
string_length = len

# Return the number of UTF-16 codepoints in a string
utf16_length = _icu.utf16_length

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

if __name__ == '__main__':
    from calibre.utils.icu_test import run
    run(verbosity=4)


"""Unit tests for pyunormalize."""

import unittest

from pyunormalize.normalization import _decompose, _reorder, _compose
from pyunormalize import (
    NFC,
    NFD,
    NFKC,
    NFKD,
    normalize,
    UNICODE_VERSION as _UNICODE_VERSION,
)

UNICODE_VERSION = "15.0.0"

class Misc(unittest.TestCase):

    def test_UNICODE_VERSION(self):
        self.assertTrue(_UNICODE_VERSION == UNICODE_VERSION)

    def test_normalize(self):
        # Characters whose normalization forms
        # under NFC, NFD, NFKC, and NFKD are all different:
        #   ϓ   U+03D3 GREEK UPSILON WITH ACUTE AND HOOK SYMBOL
        #   ϔ   U+03D4 GREEK UPSILON WITH DIAERESIS AND HOOK SYMBOL
        #   ẛ   U+1E9B LATIN SMALL LETTER LONG S WITH DOT ABOVE
        for s in ["\u03D3", "\u03D4", "\u1E9B"]:
            self.assertTrue(
                normalize("NFC", s) == NFC(s)
            )
            self.assertTrue(
                normalize("NFD", s) == NFD(s)
            )
            self.assertTrue(
                normalize("NFKC", s) == NFKC(s)
            )
            self.assertTrue(
                normalize("NFKD", s) == NFKD(s)
            )

    def test_internals(self):

        self.assertEqual(
            _decompose("\u00C0"),
            [0x0041, 0x0300]
        )

        self.assertEqual(
            _decompose("\u00BE", compatibility=True),
            [0x0033, 0x2044, 0x0034]
        )

        self.assertEqual(
            _decompose("힡"),
            [0x1112, 0x1175, 0x11C0]
        )

        self.assertEqual(
            _reorder([0x017F, 0x0307, 0x0323]),
            [0x017F, 0x0323, 0x0307]
        )

        s = "a\u0328\u0302\u0301"  # a + ogonek + circumflex + acute
        self.assertEqual(
            _decompose(s),
            [0x0061, 0x0328, 0x0302, 0x0301]
        )
        self.assertEqual(
            _reorder([0x0061, 0x0328, 0x0302, 0x0301]),
            [0x0061, 0x0328, 0x0302, 0x0301]
        )
        self.assertEqual(
            _compose([0x0061, 0x0328, 0x0302, 0x0301]),
            [0x0105, 0x0302, 0x0301]
        )

        s = "\u0105\u0302\u0301"  # a-ogonek + circumflex + acute
        self.assertEqual(
            _compose(_decompose(_reorder(s))),
            [0x0105, 0x0302, 0x0301]
        )

        s = "\u0105\u0301\u0302"  # a-ogonek + acute + circumflex
        self.assertEqual(
            _decompose(s),
            [0x0061, 0x0328, 0x0301, 0x0302]
        )
        self.assertEqual(
            _reorder([0x0061, 0x0328, 0x0301, 0x0302]),
            [0x0061, 0x0328, 0x0301, 0x0302]
        )
        self.assertEqual(
            _compose([0x0061, 0x0328, 0x0301, 0x0302]),
            [0x0105, 0x0301, 0x0302]
        )

        self.assertEqual(
            _compose(_decompose(_reorder(s))),
            [0x0105, 0x0301, 0x0302]
        )

        # In www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf,
        # p. 140:  "The replacement of the Starter L in R2 requires continuing
        # to check the succeeding characters until the character at that
        # position is no longer part of any Non-blocked Pair that can be
        # replaced by a Primary Composite. For example, consider the following
        # hypothetical coded character sequence: <U+007A z, U+0335 short stroke
        # overlay, U+0327 cedilla, U+0324 diaeresis below, U+0301 acute>. None
        # of the first three combining marks forms a Primary Composite with
        # the letter z. However, the fourth combining mark in the sequence,
        # acute, does form a Primary Composite with z, and it is not Blocked
        # from the z. Therefore, R2 mandates the replacement of the sequence
        # <U+007A z, ... U+0301 acute> with <U+017A z-acute, ...>, even though
        # there are three other combining marks intervening in the sequence."
        items = [0x007A, 0x0335, 0x0327, 0x0324, 0x0301]
        self.assertEqual(
            _compose(items),
            [0x017A, 0x0335, 0x0327, 0x0324]
        )

if __name__ == "__main__":
    unittest.main()


def pd(x):
    l = len(x)
    for i in range(0,l):
        if(x[i] < '1' or x[i] > '9'):
            return False
    return True

a = []
while True:
    try:
        x = input()
    except EOFError:
        break

    if(pd(x)):
        a.append(int(x))

a.sort(reverse=True)
print(a)


# Enter the number of food storage and food in each storage
n = int(input())
array = list(map(int, input().split()))

d = [0] * 100
# Bottom up dynamic Programming
d[0] = array[0]
d[1] = max(array[0], array[1])
for i in range(2, n):
    d[i] = max(d[i - 1], d[i - 2] + array[i])

print(d[n - 1])


from django import template

register = template.Library()

class PhoneRegex():
    @register.filter(name='phone')
    def PhoneMask(PHONE):
        """ Return (00) 0000-0000 or (00) 00000-0000 """
        try:
            if len(PHONE) >=11:
                a = PHONE[0:2]
                b = PHONE[2:7]
                c = PHONE[7:11]
                return '(' + a + ')' + ' ' + b + '-' + c
            else:
                a = PHONE[0:2]
                b = PHONE[2:6]
                c = PHONE[6:10]
                return '(' + a + ')' + ' ' + b + '-' + c
        except ValueError:
            print('error in the counting of numeric characters')

class CpfRegex():
    @register.filter(name='cpf')
    def CpfMask(CPF):
        """ Return 000.000.000-00 """
        a = CPF[0:3]
        b = CPF[3:6]
        c = CPF[6:9]
        d = CPF[9:11]
        return a + '.' + b + '.' + c + '-' + d

class RgRegex():
    @register.filter(name='rg')
    def RgMask(RG):
        """ Return 00.000.000-0 or 0.000.000 or 0.000.000-00 """
        pass

class CnpjRegex():
    @register.filter(name='cnpj')
    def CnpjMask(CNPJ):
        """ Return 00.000.000/0000-00 """
        a = CNPJ[0:2]
        b = CNPJ[2:5]
        c = CNPJ[5:8]
        d = CNPJ[8:12]
        e = CNPJ[12:14]
        return a + '.' + b + '.' + c + '/' + d + '-' + e

class CepRegex():
    @register.filter(name='cep')
    def CepMask(CEP):
        """ Return 00000-000 """
        a = CEP[0:5]
        b = CEP[5:8]
        return a + '-' + b

class NcmRegex():
    @register.filter(name='ncm')
    def NcmMask(NCM):
        """ Return 0000.00.00 """
        a = NCM[0:4]
        b = NCM[4:6]
        c = NCM[6:8]
        return a + '.' + b + '.' + c


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

place_loc = np.array([[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0],
                      [4.2, 1], [4.7, 2], [5.3, 3], [5.9, 4], [6.5, 5], [7.3, 6], [8, 7]])
connect = np.loadtxt('./connect.csv', delimiter=',', dtype=int)

def read_log(file_name):
    file = open(file_name)
    episode = -1
    record = []  # episode, up_loc, down_loc, curr_loc
    while 1:
        info = str(file.readline())
        if info[: 3] == 'end':
            break
        elif info[: 7] == 'episode':
            episode += 1
        else:
            s = info.split()
            record.append((episode, int(s[0]), int(s[1]), int(s[2])))
    plot_env(record)

# 绘制当前环境
def plot_env(record):
    # 获取轮次、出租车位置坐标序列
    ep, up_x, up_y, do_x, do_y, x1, y1 = [], [], [], [], [], [], []
    for i in range(len(record)):
        ep.append(record[i][0])
        up_x.append(place_loc[record[i][1], 0])
        up_y.append(place_loc[record[i][1], 1])
        do_x.append(place_loc[record[i][2], 0])
        do_y.append(place_loc[record[i][2], 1])
        x1.append(place_loc[record[i][3], 0])
        y1.append(place_loc[record[i][3], 1])
    ep, up_x, up_y, do_x, do_y, x1, y1 = np.array(ep), np.array(up_x), np.array(up_y), np.array(do_x), np.array(do_y), np.array(x1), np.array(y1)

    # 绘制背景
    fig, ax = plt.subplots()
    plt.axis('off')
    for i in range(16):
        plt.scatter(place_loc[i, 0], place_loc[i, 1], s=10, c='k')
    for i in range(16):
        for j in range(16):
            if connect[i, j] == 0 or j <= i:
                continue
            plt.plot(place_loc[[i, j], 0], place_loc[[i, j], 1], c='lightgray', linewidth=1.0, linestyle="-")

    # 上车点、下车点、当前位置的运动
    def update_points(num):
        t = str(ep[num])
        ax.set_title("Episode No. "+t)
        point_ani1.set_data(up_x[num], up_y[num])
        point_ani2.set_data(do_x[num], do_y[num])
        point_ani.set_data(x1[num], y1[num])
        return point_ani1, point_ani2, point_ani,

    point_ani1, = plt.plot(up_x[0], up_y[0], "g^", linewidth=6)
    point_ani2, = plt.plot(do_x[0], do_y[0], "rv", linewidth=6)
    point_ani, = plt.plot(x1[0], y1[0], "bs", linewidth=15)

    ani = animation.FuncAnimation(fig, update_points, frames=x1.shape[0], interval=10)
    ani.save('video.mp4', fps=40, extra_args=['-vcodec', 'libx264'])
    plt.show()

if __name__ == "__main__":
    read_log('log.txt')