



"""simple docstring"""


import logging
import os
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List, Optional

import faiss
import torch
from datasets import Features, Sequence, Value, load_dataset

from transformers import (
    DPRContextEncoder,
    DPRContextEncoderTokenizerFast,
    HfArgumentParser,
    RagRetriever,
    RagSequenceForGeneration,
    RagTokenizer,
)


snake_case_								=			logging.getLogger(__name__)
torch.set_grad_enabled(False)
snake_case_								=			"""cuda""" if torch.cuda.is_available() else """cpu"""

def 					_lowerCAmelCase (  lowercase_						,      lowercase_=100						,      lowercase_=" "		):
						UpperCAmelCase			 =					text.split(lowercase_		)
						return [character.join(text[i : i + n]		).strip() for i in range(0						,      len(lowercase_		)						,      lowercase_		)]

def 					_lowerCAmelCase (  lowercase_		):
						UpperCAmelCase ,       UpperCAmelCase			 =					[], []
						for title, text in zip(documents['title']						,      documents['text']		):
												if text is not None:
																		for passage in split_text(lowercase_		):
																								titles.append(title if title is not None else ''		)
																								texts.append(lowercase_		)
						return {"title": titles, "text": texts}

def 					_lowerCAmelCase (  lowercase_						,      lowercase_						,      lowercase_		):
						UpperCAmelCase			 =					ctx_tokenizer(
						    documents['title']						,      documents['text']						,      truncation=lowercase_						,      padding='longest'						,      return_tensors='pt'		)['input_ids']
						UpperCAmelCase			 =					ctx_encoder(input_ids.to(device=lowercase_		)						,      return_dict=lowercase_		).pooler_output
						return {"embeddings": embeddings.detach().cpu().numpy()}

def 					_lowerCAmelCase (  lowercase_						,      lowercase_						,      lowercase_						,      ):
						######################################
						logger.info('Step 1 - Create the dataset'		)
						######################################

						# The dataset needed for RAG must have three columns:
						# - title (string): title of the document
						# - text (string): text of a passage of the document
						# - embeddings (array of dimension d): DPR representation of the passage

						# Let's say you have documents in tab-separated csv files with columns "title" and "text"
						assert os.path.isfile(rag_example_args.csv_path		), "Please provide a valid path to a csv file"

						# You can load a Dataset object this way
						UpperCAmelCase			 =					load_dataset(
						    'csv'						,      data_files=[rag_example_args.csv_path]						,      split='train'						,      delimiter='\t'						,      column_names=['title', 'text']		)

						# More info about loading csv files in the documentation: https://huggingface.co/docs/datasets/loading_datasets.html?highlight=csv#csv-files

						# Then split the documents into passages of 100 words
						UpperCAmelCase			 =					dataset.map(lowercase_						,      batched=lowercase_						,      num_proc=processing_args.num_proc		)

						# And compute the embeddings
						UpperCAmelCase			 =					DPRContextEncoder.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name		).to(device=lowercase_		)
						UpperCAmelCase			 =					DPRContextEncoderTokenizerFast.from_pretrained(rag_example_args.dpr_ctx_encoder_model_name		)
						UpperCAmelCase			 =					Features(
						    {'text': Value('string'		), 'title': Value('string'		), 'embeddings': Sequence(Value('float32'		)		)}		)  # optional, save as float32 instead of float64 to save space
						UpperCAmelCase			 =					dataset.map(
						    partial(lowercase_						,      ctx_encoder=lowercase_						,      ctx_tokenizer=lowercase_		)						,      batched=lowercase_						,      batch_size=processing_args.batch_size						,      features=lowercase_						,      )

						# And finally save your dataset
						UpperCAmelCase			 =					os.path.join(rag_example_args.output_dir						,      'my_knowledge_dataset'		)
						dataset.save_to_disk(lowercase_		)
						# from datasets import load_from_disk
						# dataset = load_from_disk(passages_path)  # to reload the dataset

						######################################
						logger.info('Step 2 - Index the dataset'		)
						######################################

						# Let's use the Faiss implementation of HNSW for fast approximate nearest neighbor search
						UpperCAmelCase			 =					faiss.IndexHNSWFlat(index_hnsw_args.d						,      index_hnsw_args.m						,      faiss.METRIC_INNER_PRODUCT		)
						dataset.add_faiss_index('embeddings'						,      custom_index=lowercase_		)

						# And save the index
						UpperCAmelCase			 =					os.path.join(rag_example_args.output_dir						,      'my_knowledge_dataset_hnsw_index.faiss'		)
						dataset.get_index('embeddings'		).save(lowercase_		)
						# dataset.load_faiss_index("embeddings", index_path)  # to reload the index

						######################################
						logger.info('Step 3 - Load RAG'		)
						######################################

						# Easy way to load the model
						UpperCAmelCase			 =					RagRetriever.from_pretrained(
						    rag_example_args.rag_model_name						,      index_name='custom'						,      indexed_dataset=lowercase_		)
						UpperCAmelCase			 =					RagSequenceForGeneration.from_pretrained(rag_example_args.rag_model_name						,      retriever=lowercase_		)
						UpperCAmelCase			 =					RagTokenizer.from_pretrained(rag_example_args.rag_model_name		)

						# For distributed fine-tuning you'll need to provide the paths instead, as the dataset and the index are loaded separately.
						# retriever = RagRetriever.from_pretrained(rag_model_name, index_name="custom", passages_path=passages_path, index_path=index_path)

						######################################
						logger.info('Step 4 - Have fun'		)
						######################################

						UpperCAmelCase			 =					rag_example_args.question or 'What does Moses\' rod turn into ?'
						UpperCAmelCase			 =					tokenizer.question_encoder(lowercase_						,      return_tensors='pt'		)['input_ids']
						UpperCAmelCase			 =					model.generate(lowercase_		)
						UpperCAmelCase			 =					tokenizer.batch_decode(lowercase_						,      skip_special_tokens=lowercase_		)[0]
						logger.info('Q: ' + question		)
						logger.info('A: ' + generated_string		)







@dataclass
class  A_     :



						"""simple docstring"""
						__UpperCamelCase													=				field(
						    default=str(Path(SCREAMING_SNAKE_CASE_							).parent / """test_data""" / """my_knowledge_dataset.csv"""							)      ,  metadata={"""help""": """Path to a tab-separated csv file with columns 'title' and 'text'"""}      ,  )
						__UpperCamelCase													=				field(
						    default=SCREAMING_SNAKE_CASE_      ,  metadata={"""help""": """Question that is passed as input to RAG. Default is 'What does Moses' rod turn into ?'."""}      ,  )
						__UpperCamelCase													=				field(
						    default="""facebook/rag-sequence-nq"""      ,  metadata={"""help""": """The RAG model to use. Either 'facebook/rag-sequence-nq' or 'facebook/rag-token-nq'"""}      ,  )
						__UpperCamelCase													=				field(
						    default="""facebook/dpr-ctx_encoder-multiset-base"""      ,  metadata={
						        """help""": (
						            """The DPR context encoder model to use. Either 'facebook/dpr-ctx_encoder-single-nq-base' or"""
						            """ 'facebook/dpr-ctx_encoder-multiset-base'"""
						        )
						    }      ,  )
						__UpperCamelCase													=				field(
						    default=SCREAMING_SNAKE_CASE_      ,  metadata={"""help""": """Path to a directory where the dataset passages and the index will be saved"""}      ,  )







@dataclass
class  A_     :



						"""simple docstring"""
						__UpperCamelCase													=				field(
						    default=SCREAMING_SNAKE_CASE_      ,  metadata={
						        """help""": """The number of processes to use to split the documents into passages. Default is single process."""
						    }      ,  )
						__UpperCamelCase													=				field(
						    default=16      ,  metadata={
						        """help""": """The batch size to use when computing the passages embeddings using the DPR context encoder."""
						    }      ,  )







@dataclass
class  A_     :



						"""simple docstring"""
						__UpperCamelCase													=				field(
						    default=7_68      ,  metadata={"""help""": """The dimension of the embeddings to pass to the HNSW Faiss index."""}      ,  )
						__UpperCamelCase													=				field(
						    default=1_28      ,  metadata={
						        """help""": (
						            """The number of bi-directional links created for every new element during the HNSW index construction."""
						        )
						    }      ,  )


if __name__ == "__main__":
				logging.basicConfig(level=logging.WARNING)
				logger.setLevel(logging.INFO)

				snake_case_								=			HfArgumentParser((RagExampleArguments, ProcessingArguments, IndexHnswArguments))
				snake_case_	,  snake_case_	,  snake_case_								=			parser.parse_args_into_dataclasses()
				with TemporaryDirectory() as tmp_dir:
								snake_case_								=			rag_example_args.output_dir or tmp_dir
								main(rag_example_args, processing_args, index_hnsw_args)












import os

import requests

_UpperCamelCase       :				str           =			"""https://www.amdoren.com/api/currency.php"""


# Currency and their description
_UpperCamelCase       :				str           =			"""
AED	United Arab Emirates Dirham
AFN	Afghan Afghani
ALL	Albanian Lek
AMD	Armenian Dram
ANG	Netherlands Antillean Guilder
AOA	Angolan Kwanza
ARS	Argentine Peso
AUD	Australian Dollar
AWG	Aruban Florin
AZN	Azerbaijani Manat
BAM	Bosnia & Herzegovina Convertible Mark
BBD	Barbadian Dollar
BDT	Bangladeshi Taka
BGN	Bulgarian Lev
BHD	Bahraini Dinar
BIF	Burundian Franc
BMD	Bermudian Dollar
BND	Brunei Dollar
BOB	Bolivian Boliviano
BRL	Brazilian Real
BSD	Bahamian Dollar
BTN	Bhutanese Ngultrum
BWP	Botswana Pula
BYN	Belarus Ruble
BZD	Belize Dollar
CAD	Canadian Dollar
CDF	Congolese Franc
CHF	Swiss Franc
CLP	Chilean Peso
CNY	Chinese Yuan
COP	Colombian Peso
CRC	Costa Rican Colon
CUC	Cuban Convertible Peso
CVE	Cape Verdean Escudo
CZK	Czech Republic Koruna
DJF	Djiboutian Franc
DKK	Danish Krone
DOP	Dominican Peso
DZD	Algerian Dinar
EGP	Egyptian Pound
ERN	Eritrean Nakfa
ETB	Ethiopian Birr
EUR	Euro
FJD	Fiji Dollar
GBP	British Pound Sterling
GEL	Georgian Lari
GHS	Ghanaian Cedi
GIP	Gibraltar Pound
GMD	Gambian Dalasi
GNF	Guinea Franc
GTQ	Guatemalan Quetzal
GYD	Guyanaese Dollar
HKD	Hong Kong Dollar
HNL	Honduran Lempira
HRK	Croatian Kuna
HTG	Haiti Gourde
HUF	Hungarian Forint
IDR	Indonesian Rupiah
ILS	Israeli Shekel
INR	Indian Rupee
IQD	Iraqi Dinar
IRR	Iranian Rial
ISK	Icelandic Krona
JMD	Jamaican Dollar
JOD	Jordanian Dinar
JPY	Japanese Yen
KES	Kenyan Shilling
KGS	Kyrgystani Som
KHR	Cambodian Riel
KMF	Comorian Franc
KPW	North Korean Won
KRW	South Korean Won
KWD	Kuwaiti Dinar
KYD	Cayman Islands Dollar
KZT	Kazakhstan Tenge
LAK	Laotian Kip
LBP	Lebanese Pound
LKR	Sri Lankan Rupee
LRD	Liberian Dollar
LSL	Lesotho Loti
LYD	Libyan Dinar
MAD	Moroccan Dirham
MDL	Moldovan Leu
MGA	Malagasy Ariary
MKD	Macedonian Denar
MMK	Myanma Kyat
MNT	Mongolian Tugrik
MOP	Macau Pataca
MRO	Mauritanian Ouguiya
MUR	Mauritian Rupee
MVR	Maldivian Rufiyaa
MWK	Malawi Kwacha
MXN	Mexican Peso
MYR	Malaysian Ringgit
MZN	Mozambican Metical
NAD	Namibian Dollar
NGN	Nigerian Naira
NIO	Nicaragua Cordoba
NOK	Norwegian Krone
NPR	Nepalese Rupee
NZD	New Zealand Dollar
OMR	Omani Rial
PAB	Panamanian Balboa
PEN	Peruvian Nuevo Sol
PGK	Papua New Guinean Kina
PHP	Philippine Peso
PKR	Pakistani Rupee
PLN	Polish Zloty
PYG	Paraguayan Guarani
QAR	Qatari Riyal
RON	Romanian Leu
RSD	Serbian Dinar
RUB	Russian Ruble
RWF	Rwanda Franc
SAR	Saudi Riyal
SBD	Solomon Islands Dollar
SCR	Seychellois Rupee
SDG	Sudanese Pound
SEK	Swedish Krona
SGD	Singapore Dollar
SHP	Saint Helena Pound
SLL	Sierra Leonean Leone
SOS	Somali Shilling
SRD	Surinamese Dollar
SSP	South Sudanese Pound
STD	Sao Tome and Principe Dobra
SYP	Syrian Pound
SZL	Swazi Lilangeni
THB	Thai Baht
TJS	Tajikistan Somoni
TMT	Turkmenistani Manat
TND	Tunisian Dinar
TOP	Tonga Paanga
TRY	Turkish Lira
TTD	Trinidad and Tobago Dollar
TWD	New Taiwan Dollar
TZS	Tanzanian Shilling
UAH	Ukrainian Hryvnia
UGX	Ugandan Shilling
USD	United States Dollar
UYU	Uruguayan Peso
UZS	Uzbekistan Som
VEF	Venezuelan Bolivar
VND	Vietnamese Dong
VUV	Vanuatu Vatu
WST	Samoan Tala
XAF	Central African CFA franc
XCD	East Caribbean Dollar
XOF	West African CFA franc
XPF	CFP Franc
YER	Yemeni Rial
ZAR	South African Rand
ZMW	Zambian Kwacha
"""
def  __UpperCamelCase    (						snake_case = "USD"							,			snake_case = "INR"							,			snake_case = 1.0							,			snake_case = ""      )    ->		str:


   '''simple docstring'''







   __A    =  locals()
   # from is a reserved keyword
   __A    =  params.pop('''from_'''      )
   __A    =  requests.get(snake_case							,			params=snake_case      ).json()
   return str(res['''amount''']      ) if res["error"] == 0 else res["error_message"]


if __name__ == "__main__":
    _UpperCamelCase       :				Optional[int]           =			os.getenv("""CI""", """""")
    _UpperCamelCase       :				Optional[Any]           =			os.getenv("""AMDOREN_API_KEY""", """""")

    if not API_KEY and not TESTING:
        raise KeyError(
            """API key must be provided in the 'AMDOREN_API_KEY' environment variable."""
        )

    print(
        convert_currency(
            input("""Enter from currency: """).strip(),
            input("""Enter to currency: """).strip(),
            float(input("""Enter the amount: """).strip()),
            API_KEY,
        )
    )

