diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000000000000000000000000000000000000..6507a0177f9b5887bc1bdfb60ec1f614042504e3
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,12 @@
+start_endpoint.bat
+start.bat
+.gitignore
+.github
+.git
+.env
+LICENCE
+install_ubuntu.sh
+install_on_huggingface.md
+docker-compose.yml
+config.json
+docker-compose/
diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
new file mode 100644
index 0000000000000000000000000000000000000000..8281ecac41d918dc4549c439352f48feba0095ea
--- /dev/null
+++ b/.github/FUNDING.yml
@@ -0,0 +1,13 @@
+# These are supported funding model platforms
+
+github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
+patreon: # Replace with a single Patreon username
+open_collective: # Replace with a single Open Collective username
+ko_fi: # Replace with a single Ko-fi username
+tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
+community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
+liberapay: # Replace with a single Liberapay username
+issuehunt: # Replace with a single IssueHunt username
+otechie: # Replace with a single Otechie username
+lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
+custom: # ['https://boosty.to/neurogen']
diff --git a/.github/README.md b/.github/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..1f559ee78394ac95416fbc5d92cb787412821065
--- /dev/null
+++ b/.github/README.md
@@ -0,0 +1,68 @@
+
+
+
+
+
+
Русский | English
+
+> ### The project is currently undergoing a transition to another client and reconstruction of the API. Technical work is continuing. We apologize for any inconvenience.
+
+
Free API service providing access to GPT-3.5, GPT-4, and other language models. Before using it, please make sure you check out our wiki . The project utilizes a modified version of gpt4free , as well as ChuanhuChatGPT as a web interface. We extend our gratitude to the authors.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
News and feedback:
+
+
+
+
+
+
+
+
+
Support the project:
+
+
+
+
+
+
+
+## Features
+
+- Web search
+- [Model list](https://status.neuroapi.host/v1/status)
+- Dialog context
+- No-logs
+- API endpoint
+- Dialog history
+- Setting generation parameters for GPT models
+- Built-in prompt templates and jailbreaks for various tasks
+
+
+
+
+***
+
+[![Star History Chart](https://api.star-history.com/svg?repos=Em1tSan/NeuroGPT&type=Date)](https://star-history.com/#Em1tSan/NeuroGPT&Date)
+
+
diff --git a/.github/README_RU.md b/.github/README_RU.md
new file mode 100644
index 0000000000000000000000000000000000000000..3a94d9d1ceba39a83a2a912e6c918850ee2a3a32
--- /dev/null
+++ b/.github/README_RU.md
@@ -0,0 +1,66 @@
+
+
+
+
+
+
Русский | English
+
+
Бесплатный API-сервис, предоставляющий доступ к GPT-3.5, GPT-4 и другим языковым моделям. Перед использованием, пожалуйста, убедитесь, что ознакомились с нашей wiki . В проекте используется модифицированная версия gpt4free , а также ChuanhuChatGPT в качестве веб-интерфейса. Выражаем благодарность авторам.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Новости и обратная связь:
+
+
+
+
+
+
+
+
+
Поддержать проект:
+
+
+
+
+
+
+
+## Возможности
+
+- Веб-поиск
+- [Список моделей](https://status.neuroapi.host/v1/status)
+- Контекст беседы
+- Отсутствие логов
+- История диалога
+- Режим endpoint для работы с API
+- Изменение параметров генерации для GPT-моделей
+- Встроенные джейлбрейки и шаблоны промптов под разные задачи
+
+
+
+
+***
+
+[![Star History Chart](https://api.star-history.com/svg?repos=Em1tSan/NeuroGPT&type=Date)](https://star-history.com/#Em1tSan/NeuroGPT&Date)
+
+
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..94b283e1302bbf60bb942a011a9cbae82078c48d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,152 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+pip-wheel-metadata/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+history/
+index/
+
+# PyInstaller
+# Usually these files are written by a python script from a template
+# before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+.python-version
+
+# pipenv
+# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+# However, in case of collaboration, if having platform-specific dependencies or dependencies
+# having no cross-platform support, pipenv may install dependencies that don't work, or not
+# install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+python/
+git/
+history/
+
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# Mac system file
+**/.DS_Store
+
+#vscode
+.vscode
+
+# 配置文件/模型文件
+api_key.txt
+config.json
+auth.json
+.models/
+lora/
+.idea
+templates/*
+code_improver.yaml
+.history
+config.json
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000000000000000000000000000000000000..f288702d2fa16d3cdf0035b15a9fcbc552cd88e7
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,674 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+
+ Copyright (C)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ Copyright (C)
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+ .
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+.
diff --git a/README.md b/README.md
index 4ab17fe8ee974555f58de6636197558ed3c723b4..83adb255516515effb87d8d915fc6afd32f370b9 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,6 @@
---
title: NeuroGPT
-emoji: 👁
-colorFrom: pink
-colorTo: indigo
+app_file: webui_en.py
sdk: gradio
sdk_version: 4.27.0
-app_file: app.py
-pinned: false
---
-
-Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
diff --git a/assets/custom.css b/assets/custom.css
new file mode 100644
index 0000000000000000000000000000000000000000..d5b6942a979981f017b90d67936f7aa273f57151
--- /dev/null
+++ b/assets/custom.css
@@ -0,0 +1,655 @@
+:root {
+ --chatbot-color-light: #000000;
+ --chatbot-color-dark: #FFFFFF;
+ --chatbot-background-color-light: #F3F3F3;
+ --chatbot-background-color-dark: #121111;
+ --message-user-background-color-light: #95EC69;
+ --message-user-background-color-dark: #26B561;
+ --message-bot-background-color-light: #FFFFFF;
+ --message-bot-background-color-dark: #2C2C2C;
+ --switch-checkbox-color-light: #e5e7eb;
+ --switch-checkbox-color-dark: #515151;
+}
+
+
+#app_title {
+ font-weight: var(--prose-header-text-weight);
+ font-size: var(--text-xxl);
+ line-height: 1.3;
+ text-align: left;
+ margin-top: 6px;
+ white-space: nowrap;
+}
+#description {
+ text-align: center;
+ margin: 32px 0 4px 0;
+}
+
+/* Fix padding error when container=False */
+div.form {
+ background: none !important;
+}
+
+/* Advanced page */
+#advanced_warning {
+ display: flex;
+ flex-wrap: wrap;
+ flex-direction: column;
+ align-content: center;
+}
+
+/* Gradio footer info */
+footer {
+ /* display: none !important; */
+ margin-top: .2em !important;
+ font-size: 85%;
+}
+#footer {
+ text-align: center;
+}
+#footer div {
+ display: inline-block;
+}
+#footer .versions{
+ font-size: 85%;
+ opacity: 0.60;
+}
+
+#float_display {
+ position: absolute;
+ max-height: 30px;
+}
+#toast-update {
+ position: absolute;
+ display: flex;
+ top: -500px;
+ width: 100%;
+ justify-content: center;
+ z-index: var(--layer-top);
+ transition: top 0.3s ease-out;
+}
+#check-chuanhu-update {
+ position: absolute;
+ align-items: center;
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ margin: var(--size-6) var(--size-4);
+ box-shadow: var(--shadow-drop-lg);
+ border: 1px solid var(--block-label-border-color);
+ border-radius: var(--container-radius);
+ background: var(--background-fill-primary);
+ padding: var(--size-4) var(--size-6);
+ min-width: 360px;
+ max-width: 480px;
+ overflow: hidden;
+ pointer-events: auto;
+}
+#version-info-title {
+ font-size: 1.2em;
+ font-weight: bold;
+ text-align: start;
+ width: 100%;
+}
+#release-note-wrap {
+ width: 100%;
+ max-width: 400px;
+ height: 120px;
+ border: solid 1px var(--border-color-primary);
+ overflow: auto;
+ padding: 0 8px;
+}
+#release-note-wrap.hideK {
+ display: none;
+}
+.btn-update-group {
+ display: flex;
+ justify-content: space-evenly;
+ align-items: center;
+ width: 100%;
+ padding-top: 10px;
+}
+.btn-update-group.hideK {
+ display: none;
+}
+/* user_info */
+#user_info.block {
+ white-space: nowrap;
+ position: absolute; left: 8em; top: .8em;
+ z-index: var(--layer-2);
+ box-shadow: var(--block-shadow);
+ border: none!important; border-radius: var(--block-label-radius);
+ background: var(--color-accent);
+ padding: var(--block-label-padding);
+ font-size: var(--block-label-text-size); line-height: var(--line-sm);
+ width: auto; max-height: 30px!important;
+ opacity: 1;
+ transition: opacity 0.3s ease-in-out;
+}
+#user_info.block .wrap {
+ opacity: 0;
+}
+#user_info p {
+ color: white;
+ font-weight: var(--block-label-text-weight);
+}
+#user_info.hideK {
+ opacity: 0;
+ transition: opacity 1s ease-in-out;
+}
+
+/* status_display */
+#status_display {
+ display: flex;
+ min-height: 2em;
+ align-items: flex-end;
+ justify-content: flex-end;
+}
+#status_display p {
+ font-size: .85em;
+ font-family: ui-monospace, "SF Mono", "SFMono-Regular", "Menlo", "Consolas", "Liberation Mono", "Microsoft Yahei UI", "Microsoft Yahei", monospace;
+ /* Fallback to Microsoft Yahei on Windows */
+ color: var(--body-text-color-subdued);
+}
+
+#status_display {
+ transition: all 0.6s;
+}
+#chuanhu_chatbot {
+ transition: height 0.3s ease;
+}
+
+/* usage_display */
+.insert_block {
+ position: relative;
+ margin: 0;
+ padding: 8px 12px;
+ box-shadow: var(--block-shadow);
+ border-width: var(--block-border-width);
+ border-color: var(--block-border-color);
+ border-radius: var(--block-radius);
+ background: var(--block-background-fill);
+ width: 100%;
+ line-height: var(--line-sm);
+ min-height: 2em;
+}
+#usage_display p, #usage_display span {
+ margin: 0;
+ font-size: .85em;
+ color: var(--body-text-color-subdued);
+}
+.progress-bar {
+ background-color: var(--input-background-fill);;
+ margin: .5em 0 !important;
+ height: 20px;
+ border-radius: 10px;
+ overflow: hidden;
+}
+.progress {
+ background-color: var(--block-title-background-fill);
+ height: 100%;
+ border-radius: 10px;
+ text-align: right;
+ transition: width 0.5s ease-in-out;
+}
+.progress-text {
+ /* color: white; */
+ color: var(--color-accent) !important;
+ font-size: 1em !important;
+ font-weight: bold;
+ padding-right: 10px;
+ line-height: 20px;
+}
+
+/* Light/dark mode switch */
+#apSwitch input[type="checkbox"] {
+ margin: 0 !important;
+}
+#apSwitch label.apSwitch {
+ display: flex;
+ align-items: center;
+ cursor: pointer;
+ color: var(--body-text-color);
+ font-weight: var(--checkbox-label-text-weight);
+ font-size: var(--checkbox-label-text-size);
+ line-height: var(--line-md);
+ margin: 2px 0 !important;
+}
+input[type="checkbox"]#apSwitch_checkbox::before {
+ background: none !important;
+ content: '';
+ border: none !important;
+ box-shadow: none !important;
+ font-size: 22px;
+ top: -4.4px;
+ left: -1px;
+}
+input:checked[type="checkbox"]#apSwitch_checkbox::before {
+ content: '';
+ left: 16px;
+}
+/* .apSwitch {
+ top: 2px;
+ display: inline-block;
+ height: 22px;
+ position: relative;
+ width: 40px;
+ border-radius: 11px;
+ box-shadow: inset 0 0 1px 0 rgba(0,0,0,0.05), inset 0 0 2px 0 rgba(0,0,0,0.08) !important;
+}
+.apSwitch input {
+ display: none !important;
+}
+.apSlider {
+ background-color: var(--neutral-200);
+ bottom: 0;
+ cursor: pointer;
+ left: 0;
+ position: absolute;
+ right: 0;
+ top: 0;
+ transition: .4s;
+ font-size: 22px;
+ border-radius: 11px;
+}
+.apSlider::before {
+ transform: scale(0.9);
+ position: absolute;
+ transition: .4s;
+ content: "🌞";
+}
+input:checked + .apSlider {
+ background-color: var(--primary-600);
+}
+input:checked + .apSlider::before {
+ transform: translateX(18px);
+ content:"🌚";
+} */
+.switch_checkbox label {
+ flex-direction: row-reverse;
+ justify-content: space-between;
+}
+.switch_checkbox input[type="checkbox"] + span {
+ margin-left: 0 !important;
+}
+.switch_checkbox input[type="checkbox"] {
+ -moz-appearance: none;
+ appearance: none;
+ -webkit-appearance: none;
+ outline: none;
+}
+.switch_checkbox input[type="checkbox"] {
+ display: inline-block !important;
+ position: relative !important;
+ border: none !important;
+ outline: none;
+ width: 40px !important;
+ height: 22px !important;
+ border-radius: 11px !important;
+ background-image: none !important;
+ box-shadow: inset 0 0 1px 0 rgba(0,0,0,0.05), inset 0 0 2px 0 rgba(0,0,0,0.08) !important;
+ background-image: none !important;
+ background-color: var(--switch-checkbox-color-light) !important;
+ transition: .2s ease background-color;
+}
+.dark .switch_checkbox input[type="checkbox"] {
+ background-color: var(--switch-checkbox-color-dark) !important;
+}
+.switch_checkbox input[type="checkbox"]::before {
+ content: "";
+ position: absolute;
+ width: 22px;
+ height: 22px;
+ top: 0;
+ left: 0;
+ background: #FFFFFF;
+ border: 0.5px solid rgba(0,0,0,0.02);
+ box-shadow: 0 0 0 0 rgba(0,0,0,0.15), 0 1px 0 0 rgba(0,0,0,0.05);
+ transform: scale(0.9);
+ border-radius: 11px !important;
+ transition: .4s ease all;
+ box-shadow: var(--input-shadow);
+}
+.switch_checkbox input:checked[type="checkbox"] {
+ background-color: var(--primary-600) !important;
+}
+.switch_checkbox input:checked[type="checkbox"]::before {
+ background-color: #fff;
+ left: 18px;
+}
+
+/* Override Slider Styles (for webkit browsers like Safari and Chrome)
+ * Hope this proposal can be implemented soon https://github.com/w3c/csswg-drafts/issues/4410
+ * Sliders are too inconsistent across platforms
+ */
+input[type="range"] {
+ -webkit-appearance: none;
+ height: 4px;
+ background: var(--input-background-fill);
+ border-radius: 5px;
+ background-image: linear-gradient(var(--primary-500),var(--primary-500));
+ background-size: 0% 100%;
+ background-repeat: no-repeat;
+}
+input[type="range"]::-webkit-slider-thumb {
+ -webkit-appearance: none;
+ height: 20px;
+ width: 20px;
+ border-radius: 50%;
+ border: solid 0.5px #ddd;
+ background-color: white;
+ cursor: ew-resize;
+ box-shadow: var(--input-shadow);
+ transition: background-color .1s ease;
+}
+input[type="range"]::-webkit-slider-thumb:hover {
+ background: var(--neutral-50);
+}
+input[type=range]::-webkit-slider-runnable-track {
+ -webkit-appearance: none;
+ box-shadow: none;
+ border: none;
+ background: transparent;
+}
+
+hr.append-display {
+ margin: 8px 0;
+ border: none;
+ height: 1px;
+ border-top-width: 0;
+ background-image: linear-gradient(to right, rgba(50,50,50, 0.1), rgba(150, 150, 150, 0.8), rgba(50,50,50, 0.1));
+}
+.source-a {
+ font-size: 0.8em;
+ max-width: 100%;
+ margin: 0;
+ display: flex;
+ flex-direction: row;
+ flex-wrap: wrap;
+ align-items: center;
+ /* background-color: #dddddd88; */
+ border-radius: 1.5rem;
+ padding: 0.2em;
+}
+.source-a a {
+ display: inline-block;
+ background-color: #aaaaaa50;
+ border-radius: 1rem;
+ padding: 0.5em;
+ text-align: center;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ min-width: 20%;
+ white-space: nowrap;
+ margin: 0.2rem 0.1rem;
+ text-decoration: none !important;
+ flex: 1;
+ transition: flex 0.5s;
+}
+.source-a a:hover {
+ background-color: #aaaaaa20;
+ flex: 2;
+}
+
+#submit_btn, #cancel_btn {
+ height: 42px !important;
+}
+#submit_btn::before {
+ content: url("data:image/svg+xml, %3Csvg width='21px' height='20px' viewBox='0 0 21 20' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Cg id='page' stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E %3Cg id='send' transform='translate(0.435849, 0.088463)' fill='%23FFFFFF' fill-rule='nonzero'%3E %3Cpath d='M0.579148261,0.0428666046 C0.301105539,-0.0961547561 -0.036517765,0.122307382 0.0032026237,0.420210298 L1.4927172,18.1553639 C1.5125774,18.4334066 1.79062012,18.5922882 2.04880264,18.4929872 L8.24518329,15.8913017 L11.6412765,19.7441794 C11.8597387,19.9825018 12.2370824,19.8832008 12.3165231,19.5852979 L13.9450591,13.4882182 L19.7839562,11.0255541 C20.0619989,10.8865327 20.0818591,10.4694687 19.7839562,10.3105871 L0.579148261,0.0428666046 Z M11.6138902,17.0883151 L9.85385903,14.7195502 L0.718169621,0.618812241 L12.69945,12.9346347 L11.6138902,17.0883151 Z' id='shape'%3E%3C/path%3E %3C/g%3E %3C/g%3E %3C/svg%3E");
+ height: 21px;
+}
+#cancel_btn::before {
+ content: url("data:image/svg+xml,%3Csvg width='21px' height='21px' viewBox='0 0 21 21' version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'%3E %3Cg id='pg' stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E %3Cpath d='M10.2072007,20.088463 C11.5727865,20.088463 12.8594566,19.8259823 14.067211,19.3010209 C15.2749653,18.7760595 16.3386126,18.0538087 17.2581528,17.1342685 C18.177693,16.2147282 18.8982283,15.1527965 19.4197586,13.9484733 C19.9412889,12.7441501 20.202054,11.4557644 20.202054,10.0833163 C20.202054,8.71773046 19.9395733,7.43106036 19.4146119,6.22330603 C18.8896505,5.01555169 18.1673997,3.95018885 17.2478595,3.0272175 C16.3283192,2.10424615 15.2646719,1.3837109 14.0569176,0.865611739 C12.8491633,0.34751258 11.5624932,0.088463 10.1969073,0.088463 C8.83132146,0.088463 7.54636692,0.34751258 6.34204371,0.865611739 C5.1377205,1.3837109 4.07407321,2.10424615 3.15110186,3.0272175 C2.22813051,3.95018885 1.5058797,5.01555169 0.984349419,6.22330603 C0.46281914,7.43106036 0.202054,8.71773046 0.202054,10.0833163 C0.202054,11.4557644 0.4645347,12.7441501 0.9894961,13.9484733 C1.5144575,15.1527965 2.23670831,16.2147282 3.15624854,17.1342685 C4.07578877,18.0538087 5.1377205,18.7760595 6.34204371,19.3010209 C7.54636692,19.8259823 8.83475258,20.088463 10.2072007,20.088463 Z M10.2072007,18.2562448 C9.07493099,18.2562448 8.01471483,18.0452309 7.0265522,17.6232031 C6.03838956,17.2011753 5.17031614,16.6161693 4.42233192,15.8681851 C3.6743477,15.1202009 3.09105726,14.2521274 2.67246059,13.2639648 C2.25386392,12.2758022 2.04456558,11.215586 2.04456558,10.0833163 C2.04456558,8.95104663 2.25386392,7.89083047 2.67246059,6.90266784 C3.09105726,5.9145052 3.6743477,5.04643178 4.42233192,4.29844756 C5.17031614,3.55046334 6.036674,2.9671729 7.02140552,2.54857623 C8.00613703,2.12997956 9.06463763,1.92068122 10.1969073,1.92068122 C11.329177,1.92068122 12.3911087,2.12997956 13.3827025,2.54857623 C14.3742962,2.9671729 15.2440852,3.55046334 15.9920694,4.29844756 C16.7400537,5.04643178 17.3233441,5.9145052 17.7419408,6.90266784 C18.1605374,7.89083047 18.3698358,8.95104663 18.3698358,10.0833163 C18.3698358,11.215586 18.1605374,12.2758022 17.7419408,13.2639648 C17.3233441,14.2521274 16.7400537,15.1202009 15.9920694,15.8681851 C15.2440852,16.6161693 14.3760118,17.2011753 13.3878492,17.6232031 C12.3996865,18.0452309 11.3394704,18.2562448 10.2072007,18.2562448 Z M7.65444721,13.6242324 L12.7496608,13.6242324 C13.0584616,13.6242324 13.3003556,13.5384544 13.4753427,13.3668984 C13.6503299,13.1953424 13.7378234,12.9585951 13.7378234,12.6566565 L13.7378234,7.49968276 C13.7378234,7.19774418 13.6503299,6.96099688 13.4753427,6.78944087 C13.3003556,6.61788486 13.0584616,6.53210685 12.7496608,6.53210685 L7.65444721,6.53210685 C7.33878414,6.53210685 7.09345904,6.61788486 6.91847191,6.78944087 C6.74348478,6.96099688 6.65599121,7.19774418 6.65599121,7.49968276 L6.65599121,12.6566565 C6.65599121,12.9585951 6.74348478,13.1953424 6.91847191,13.3668984 C7.09345904,13.5384544 7.33878414,13.6242324 7.65444721,13.6242324 Z' id='shape' fill='%23FF3B30' fill-rule='nonzero'%3E%3C/path%3E %3C/g%3E %3C/svg%3E");
+ height: 21px;
+}
+/* list */
+ol:not(.options), ul:not(.options) {
+ padding-inline-start: 2em !important;
+}
+
+/* Light theme (default) */
+#chuanhu_chatbot {
+ background-color: var(--chatbot-background-color-light) !important;
+ color: var(--chatbot-color-light) !important;
+}
+[data-testid = "bot"] {
+ background-color: var(--message-bot-background-color-light) !important;
+}
+[data-testid = "user"] {
+ background-color: var(--message-user-background-color-light) !important;
+}
+/* Dark theme */
+.dark #chuanhu_chatbot {
+ background-color: var(--chatbot-background-color-dark) !important;
+ color: var(--chatbot-color-dark) !important;
+}
+.dark [data-testid = "bot"] {
+ background-color: var(--message-bot-background-color-dark) !important;
+}
+.dark [data-testid = "user"] {
+ background-color: var(--message-user-background-color-dark) !important;
+}
+
+/* Devices with screen width >= 500px */
+/* Height adjustments written in JavaScript since 2023.4.8 */
+@media screen and (min-width: 500px) {
+ #chuanhu_chatbot {
+ height: calc(100vh - 200px);
+ }
+ #chuanhu_chatbot>.wrapper>.wrap {
+ max-height: calc(100vh - 200px - var(--line-sm)*1rem - 2*var(--block-label-margin) );
+ }
+}
+/* Devices with screen width < 500px */
+@media screen and (max-width: 499px) {
+ #chuanhu_chatbot {
+ height: calc(100vh - 140px);
+ }
+ #chuanhu_chatbot>.wrapper>.wrap {
+ max-height: calc(100vh - 140px - var(--line-sm)*1rem - 2*var(--block-label-margin) );
+ }
+ [data-testid = "bot"] {
+ max-width: 95% !important;
+ }
+ #app_title h1{
+ letter-spacing: -1px; font-size: 22px;
+ }
+}
+#chuanhu_chatbot>.wrapper>.wrap {
+ overflow-x: hidden;
+}
+/* Chat bubbles */
+.message {
+ border-radius: var(--radius-xl) !important;
+ border: none;
+ padding: var(--spacing-xl) !important;
+ font-size: var(--text-md) !important;
+ line-height: var(--line-md) !important;
+ min-height: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl));
+ min-width: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl));
+}
+[data-testid = "bot"] {
+ max-width: 85%;
+ border-bottom-left-radius: 0 !important;
+}
+[data-testid = "user"] {
+ max-width: 85%;
+ width: auto !important;
+ border-bottom-right-radius: 0 !important;
+}
+
+.message.user p {
+ white-space: pre-wrap;
+}
+.message .user-message {
+ display: block;
+ padding: 0 !important;
+ white-space: pre-wrap;
+}
+
+.message .md-message p {
+ margin-top: 0.6em !important;
+ margin-bottom: 0.6em !important;
+}
+.message .md-message p:first-child { margin-top: 0 !important; }
+.message .md-message p:last-of-type { margin-bottom: 0 !important; }
+
+.message .md-message {
+ display: block;
+ padding: 0 !important;
+}
+.message .raw-message p {
+ margin:0 !important;
+}
+.message .raw-message {
+ display: block;
+ padding: 0 !important;
+ white-space: pre-wrap;
+}
+.raw-message.hideM, .md-message.hideM {
+ display: none;
+}
+
+/* Custom buttons */
+.chuanhu-btn {
+ border-radius: 5px;
+ /* background-color: #E6E6E6 !important; */
+ color: rgba(120, 120, 120, 0.64) !important;
+ padding: 4px !important;
+ position: absolute;
+ right: -22px;
+ cursor: pointer !important;
+ transition: color .2s ease, background-color .2s ease;
+}
+.chuanhu-btn:hover {
+ background-color: rgba(167, 167, 167, 0.25) !important;
+ color: unset !important;
+}
+.chuanhu-btn:active {
+ background-color: rgba(167, 167, 167, 0.5) !important;
+}
+.chuanhu-btn:focus {
+ outline: none;
+}
+.copy-bot-btn {
+ /* top: 18px; */
+ bottom: 0;
+}
+.toggle-md-btn {
+ /* top: 0; */
+ bottom: 20px;
+}
+.copy-code-btn {
+ position: relative;
+ float: right;
+ font-size: 1em;
+ cursor: pointer;
+}
+
+.message-wrap>div img{
+ border-radius: 10px !important;
+}
+
+/* History message */
+.wrapper>.wrap>.history-message {
+ padding: 10px !important;
+}
+.history-message {
+ /* padding: 0 !important; */
+ opacity: 80%;
+ display: flex;
+ flex-direction: column;
+}
+.history-message>.history-message {
+ padding: 0 !important;
+}
+.history-message>.message-wrap {
+ padding: 0 !important;
+ margin-bottom: 16px;
+}
+.history-message>.message {
+ margin-bottom: 16px;
+}
+.wrapper>.wrap>.history-message::after {
+ content: "";
+ display: block;
+ height: 2px;
+ background-color: var(--body-text-color-subdued);
+ margin-bottom: 10px;
+ margin-top: -10px;
+ clear: both;
+}
+.wrapper>.wrap>.history-message>:last-child::after {
+ content: "For viewing only";
+ display: block;
+ text-align: center;
+ color: var(--body-text-color-subdued);
+ font-size: 0.8em;
+}
+
+/* Table */
+table {
+ margin: 1em 0;
+ border-collapse: collapse;
+ empty-cells: show;
+}
+td,th {
+ border: 1.2px solid var(--border-color-primary) !important;
+ padding: 0.2em;
+}
+thead {
+ background-color: rgba(175,184,193,0.2);
+}
+thead th {
+ padding: .5em .2em;
+}
+/* Inline code */
+.message :not(pre) code {
+ display: inline;
+ white-space: break-spaces;
+ font-family: var(--font-mono);
+ border-radius: 6px;
+ margin: 0 2px 0 2px;
+ padding: .2em .4em .1em .4em;
+ background-color: rgba(175,184,193,0.2);
+}
+/* Code block */
+.message pre,
+.message pre[class*=language-] {
+ color: #fff;
+ overflow-x: auto;
+ overflow-y: hidden;
+ margin: .8em 1em 1em 0em !important;
+ padding: var(--spacing-xl) 1.2em !important;
+ border-radius: var(--radius-lg) !important;
+}
+.message pre code,
+.message pre code[class*=language-] {
+ color: #fff;
+ padding: 0;
+ margin: 0;
+ background-color: unset;
+ text-shadow: none;
+ font-family: var(--font-mono);
+}
+/* Override Gradio's ugly copy button styles */
+pre button[title="copy"] {
+ border-radius: 5px;
+ transition: background-color .2s ease;
+}
+pre button[title="copy"]:hover {
+ background-color: #333232;
+}
+pre button .check {
+ color: #fff !important;
+ background: var(--neutral-950) !important;
+}
+
+/* Override prism.css */
+.language-css .token.string,
+.style .token.string,
+.token.entity,
+.token.operator,
+.token.url {
+ background: none !important;
+}
\ No newline at end of file
diff --git a/assets/custom.js b/assets/custom.js
new file mode 100644
index 0000000000000000000000000000000000000000..6545e12a54e9373bab6b880b04d955ec68e4dc5a
--- /dev/null
+++ b/assets/custom.js
@@ -0,0 +1,705 @@
+const MAX_HISTORY_LENGTH = 32;
+
+var key_down_history = [];
+var currentIndex = -1;
+var user_input_ta;
+
+var gradioContainer = null;
+var user_input_ta = null;
+var user_input_tb = null;
+var userInfoDiv = null;
+var appTitleDiv = null;
+var chatbot = null;
+var chatbotWrap = null;
+var apSwitch = null;
+var messageBotDivs = null;
+var loginUserForm = null;
+var logginUser = null;
+var updateToast = null;
+var sendBtn = null;
+var cancelBtn = null;
+var sliders = null;
+
+var userLogged = false;
+var usernameGotten = false;
+var historyLoaded = false;
+var updateInfoGotten = false;
+var isLatestVersion = localStorage.getItem('isLatestVersion') || false;
+
+var ga = document.getElementsByTagName("gradio-app");
+var targetNode = ga[0];
+var isInIframe = (window.self !== window.top);
+var language = navigator.language.slice(0,2);
+var currentTime = new Date().getTime();
+
+var forView_i18n = {
+ 'zh': "For viewing only",
+ 'en': "For viewing only",
+ 'ru': "История прошлых сообщений",
+ 'ja': "For viewing only",
+ 'ko': "For viewing only",
+ 'fr': "For viewing only",
+ 'es': "For viewing only",
+};
+
+var deleteConfirm_i18n_pref = {
+ 'zh': "Are you sure you want to delete ",
+ 'en': "Are you sure you want to delete ",
+ 'ru': "Are you sure you want to delete ",
+ 'ja': "Are you sure you want to delete ",
+ 'ko': "Are you sure you want to delete ",
+};
+var deleteConfirm_i18n_suff = {
+ 'zh': "?",
+ 'en': "?",
+ 'ru': "?",
+ 'ja': "?",
+ 'ko': "?",
+};
+var deleteConfirm_msg_pref = "Are you sure you want to delete ";
+var deleteConfirm_msg_suff = " ?";
+
+var usingLatest_i18n = {
+ 'zh': "You are using the latest version!",
+ 'en': "You are using the latest version!",
+ 'ru': "Вы используете последнюю версию!",
+ 'ja': "You are using the latest version!",
+ 'ko': "You are using the latest version!",
+};
+
+// Is gradio page loaded? Can interact with elements?
+function gradioLoaded(mutations) {
+ for (var i = 0; i < mutations.length; i++) {
+ if (mutations[i].addedNodes.length) {
+ loginUserForm = document.querySelector(".gradio-container > .main > .wrap > .panel > .form")
+ gradioContainer = document.querySelector(".gradio-container");
+ user_input_tb = document.getElementById('user_input_tb');
+ userInfoDiv = document.getElementById("user_info");
+ appTitleDiv = document.getElementById("app_title");
+ chatbot = document.querySelector('#chuanhu_chatbot');
+ chatbotWrap = document.querySelector('#chuanhu_chatbot > .wrapper > .wrap');
+ apSwitch = document.querySelector('.apSwitch input[type="checkbox"]');
+ updateToast = document.querySelector("#toast-update");
+ sendBtn = document.getElementById("submit_btn");
+ cancelBtn = document.getElementById("cancel_btn");
+ sliders = document.querySelectorAll('input[type="range"]');
+
+ if (loginUserForm) {
+ localStorage.setItem("userLogged", true);
+ userLogged = true;
+ }
+
+ if (gradioContainer && apSwitch) { // Is gradioContainer loaded?
+ adjustDarkMode();
+ }
+ if (user_input_tb) { // Is user_input_tb loaded?
+ selectHistory();
+ }
+ if (userInfoDiv && appTitleDiv) { // Are userInfoDiv and appTitleDiv loaded?
+ if (!usernameGotten) {
+ getUserInfo();
+ }
+ setTimeout(showOrHideUserInfo(), 2000);
+ }
+ if (chatbot) { // Is chatbot loaded?
+ setChatbotHeight();
+ }
+ if (chatbotWrap) {
+ if (!historyLoaded) {
+ loadHistoryHtml();
+ }
+ setChatbotScroll();
+ mObserver.observe(chatbotWrap, { attributes: true, childList: true, subtree: true, characterData: true});
+ }
+ if (sliders) {
+ setSlider();
+ }
+ if (updateToast) {
+ const lastCheckTime = localStorage.getItem('lastCheckTime') || 0;
+ const longTimeNoCheck = currentTime - lastCheckTime > 3 * 24 * 60 * 60 * 1000;
+ if (longTimeNoCheck && !updateInfoGotten && !isLatestVersion || isLatestVersion && !updateInfoGotten) {
+ updateLatestVersion();
+ }
+ }
+ if (cancelBtn) {
+ submitObserver.observe(cancelBtn, { attributes: true, characterData: true});
+ }
+ }
+ }
+}
+
+function webLocale() {
+ //console.log("webLocale", language);
+ if (forView_i18n.hasOwnProperty(language)) {
+ var forView = forView_i18n[language];
+ var forViewStyle = document.createElement('style');
+ forViewStyle.innerHTML = '.wrapper>.wrap>.history-message>:last-child::after { content: "' + forView + '"!important; }';
+ document.head.appendChild(forViewStyle);
+ }
+ if (deleteConfirm_i18n_pref.hasOwnProperty(language)) {
+ deleteConfirm_msg_pref = deleteConfirm_i18n_pref[language];
+ deleteConfirm_msg_suff = deleteConfirm_i18n_suff[language];
+ }
+}
+
+function showConfirmationDialog(a, file, c) {
+ if (file != "") {
+ var result = confirm(deleteConfirm_msg_pref + file + deleteConfirm_msg_suff);
+ if (result) {
+ return [a, file, c];
+ }
+ }
+ return [a, "CANCELED", c];
+}
+
+function selectHistory() {
+ user_input_ta = user_input_tb.querySelector("textarea");
+ if (user_input_ta) {
+ observer.disconnect(); // stop observing
+ disableSendBtn();
+ // listen to keydown event on textarea
+ user_input_ta.addEventListener("keydown", function (event) {
+ var value = user_input_ta.value.trim();
+ // check if pressed key is up/down arrow
+ if (event.code === 'ArrowUp' || event.code === 'ArrowDown') {
+ // if arrow pressed and input not empty and history doesn't contain value, do nothing
+ if (value && key_down_history.indexOf(value) === -1)
+ return;
+ // prevent default behavior of arrow press
+ event.preventDefault();
+ var length = key_down_history.length;
+ if (length === 0) {
+ currentIndex = -1; // if history empty, reset current selected record
+ return;
+ }
+ if (currentIndex === -1) {
+ currentIndex = length;
+ }
+ if (event.code === 'ArrowUp' && currentIndex > 0) {
+ currentIndex--;
+ user_input_ta.value = key_down_history[currentIndex];
+ } else if (event.code === 'ArrowDown' && currentIndex < length - 1) {
+ currentIndex++;
+ user_input_ta.value = key_down_history[currentIndex];
+ }
+ user_input_ta.selectionStart = user_input_ta.value.length;
+ user_input_ta.selectionEnd = user_input_ta.value.length;
+ const input_event = new InputEvent("input", { bubbles: true, cancelable: true });
+ user_input_ta.dispatchEvent(input_event);
+ } else if (event.code === "Enter") {
+ if (value) {
+ currentIndex = -1;
+ if (key_down_history.indexOf(value) === -1) {
+ key_down_history.push(value);
+ if (key_down_history.length > MAX_HISTORY_LENGTH) {
+ key_down_history.shift();
+ }
+ }
+ }
+ }
+ });
+ }
+}
+
+function disableSendBtn() {
+ sendBtn.disabled = user_input_ta.value.trim() === '';
+ user_input_ta.addEventListener('input', () => {
+ sendBtn.disabled = user_input_ta.value.trim() === '';
+ });
+}
+
+var username = null;
+function getUserInfo() {
+ if (usernameGotten) {
+ return;
+ }
+ userLogged = localStorage.getItem('userLogged');
+ if (userLogged) {
+ username = userInfoDiv.innerText;
+ if (username) {
+ if (username.includes("getting user info...")) {
+ setTimeout(getUserInfo, 500);
+ return;
+ } else if (username === " ") {
+ localStorage.removeItem("username");
+ localStorage.removeItem("userLogged")
+ userLogged = false;
+ usernameGotten = true;
+ return;
+ } else {
+ username = username.match(/User:\s*(.*)/)[1] || username;
+ localStorage.setItem("username", username);
+ usernameGotten = true;
+ clearHistoryHtml();
+ }
+ }
+ }
+}
+
+function toggleUserInfoVisibility(shouldHide) {
+ if (userInfoDiv) {
+ if (shouldHide) {
+ userInfoDiv.classList.add("hideK");
+ } else {
+ userInfoDiv.classList.remove("hideK");
+ }
+ }
+}
+function showOrHideUserInfo() {
+ // bind mouse/touch events to show/hide user info
+ appTitleDiv.addEventListener("mouseenter", function () {
+ toggleUserInfoVisibility(false);
+ });
+ userInfoDiv.addEventListener("mouseenter", function () {
+ toggleUserInfoVisibility(false);
+ });
+ sendBtn.addEventListener("mouseenter", function () {
+ toggleUserInfoVisibility(false);
+ });
+
+ appTitleDiv.addEventListener("mouseleave", function () {
+ toggleUserInfoVisibility(true);
+ });
+ userInfoDiv.addEventListener("mouseleave", function () {
+ toggleUserInfoVisibility(true);
+ });
+ sendBtn.addEventListener("mouseleave", function () {
+ toggleUserInfoVisibility(true);
+ });
+
+ appTitleDiv.ontouchstart = function () {
+ toggleUserInfoVisibility(false);
+ };
+ userInfoDiv.ontouchstart = function () {
+ toggleUserInfoVisibility(false);
+ };
+ sendBtn.ontouchstart = function () {
+ toggleUserInfoVisibility(false);
+ };
+
+ appTitleDiv.ontouchend = function () {
+ setTimeout(function () {
+ toggleUserInfoVisibility(true);
+ }, 3000);
+ };
+ userInfoDiv.ontouchend = function () {
+ setTimeout(function () {
+ toggleUserInfoVisibility(true);
+ }, 3000);
+ };
+ sendBtn.ontouchend = function () {
+ setTimeout(function () {
+ toggleUserInfoVisibility(true);
+ }, 3000); // 1 sec delay to hide user info
+ };
+
+ // hide user info after 2 seconds
+ setTimeout(function () {
+ toggleUserInfoVisibility(true);
+ }, 2000);
+}
+
+function toggleDarkMode(isEnabled) {
+ if (isEnabled) {
+ document.body.classList.add("dark");
+ document.body.style.setProperty("background-color", "var(--neutral-950)", "important");
+ } else {
+ document.body.classList.remove("dark");
+ document.body.style.backgroundColor = "";
+ }
+}
+function adjustDarkMode() {
+ const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
+
+// set initial state based on current color scheme or saved theme
+let isDarkMode = localStorage.getItem('darkMode') === 'true' || darkModeQuery.matches;
+apSwitch.checked = isDarkMode;
+toggleDarkMode(isDarkMode);
+// listen for changes to color scheme
+darkModeQuery.addEventListener("change", (e) => {
+ isDarkMode = e.matches;
+ localStorage.setItem('darkMode', isDarkMode);
+ apSwitch.checked = isDarkMode;
+ toggleDarkMode(isDarkMode);
+});
+// apSwitch = document.querySelector(".apSwitch input[type="checkbox"]");
+apSwitch.addEventListener("change", (e) => {
+ isDarkMode = e.target.checked;
+ localStorage.setItem('darkMode', isDarkMode);
+ toggleDarkMode(isDarkMode);
+});
+}
+
+function setChatbotHeight() {
+ const screenWidth = window.innerWidth;
+ const statusDisplay = document.querySelector('#status_display');
+ const statusDisplayHeight = statusDisplay ? statusDisplay.offsetHeight : 0;
+ const vh = window.innerHeight * 0.01;
+ document.documentElement.style.setProperty('--vh', `${vh}px`);
+ if (isInIframe) {
+ chatbot.style.height = `700px`;
+ chatbotWrap.style.maxHeight = `calc(700px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`
+ } else {
+ if (screenWidth <= 320) {
+ chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px)`;
+ chatbotWrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 150}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
+ } else if (screenWidth <= 499) {
+ chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px)`;
+ chatbotWrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 100}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
+ } else {
+ chatbot.style.height = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px)`;
+ chatbotWrap.style.maxHeight = `calc(var(--vh, 1vh) * 100 - ${statusDisplayHeight + 160}px - var(--line-sm) * 1rem - 2 * var(--block-label-margin))`;
+ }
+ }
+}
+function setChatbotScroll() {
+ var scrollHeight = chatbotWrap.scrollHeight;
+ chatbotWrap.scrollTo(0,scrollHeight)
+}
+var rangeInputs = null;
+var numberInputs = null;
+function setSlider() {
+ rangeInputs = document.querySelectorAll('input[type="range"]');
+ numberInputs = document.querySelectorAll('input[type="number"]')
+ setSliderRange();
+ rangeInputs.forEach(rangeInput => {
+ rangeInput.addEventListener('input', setSliderRange);
+ });
+ numberInputs.forEach(numberInput => {
+ numberInput.addEventListener('input', setSliderRange);
+ })
+}
+function setSliderRange() {
+ var range = document.querySelectorAll('input[type="range"]');
+ range.forEach(range => {
+ range.style.backgroundSize = (range.value - range.min) / (range.max - range.min) * 100 + '% 100%';
+ });
+}
+
+function addChuanhuButton(botElement) {
+ var rawMessage = null;
+ var mdMessage = null;
+ rawMessage = botElement.querySelector('.raw-message');
+ mdMessage = botElement.querySelector('.md-message');
+ if (!rawMessage) {
+ var buttons = botElement.querySelectorAll('button.chuanhu-btn');
+ for (var i = 0; i < buttons.length; i++) {
+ buttons[i].parentNode.removeChild(buttons[i]);
+ }
+ return;
+ }
+ var oldCopyButton = null;
+ var oldToggleButton = null;
+ oldCopyButton = botElement.querySelector('button.copy-bot-btn');
+ oldToggleButton = botElement.querySelector('button.toggle-md-btn');
+ if (oldCopyButton) oldCopyButton.remove();
+ if (oldToggleButton) oldToggleButton.remove();
+
+ // Copy bot button
+ var copyButton = document.createElement('button');
+ copyButton.classList.add('chuanhu-btn');
+ copyButton.classList.add('copy-bot-btn');
+ copyButton.setAttribute('aria-label', 'Copy');
+ copyButton.innerHTML = copyIcon;
+ copyButton.addEventListener('click', async () => {
+ const textToCopy = rawMessage.innerText;
+ try {
+ if ("clipboard" in navigator) {
+ await navigator.clipboard.writeText(textToCopy);
+ copyButton.innerHTML = copiedIcon;
+ setTimeout(() => {
+ copyButton.innerHTML = copyIcon;
+ }, 1500);
+ } else {
+ const textArea = document.createElement("textarea");
+ textArea.value = textToCopy;
+ document.body.appendChild(textArea);
+ textArea.select();
+ try {
+ document.execCommand('copy');
+ copyButton.innerHTML = copiedIcon;
+ setTimeout(() => {
+ copyButton.innerHTML = copyIcon;
+ }, 1500);
+ } catch (error) {
+ console.error("Error copying: ", error);
+ }
+ document.body.removeChild(textArea);
+ }
+ } catch (error) {
+ console.error("Error copying: ", error);
+ }
+ });
+ botElement.appendChild(copyButton);
+
+ // Toggle button
+ var toggleButton = document.createElement('button');
+ toggleButton.classList.add('chuanhu-btn');
+ toggleButton.classList.add('toggle-md-btn');
+ toggleButton.setAttribute('aria-label', 'Toggle');
+ var renderMarkdown = mdMessage.classList.contains('hideM');
+ toggleButton.innerHTML = renderMarkdown ? mdIcon : rawIcon;
+ toggleButton.addEventListener('click', () => {
+ renderMarkdown = mdMessage.classList.contains('hideM');
+ if (renderMarkdown){
+ renderMarkdownText(botElement);
+ toggleButton.innerHTML=rawIcon;
+ } else {
+ removeMarkdownText(botElement);
+ toggleButton.innerHTML=mdIcon;
+ }
+ });
+ botElement.insertBefore(toggleButton, copyButton);
+}
+
+function renderMarkdownText(message) {
+ var mdDiv = message.querySelector('.md-message');
+ if (mdDiv) mdDiv.classList.remove('hideM');
+ var rawDiv = message.querySelector('.raw-message');
+ if (rawDiv) rawDiv.classList.add('hideM');
+}
+function removeMarkdownText(message) {
+ var rawDiv = message.querySelector('.raw-message');
+ if (rawDiv) rawDiv.classList.remove('hideM');
+ var mdDiv = message.querySelector('.md-message');
+ if (mdDiv) mdDiv.classList.add('hideM');
+}
+
+let timeoutId;
+let isThrottled = false;
+var mmutation
+// Observe changes inside DOM
+var mObserver = new MutationObserver(function (mutationsList) {
+ for (mmutation of mutationsList) {
+ if (mmutation.type === 'childList') {
+ for (var node of mmutation.addedNodes) {
+ if (node.nodeType === 1 && node.classList.contains('message')) {
+ saveHistoryHtml();
+ disableSendBtn();
+ document.querySelectorAll('#chuanhu_chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
+ }
+ }
+ for (var node of mmutation.removedNodes) {
+ if (node.nodeType === 1 && node.classList.contains('message')) {
+ saveHistoryHtml();
+ disableSendBtn();
+ document.querySelectorAll('#chuanhu_chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
+ }
+ }
+ } else if (mmutation.type === 'attributes') {
+ if (isThrottled) break; // avoid re-rendering too much
+ isThrottled = true;
+ clearTimeout(timeoutId);
+ timeoutId = setTimeout(() => {
+ isThrottled = false;
+ document.querySelectorAll('#chuanhu_chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
+ saveHistoryHtml();
+ disableSendBtn();
+ }, 1500);
+ }
+ }
+});
+// mObserver.observe(targetNode, { attributes: true, childList: true, subtree: true, characterData: true});
+
+var submitObserver = new MutationObserver(function (mutationsList) {
+ document.querySelectorAll('#chuanhu_chatbot .message-wrap .message.bot').forEach(addChuanhuButton);
+ saveHistoryHtml();
+});
+
+var loadhistorytime = 0; // for debugging
+function saveHistoryHtml() {
+ var historyHtml = document.querySelector('#chuanhu_chatbot>.wrapper>.wrap');
+ if (!historyHtml) return; // no history, do nothing
+ localStorage.setItem('chatHistory', historyHtml.innerHTML);
+ // console.log("History saved")
+ historyLoaded = false;
+}
+function loadHistoryHtml() {
+ var historyHtml = localStorage.getItem('chatHistory');
+ if (!historyHtml) {
+ historyLoaded = true;
+ return; // no history, do nothing
+ }
+ userLogged = localStorage.getItem('userLogged');
+ if (userLogged){
+ historyLoaded = true;
+ return; // user logged in, do nothing
+ }
+ if (!historyLoaded) {
+ var tempDiv = document.createElement('div');
+ tempDiv.innerHTML = historyHtml;
+ var buttons = tempDiv.querySelectorAll('button.chuanhu-btn');
+ var gradioCopyButtons = tempDiv.querySelectorAll('button.copy_code_button');
+ for (var i = 0; i < buttons.length; i++) {
+ buttons[i].parentNode.removeChild(buttons[i]);
+ }
+ for (var i = 0; i < gradioCopyButtons.length; i++) {
+ gradioCopyButtons[i].parentNode.removeChild(gradioCopyButtons[i]);
+ }
+ var fakeHistory = document.createElement('div');
+ fakeHistory.classList.add('history-message');
+ fakeHistory.innerHTML = tempDiv.innerHTML;
+ webLocale();
+ chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
+ // var fakeHistory = document.createElement('div');
+ // fakeHistory.classList.add('history-message');
+ // fakeHistory.innerHTML = historyHtml;
+ // chatbotWrap.insertBefore(fakeHistory, chatbotWrap.firstChild);
+ historyLoaded = true;
+ console.log("History loaded");
+ loadhistorytime += 1; // for debugging
+ } else {
+ historyLoaded = false;
+ }
+}
+function clearHistoryHtml() {
+ localStorage.removeItem("chatHistory");
+ historyMessages = chatbotWrap.querySelector('.history-message');
+ if (historyMessages) {
+ chatbotWrap.removeChild(historyMessages);
+ console.log("History cleared");
+ }
+}
+
+var showingUpdateInfo = false;
+async function getLatestRelease() {
+ try {
+ const response = await fetch('https://api.github.com/repos/gaizhenbiao/chuanhuchatgpt/releases/latest');
+ if (!response.ok) {
+ console.log(`Error: ${response.status} - ${response.statusText}`);
+ updateInfoGotten = true;
+ return null;
+ }
+ const data = await response.json();
+ updateInfoGotten = true;
+ return data;
+ } catch (error) {
+ console.log(`Error: ${error}`);
+ updateInfoGotten = true;
+ return null;
+ }
+}
+async function updateLatestVersion() {
+ const currentVersionElement = document.getElementById('current-version');
+ const latestVersionElement = document.getElementById('latest-version-title');
+ const releaseNoteElement = document.getElementById('release-note-content');
+ const currentVersion = currentVersionElement.textContent;
+ const versionTime = document.getElementById('version-time').innerText;
+ const localVersionTime = versionTime !== "unknown" ? (new Date(versionTime)).getTime() : 0;
+ updateInfoGotten = true; // only execute once regardless of success to avoid exceeding API limit...
+ try {
+ const data = await getLatestRelease();
+ const releaseNote = data.body;
+ if (releaseNote) {
+ releaseNoteElement.innerHTML = marked.parse(releaseNote);
+ }
+ const latestVersion = data.tag_name;
+ const latestVersionTime = (new Date(data.created_at)).getTime();
+ if (latestVersionTime) {
+ if (localVersionTime < latestVersionTime) {
+ latestVersionElement.textContent = latestVersion;
+ console.log(`New version ${latestVersion} found!`);
+ if (!isInIframe) {openUpdateToast();}
+ } else {
+ noUpdate();
+ }
+ currentTime = new Date().getTime();
+ localStorage.setItem('lastCheckTime', currentTime);
+ }
+ } catch (error) {
+ console.error(error);
+ }
+}
+function getUpdate() {
+ window.open('https://github.com/gaizhenbiao/chuanhuchatgpt/releases/latest', '_blank');
+ closeUpdateToast();
+}
+function cancelUpdate() {
+ closeUpdateToast();
+}
+function openUpdateToast() {
+ showingUpdateInfo = true;
+ setUpdateWindowHeight();
+}
+function closeUpdateToast() {
+ updateToast.style.setProperty('top', '-500px');
+ showingUpdateInfo = false;
+}
+function manualCheckUpdate() {
+ openUpdateToast();
+ updateLatestVersion();
+ currentTime = new Date().getTime();
+ localStorage.setItem('lastCheckTime', currentTime);
+}
+function noUpdate() {
+ localStorage.setItem('isLatestVersion', 'true');
+ isLatestVersion = true;
+ const versionInfoElement = document.getElementById('version-info-title');
+ const releaseNoteWrap = document.getElementById('release-note-wrap');
+ const gotoUpdateBtn = document.getElementById('goto-update-btn');
+ const closeUpdateBtn = document.getElementById('close-update-btn');
+
+ versionInfoElement.textContent = usingLatest_i18n.hasOwnProperty(language) ? usingLatest_i18n[language] : usingLatest_i18n['ru'];
+ releaseNoteWrap.style.setProperty('display', 'none');
+ gotoUpdateBtn.classList.add('hideK');
+ closeUpdateBtn.classList.remove('hideK');
+}
+function setUpdateWindowHeight() {
+ if (!showingUpdateInfo) {return;}
+ const scrollPosition = window.scrollY;
+ // const originalTop = updateToast.style.getPropertyValue('top');
+ const resultTop = scrollPosition - 20 + 'px';
+ updateToast.style.setProperty('top', resultTop);
+}
+
+// Observe changes in page
+var observer = new MutationObserver(function (mutations) {
+ gradioLoaded(mutations);
+});
+observer.observe(targetNode, { childList: true, subtree: true });
+
+// Observe changes in page
+window.addEventListener("DOMContentLoaded", function () {
+ isInIframe = (window.self !== window.top);
+ historyLoaded = false;
+});
+window.addEventListener('resize', setChatbotHeight);
+window.addEventListener('scroll', function(){setChatbotHeight();setUpdateWindowHeight();});
+window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", adjustDarkMode);
+
+// console surprise
+var styleTitle1 = `
+font-size: 16px;
+font-family: ui-monospace,monospace;
+color: rgb(244,167,89);
+`
+var styleDesc1 = `
+font-size: 12px;
+font-family: ui-monospace,monospace;
+`
+function makeML(str) {
+ let l = new String(str)
+ l = l.substring(l.indexOf("/*") + 3, l.lastIndexOf("*/"))
+ return l
+}
+let ChuanhuInfo = function () {
+ /*
+ ________ __ ________ __
+/ ____/ /_ __ ______ _____ / /_ __ __ / ____/ /_ ____ _/ /_
+/ / / __ \/ / / / __ `/ __ \/ __ \/ / / / / / / __ \/ __ `/ __/
+/ /___/ / / / /_/ / /_/ / / / / / / / /_/ / / /___/ / / / /_/ / /_
+\____/_/ /_/\__,_/\__,_/_/ /_/_/ /_/\__,_/ \____/_/ /_/\__,_/\__/
+
+ Chuanhu Chat - Graphical interface for ChatGPT API and many LLMs
+ */
+}
+let description = `
+`
+console.log(`%c${makeML(ChuanhuInfo)}`,styleTitle1)
+console.log(`%c${description}`, styleDesc1)
+
+// svg icon codes
+const copyIcon = ' ';
+const copiedIcon = ' ';
+const mdIcon = ' ';
+const rawIcon = ' ';
\ No newline at end of file
diff --git a/assets/external-scripts.js b/assets/external-scripts.js
new file mode 100644
index 0000000000000000000000000000000000000000..8d0352669045537af5698b1824dbc1dba21df478
--- /dev/null
+++ b/assets/external-scripts.js
@@ -0,0 +1,2 @@
+
+// external javascript here
diff --git a/assets/favicon.ico b/assets/favicon.ico
new file mode 100644
index 0000000000000000000000000000000000000000..18f9701834de5c7b6eb45ac211013c29ca07118a
Binary files /dev/null and b/assets/favicon.ico differ
diff --git a/assets/html/appearance_switcher.html b/assets/html/appearance_switcher.html
new file mode 100644
index 0000000000000000000000000000000000000000..6bb0101bd24478e5332d540cb53211141056df5f
--- /dev/null
+++ b/assets/html/appearance_switcher.html
@@ -0,0 +1,6 @@
+
+
+
+ {label}
+
+
diff --git a/assets/html/billing_info.html b/assets/html/billing_info.html
new file mode 100644
index 0000000000000000000000000000000000000000..71abcc802da3c70716919c1a4738ac077c47bf01
--- /dev/null
+++ b/assets/html/billing_info.html
@@ -0,0 +1,9 @@
+{label}
+
+
+ ${rounded_usage} ${usage_limit}
+
\ No newline at end of file
diff --git a/assets/html/footer.html b/assets/html/footer.html
new file mode 100644
index 0000000000000000000000000000000000000000..bca27bb8066dfab5cc0acf7be349a514de5f9a58
--- /dev/null
+++ b/assets/html/footer.html
@@ -0,0 +1 @@
+{versions}
diff --git a/assets/html/update.html b/assets/html/update.html
new file mode 100644
index 0000000000000000000000000000000000000000..3160c277aaa02b51a186e7a9ef8ef5ef6cad9aaf
--- /dev/null
+++ b/assets/html/update.html
@@ -0,0 +1,25 @@
+
+
+
+ {current_version}
+ {version_time}
+
+
+ Latest Version: getting latest version...
+
+
+
+ Getting Release Note...
+
+
+
+ {cancel_btn}
+ {update_btn}
+
+
+ {seenew_btn}
+ {ok_btn}
+
+
+
\ No newline at end of file
diff --git a/autoinstall_linux.sh b/autoinstall_linux.sh
new file mode 100644
index 0000000000000000000000000000000000000000..afcf3b6c3c3aa039866523e23f0f54e4c760c13e
--- /dev/null
+++ b/autoinstall_linux.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+# Обновить систему
+sudo apt-get update -y
+
+# Установить python3, pip, git и зависимости
+sudo apt-get install -y python3-full python3-venv python3-pip python-is-python3 git build-essential libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
+
+# Клонировать репозиторий
+git clone https://github.com/Em1tSan/NeuroGPT.git
+cd NeuroGPT
+
+# Обновить репозиторий
+git checkout main
+git fetch --all
+git reset --hard origin/main
+git pull
+
+# Проверка версии Python
+version=$(python3 --version)
+version=${version:7}
+if [[ "$version" < "3.9.0" ]]; then
+ echo "Your version of Python ${version} is not supported. Please install Python 3.10.X"
+ exit 1
+elif [[ "$version" > "3.11.14" ]]; then
+ echo "Your version of Python ${version} is not supported. Please install Python 3.10.X"
+ exit 1
+fi
+
+# Создание и активация виртуальной среды
+python3 -m venv venv
+. venv/bin/activate
+
+# Установка необходимых пакетов
+python3 -m pip install --upgrade pip
+python3 -m pip install -U setuptools
+python3 -m pip install -r requirements.txt
+
+# Проверка и загрузка моделей Spacy при необходимости
+if [ ! -d "venv/lib/python3.10/site-packages/en_core_web_sm" ]; then
+ echo "English language model not found, downloading..."
+ python3 -m spacy download en_core_web_sm
+fi
+
+if [ ! -d "venv/lib/python3.10/site-packages/zh_core_web_sm" ]; then
+ echo "Chinese language model not found, downloading..."
+ python3 -m spacy download zh_core_web_sm
+fi
+
+if [ ! -d "venv/lib/python3.10/site-packages/ru_core_news_sm" ]; then
+ echo "Russian language model not found, downloading..."
+ python3 -m spacy download ru_core_news_sm
+fi
+
+echo "Completed."
+
+# Определение языка операционной системы и запуск соответствующего скрипта
+language=$(locale | grep LANG= | cut -d "=" -f2 | cut -d "_" -f1)
+if [ "$language" = "ru" ]; then
+ python3 webui_ru.py
+else
+ python3 webui_en.py
+fi
\ No newline at end of file
diff --git a/backend/auto_proxy.py b/backend/auto_proxy.py
new file mode 100644
index 0000000000000000000000000000000000000000..509c4255e0e331d4c907b4b47421fcb912221413
--- /dev/null
+++ b/backend/auto_proxy.py
@@ -0,0 +1,121 @@
+import random
+import requests
+import time
+import threading
+import socket
+
+
+def fetch_proxies():
+ """Fetch a list of proxy servers from proxyscrape.com.
+
+ Returns:
+ list: A list of proxy servers in the format "IP:Port".
+ """
+ url = "https://www.proxy-list.download/api/v1/get?type=https"
+ response = requests.get(url)
+ if response.status_code == 200:
+ return response.text.split("\r\n")[:-1]
+ print(f"Error fetching proxies: {response.status_code}")
+ return []
+
+
+def test_proxy(proxy, prompt, timeout):
+ """Test the given proxy server with a specified prompt and timeout.
+
+ Args:
+ proxy (str): The proxy server in the format "IP:Port".
+ prompt (str): The test prompt to be used for testing.
+ timeout (int): The maximum time in seconds allowed for the test.
+ """
+ try:
+ # Split IP and Port
+ ip, port = proxy.split(':')
+
+ # Create a socket object
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+
+ # Start the timer
+ start_time = time.time()
+
+ # Connect to the proxy server
+ sock.connect((ip, int(port)))
+
+ # Stop the timer and calculate the elapsed time
+ end_time = time.time()
+ elapsed_time = end_time - start_time
+
+ # Print the elapsed time
+ #print(f"Elapsed time: {elapsed_time} seconds")
+
+ # Close the socket
+ sock.close()
+
+ # Check if the elapsed time is below the timeout
+ if elapsed_time < timeout:
+ print(f"proxy: {proxy} ✅ | Elapsed time: {elapsed_time} seconds")
+ add_working_proxy(proxy)
+ except Exception as e:
+ pass
+
+
+def add_working_proxy(proxy):
+ """Add a working proxy server to the global working_proxies list.
+
+ Args:
+ proxy (str): The proxy server in the format "IP:Port".
+ """
+ global working_proxies
+ working_proxies.append(proxy)
+
+
+def remove_proxy(proxy):
+ """Remove a proxy server from the global working_proxies list.
+
+ Args:
+ proxy (str): The proxy server in the format "IP:Port".
+ """
+ global working_proxies
+ if proxy in working_proxies:
+ working_proxies.remove(proxy)
+
+
+def get_working_proxies(prompt, timeout=5):
+ """Fetch and test proxy servers, adding working proxies to the global working_proxies list.
+
+ Args:
+ prompt (str): The test prompt to be used for testing.
+ timeout (int, optional): The maximum time in seconds allowed for testing. Defaults to 5.
+ """
+ proxy_list = fetch_proxies()
+ threads = []
+
+ for proxy in proxy_list:
+ thread = threading.Thread(target=test_proxy, args=(
+ proxy, prompt, timeout))
+ threads.append(thread)
+ thread.start()
+
+ for t in threads:
+ t.join(timeout)
+
+
+def update_working_proxies():
+ """Continuously update the global working_proxies list with working proxy servers."""
+ global working_proxies
+ test_prompt = "What is the capital of France?"
+
+ while True:
+ working_proxies = [] # Clear the list before updating
+ get_working_proxies(test_prompt)
+ print('proxies updated')
+ time.sleep(1800) # Update proxies list every 30 minutes
+
+
+def get_random_proxy():
+ """Get a random working proxy server from the global working_proxies list.
+
+ Returns:
+ str: A random working proxy server in the format "IP:Port".
+ """
+ global working_proxies
+ return random.choice(working_proxies)
\ No newline at end of file
diff --git a/backend/backend.py b/backend/backend.py
new file mode 100644
index 0000000000000000000000000000000000000000..b7f39fe680ed7ba822b798bf572b3f596f543c39
--- /dev/null
+++ b/backend/backend.py
@@ -0,0 +1,253 @@
+import os
+import time
+import json
+import random
+import string
+import socket
+import nest_asyncio
+
+import requests
+from typing import Any
+
+from flask import Flask, request, jsonify, Response
+from flask_cors import CORS
+import logging
+from .embedding_processing import embedding_processing
+
+import g4f
+from g4f import ChatCompletion, Provider, BaseProvider, models
+from g4f.models import ModelUtils
+
+nest_asyncio.apply()
+
+app = Flask(__name__)
+CORS(app)
+
+LOG = logging.getLogger(__name__)
+embedding_proc = embedding_processing()
+
+@app.route("/chat/completions", methods=['POST'])
+@app.route("/v1/chat/completions", methods=['POST'])
+@app.route("/", methods=['POST'])
+def chat_completions():
+ request_data = request.get_json()
+ model = request_data.get('model', 'gpt-3.5-turbo').replace("neuro-", "")
+ messages = request_data.get('messages')
+ stream = request_data.get('stream', False)
+ streaming_ = request_data.get('stream', False)
+ temperature = request_data.get('temperature', 1.0)
+ top_p = request_data.get('top_p', 1.0)
+ max_tokens = request_data.get('max_tokens', 1024)
+
+ response = ChatCompletion.create(model=model, stream=stream, messages=messages, temperature=temperature, top_p=top_p, max_tokens=max_tokens, system_prompt="")
+
+ completion_id = "".join(random.choices(string.ascii_letters + string.digits, k=28))
+ completion_timestamp = int(time.time())
+
+ if not streaming_:
+ completion_timestamp = int(time.time())
+ completion_id = ''.join(random.choices(
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=28))
+
+ return {
+ "id": f"chatcmpl-{completion_id}",
+ "object": "chat.completion",
+ "created": completion_timestamp,
+ "model": model,
+ "choices": [
+ {
+ "index": 0,
+ "message": {
+ "role": "assistant",
+ "content": response,
+ },
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {
+ "prompt_tokens": None,
+ "completion_tokens": None,
+ "total_tokens": None,
+ },
+ }
+
+ def streaming():
+ for chunk in response:
+ completion_data = {
+ 'id': f'chatcmpl-{completion_id}',
+ 'object': 'chat.completion.chunk',
+ 'created': completion_timestamp,
+ 'model': model,
+ 'choices': [
+ {
+ 'index': 0,
+ 'delta': {
+ 'content': chunk,
+ },
+ 'finish_reason': None,
+ }
+ ],
+ }
+
+ content = json.dumps(completion_data, separators=(',', ':'))
+ yield f'data: {content}\n\n'
+ time.sleep(0.05)
+
+ end_completion_data: dict[str, Any] = {
+ 'id': f'chatcmpl-{completion_id}',
+ 'object': 'chat.completion.chunk',
+ 'created': completion_timestamp,
+ 'model': model,
+ 'choices': [
+ {
+ 'index': 0,
+ 'delta': {},
+ 'finish_reason': 'stop',
+ }
+ ],
+ }
+ content = json.dumps(end_completion_data, separators=(',', ':'))
+ yield f'data: {content}\n\n'
+
+ return app.response_class(streaming(), mimetype='text/event-stream')
+
+@app.route("/engines/text-embedding-ada-002/embeddings", methods=["POST"])
+@app.route("/engines/text-similarity-davinci-001/embeddings", methods=["POST"])
+@app.route('/v1/embeddings', methods=['POST'])
+@app.route('/embeddings', methods=['POST'])
+def create_embedding():
+ j_input = request.get_json()
+ #model = embedding_processing()
+ embedding = embedding_proc.embedding(text_list=j_input['input'])
+ log_event()
+ return jsonify(
+ embedding
+ )
+
+def log_event():
+ LOG.info('served')
+
+@app.route("/v1/dashboard/billing/subscription", methods=['GET'])
+@app.route("/dashboard/billing/subscription", methods=['GET'])
+def billing_subscription():
+ return jsonify({
+ "object": "billing_subscription",
+ "has_payment_method": True,
+ "canceled": False,
+ "canceled_at": None,
+ "delinquent": None,
+ "access_until": 2556028800,
+ "soft_limit": 6944500,
+ "hard_limit": 166666666,
+ "system_hard_limit": 166666666,
+ "soft_limit_usd": 416.67,
+ "hard_limit_usd": 9999.99996,
+ "system_hard_limit_usd": 9999.99996,
+ "plan": {
+ "title": "Pay-as-you-go",
+ "id": "payg"
+ },
+ "primary": True,
+ "account_name": "OpenAI",
+ "po_number": None,
+ "billing_email": None,
+ "tax_ids": None,
+ "billing_address": {
+ "city": "New York",
+ "line1": "OpenAI",
+ "country": "US",
+ "postal_code": "NY10031"
+ },
+ "business_address": None
+}
+)
+
+
+@app.route("/v1/dashboard/billing/usage", methods=['GET'])
+@app.route("/dashboard/billing/usage", methods=['GET'])
+def billing_usage():
+ return jsonify({
+ "object": "list",
+ "daily_costs": [
+ {
+ "timestamp": time.time(),
+ "line_items": [
+ {
+ "name": "GPT-4",
+ "cost": 0.0
+ },
+ {
+ "name": "Chat models",
+ "cost": 1.01
+ },
+ {
+ "name": "InstructGPT",
+ "cost": 0.0
+ },
+ {
+ "name": "Fine-tuning models",
+ "cost": 0.0
+ },
+ {
+ "name": "Embedding models",
+ "cost": 0.0
+ },
+ {
+ "name": "Image models",
+ "cost": 16.0
+ },
+ {
+ "name": "Audio models",
+ "cost": 0.0
+ }
+ ]
+ }
+ ],
+ "total_usage": 1.01
+}
+)
+
+@app.route("/v1/models", methods=['GET'])
+@app.route("/models", methods=['GET'])
+def models():
+ import g4f.models
+ model = {"data":[]}
+ for i in g4f.models.ModelUtils.convert:
+ model['data'].append({
+ "id": i,
+ "object": "model",
+ "owned_by": g4f.models.ModelUtils.convert[i].base_provider,
+ "tokens": 99999,
+ "fallbacks": None,
+ "endpoints": [
+ "/v1/chat/completions"
+ ],
+ "limits": None,
+ "permission": []
+ })
+ return jsonify(model)
+
+@app.route("/v1/providers", methods=['GET'])
+@app.route("/providers", methods=['GET'])
+def providers():
+ files = os.listdir("g4f/Provider/Providers")
+ files = [f for f in files if os.path.isfile(os.path.join("g4f/Provider/Providers", f))]
+ files.sort(key=str.lower)
+ providers_data = {"data":[]}
+ for file in files:
+ if file.endswith(".py"):
+ name = file[:-3]
+ try:
+ p = getattr(g4f.Provider,name)
+ providers_data["data"].append({
+ "provider": str(name),
+ "model": list(p.model),
+ "url": str(p.url),
+ "working": bool(p.working),
+ "supports_stream": bool(p.supports_stream)
+ })
+ except:
+ pass
+ return jsonify(providers_data)
+
+
diff --git a/backend/backend_old_async.py b/backend/backend_old_async.py
new file mode 100644
index 0000000000000000000000000000000000000000..0adf4d58a443d180ca4e1d034e09a7ca13d0e579
--- /dev/null
+++ b/backend/backend_old_async.py
@@ -0,0 +1,369 @@
+import os
+import time
+import json
+import random
+import string
+import asyncio
+import async_timeout
+import aiohttp, aiofiles
+import requests
+import pytz
+import logging
+from datetime import datetime
+from fastapi import FastAPI, Request
+from fastapi.middleware.gzip import GZipMiddleware
+from fastapi.responses import JSONResponse, StreamingResponse
+from starlette.middleware.cors import CORSMiddleware
+from typing import Any
+import g4f
+from g4f import ChatCompletion, Provider, BaseProvider
+from g4f.models import ModelUtils
+from cachetools import LRUCache
+
+import aiofiles
+import async_timeout
+
+from fp.fp import FreeProxy
+from embedding_processing import embedding_processing
+import concurrent.futures
+
+app = FastAPI()
+embedding_proc = embedding_processing()
+LOG = logging.getLogger(__name__)
+
+app.add_middleware(GZipMiddleware)
+app.add_middleware(
+ CORSMiddleware,
+ allow_origins=["*"],
+ allow_methods=["*"],
+ allow_headers=["*"],
+)
+
+def get_proxy():
+ proxy = FreeProxy(rand=True, timeout=1).get()
+ return proxy
+
+
+@app.post("/chat/completions")
+@app.post("/v1/chat/completions")
+async def chat_completions(request: Request):
+ req_data = await request.json()
+ stream = req_data.get('stream', False)
+ model = req_data.get('model', 'gpt-3.5-turbo')
+ messages = req_data.get('messages')
+ temperature = req_data.get('temperature', 1.0)
+ top_p = req_data.get('top_p', 1.0)
+ max_tokens = req_data.get('max_tokens', 4096)
+
+ response = ChatCompletion.create(model=model, stream=stream, messages=messages, temperature=temperature, top_p=top_p, max_tokens=max_tokens, system_prompt="")
+
+ completion_id = "".join(random.choices(string.ascii_letters + string.digits, k=28))
+ completion_timestamp = int(time.time())
+
+ if not stream:
+ return {
+ "id": f"chatcmpl-{completion_id}",
+ "object": "chat.completion",
+ "created": completion_timestamp,
+ "model": model,
+ "choices": [
+ {
+ "index": 0,
+ "message": {
+ "role": "assistant",
+ "content": response,
+ },
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {
+ "prompt_tokens": None,
+ "completion_tokens": None,
+ "total_tokens": None,
+ },
+ }
+
+ def streaming():
+ for chunk in response:
+ completion_data = {
+ "id": f"chatcmpl-{completion_id}",
+ "object": "chat.completion.chunk",
+ "created": completion_timestamp,
+ "model": model,
+ "choices": [
+ {
+ "index": 0,
+ "delta": {
+ "content": chunk,
+ },
+ "finish_reason": None,
+ }
+ ],
+ }
+
+ content = json.dumps(completion_data, separators=(",", ":"))
+ yield f"data: {content}\n\n"
+ time.sleep(0.1)
+
+ end_completion_data: dict[str, Any] = {
+ "id": f"chatcmpl-{completion_id}",
+ "object": "chat.completion.chunk",
+ "created": completion_timestamp,
+ "model": model,
+ "choices": [
+ {
+ "index": 0,
+ "delta": {},
+ "finish_reason": "stop",
+ }
+ ],
+ }
+ content = json.dumps(end_completion_data, separators=(",", ":"))
+ yield f"data: {content}\n\n"
+
+ return StreamingResponse(streaming(), media_type='text/event-stream')
+
+@app.post('/v1/embeddings')
+async def create_embedding(request: Request):
+ j_input = await request.json()
+ #model = embedding_processing()
+ embedding = embedding_proc.embedding(text_list=j_input['input'])
+ await log_event()
+ return JSONResponse(
+ embedding
+ )
+
+async def log_event():
+ LOG.info('served')
+
+@app.post("/v1/completions")
+async def completions(request: Request):
+ req_data = await request.json()
+ model = req_data.get('model', 'text-davinci-003')
+ prompt = req_data.get('prompt')
+ messages = req_data.get('messages')
+ temperature = req_data.get('temperature', 1.0)
+ top_p = req_data.get('top_p', 1.0)
+ max_tokens = req_data.get('max_tokens', 4096)
+
+ response = g4f.Completion.create(model='text-davinci-003', prompt=prompt, temperature=temperature, top_p=top_p, max_tokens=max_tokens,)
+
+ completion_id = "".join(random.choices(string.ascii_letters + string.digits, k=24))
+ completion_timestamp = int(time.time())
+
+ return {
+ "id": f"cmpl-{completion_id}",
+ "object": "text_completion",
+ "created": completion_timestamp,
+ "model": "text-davinci-003",
+ "choices": [
+ {
+ "text": response,
+ "index": 0,
+ "logprobs": None,
+ "finish_reason": "length"
+ }
+ ],
+ "usage": {
+ "prompt_tokens": None,
+ "completion_tokens": None,
+ "total_tokens": None
+ }
+ }
+
+@app.get("/v1/dashboard/billing/subscription")
+@app.get("/dashboard/billing/subscription")
+async def billing_subscription():
+ return JSONResponse({
+ "object": "billing_subscription",
+ "has_payment_method": True,
+ "canceled": False,
+ "canceled_at": None,
+ "delinquent": None,
+ "access_until": 2556028800,
+ "soft_limit": 6944500,
+ "hard_limit": 166666666,
+ "system_hard_limit": 166666666,
+ "soft_limit_usd": 416.67,
+ "hard_limit_usd": 9999.99996,
+ "system_hard_limit_usd": 9999.99996,
+ "plan": {
+ "title": "Pay-as-you-go",
+ "id": "payg"
+ },
+ "primary": True,
+ "account_name": "OpenAI",
+ "po_number": None,
+ "billing_email": None,
+ "tax_ids": None,
+ "billing_address": {
+ "city": "New York",
+ "line1": "OpenAI",
+ "country": "US",
+ "postal_code": "NY10031"
+ },
+ "business_address": None
+ })
+
+@app.get("/v1/dashboard/billing/usage")
+@app.get("/dashboard/billing/usage")
+async def billing_usage():
+ return JSONResponse({
+ "object": "list",
+ "daily_costs": [
+ {
+ "timestamp": time.time(),
+ "line_items": [
+ {
+ "name": "GPT-4",
+ "cost": 0.0
+ },
+ {
+ "name": "Chat models",
+ "cost": 1.01
+ },
+ {
+ "name": "InstructGPT",
+ "cost": 0.0
+ },
+ {
+ "name": "Fine-tuning models",
+ "cost": 0.0
+ },
+ {
+ "name": "Embedding models",
+ "cost": 0.0
+ },
+ {
+ "name": "Image models",
+ "cost": 16.0
+ },
+ {
+ "name": "Audio models",
+ "cost": 0.0
+ }
+ ]
+ }
+ ],
+ "total_usage": 1.01
+ })
+
+@app.get("/v1/models")
+@app.get("/models")
+async def get_models():
+ models_data = {"data": []}
+ for model_name, model in ModelUtils.convert.items():
+ models_data['data'].append({
+ "id": model_name,
+ "object": "model",
+ "owned_by": model.base_provider,
+ "tokens": 99999,
+ "fallbacks": None,
+ "endpoints": [
+ "/v1/chat/completions"
+ ],
+ "limits": None,
+ "permission": []
+ })
+ return JSONResponse(models_data)
+
+@app.get("/v1/providers")
+@app.get("/providers")
+async def get_providers():
+ providers_data = {"data": []}
+ for provider_name in dir(Provider):
+ if not provider_name.startswith('__'):
+ try:
+ provider = getattr(Provider, provider_name)
+ providers_data["data"].append({
+ "provider": provider_name,
+ "model": list(provider.model),
+ "url": provider.url,
+ "working": bool(provider.working),
+ "supports_stream": bool(provider.supports_stream)
+ })
+ except:
+ pass
+ return JSONResponse(providers_data)
+
+def process_provider(provider_name, model_name):
+ try:
+ p = getattr(g4f.Provider, provider_name)
+ provider_status = {
+ "provider": provider_name,
+ "model": model_name,
+ "url": p.url,
+ "status": ""
+ }
+
+ # Проверяем только модель 'gpt-3.5-turbo' для провайдеров Wewordle и Qidinam
+ if provider_name in ['Wewordle', 'Qidinam', 'DeepAi', 'GetGpt', 'Yqcloud'] and model_name != 'gpt-3.5-turbo':
+ provider_status['status'] = 'Inactive'
+ #print(f"{provider_name} with {model_name} skipped")
+ return provider_status
+
+ try:
+ response = ChatCompletion.create(model=model_name, provider=p,
+ messages=[{"role": "user", "content": "Say 'Hello World!'"}], stream=False)
+ if any(word in response for word in ['Hello World', 'Hello', 'hello', 'world']):
+ provider_status['status'] = 'Active'
+ #print(f"{provider_name} with {model_name} say: {response}")
+ else:
+ provider_status['status'] = 'Inactive'
+ #print(f"{provider_name} with {model_name} say: Inactive")
+ except Exception as e:
+ provider_status['status'] = 'Inactive'
+ # print(f"{provider_name} with {model_name} say: Error")
+
+ return provider_status
+ except:
+ return None
+
+async def run_check_script():
+ session = aiohttp.ClientSession()
+ while True:
+ models = [model for model in g4f.models.ModelUtils.convert if model.startswith('gpt-') or model.startswith('claude') or model.startswith('text-')]
+ providers = [provider for provider in dir(g4f.Provider) if not provider.startswith('__')]
+
+ status = {'data': []}
+ with concurrent.futures.ThreadPoolExecutor() as executor:
+ futures = []
+ for provider_name in providers:
+ for model_name in models:
+ future = executor.submit(process_provider, provider_name, model_name)
+ futures.append(future)
+
+ for future in concurrent.futures.as_completed(futures):
+ result = future.result()
+ if result is not None and result['status'] == 'Active':
+ status['data'].append(result)
+
+ print(status)
+ status['key'] = "test"
+ tz = pytz.timezone('Asia/Shanghai')
+ now = datetime.now(tz)
+ print(now)
+ status['time'] = now.strftime("%Y-%m-%d %H:%M:%S")
+
+ if status['data']:
+ # Здесь мы используем aiofiles для асинхронного записывания в файл
+ async with aiofiles.open('status.json', 'w') as f:
+ await f.write(json.dumps(status))
+
+ # Pause for 5 minutes before starting the next cycle
+ time.sleep(360)
+
+# Запуск асинхронных задач
+async def run_tasks():
+ while True:
+ await asyncio.gather(run_check_script())
+ await asyncio.sleep(300)
+
+# Запуск приложения
+def main():
+ loop = asyncio.get_event_loop()
+ loop.run_until_complete(run_tasks())
+ loop.close()
+
+if __name__ == "__main__":
+ main()
\ No newline at end of file
diff --git a/backend/check.py b/backend/check.py
new file mode 100644
index 0000000000000000000000000000000000000000..68d2defd099f5145252169a363db38186b8888ca
--- /dev/null
+++ b/backend/check.py
@@ -0,0 +1,85 @@
+import os
+import g4f
+import json
+import time
+import pytz
+from datetime import datetime
+import concurrent.futures
+import asyncio
+from g4f import ChatCompletion
+from fp.fp import FreeProxy
+import threading
+import socket
+from auto_proxy import get_random_proxy, update_working_proxies
+
+def process_provider(provider_name, model_name):
+ try:
+ p = getattr(g4f.Provider, provider_name)
+ provider_status = {
+ "provider": provider_name,
+ "model": model_name,
+ "url": p.url,
+ "status": ""
+ }
+
+ # Проверяем только модель 'gpt-3.5-turbo' для провайдеров Wewordle и Qidinam
+ if provider_name in ['Wewordle', 'Qidinam', 'DeepAi', 'GetGpt', 'Yqcloud', 'WewordleApple'] and model_name != 'gpt-3.5-turbo':
+ provider_status['status'] = 'Inactive'
+ print(f"{provider_name} with {model_name} skipped")
+ return provider_status
+
+ try:
+ proxy = get_random_proxy().decode("utf-8")
+ formatted_proxy = f'https://{proxy}'
+
+ response = ChatCompletion.create(model=model_name, provider=p,
+ messages=[{"role": "user", "content": "Say 'Hello World!'"}], stream=False)
+ if any(word in response for word in ['Hello World', 'Hello', 'hello', 'world']):
+ provider_status['status'] = 'Active'
+ print(f"{provider_name} with {model_name} say: {response}")
+ else:
+ provider_status['status'] = 'Inactive'
+ print(f"{provider_name} with {model_name} say: Inactive")
+ except Exception as e:
+ provider_status['status'] = 'Inactive'
+ print(f"{provider_name} with {model_name} say: Error")
+
+ return provider_status
+ except:
+ return None
+
+def main():
+ while True:
+ models = [model for model in g4f.models.ModelUtils.convert if model.startswith('gpt-') or model.startswith('claude') or model.startswith('text-')]
+ providers = [provider for provider in dir(g4f.Provider) if not provider.startswith('__')]
+
+ status = {'data': []}
+ with concurrent.futures.ThreadPoolExecutor() as executor:
+ futures = []
+ for provider_name in providers:
+ for model_name in models:
+ future = executor.submit(process_provider, provider_name, model_name)
+ futures.append(future)
+
+ for future in concurrent.futures.as_completed(futures):
+ result = future.result()
+ if result is not None and result['status'] == 'Active':
+ status['data'].append(result)
+
+ print(status)
+ status['key'] = "test"
+ tz = pytz.timezone('Asia/Shanghai')
+ now = datetime.now(tz)
+ print(now)
+ status['time'] = now.strftime("%Y-%m-%d %H:%M:%S")
+
+ # Save the status data to a JSON file only if there are active providers
+ if status['data']:
+ with open('status.json', 'w') as f:
+ json.dump(status, f)
+
+ # Pause for 10 minutes before starting the next cycle
+ time.sleep(600)
+
+if __name__ == "__main__":
+ main()
\ No newline at end of file
diff --git a/backend/config.py b/backend/config.py
new file mode 100644
index 0000000000000000000000000000000000000000..cfc7613d3cc103e8e4ab998916ce7eece1a018d8
--- /dev/null
+++ b/backend/config.py
@@ -0,0 +1,9 @@
+app = {
+ 'host': '0.0.0.0',
+ 'port': 1337,
+ 'debug': True
+}
+
+request = {
+ 'timeout': 60
+}
\ No newline at end of file
diff --git a/backend/embedding_processing.py b/backend/embedding_processing.py
new file mode 100644
index 0000000000000000000000000000000000000000..b5bb997e22ff070eb1721b0937b9cd15804c070e
--- /dev/null
+++ b/backend/embedding_processing.py
@@ -0,0 +1,72 @@
+import spacy
+import numpy as np
+import os
+from zhconv import convert
+import re
+import random
+
+# добавьте специфическую для русского языка модель
+import ru_core_news_sm
+
+def detect_lang(text):
+ # 定义语言占比字典
+ lang_dict = {'zh-cn': 0, 'zh-tw': 0, 'en': 0, 'ru': 0, 'other': 0} # добавьте русский язык
+ # 随机抽样最多十个字符
+ sample = random.sample(text, min(10, len(text)))
+ # 计算每种语言的字符占比
+ for char in sample:
+ if re.search(r'[\u4e00-\u9fa5]', char):
+ lang_dict['zh-cn'] += 1
+ elif re.search(r'[\u4e00-\u9fff]', char):
+ lang_dict['zh-tw'] += 1
+ elif re.search(r'[a-zA-Z]', char):
+ lang_dict['en'] += 1
+ elif re.search(r'[а-яА-Я]', char): # добавьте соответствующий диапазон для русских букв
+ lang_dict['ru'] += 1
+ else:
+ lang_dict['other'] += 1
+ # 返回占比最高的语言
+ return max(lang_dict, key=lang_dict.get)
+
+class embedding_processing:
+
+ def __init__(self, model_path='./model'):
+ self.en_model = spacy.load('en_core_web_sm')
+ self.zh_model = spacy.load('zh_core_web_sm')
+ self.ru_model = ru_core_news_sm.load() # добавьте модель для русского языка
+
+ def model(self,text):
+ lang = detect_lang(text)
+ if lang == "zh-tw":
+ ans_cn = self.zh_model(convert(text)).vector.tolist()
+ else:
+ ans_cn = self.zh_model(text).vector.tolist()
+ ans = self.en_model(text).vector.tolist()
+ return ans_cn+ans
+
+ def embedding(self, text_list):
+ embeddings_list = [self.model(text) for text in text_list]
+ response_embedding = self.transform_embedding_to_dict(embeddings_list,text_list)
+ return response_embedding
+
+ def transform_embedding_to_dict(self, embedding_list, text_list, model_name="text-embedding-ada-002"):
+ prompt_tokens = sum(len(text) for text in text_list)
+ total_tokens = sum(len(embedding) for embedding in embedding_list)
+
+ transformed_data = {
+ "data": [
+ {
+ "embedding": embedding,
+ "index": index,
+ "object": "embedding"
+ }
+ for index, embedding in enumerate(embedding_list)
+ ],
+ "model": model_name,
+ "object": "list",
+ "usage": {
+ "prompt_tokens": prompt_tokens,
+ "total_tokens": total_tokens
+ }
+ }
+ return transformed_data
\ No newline at end of file
diff --git a/configs/ds_config_chatbot.json b/configs/ds_config_chatbot.json
new file mode 100644
index 0000000000000000000000000000000000000000..09b0b7ae082ff57d45b87bf6ee3662459b741def
--- /dev/null
+++ b/configs/ds_config_chatbot.json
@@ -0,0 +1,17 @@
+{
+ "fp16": {
+ "enabled": false
+ },
+ "bf16": {
+ "enabled": true
+ },
+ "comms_logger": {
+ "enabled": false,
+ "verbose": false,
+ "prof_all": false,
+ "debug": false
+ },
+ "steps_per_print": 20000000000000000,
+ "train_micro_batch_size_per_gpu": 1,
+ "wall_clock_breakdown": false
+}
diff --git a/docker-compose/.env b/docker-compose/.env
new file mode 100644
index 0000000000000000000000000000000000000000..d2d20363449e2141f3f85220222031150060ee5f
--- /dev/null
+++ b/docker-compose/.env
@@ -0,0 +1,9 @@
+LANG=ru
+# Paste ur email below
+EMAIL=acme@example.com
+TRAEFIK_USER=admin
+CERT_RESOLVER=letsencrypt
+# To change password use " htpasswd -nBC 10 admin " (or whatever username u want) and paste below the part after :
+TRAEFIK_PASSWORD_HASH=
+OPENAI_API_KEY=
+DOMAIN=
diff --git a/docker-compose/docker-compose.traefik.yml b/docker-compose/docker-compose.traefik.yml
new file mode 100644
index 0000000000000000000000000000000000000000..bcb98cbc9b44b716cecdb05bc8e0930c8ecb91d3
--- /dev/null
+++ b/docker-compose/docker-compose.traefik.yml
@@ -0,0 +1,73 @@
+version: "3.7"
+services:
+
+ traefik:
+ image: traefik:2.4.8
+ container_name: proxy
+ hostname: proxy
+ command:
+ - --entrypoints.http.address=:80
+ - --entrypoints.https.address=:443
+ - --providers.docker=true
+ - --api=false
+ - --log.level=DEBUG
+ - --certificatesresolvers.letsencrypt.acme.httpchallenge=true
+ - --certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=http
+ - --certificatesresolvers.letsencrypt.acme.email=${EMAIL}
+ - --certificatesresolvers.letsencrypt.acme.storage=/letsencrypt/acme.json
+ networks:
+ - ngptnet
+ labels:
+ - traefik.enable=true
+ - traefik.docker.network=ngptnet
+ - traefik.http.routers.to-https.rule=HostRegexp(`{host:.+}`)
+ - traefik.http.routers.to-https.entrypoints=http
+ - traefik.http.routers.to-https.middlewares=to-https
+ - traefik.http.middlewares.to-https.redirectscheme.scheme=https
+ - traefik.http.middlewares.basicauth.basicauth.users=${TRAEFIK_USER}:${TRAEFIK_PASSWORD_HASH}
+ ports:
+ - 80:80
+ - 443:443
+ volumes:
+ - acmes:/letsencrypt
+ - /var/run/docker.sock:/var/run/docker.sock:ro
+
+ neurogengpt:
+ image: neurogen/neurogengpt:latest
+ build:
+ context: ../
+ dockerfile: Dockerfile
+ container_name: neurogengpt
+ hostname: neurogengpt
+ environment:
+ - SUBF=/sub
+ env_file:
+ - .env
+ networks:
+ - ngptnet
+ labels:
+ - traefik.enable=true
+ - traefik.docker.network=ngptnet
+ # WebUI config
+ - traefik.http.routers.ngpt-webui.rule=Host(`${DOMAIN}`)
+ - traefik.http.routers.ngpt-webui.entrypoints=https
+ - traefik.http.routers.ngpt-webui.tls=true
+ - traefik.http.routers.ngpt-webui.tls.certresolver=${CERT_RESOLVER}
+ - traefik.http.routers.ngpt-webui.service=ngpt-webui
+ - traefik.http.services.ngpt-webui.loadbalancer.server.port=7860
+ - traefik.http.routers.ngpt-webui.middlewares=basicauth
+ # API config
+ - traefik.http.routers.ngpt-api.rule=Host(`${DOMAIN}`) && PathPrefix(`/api`)
+ - traefik.http.routers.ngpt-api.middlewares=api-stripprefix
+ - traefik.http.middlewares.api-stripprefix.stripprefix.prefixes=/api
+ - traefik.http.routers.ngpt-api.entrypoints=https
+ - traefik.http.routers.ngpt-api.service=ngpt-api
+ - traefik.http.services.ngpt-api.loadbalancer.server.port=1337
+ volumes:
+ - ../config.json:/app/config.json
+
+volumes:
+ acmes:
+
+networks:
+ ngptnet:
diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml
new file mode 100644
index 0000000000000000000000000000000000000000..8ca796a7390a956048fa3deff390ce607d6a4d66
--- /dev/null
+++ b/docker-compose/docker-compose.yml
@@ -0,0 +1,16 @@
+version: "3.10"
+
+services:
+ neurogengpt:
+ build:
+ context: ../
+ dockerfile: Dockerfile
+ ports:
+ - "7860:7860"
+ - "1337:1337"
+ volumes:
+ - ../config.json:/app/config.json
+ environment:
+ - SUBF=/sub
+ env_file:
+ - .env
diff --git a/dockerfile b/dockerfile
new file mode 100644
index 0000000000000000000000000000000000000000..a175f39a3eed5a2b213c15f3511542cf79bf02c3
--- /dev/null
+++ b/dockerfile
@@ -0,0 +1,28 @@
+# Stage 1: Build
+
+FROM python:3.10-slim-buster as builder
+
+WORKDIR /app
+
+RUN apt-get update && apt-get install -y --no-install-recommends build-essential git
+COPY requirements.txt .
+RUN pip install --no-cache-dir --user -r requirements.txt
+
+FROM python:3.10-slim-buster
+
+WORKDIR /app
+
+COPY --from=builder /root/.local /root/.local
+ENV PATH=/root/.local/bin:$PATH
+COPY . .
+RUN python -m spacy download ru_core_news_sm && \
+ python -m spacy download en_core_web_sm && \
+ python -m spacy download zh_core_web_sm
+RUN chmod +x entrypoint.sh
+
+EXPOSE 7860
+EXPOSE 1337
+
+ENV LANG=ru
+
+CMD ["./entrypoint.sh"]
diff --git a/endpoint.py b/endpoint.py
new file mode 100644
index 0000000000000000000000000000000000000000..4d492f917061d988230a7e8227116fe51e764636
--- /dev/null
+++ b/endpoint.py
@@ -0,0 +1,21 @@
+import socket
+import nest_asyncio
+from backend.backend import app
+from waitress import serve
+
+nest_asyncio.apply()
+
+site_config = {
+ 'host': '0.0.0.0',
+ 'port': 1337,
+ 'debug': False
+ }
+
+if __name__ == "__main__":
+ hostname = socket.gethostname()
+ ip_address = socket.gethostbyname(hostname)
+
+ print(f"Running on http://127.0.0.1:{site_config['port']}")
+ print(f"Running on http://{ip_address}:{site_config['port']}")
+
+ serve(app, host='0.0.0.0', port=site_config['port'],threads=4)
\ No newline at end of file
diff --git a/entrypoint.sh b/entrypoint.sh
new file mode 100644
index 0000000000000000000000000000000000000000..1c8cc300ded3a9e1c2a25da08fa09fecb8ebb31b
--- /dev/null
+++ b/entrypoint.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+LANG_VALUE="$LANG"
+
+if [[ "$LANG_VALUE" == "en" ]]; then
+ echo "Running $LANG version"
+ python webui_en.py
+elif [[ "$LANG_VALUE" == "ru" ]]; then
+ echo "Запускается $LANG версия"
+ python webui_ru.py
+else
+ echo "Недопустимое значение переменной окружения LANG. Поддерживаемые значения: en, ru"
+ exit 1
+fi
diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py
new file mode 100644
index 0000000000000000000000000000000000000000..56d9a9abdf39a0fb7f4bdd4561ad117fc14f8ed0
--- /dev/null
+++ b/g4f/Provider/AItianhu.py
@@ -0,0 +1,77 @@
+from __future__ import annotations
+
+import json
+
+from ..typing import AsyncResult, Messages
+from ..requests import StreamSession
+from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies
+
+
+class AItianhu(AsyncGeneratorProvider):
+ url = "https://www.aitianhu.com"
+ working = True
+ supports_gpt_35_turbo = True
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ proxy: str = None,
+ cookies: dict = None,
+ timeout: int = 120,
+ **kwargs
+ ) -> AsyncResult:
+ if not cookies:
+ cookies = get_cookies("www.aitianhu.com")
+ data = {
+ "prompt": format_prompt(messages),
+ "options": {},
+ "systemMessage": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.",
+ "temperature": 0.8,
+ "top_p": 1,
+ **kwargs
+ }
+ headers = {
+ "Authority": cls.url,
+ "Accept": "application/json, text/plain, */*",
+ "Origin": cls.url,
+ "Referer": f"{cls.url}/"
+ }
+ async with StreamSession(
+ headers=headers,
+ cookies=cookies,
+ timeout=timeout,
+ proxies={"https": proxy},
+ impersonate="chrome107",
+ verify=False
+ ) as session:
+ async with session.post(f"{cls.url}/api/chat-process", json=data) as response:
+ response.raise_for_status()
+ async for line in response.iter_lines():
+ if line == b"
+
+```
+
+## API
+
+See: https://cryptojs.gitbook.io/docs/
+
+### AES Encryption
+
+#### Plain text encryption
+
+```javascript
+var CryptoJS = require("crypto-js");
+
+// Encrypt
+var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123').toString();
+
+// Decrypt
+var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
+var originalText = bytes.toString(CryptoJS.enc.Utf8);
+
+console.log(originalText); // 'my message'
+```
+
+#### Object encryption
+
+```javascript
+var CryptoJS = require("crypto-js");
+
+var data = [{id: 1}, {id: 2}]
+
+// Encrypt
+var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString();
+
+// Decrypt
+var bytes = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
+var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
+
+console.log(decryptedData); // [{id: 1}, {id: 2}]
+```
+
+### List of modules
+
+
+- ```crypto-js/core```
+- ```crypto-js/x64-core```
+- ```crypto-js/lib-typedarrays```
+
+---
+
+- ```crypto-js/md5```
+- ```crypto-js/sha1```
+- ```crypto-js/sha256```
+- ```crypto-js/sha224```
+- ```crypto-js/sha512```
+- ```crypto-js/sha384```
+- ```crypto-js/sha3```
+- ```crypto-js/ripemd160```
+
+---
+
+- ```crypto-js/hmac-md5```
+- ```crypto-js/hmac-sha1```
+- ```crypto-js/hmac-sha256```
+- ```crypto-js/hmac-sha224```
+- ```crypto-js/hmac-sha512```
+- ```crypto-js/hmac-sha384```
+- ```crypto-js/hmac-sha3```
+- ```crypto-js/hmac-ripemd160```
+
+---
+
+- ```crypto-js/pbkdf2```
+
+---
+
+- ```crypto-js/aes```
+- ```crypto-js/tripledes```
+- ```crypto-js/rc4```
+- ```crypto-js/rabbit```
+- ```crypto-js/rabbit-legacy```
+- ```crypto-js/evpkdf```
+
+---
+
+- ```crypto-js/format-openssl```
+- ```crypto-js/format-hex```
+
+---
+
+- ```crypto-js/enc-latin1```
+- ```crypto-js/enc-utf8```
+- ```crypto-js/enc-hex```
+- ```crypto-js/enc-utf16```
+- ```crypto-js/enc-base64```
+
+---
+
+- ```crypto-js/mode-cfb```
+- ```crypto-js/mode-ctr```
+- ```crypto-js/mode-ctr-gladman```
+- ```crypto-js/mode-ofb```
+- ```crypto-js/mode-ecb```
+
+---
+
+- ```crypto-js/pad-pkcs7```
+- ```crypto-js/pad-ansix923```
+- ```crypto-js/pad-iso10126```
+- ```crypto-js/pad-iso97971```
+- ```crypto-js/pad-zeropadding```
+- ```crypto-js/pad-nopadding```
+
+
+## Release notes
+
+### 4.1.1
+
+Fix module order in bundled release.
+
+Include the browser field in the released package.json.
+
+### 4.1.0
+
+Added url safe variant of base64 encoding. [357](https://github.com/brix/crypto-js/pull/357)
+
+Avoid webpack to add crypto-browser package. [364](https://github.com/brix/crypto-js/pull/364)
+
+### 4.0.0
+
+This is an update including breaking changes for some environments.
+
+In this version `Math.random()` has been replaced by the random methods of the native crypto module.
+
+For this reason CryptoJS might not run in some JavaScript environments without native crypto module. Such as IE 10 or before or React Native.
+
+### 3.3.0
+
+Rollback, `3.3.0` is the same as `3.1.9-1`.
+
+The move of using native secure crypto module will be shifted to a new `4.x.x` version. As it is a breaking change the impact is too big for a minor release.
+
+### 3.2.1
+
+The usage of the native crypto module has been fixed. The import and access of the native crypto module has been improved.
+
+### 3.2.0
+
+In this version `Math.random()` has been replaced by the random methods of the native crypto module.
+
+For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before.
+
+If it's absolute required to run CryptoJS in such an environment, stay with `3.1.x` version. Encrypting and decrypting stays compatible. But keep in mind `3.1.x` versions still use `Math.random()` which is cryptographically not secure, as it's not random enough.
+
+This version came along with `CRITICAL` `BUG`.
+
+DO NOT USE THIS VERSION! Please, go for a newer version!
+
+### 3.1.x
+
+The `3.1.x` are based on the original CryptoJS, wrapped in CommonJS modules.
+
+
diff --git a/g4f/Provider/npm/node_modules/crypto-js/crypto-js.js b/g4f/Provider/npm/node_modules/crypto-js/crypto-js.js
new file mode 100644
index 0000000000000000000000000000000000000000..27f0a620a270e0bafc43ab15df6f31c0b95b4189
--- /dev/null
+++ b/g4f/Provider/npm/node_modules/crypto-js/crypto-js.js
@@ -0,0 +1,6191 @@
+;(function (root, factory) {
+ if (typeof exports === "object") {
+ // CommonJS
+ module.exports = exports = factory();
+ }
+ else if (typeof define === "function" && define.amd) {
+ // AMD
+ define([], factory);
+ }
+ else {
+ // Global (browser)
+ root.CryptoJS = factory();
+ }
+}(this, function () {
+
+ /*globals window, global, require*/
+
+ /**
+ * CryptoJS core components.
+ */
+ var CryptoJS = CryptoJS || (function (Math, undefined) {
+
+ var crypto;
+
+ // Native crypto from window (Browser)
+ if (typeof window !== 'undefined' && window.crypto) {
+ crypto = window.crypto;
+ }
+
+ // Native crypto in web worker (Browser)
+ if (typeof self !== 'undefined' && self.crypto) {
+ crypto = self.crypto;
+ }
+
+ // Native crypto from worker
+ if (typeof globalThis !== 'undefined' && globalThis.crypto) {
+ crypto = globalThis.crypto;
+ }
+
+ // Native (experimental IE 11) crypto from window (Browser)
+ if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
+ crypto = window.msCrypto;
+ }
+
+ // Native crypto from global (NodeJS)
+ if (!crypto && typeof global !== 'undefined' && global.crypto) {
+ crypto = global.crypto;
+ }
+
+ // Native crypto import via require (NodeJS)
+ if (!crypto && typeof require === 'function') {
+ try {
+ crypto = require('crypto');
+ } catch (err) {}
+ }
+
+ /*
+ * Cryptographically secure pseudorandom number generator
+ *
+ * As Math.random() is cryptographically not safe to use
+ */
+ var cryptoSecureRandomInt = function () {
+ if (crypto) {
+ // Use getRandomValues method (Browser)
+ if (typeof crypto.getRandomValues === 'function') {
+ try {
+ return crypto.getRandomValues(new Uint32Array(1))[0];
+ } catch (err) {}
+ }
+
+ // Use randomBytes method (NodeJS)
+ if (typeof crypto.randomBytes === 'function') {
+ try {
+ return crypto.randomBytes(4).readInt32LE();
+ } catch (err) {}
+ }
+ }
+
+ throw new Error('Native crypto module could not be used to get secure random number.');
+ };
+
+ /*
+ * Local polyfill of Object.create
+
+ */
+ var create = Object.create || (function () {
+ function F() {}
+
+ return function (obj) {
+ var subtype;
+
+ F.prototype = obj;
+
+ subtype = new F();
+
+ F.prototype = null;
+
+ return subtype;
+ };
+ }());
+
+ /**
+ * CryptoJS namespace.
+ */
+ var C = {};
+
+ /**
+ * Library namespace.
+ */
+ var C_lib = C.lib = {};
+
+ /**
+ * Base object for prototypal inheritance.
+ */
+ var Base = C_lib.Base = (function () {
+
+
+ return {
+ /**
+ * Creates a new object that inherits from this object.
+ *
+ * @param {Object} overrides Properties to copy into the new object.
+ *
+ * @return {Object} The new object.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var MyType = CryptoJS.lib.Base.extend({
+ * field: 'value',
+ *
+ * method: function () {
+ * }
+ * });
+ */
+ extend: function (overrides) {
+ // Spawn
+ var subtype = create(this);
+
+ // Augment
+ if (overrides) {
+ subtype.mixIn(overrides);
+ }
+
+ // Create default initializer
+ if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
+ subtype.init = function () {
+ subtype.$super.init.apply(this, arguments);
+ };
+ }
+
+ // Initializer's prototype is the subtype object
+ subtype.init.prototype = subtype;
+
+ // Reference supertype
+ subtype.$super = this;
+
+ return subtype;
+ },
+
+ /**
+ * Extends this object and runs the init method.
+ * Arguments to create() will be passed to init().
+ *
+ * @return {Object} The new object.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var instance = MyType.create();
+ */
+ create: function () {
+ var instance = this.extend();
+ instance.init.apply(instance, arguments);
+
+ return instance;
+ },
+
+ /**
+ * Initializes a newly created object.
+ * Override this method to add some logic when your objects are created.
+ *
+ * @example
+ *
+ * var MyType = CryptoJS.lib.Base.extend({
+ * init: function () {
+ * // ...
+ * }
+ * });
+ */
+ init: function () {
+ },
+
+ /**
+ * Copies properties into this object.
+ *
+ * @param {Object} properties The properties to mix in.
+ *
+ * @example
+ *
+ * MyType.mixIn({
+ * field: 'value'
+ * });
+ */
+ mixIn: function (properties) {
+ for (var propertyName in properties) {
+ if (properties.hasOwnProperty(propertyName)) {
+ this[propertyName] = properties[propertyName];
+ }
+ }
+
+ // IE won't copy toString using the loop above
+ if (properties.hasOwnProperty('toString')) {
+ this.toString = properties.toString;
+ }
+ },
+
+ /**
+ * Creates a copy of this object.
+ *
+ * @return {Object} The clone.
+ *
+ * @example
+ *
+ * var clone = instance.clone();
+ */
+ clone: function () {
+ return this.init.prototype.extend(this);
+ }
+ };
+ }());
+
+ /**
+ * An array of 32-bit words.
+ *
+ * @property {Array} words The array of 32-bit words.
+ * @property {number} sigBytes The number of significant bytes in this word array.
+ */
+ var WordArray = C_lib.WordArray = Base.extend({
+ /**
+ * Initializes a newly created word array.
+ *
+ * @param {Array} words (Optional) An array of 32-bit words.
+ * @param {number} sigBytes (Optional) The number of significant bytes in the words.
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.lib.WordArray.create();
+ * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
+ * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
+ */
+ init: function (words, sigBytes) {
+ words = this.words = words || [];
+
+ if (sigBytes != undefined) {
+ this.sigBytes = sigBytes;
+ } else {
+ this.sigBytes = words.length * 4;
+ }
+ },
+
+ /**
+ * Converts this word array to a string.
+ *
+ * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
+ *
+ * @return {string} The stringified word array.
+ *
+ * @example
+ *
+ * var string = wordArray + '';
+ * var string = wordArray.toString();
+ * var string = wordArray.toString(CryptoJS.enc.Utf8);
+ */
+ toString: function (encoder) {
+ return (encoder || Hex).stringify(this);
+ },
+
+ /**
+ * Concatenates a word array to this word array.
+ *
+ * @param {WordArray} wordArray The word array to append.
+ *
+ * @return {WordArray} This word array.
+ *
+ * @example
+ *
+ * wordArray1.concat(wordArray2);
+ */
+ concat: function (wordArray) {
+ // Shortcuts
+ var thisWords = this.words;
+ var thatWords = wordArray.words;
+ var thisSigBytes = this.sigBytes;
+ var thatSigBytes = wordArray.sigBytes;
+
+ // Clamp excess bits
+ this.clamp();
+
+ // Concat
+ if (thisSigBytes % 4) {
+ // Copy one byte at a time
+ for (var i = 0; i < thatSigBytes; i++) {
+ var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
+ thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
+ }
+ } else {
+ // Copy one word at a time
+ for (var j = 0; j < thatSigBytes; j += 4) {
+ thisWords[(thisSigBytes + j) >>> 2] = thatWords[j >>> 2];
+ }
+ }
+ this.sigBytes += thatSigBytes;
+
+ // Chainable
+ return this;
+ },
+
+ /**
+ * Removes insignificant bits.
+ *
+ * @example
+ *
+ * wordArray.clamp();
+ */
+ clamp: function () {
+ // Shortcuts
+ var words = this.words;
+ var sigBytes = this.sigBytes;
+
+ // Clamp
+ words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
+ words.length = Math.ceil(sigBytes / 4);
+ },
+
+ /**
+ * Creates a copy of this word array.
+ *
+ * @return {WordArray} The clone.
+ *
+ * @example
+ *
+ * var clone = wordArray.clone();
+ */
+ clone: function () {
+ var clone = Base.clone.call(this);
+ clone.words = this.words.slice(0);
+
+ return clone;
+ },
+
+ /**
+ * Creates a word array filled with random bytes.
+ *
+ * @param {number} nBytes The number of random bytes to generate.
+ *
+ * @return {WordArray} The random word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.lib.WordArray.random(16);
+ */
+ random: function (nBytes) {
+ var words = [];
+
+ for (var i = 0; i < nBytes; i += 4) {
+ words.push(cryptoSecureRandomInt());
+ }
+
+ return new WordArray.init(words, nBytes);
+ }
+ });
+
+ /**
+ * Encoder namespace.
+ */
+ var C_enc = C.enc = {};
+
+ /**
+ * Hex encoding strategy.
+ */
+ var Hex = C_enc.Hex = {
+ /**
+ * Converts a word array to a hex string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @return {string} The hex string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
+ */
+ stringify: function (wordArray) {
+ // Shortcuts
+ var words = wordArray.words;
+ var sigBytes = wordArray.sigBytes;
+
+ // Convert
+ var hexChars = [];
+ for (var i = 0; i < sigBytes; i++) {
+ var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
+ hexChars.push((bite >>> 4).toString(16));
+ hexChars.push((bite & 0x0f).toString(16));
+ }
+
+ return hexChars.join('');
+ },
+
+ /**
+ * Converts a hex string to a word array.
+ *
+ * @param {string} hexStr The hex string.
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Hex.parse(hexString);
+ */
+ parse: function (hexStr) {
+ // Shortcut
+ var hexStrLength = hexStr.length;
+
+ // Convert
+ var words = [];
+ for (var i = 0; i < hexStrLength; i += 2) {
+ words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
+ }
+
+ return new WordArray.init(words, hexStrLength / 2);
+ }
+ };
+
+ /**
+ * Latin1 encoding strategy.
+ */
+ var Latin1 = C_enc.Latin1 = {
+ /**
+ * Converts a word array to a Latin1 string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @return {string} The Latin1 string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
+ */
+ stringify: function (wordArray) {
+ // Shortcuts
+ var words = wordArray.words;
+ var sigBytes = wordArray.sigBytes;
+
+ // Convert
+ var latin1Chars = [];
+ for (var i = 0; i < sigBytes; i++) {
+ var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
+ latin1Chars.push(String.fromCharCode(bite));
+ }
+
+ return latin1Chars.join('');
+ },
+
+ /**
+ * Converts a Latin1 string to a word array.
+ *
+ * @param {string} latin1Str The Latin1 string.
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
+ */
+ parse: function (latin1Str) {
+ // Shortcut
+ var latin1StrLength = latin1Str.length;
+
+ // Convert
+ var words = [];
+ for (var i = 0; i < latin1StrLength; i++) {
+ words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
+ }
+
+ return new WordArray.init(words, latin1StrLength);
+ }
+ };
+
+ /**
+ * UTF-8 encoding strategy.
+ */
+ var Utf8 = C_enc.Utf8 = {
+ /**
+ * Converts a word array to a UTF-8 string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @return {string} The UTF-8 string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
+ */
+ stringify: function (wordArray) {
+ try {
+ return decodeURIComponent(escape(Latin1.stringify(wordArray)));
+ } catch (e) {
+ throw new Error('Malformed UTF-8 data');
+ }
+ },
+
+ /**
+ * Converts a UTF-8 string to a word array.
+ *
+ * @param {string} utf8Str The UTF-8 string.
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
+ */
+ parse: function (utf8Str) {
+ return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
+ }
+ };
+
+ /**
+ * Abstract buffered block algorithm template.
+ *
+ * The property blockSize must be implemented in a concrete subtype.
+ *
+ * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
+ */
+ var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
+ /**
+ * Resets this block algorithm's data buffer to its initial state.
+ *
+ * @example
+ *
+ * bufferedBlockAlgorithm.reset();
+ */
+ reset: function () {
+ // Initial values
+ this._data = new WordArray.init();
+ this._nDataBytes = 0;
+ },
+
+ /**
+ * Adds new data to this block algorithm's buffer.
+ *
+ * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
+ *
+ * @example
+ *
+ * bufferedBlockAlgorithm._append('data');
+ * bufferedBlockAlgorithm._append(wordArray);
+ */
+ _append: function (data) {
+ // Convert string to WordArray, else assume WordArray already
+ if (typeof data == 'string') {
+ data = Utf8.parse(data);
+ }
+
+ // Append
+ this._data.concat(data);
+ this._nDataBytes += data.sigBytes;
+ },
+
+ /**
+ * Processes available data blocks.
+ *
+ * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
+ *
+ * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
+ *
+ * @return {WordArray} The processed data.
+ *
+ * @example
+ *
+ * var processedData = bufferedBlockAlgorithm._process();
+ * var processedData = bufferedBlockAlgorithm._process(!!'flush');
+ */
+ _process: function (doFlush) {
+ var processedWords;
+
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+ var dataSigBytes = data.sigBytes;
+ var blockSize = this.blockSize;
+ var blockSizeBytes = blockSize * 4;
+
+ // Count blocks ready
+ var nBlocksReady = dataSigBytes / blockSizeBytes;
+ if (doFlush) {
+ // Round up to include partial blocks
+ nBlocksReady = Math.ceil(nBlocksReady);
+ } else {
+ // Round down to include only full blocks,
+ // less the number of blocks that must remain in the buffer
+ nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
+ }
+
+ // Count words ready
+ var nWordsReady = nBlocksReady * blockSize;
+
+ // Count bytes ready
+ var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
+
+ // Process blocks
+ if (nWordsReady) {
+ for (var offset = 0; offset < nWordsReady; offset += blockSize) {
+ // Perform concrete-algorithm logic
+ this._doProcessBlock(dataWords, offset);
+ }
+
+ // Remove processed words
+ processedWords = dataWords.splice(0, nWordsReady);
+ data.sigBytes -= nBytesReady;
+ }
+
+ // Return processed words
+ return new WordArray.init(processedWords, nBytesReady);
+ },
+
+ /**
+ * Creates a copy of this object.
+ *
+ * @return {Object} The clone.
+ *
+ * @example
+ *
+ * var clone = bufferedBlockAlgorithm.clone();
+ */
+ clone: function () {
+ var clone = Base.clone.call(this);
+ clone._data = this._data.clone();
+
+ return clone;
+ },
+
+ _minBufferSize: 0
+ });
+
+ /**
+ * Abstract hasher template.
+ *
+ * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
+ */
+ var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
+ /**
+ * Configuration options.
+ */
+ cfg: Base.extend(),
+
+ /**
+ * Initializes a newly created hasher.
+ *
+ * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
+ *
+ * @example
+ *
+ * var hasher = CryptoJS.algo.SHA256.create();
+ */
+ init: function (cfg) {
+ // Apply config defaults
+ this.cfg = this.cfg.extend(cfg);
+
+ // Set initial values
+ this.reset();
+ },
+
+ /**
+ * Resets this hasher to its initial state.
+ *
+ * @example
+ *
+ * hasher.reset();
+ */
+ reset: function () {
+ // Reset data buffer
+ BufferedBlockAlgorithm.reset.call(this);
+
+ // Perform concrete-hasher logic
+ this._doReset();
+ },
+
+ /**
+ * Updates this hasher with a message.
+ *
+ * @param {WordArray|string} messageUpdate The message to append.
+ *
+ * @return {Hasher} This hasher.
+ *
+ * @example
+ *
+ * hasher.update('message');
+ * hasher.update(wordArray);
+ */
+ update: function (messageUpdate) {
+ // Append
+ this._append(messageUpdate);
+
+ // Update the hash
+ this._process();
+
+ // Chainable
+ return this;
+ },
+
+ /**
+ * Finalizes the hash computation.
+ * Note that the finalize operation is effectively a destructive, read-once operation.
+ *
+ * @param {WordArray|string} messageUpdate (Optional) A final message update.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @example
+ *
+ * var hash = hasher.finalize();
+ * var hash = hasher.finalize('message');
+ * var hash = hasher.finalize(wordArray);
+ */
+ finalize: function (messageUpdate) {
+ // Final message update
+ if (messageUpdate) {
+ this._append(messageUpdate);
+ }
+
+ // Perform concrete-hasher logic
+ var hash = this._doFinalize();
+
+ return hash;
+ },
+
+ blockSize: 512/32,
+
+ /**
+ * Creates a shortcut function to a hasher's object interface.
+ *
+ * @param {Hasher} hasher The hasher to create a helper for.
+ *
+ * @return {Function} The shortcut function.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
+ */
+ _createHelper: function (hasher) {
+ return function (message, cfg) {
+ return new hasher.init(cfg).finalize(message);
+ };
+ },
+
+ /**
+ * Creates a shortcut function to the HMAC's object interface.
+ *
+ * @param {Hasher} hasher The hasher to use in this HMAC helper.
+ *
+ * @return {Function} The shortcut function.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
+ */
+ _createHmacHelper: function (hasher) {
+ return function (message, key) {
+ return new C_algo.HMAC.init(hasher, key).finalize(message);
+ };
+ }
+ });
+
+ /**
+ * Algorithm namespace.
+ */
+ var C_algo = C.algo = {};
+
+ return C;
+ }(Math));
+
+
+ (function (undefined) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var Base = C_lib.Base;
+ var X32WordArray = C_lib.WordArray;
+
+ /**
+ * x64 namespace.
+ */
+ var C_x64 = C.x64 = {};
+
+ /**
+ * A 64-bit word.
+ */
+ var X64Word = C_x64.Word = Base.extend({
+ /**
+ * Initializes a newly created 64-bit word.
+ *
+ * @param {number} high The high 32 bits.
+ * @param {number} low The low 32 bits.
+ *
+ * @example
+ *
+ * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607);
+ */
+ init: function (high, low) {
+ this.high = high;
+ this.low = low;
+ }
+
+ /**
+ * Bitwise NOTs this word.
+ *
+ * @return {X64Word} A new x64-Word object after negating.
+ *
+ * @example
+ *
+ * var negated = x64Word.not();
+ */
+ // not: function () {
+ // var high = ~this.high;
+ // var low = ~this.low;
+
+ // return X64Word.create(high, low);
+ // },
+
+ /**
+ * Bitwise ANDs this word with the passed word.
+ *
+ * @param {X64Word} word The x64-Word to AND with this word.
+ *
+ * @return {X64Word} A new x64-Word object after ANDing.
+ *
+ * @example
+ *
+ * var anded = x64Word.and(anotherX64Word);
+ */
+ // and: function (word) {
+ // var high = this.high & word.high;
+ // var low = this.low & word.low;
+
+ // return X64Word.create(high, low);
+ // },
+
+ /**
+ * Bitwise ORs this word with the passed word.
+ *
+ * @param {X64Word} word The x64-Word to OR with this word.
+ *
+ * @return {X64Word} A new x64-Word object after ORing.
+ *
+ * @example
+ *
+ * var ored = x64Word.or(anotherX64Word);
+ */
+ // or: function (word) {
+ // var high = this.high | word.high;
+ // var low = this.low | word.low;
+
+ // return X64Word.create(high, low);
+ // },
+
+ /**
+ * Bitwise XORs this word with the passed word.
+ *
+ * @param {X64Word} word The x64-Word to XOR with this word.
+ *
+ * @return {X64Word} A new x64-Word object after XORing.
+ *
+ * @example
+ *
+ * var xored = x64Word.xor(anotherX64Word);
+ */
+ // xor: function (word) {
+ // var high = this.high ^ word.high;
+ // var low = this.low ^ word.low;
+
+ // return X64Word.create(high, low);
+ // },
+
+ /**
+ * Shifts this word n bits to the left.
+ *
+ * @param {number} n The number of bits to shift.
+ *
+ * @return {X64Word} A new x64-Word object after shifting.
+ *
+ * @example
+ *
+ * var shifted = x64Word.shiftL(25);
+ */
+ // shiftL: function (n) {
+ // if (n < 32) {
+ // var high = (this.high << n) | (this.low >>> (32 - n));
+ // var low = this.low << n;
+ // } else {
+ // var high = this.low << (n - 32);
+ // var low = 0;
+ // }
+
+ // return X64Word.create(high, low);
+ // },
+
+ /**
+ * Shifts this word n bits to the right.
+ *
+ * @param {number} n The number of bits to shift.
+ *
+ * @return {X64Word} A new x64-Word object after shifting.
+ *
+ * @example
+ *
+ * var shifted = x64Word.shiftR(7);
+ */
+ // shiftR: function (n) {
+ // if (n < 32) {
+ // var low = (this.low >>> n) | (this.high << (32 - n));
+ // var high = this.high >>> n;
+ // } else {
+ // var low = this.high >>> (n - 32);
+ // var high = 0;
+ // }
+
+ // return X64Word.create(high, low);
+ // },
+
+ /**
+ * Rotates this word n bits to the left.
+ *
+ * @param {number} n The number of bits to rotate.
+ *
+ * @return {X64Word} A new x64-Word object after rotating.
+ *
+ * @example
+ *
+ * var rotated = x64Word.rotL(25);
+ */
+ // rotL: function (n) {
+ // return this.shiftL(n).or(this.shiftR(64 - n));
+ // },
+
+ /**
+ * Rotates this word n bits to the right.
+ *
+ * @param {number} n The number of bits to rotate.
+ *
+ * @return {X64Word} A new x64-Word object after rotating.
+ *
+ * @example
+ *
+ * var rotated = x64Word.rotR(7);
+ */
+ // rotR: function (n) {
+ // return this.shiftR(n).or(this.shiftL(64 - n));
+ // },
+
+ /**
+ * Adds this word with the passed word.
+ *
+ * @param {X64Word} word The x64-Word to add with this word.
+ *
+ * @return {X64Word} A new x64-Word object after adding.
+ *
+ * @example
+ *
+ * var added = x64Word.add(anotherX64Word);
+ */
+ // add: function (word) {
+ // var low = (this.low + word.low) | 0;
+ // var carry = (low >>> 0) < (this.low >>> 0) ? 1 : 0;
+ // var high = (this.high + word.high + carry) | 0;
+
+ // return X64Word.create(high, low);
+ // }
+ });
+
+ /**
+ * An array of 64-bit words.
+ *
+ * @property {Array} words The array of CryptoJS.x64.Word objects.
+ * @property {number} sigBytes The number of significant bytes in this word array.
+ */
+ var X64WordArray = C_x64.WordArray = Base.extend({
+ /**
+ * Initializes a newly created word array.
+ *
+ * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects.
+ * @param {number} sigBytes (Optional) The number of significant bytes in the words.
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.x64.WordArray.create();
+ *
+ * var wordArray = CryptoJS.x64.WordArray.create([
+ * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
+ * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
+ * ]);
+ *
+ * var wordArray = CryptoJS.x64.WordArray.create([
+ * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
+ * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
+ * ], 10);
+ */
+ init: function (words, sigBytes) {
+ words = this.words = words || [];
+
+ if (sigBytes != undefined) {
+ this.sigBytes = sigBytes;
+ } else {
+ this.sigBytes = words.length * 8;
+ }
+ },
+
+ /**
+ * Converts this 64-bit word array to a 32-bit word array.
+ *
+ * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array.
+ *
+ * @example
+ *
+ * var x32WordArray = x64WordArray.toX32();
+ */
+ toX32: function () {
+ // Shortcuts
+ var x64Words = this.words;
+ var x64WordsLength = x64Words.length;
+
+ // Convert
+ var x32Words = [];
+ for (var i = 0; i < x64WordsLength; i++) {
+ var x64Word = x64Words[i];
+ x32Words.push(x64Word.high);
+ x32Words.push(x64Word.low);
+ }
+
+ return X32WordArray.create(x32Words, this.sigBytes);
+ },
+
+ /**
+ * Creates a copy of this word array.
+ *
+ * @return {X64WordArray} The clone.
+ *
+ * @example
+ *
+ * var clone = x64WordArray.clone();
+ */
+ clone: function () {
+ var clone = Base.clone.call(this);
+
+ // Clone "words" array
+ var words = clone.words = this.words.slice(0);
+
+ // Clone each X64Word object
+ var wordsLength = words.length;
+ for (var i = 0; i < wordsLength; i++) {
+ words[i] = words[i].clone();
+ }
+
+ return clone;
+ }
+ });
+ }());
+
+
+ (function () {
+ // Check if typed arrays are supported
+ if (typeof ArrayBuffer != 'function') {
+ return;
+ }
+
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+
+ // Reference original init
+ var superInit = WordArray.init;
+
+ // Augment WordArray.init to handle typed arrays
+ var subInit = WordArray.init = function (typedArray) {
+ // Convert buffers to uint8
+ if (typedArray instanceof ArrayBuffer) {
+ typedArray = new Uint8Array(typedArray);
+ }
+
+ // Convert other array views to uint8
+ if (
+ typedArray instanceof Int8Array ||
+ (typeof Uint8ClampedArray !== "undefined" && typedArray instanceof Uint8ClampedArray) ||
+ typedArray instanceof Int16Array ||
+ typedArray instanceof Uint16Array ||
+ typedArray instanceof Int32Array ||
+ typedArray instanceof Uint32Array ||
+ typedArray instanceof Float32Array ||
+ typedArray instanceof Float64Array
+ ) {
+ typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
+ }
+
+ // Handle Uint8Array
+ if (typedArray instanceof Uint8Array) {
+ // Shortcut
+ var typedArrayByteLength = typedArray.byteLength;
+
+ // Extract bytes
+ var words = [];
+ for (var i = 0; i < typedArrayByteLength; i++) {
+ words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
+ }
+
+ // Initialize this word array
+ superInit.call(this, words, typedArrayByteLength);
+ } else {
+ // Else call normal init
+ superInit.apply(this, arguments);
+ }
+ };
+
+ subInit.prototype = WordArray;
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var C_enc = C.enc;
+
+ /**
+ * UTF-16 BE encoding strategy.
+ */
+ var Utf16BE = C_enc.Utf16 = C_enc.Utf16BE = {
+ /**
+ * Converts a word array to a UTF-16 BE string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @return {string} The UTF-16 BE string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
+ */
+ stringify: function (wordArray) {
+ // Shortcuts
+ var words = wordArray.words;
+ var sigBytes = wordArray.sigBytes;
+
+ // Convert
+ var utf16Chars = [];
+ for (var i = 0; i < sigBytes; i += 2) {
+ var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
+ utf16Chars.push(String.fromCharCode(codePoint));
+ }
+
+ return utf16Chars.join('');
+ },
+
+ /**
+ * Converts a UTF-16 BE string to a word array.
+ *
+ * @param {string} utf16Str The UTF-16 BE string.
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Utf16.parse(utf16String);
+ */
+ parse: function (utf16Str) {
+ // Shortcut
+ var utf16StrLength = utf16Str.length;
+
+ // Convert
+ var words = [];
+ for (var i = 0; i < utf16StrLength; i++) {
+ words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
+ }
+
+ return WordArray.create(words, utf16StrLength * 2);
+ }
+ };
+
+ /**
+ * UTF-16 LE encoding strategy.
+ */
+ C_enc.Utf16LE = {
+ /**
+ * Converts a word array to a UTF-16 LE string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @return {string} The UTF-16 LE string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
+ */
+ stringify: function (wordArray) {
+ // Shortcuts
+ var words = wordArray.words;
+ var sigBytes = wordArray.sigBytes;
+
+ // Convert
+ var utf16Chars = [];
+ for (var i = 0; i < sigBytes; i += 2) {
+ var codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
+ utf16Chars.push(String.fromCharCode(codePoint));
+ }
+
+ return utf16Chars.join('');
+ },
+
+ /**
+ * Converts a UTF-16 LE string to a word array.
+ *
+ * @param {string} utf16Str The UTF-16 LE string.
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
+ */
+ parse: function (utf16Str) {
+ // Shortcut
+ var utf16StrLength = utf16Str.length;
+
+ // Convert
+ var words = [];
+ for (var i = 0; i < utf16StrLength; i++) {
+ words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
+ }
+
+ return WordArray.create(words, utf16StrLength * 2);
+ }
+ };
+
+ function swapEndian(word) {
+ return ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
+ }
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var C_enc = C.enc;
+
+ /**
+ * Base64 encoding strategy.
+ */
+ var Base64 = C_enc.Base64 = {
+ /**
+ * Converts a word array to a Base64 string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @return {string} The Base64 string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
+ */
+ stringify: function (wordArray) {
+ // Shortcuts
+ var words = wordArray.words;
+ var sigBytes = wordArray.sigBytes;
+ var map = this._map;
+
+ // Clamp excess bits
+ wordArray.clamp();
+
+ // Convert
+ var base64Chars = [];
+ for (var i = 0; i < sigBytes; i += 3) {
+ var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
+ var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
+ var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
+
+ var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
+
+ for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
+ base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
+ }
+ }
+
+ // Add padding
+ var paddingChar = map.charAt(64);
+ if (paddingChar) {
+ while (base64Chars.length % 4) {
+ base64Chars.push(paddingChar);
+ }
+ }
+
+ return base64Chars.join('');
+ },
+
+ /**
+ * Converts a Base64 string to a word array.
+ *
+ * @param {string} base64Str The Base64 string.
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Base64.parse(base64String);
+ */
+ parse: function (base64Str) {
+ // Shortcuts
+ var base64StrLength = base64Str.length;
+ var map = this._map;
+ var reverseMap = this._reverseMap;
+
+ if (!reverseMap) {
+ reverseMap = this._reverseMap = [];
+ for (var j = 0; j < map.length; j++) {
+ reverseMap[map.charCodeAt(j)] = j;
+ }
+ }
+
+ // Ignore padding
+ var paddingChar = map.charAt(64);
+ if (paddingChar) {
+ var paddingIndex = base64Str.indexOf(paddingChar);
+ if (paddingIndex !== -1) {
+ base64StrLength = paddingIndex;
+ }
+ }
+
+ // Convert
+ return parseLoop(base64Str, base64StrLength, reverseMap);
+
+ },
+
+ _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
+ };
+
+ function parseLoop(base64Str, base64StrLength, reverseMap) {
+ var words = [];
+ var nBytes = 0;
+ for (var i = 0; i < base64StrLength; i++) {
+ if (i % 4) {
+ var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
+ var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
+ var bitsCombined = bits1 | bits2;
+ words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
+ nBytes++;
+ }
+ }
+ return WordArray.create(words, nBytes);
+ }
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var C_enc = C.enc;
+
+ /**
+ * Base64url encoding strategy.
+ */
+ var Base64url = C_enc.Base64url = {
+ /**
+ * Converts a word array to a Base64url string.
+ *
+ * @param {WordArray} wordArray The word array.
+ *
+ * @param {boolean} urlSafe Whether to use url safe
+ *
+ * @return {string} The Base64url string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var base64String = CryptoJS.enc.Base64url.stringify(wordArray);
+ */
+ stringify: function (wordArray, urlSafe=true) {
+ // Shortcuts
+ var words = wordArray.words;
+ var sigBytes = wordArray.sigBytes;
+ var map = urlSafe ? this._safe_map : this._map;
+
+ // Clamp excess bits
+ wordArray.clamp();
+
+ // Convert
+ var base64Chars = [];
+ for (var i = 0; i < sigBytes; i += 3) {
+ var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
+ var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
+ var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
+
+ var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
+
+ for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
+ base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
+ }
+ }
+
+ // Add padding
+ var paddingChar = map.charAt(64);
+ if (paddingChar) {
+ while (base64Chars.length % 4) {
+ base64Chars.push(paddingChar);
+ }
+ }
+
+ return base64Chars.join('');
+ },
+
+ /**
+ * Converts a Base64url string to a word array.
+ *
+ * @param {string} base64Str The Base64url string.
+ *
+ * @param {boolean} urlSafe Whether to use url safe
+ *
+ * @return {WordArray} The word array.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var wordArray = CryptoJS.enc.Base64url.parse(base64String);
+ */
+ parse: function (base64Str, urlSafe=true) {
+ // Shortcuts
+ var base64StrLength = base64Str.length;
+ var map = urlSafe ? this._safe_map : this._map;
+ var reverseMap = this._reverseMap;
+
+ if (!reverseMap) {
+ reverseMap = this._reverseMap = [];
+ for (var j = 0; j < map.length; j++) {
+ reverseMap[map.charCodeAt(j)] = j;
+ }
+ }
+
+ // Ignore padding
+ var paddingChar = map.charAt(64);
+ if (paddingChar) {
+ var paddingIndex = base64Str.indexOf(paddingChar);
+ if (paddingIndex !== -1) {
+ base64StrLength = paddingIndex;
+ }
+ }
+
+ // Convert
+ return parseLoop(base64Str, base64StrLength, reverseMap);
+
+ },
+
+ _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
+ _safe_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
+ };
+
+ function parseLoop(base64Str, base64StrLength, reverseMap) {
+ var words = [];
+ var nBytes = 0;
+ for (var i = 0; i < base64StrLength; i++) {
+ if (i % 4) {
+ var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
+ var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
+ var bitsCombined = bits1 | bits2;
+ words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
+ nBytes++;
+ }
+ }
+ return WordArray.create(words, nBytes);
+ }
+ }());
+
+ (function (Math) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var Hasher = C_lib.Hasher;
+ var C_algo = C.algo;
+
+ // Constants table
+ var T = [];
+
+ // Compute constants
+ (function () {
+ for (var i = 0; i < 64; i++) {
+ T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
+ }
+ }());
+
+ /**
+ * MD5 hash algorithm.
+ */
+ var MD5 = C_algo.MD5 = Hasher.extend({
+ _doReset: function () {
+ this._hash = new WordArray.init([
+ 0x67452301, 0xefcdab89,
+ 0x98badcfe, 0x10325476
+ ]);
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Swap endian
+ for (var i = 0; i < 16; i++) {
+ // Shortcuts
+ var offset_i = offset + i;
+ var M_offset_i = M[offset_i];
+
+ M[offset_i] = (
+ (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
+ (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
+ );
+ }
+
+ // Shortcuts
+ var H = this._hash.words;
+
+ var M_offset_0 = M[offset + 0];
+ var M_offset_1 = M[offset + 1];
+ var M_offset_2 = M[offset + 2];
+ var M_offset_3 = M[offset + 3];
+ var M_offset_4 = M[offset + 4];
+ var M_offset_5 = M[offset + 5];
+ var M_offset_6 = M[offset + 6];
+ var M_offset_7 = M[offset + 7];
+ var M_offset_8 = M[offset + 8];
+ var M_offset_9 = M[offset + 9];
+ var M_offset_10 = M[offset + 10];
+ var M_offset_11 = M[offset + 11];
+ var M_offset_12 = M[offset + 12];
+ var M_offset_13 = M[offset + 13];
+ var M_offset_14 = M[offset + 14];
+ var M_offset_15 = M[offset + 15];
+
+ // Working varialbes
+ var a = H[0];
+ var b = H[1];
+ var c = H[2];
+ var d = H[3];
+
+ // Computation
+ a = FF(a, b, c, d, M_offset_0, 7, T[0]);
+ d = FF(d, a, b, c, M_offset_1, 12, T[1]);
+ c = FF(c, d, a, b, M_offset_2, 17, T[2]);
+ b = FF(b, c, d, a, M_offset_3, 22, T[3]);
+ a = FF(a, b, c, d, M_offset_4, 7, T[4]);
+ d = FF(d, a, b, c, M_offset_5, 12, T[5]);
+ c = FF(c, d, a, b, M_offset_6, 17, T[6]);
+ b = FF(b, c, d, a, M_offset_7, 22, T[7]);
+ a = FF(a, b, c, d, M_offset_8, 7, T[8]);
+ d = FF(d, a, b, c, M_offset_9, 12, T[9]);
+ c = FF(c, d, a, b, M_offset_10, 17, T[10]);
+ b = FF(b, c, d, a, M_offset_11, 22, T[11]);
+ a = FF(a, b, c, d, M_offset_12, 7, T[12]);
+ d = FF(d, a, b, c, M_offset_13, 12, T[13]);
+ c = FF(c, d, a, b, M_offset_14, 17, T[14]);
+ b = FF(b, c, d, a, M_offset_15, 22, T[15]);
+
+ a = GG(a, b, c, d, M_offset_1, 5, T[16]);
+ d = GG(d, a, b, c, M_offset_6, 9, T[17]);
+ c = GG(c, d, a, b, M_offset_11, 14, T[18]);
+ b = GG(b, c, d, a, M_offset_0, 20, T[19]);
+ a = GG(a, b, c, d, M_offset_5, 5, T[20]);
+ d = GG(d, a, b, c, M_offset_10, 9, T[21]);
+ c = GG(c, d, a, b, M_offset_15, 14, T[22]);
+ b = GG(b, c, d, a, M_offset_4, 20, T[23]);
+ a = GG(a, b, c, d, M_offset_9, 5, T[24]);
+ d = GG(d, a, b, c, M_offset_14, 9, T[25]);
+ c = GG(c, d, a, b, M_offset_3, 14, T[26]);
+ b = GG(b, c, d, a, M_offset_8, 20, T[27]);
+ a = GG(a, b, c, d, M_offset_13, 5, T[28]);
+ d = GG(d, a, b, c, M_offset_2, 9, T[29]);
+ c = GG(c, d, a, b, M_offset_7, 14, T[30]);
+ b = GG(b, c, d, a, M_offset_12, 20, T[31]);
+
+ a = HH(a, b, c, d, M_offset_5, 4, T[32]);
+ d = HH(d, a, b, c, M_offset_8, 11, T[33]);
+ c = HH(c, d, a, b, M_offset_11, 16, T[34]);
+ b = HH(b, c, d, a, M_offset_14, 23, T[35]);
+ a = HH(a, b, c, d, M_offset_1, 4, T[36]);
+ d = HH(d, a, b, c, M_offset_4, 11, T[37]);
+ c = HH(c, d, a, b, M_offset_7, 16, T[38]);
+ b = HH(b, c, d, a, M_offset_10, 23, T[39]);
+ a = HH(a, b, c, d, M_offset_13, 4, T[40]);
+ d = HH(d, a, b, c, M_offset_0, 11, T[41]);
+ c = HH(c, d, a, b, M_offset_3, 16, T[42]);
+ b = HH(b, c, d, a, M_offset_6, 23, T[43]);
+ a = HH(a, b, c, d, M_offset_9, 4, T[44]);
+ d = HH(d, a, b, c, M_offset_12, 11, T[45]);
+ c = HH(c, d, a, b, M_offset_15, 16, T[46]);
+ b = HH(b, c, d, a, M_offset_2, 23, T[47]);
+
+ a = II(a, b, c, d, M_offset_0, 6, T[48]);
+ d = II(d, a, b, c, M_offset_7, 10, T[49]);
+ c = II(c, d, a, b, M_offset_14, 15, T[50]);
+ b = II(b, c, d, a, M_offset_5, 21, T[51]);
+ a = II(a, b, c, d, M_offset_12, 6, T[52]);
+ d = II(d, a, b, c, M_offset_3, 10, T[53]);
+ c = II(c, d, a, b, M_offset_10, 15, T[54]);
+ b = II(b, c, d, a, M_offset_1, 21, T[55]);
+ a = II(a, b, c, d, M_offset_8, 6, T[56]);
+ d = II(d, a, b, c, M_offset_15, 10, T[57]);
+ c = II(c, d, a, b, M_offset_6, 15, T[58]);
+ b = II(b, c, d, a, M_offset_13, 21, T[59]);
+ a = II(a, b, c, d, M_offset_4, 6, T[60]);
+ d = II(d, a, b, c, M_offset_11, 10, T[61]);
+ c = II(c, d, a, b, M_offset_2, 15, T[62]);
+ b = II(b, c, d, a, M_offset_9, 21, T[63]);
+
+ // Intermediate hash value
+ H[0] = (H[0] + a) | 0;
+ H[1] = (H[1] + b) | 0;
+ H[2] = (H[2] + c) | 0;
+ H[3] = (H[3] + d) | 0;
+ },
+
+ _doFinalize: function () {
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+
+ var nBitsTotal = this._nDataBytes * 8;
+ var nBitsLeft = data.sigBytes * 8;
+
+ // Add padding
+ dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
+
+ var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
+ var nBitsTotalL = nBitsTotal;
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
+ (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
+ (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
+ );
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
+ (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
+ (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
+ );
+
+ data.sigBytes = (dataWords.length + 1) * 4;
+
+ // Hash final blocks
+ this._process();
+
+ // Shortcuts
+ var hash = this._hash;
+ var H = hash.words;
+
+ // Swap endian
+ for (var i = 0; i < 4; i++) {
+ // Shortcut
+ var H_i = H[i];
+
+ H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
+ (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
+ }
+
+ // Return final computed hash
+ return hash;
+ },
+
+ clone: function () {
+ var clone = Hasher.clone.call(this);
+ clone._hash = this._hash.clone();
+
+ return clone;
+ }
+ });
+
+ function FF(a, b, c, d, x, s, t) {
+ var n = a + ((b & c) | (~b & d)) + x + t;
+ return ((n << s) | (n >>> (32 - s))) + b;
+ }
+
+ function GG(a, b, c, d, x, s, t) {
+ var n = a + ((b & d) | (c & ~d)) + x + t;
+ return ((n << s) | (n >>> (32 - s))) + b;
+ }
+
+ function HH(a, b, c, d, x, s, t) {
+ var n = a + (b ^ c ^ d) + x + t;
+ return ((n << s) | (n >>> (32 - s))) + b;
+ }
+
+ function II(a, b, c, d, x, s, t) {
+ var n = a + (c ^ (b | ~d)) + x + t;
+ return ((n << s) | (n >>> (32 - s))) + b;
+ }
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.MD5('message');
+ * var hash = CryptoJS.MD5(wordArray);
+ */
+ C.MD5 = Hasher._createHelper(MD5);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacMD5(message, key);
+ */
+ C.HmacMD5 = Hasher._createHmacHelper(MD5);
+ }(Math));
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var Hasher = C_lib.Hasher;
+ var C_algo = C.algo;
+
+ // Reusable object
+ var W = [];
+
+ /**
+ * SHA-1 hash algorithm.
+ */
+ var SHA1 = C_algo.SHA1 = Hasher.extend({
+ _doReset: function () {
+ this._hash = new WordArray.init([
+ 0x67452301, 0xefcdab89,
+ 0x98badcfe, 0x10325476,
+ 0xc3d2e1f0
+ ]);
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Shortcut
+ var H = this._hash.words;
+
+ // Working variables
+ var a = H[0];
+ var b = H[1];
+ var c = H[2];
+ var d = H[3];
+ var e = H[4];
+
+ // Computation
+ for (var i = 0; i < 80; i++) {
+ if (i < 16) {
+ W[i] = M[offset + i] | 0;
+ } else {
+ var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
+ W[i] = (n << 1) | (n >>> 31);
+ }
+
+ var t = ((a << 5) | (a >>> 27)) + e + W[i];
+ if (i < 20) {
+ t += ((b & c) | (~b & d)) + 0x5a827999;
+ } else if (i < 40) {
+ t += (b ^ c ^ d) + 0x6ed9eba1;
+ } else if (i < 60) {
+ t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
+ } else /* if (i < 80) */ {
+ t += (b ^ c ^ d) - 0x359d3e2a;
+ }
+
+ e = d;
+ d = c;
+ c = (b << 30) | (b >>> 2);
+ b = a;
+ a = t;
+ }
+
+ // Intermediate hash value
+ H[0] = (H[0] + a) | 0;
+ H[1] = (H[1] + b) | 0;
+ H[2] = (H[2] + c) | 0;
+ H[3] = (H[3] + d) | 0;
+ H[4] = (H[4] + e) | 0;
+ },
+
+ _doFinalize: function () {
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+
+ var nBitsTotal = this._nDataBytes * 8;
+ var nBitsLeft = data.sigBytes * 8;
+
+ // Add padding
+ dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
+ data.sigBytes = dataWords.length * 4;
+
+ // Hash final blocks
+ this._process();
+
+ // Return final computed hash
+ return this._hash;
+ },
+
+ clone: function () {
+ var clone = Hasher.clone.call(this);
+ clone._hash = this._hash.clone();
+
+ return clone;
+ }
+ });
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.SHA1('message');
+ * var hash = CryptoJS.SHA1(wordArray);
+ */
+ C.SHA1 = Hasher._createHelper(SHA1);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacSHA1(message, key);
+ */
+ C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
+ }());
+
+
+ (function (Math) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var Hasher = C_lib.Hasher;
+ var C_algo = C.algo;
+
+ // Initialization and round constants tables
+ var H = [];
+ var K = [];
+
+ // Compute constants
+ (function () {
+ function isPrime(n) {
+ var sqrtN = Math.sqrt(n);
+ for (var factor = 2; factor <= sqrtN; factor++) {
+ if (!(n % factor)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ function getFractionalBits(n) {
+ return ((n - (n | 0)) * 0x100000000) | 0;
+ }
+
+ var n = 2;
+ var nPrime = 0;
+ while (nPrime < 64) {
+ if (isPrime(n)) {
+ if (nPrime < 8) {
+ H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
+ }
+ K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
+
+ nPrime++;
+ }
+
+ n++;
+ }
+ }());
+
+ // Reusable object
+ var W = [];
+
+ /**
+ * SHA-256 hash algorithm.
+ */
+ var SHA256 = C_algo.SHA256 = Hasher.extend({
+ _doReset: function () {
+ this._hash = new WordArray.init(H.slice(0));
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Shortcut
+ var H = this._hash.words;
+
+ // Working variables
+ var a = H[0];
+ var b = H[1];
+ var c = H[2];
+ var d = H[3];
+ var e = H[4];
+ var f = H[5];
+ var g = H[6];
+ var h = H[7];
+
+ // Computation
+ for (var i = 0; i < 64; i++) {
+ if (i < 16) {
+ W[i] = M[offset + i] | 0;
+ } else {
+ var gamma0x = W[i - 15];
+ var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
+ ((gamma0x << 14) | (gamma0x >>> 18)) ^
+ (gamma0x >>> 3);
+
+ var gamma1x = W[i - 2];
+ var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
+ ((gamma1x << 13) | (gamma1x >>> 19)) ^
+ (gamma1x >>> 10);
+
+ W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
+ }
+
+ var ch = (e & f) ^ (~e & g);
+ var maj = (a & b) ^ (a & c) ^ (b & c);
+
+ var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
+ var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
+
+ var t1 = h + sigma1 + ch + K[i] + W[i];
+ var t2 = sigma0 + maj;
+
+ h = g;
+ g = f;
+ f = e;
+ e = (d + t1) | 0;
+ d = c;
+ c = b;
+ b = a;
+ a = (t1 + t2) | 0;
+ }
+
+ // Intermediate hash value
+ H[0] = (H[0] + a) | 0;
+ H[1] = (H[1] + b) | 0;
+ H[2] = (H[2] + c) | 0;
+ H[3] = (H[3] + d) | 0;
+ H[4] = (H[4] + e) | 0;
+ H[5] = (H[5] + f) | 0;
+ H[6] = (H[6] + g) | 0;
+ H[7] = (H[7] + h) | 0;
+ },
+
+ _doFinalize: function () {
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+
+ var nBitsTotal = this._nDataBytes * 8;
+ var nBitsLeft = data.sigBytes * 8;
+
+ // Add padding
+ dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
+ data.sigBytes = dataWords.length * 4;
+
+ // Hash final blocks
+ this._process();
+
+ // Return final computed hash
+ return this._hash;
+ },
+
+ clone: function () {
+ var clone = Hasher.clone.call(this);
+ clone._hash = this._hash.clone();
+
+ return clone;
+ }
+ });
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.SHA256('message');
+ * var hash = CryptoJS.SHA256(wordArray);
+ */
+ C.SHA256 = Hasher._createHelper(SHA256);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacSHA256(message, key);
+ */
+ C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
+ }(Math));
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var C_algo = C.algo;
+ var SHA256 = C_algo.SHA256;
+
+ /**
+ * SHA-224 hash algorithm.
+ */
+ var SHA224 = C_algo.SHA224 = SHA256.extend({
+ _doReset: function () {
+ this._hash = new WordArray.init([
+ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
+ 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
+ ]);
+ },
+
+ _doFinalize: function () {
+ var hash = SHA256._doFinalize.call(this);
+
+ hash.sigBytes -= 4;
+
+ return hash;
+ }
+ });
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.SHA224('message');
+ * var hash = CryptoJS.SHA224(wordArray);
+ */
+ C.SHA224 = SHA256._createHelper(SHA224);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacSHA224(message, key);
+ */
+ C.HmacSHA224 = SHA256._createHmacHelper(SHA224);
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var Hasher = C_lib.Hasher;
+ var C_x64 = C.x64;
+ var X64Word = C_x64.Word;
+ var X64WordArray = C_x64.WordArray;
+ var C_algo = C.algo;
+
+ function X64Word_create() {
+ return X64Word.create.apply(X64Word, arguments);
+ }
+
+ // Constants
+ var K = [
+ X64Word_create(0x428a2f98, 0xd728ae22), X64Word_create(0x71374491, 0x23ef65cd),
+ X64Word_create(0xb5c0fbcf, 0xec4d3b2f), X64Word_create(0xe9b5dba5, 0x8189dbbc),
+ X64Word_create(0x3956c25b, 0xf348b538), X64Word_create(0x59f111f1, 0xb605d019),
+ X64Word_create(0x923f82a4, 0xaf194f9b), X64Word_create(0xab1c5ed5, 0xda6d8118),
+ X64Word_create(0xd807aa98, 0xa3030242), X64Word_create(0x12835b01, 0x45706fbe),
+ X64Word_create(0x243185be, 0x4ee4b28c), X64Word_create(0x550c7dc3, 0xd5ffb4e2),
+ X64Word_create(0x72be5d74, 0xf27b896f), X64Word_create(0x80deb1fe, 0x3b1696b1),
+ X64Word_create(0x9bdc06a7, 0x25c71235), X64Word_create(0xc19bf174, 0xcf692694),
+ X64Word_create(0xe49b69c1, 0x9ef14ad2), X64Word_create(0xefbe4786, 0x384f25e3),
+ X64Word_create(0x0fc19dc6, 0x8b8cd5b5), X64Word_create(0x240ca1cc, 0x77ac9c65),
+ X64Word_create(0x2de92c6f, 0x592b0275), X64Word_create(0x4a7484aa, 0x6ea6e483),
+ X64Word_create(0x5cb0a9dc, 0xbd41fbd4), X64Word_create(0x76f988da, 0x831153b5),
+ X64Word_create(0x983e5152, 0xee66dfab), X64Word_create(0xa831c66d, 0x2db43210),
+ X64Word_create(0xb00327c8, 0x98fb213f), X64Word_create(0xbf597fc7, 0xbeef0ee4),
+ X64Word_create(0xc6e00bf3, 0x3da88fc2), X64Word_create(0xd5a79147, 0x930aa725),
+ X64Word_create(0x06ca6351, 0xe003826f), X64Word_create(0x14292967, 0x0a0e6e70),
+ X64Word_create(0x27b70a85, 0x46d22ffc), X64Word_create(0x2e1b2138, 0x5c26c926),
+ X64Word_create(0x4d2c6dfc, 0x5ac42aed), X64Word_create(0x53380d13, 0x9d95b3df),
+ X64Word_create(0x650a7354, 0x8baf63de), X64Word_create(0x766a0abb, 0x3c77b2a8),
+ X64Word_create(0x81c2c92e, 0x47edaee6), X64Word_create(0x92722c85, 0x1482353b),
+ X64Word_create(0xa2bfe8a1, 0x4cf10364), X64Word_create(0xa81a664b, 0xbc423001),
+ X64Word_create(0xc24b8b70, 0xd0f89791), X64Word_create(0xc76c51a3, 0x0654be30),
+ X64Word_create(0xd192e819, 0xd6ef5218), X64Word_create(0xd6990624, 0x5565a910),
+ X64Word_create(0xf40e3585, 0x5771202a), X64Word_create(0x106aa070, 0x32bbd1b8),
+ X64Word_create(0x19a4c116, 0xb8d2d0c8), X64Word_create(0x1e376c08, 0x5141ab53),
+ X64Word_create(0x2748774c, 0xdf8eeb99), X64Word_create(0x34b0bcb5, 0xe19b48a8),
+ X64Word_create(0x391c0cb3, 0xc5c95a63), X64Word_create(0x4ed8aa4a, 0xe3418acb),
+ X64Word_create(0x5b9cca4f, 0x7763e373), X64Word_create(0x682e6ff3, 0xd6b2b8a3),
+ X64Word_create(0x748f82ee, 0x5defb2fc), X64Word_create(0x78a5636f, 0x43172f60),
+ X64Word_create(0x84c87814, 0xa1f0ab72), X64Word_create(0x8cc70208, 0x1a6439ec),
+ X64Word_create(0x90befffa, 0x23631e28), X64Word_create(0xa4506ceb, 0xde82bde9),
+ X64Word_create(0xbef9a3f7, 0xb2c67915), X64Word_create(0xc67178f2, 0xe372532b),
+ X64Word_create(0xca273ece, 0xea26619c), X64Word_create(0xd186b8c7, 0x21c0c207),
+ X64Word_create(0xeada7dd6, 0xcde0eb1e), X64Word_create(0xf57d4f7f, 0xee6ed178),
+ X64Word_create(0x06f067aa, 0x72176fba), X64Word_create(0x0a637dc5, 0xa2c898a6),
+ X64Word_create(0x113f9804, 0xbef90dae), X64Word_create(0x1b710b35, 0x131c471b),
+ X64Word_create(0x28db77f5, 0x23047d84), X64Word_create(0x32caab7b, 0x40c72493),
+ X64Word_create(0x3c9ebe0a, 0x15c9bebc), X64Word_create(0x431d67c4, 0x9c100d4c),
+ X64Word_create(0x4cc5d4be, 0xcb3e42b6), X64Word_create(0x597f299c, 0xfc657e2a),
+ X64Word_create(0x5fcb6fab, 0x3ad6faec), X64Word_create(0x6c44198c, 0x4a475817)
+ ];
+
+ // Reusable objects
+ var W = [];
+ (function () {
+ for (var i = 0; i < 80; i++) {
+ W[i] = X64Word_create();
+ }
+ }());
+
+ /**
+ * SHA-512 hash algorithm.
+ */
+ var SHA512 = C_algo.SHA512 = Hasher.extend({
+ _doReset: function () {
+ this._hash = new X64WordArray.init([
+ new X64Word.init(0x6a09e667, 0xf3bcc908), new X64Word.init(0xbb67ae85, 0x84caa73b),
+ new X64Word.init(0x3c6ef372, 0xfe94f82b), new X64Word.init(0xa54ff53a, 0x5f1d36f1),
+ new X64Word.init(0x510e527f, 0xade682d1), new X64Word.init(0x9b05688c, 0x2b3e6c1f),
+ new X64Word.init(0x1f83d9ab, 0xfb41bd6b), new X64Word.init(0x5be0cd19, 0x137e2179)
+ ]);
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Shortcuts
+ var H = this._hash.words;
+
+ var H0 = H[0];
+ var H1 = H[1];
+ var H2 = H[2];
+ var H3 = H[3];
+ var H4 = H[4];
+ var H5 = H[5];
+ var H6 = H[6];
+ var H7 = H[7];
+
+ var H0h = H0.high;
+ var H0l = H0.low;
+ var H1h = H1.high;
+ var H1l = H1.low;
+ var H2h = H2.high;
+ var H2l = H2.low;
+ var H3h = H3.high;
+ var H3l = H3.low;
+ var H4h = H4.high;
+ var H4l = H4.low;
+ var H5h = H5.high;
+ var H5l = H5.low;
+ var H6h = H6.high;
+ var H6l = H6.low;
+ var H7h = H7.high;
+ var H7l = H7.low;
+
+ // Working variables
+ var ah = H0h;
+ var al = H0l;
+ var bh = H1h;
+ var bl = H1l;
+ var ch = H2h;
+ var cl = H2l;
+ var dh = H3h;
+ var dl = H3l;
+ var eh = H4h;
+ var el = H4l;
+ var fh = H5h;
+ var fl = H5l;
+ var gh = H6h;
+ var gl = H6l;
+ var hh = H7h;
+ var hl = H7l;
+
+ // Rounds
+ for (var i = 0; i < 80; i++) {
+ var Wil;
+ var Wih;
+
+ // Shortcut
+ var Wi = W[i];
+
+ // Extend message
+ if (i < 16) {
+ Wih = Wi.high = M[offset + i * 2] | 0;
+ Wil = Wi.low = M[offset + i * 2 + 1] | 0;
+ } else {
+ // Gamma0
+ var gamma0x = W[i - 15];
+ var gamma0xh = gamma0x.high;
+ var gamma0xl = gamma0x.low;
+ var gamma0h = ((gamma0xh >>> 1) | (gamma0xl << 31)) ^ ((gamma0xh >>> 8) | (gamma0xl << 24)) ^ (gamma0xh >>> 7);
+ var gamma0l = ((gamma0xl >>> 1) | (gamma0xh << 31)) ^ ((gamma0xl >>> 8) | (gamma0xh << 24)) ^ ((gamma0xl >>> 7) | (gamma0xh << 25));
+
+ // Gamma1
+ var gamma1x = W[i - 2];
+ var gamma1xh = gamma1x.high;
+ var gamma1xl = gamma1x.low;
+ var gamma1h = ((gamma1xh >>> 19) | (gamma1xl << 13)) ^ ((gamma1xh << 3) | (gamma1xl >>> 29)) ^ (gamma1xh >>> 6);
+ var gamma1l = ((gamma1xl >>> 19) | (gamma1xh << 13)) ^ ((gamma1xl << 3) | (gamma1xh >>> 29)) ^ ((gamma1xl >>> 6) | (gamma1xh << 26));
+
+ // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
+ var Wi7 = W[i - 7];
+ var Wi7h = Wi7.high;
+ var Wi7l = Wi7.low;
+
+ var Wi16 = W[i - 16];
+ var Wi16h = Wi16.high;
+ var Wi16l = Wi16.low;
+
+ Wil = gamma0l + Wi7l;
+ Wih = gamma0h + Wi7h + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0);
+ Wil = Wil + gamma1l;
+ Wih = Wih + gamma1h + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0);
+ Wil = Wil + Wi16l;
+ Wih = Wih + Wi16h + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0);
+
+ Wi.high = Wih;
+ Wi.low = Wil;
+ }
+
+ var chh = (eh & fh) ^ (~eh & gh);
+ var chl = (el & fl) ^ (~el & gl);
+ var majh = (ah & bh) ^ (ah & ch) ^ (bh & ch);
+ var majl = (al & bl) ^ (al & cl) ^ (bl & cl);
+
+ var sigma0h = ((ah >>> 28) | (al << 4)) ^ ((ah << 30) | (al >>> 2)) ^ ((ah << 25) | (al >>> 7));
+ var sigma0l = ((al >>> 28) | (ah << 4)) ^ ((al << 30) | (ah >>> 2)) ^ ((al << 25) | (ah >>> 7));
+ var sigma1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((eh << 23) | (el >>> 9));
+ var sigma1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((el << 23) | (eh >>> 9));
+
+ // t1 = h + sigma1 + ch + K[i] + W[i]
+ var Ki = K[i];
+ var Kih = Ki.high;
+ var Kil = Ki.low;
+
+ var t1l = hl + sigma1l;
+ var t1h = hh + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0);
+ var t1l = t1l + chl;
+ var t1h = t1h + chh + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0);
+ var t1l = t1l + Kil;
+ var t1h = t1h + Kih + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0);
+ var t1l = t1l + Wil;
+ var t1h = t1h + Wih + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0);
+
+ // t2 = sigma0 + maj
+ var t2l = sigma0l + majl;
+ var t2h = sigma0h + majh + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0);
+
+ // Update working variables
+ hh = gh;
+ hl = gl;
+ gh = fh;
+ gl = fl;
+ fh = eh;
+ fl = el;
+ el = (dl + t1l) | 0;
+ eh = (dh + t1h + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0;
+ dh = ch;
+ dl = cl;
+ ch = bh;
+ cl = bl;
+ bh = ah;
+ bl = al;
+ al = (t1l + t2l) | 0;
+ ah = (t1h + t2h + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0;
+ }
+
+ // Intermediate hash value
+ H0l = H0.low = (H0l + al);
+ H0.high = (H0h + ah + ((H0l >>> 0) < (al >>> 0) ? 1 : 0));
+ H1l = H1.low = (H1l + bl);
+ H1.high = (H1h + bh + ((H1l >>> 0) < (bl >>> 0) ? 1 : 0));
+ H2l = H2.low = (H2l + cl);
+ H2.high = (H2h + ch + ((H2l >>> 0) < (cl >>> 0) ? 1 : 0));
+ H3l = H3.low = (H3l + dl);
+ H3.high = (H3h + dh + ((H3l >>> 0) < (dl >>> 0) ? 1 : 0));
+ H4l = H4.low = (H4l + el);
+ H4.high = (H4h + eh + ((H4l >>> 0) < (el >>> 0) ? 1 : 0));
+ H5l = H5.low = (H5l + fl);
+ H5.high = (H5h + fh + ((H5l >>> 0) < (fl >>> 0) ? 1 : 0));
+ H6l = H6.low = (H6l + gl);
+ H6.high = (H6h + gh + ((H6l >>> 0) < (gl >>> 0) ? 1 : 0));
+ H7l = H7.low = (H7l + hl);
+ H7.high = (H7h + hh + ((H7l >>> 0) < (hl >>> 0) ? 1 : 0));
+ },
+
+ _doFinalize: function () {
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+
+ var nBitsTotal = this._nDataBytes * 8;
+ var nBitsLeft = data.sigBytes * 8;
+
+ // Add padding
+ dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
+ dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000);
+ dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 31] = nBitsTotal;
+ data.sigBytes = dataWords.length * 4;
+
+ // Hash final blocks
+ this._process();
+
+ // Convert hash to 32-bit word array before returning
+ var hash = this._hash.toX32();
+
+ // Return final computed hash
+ return hash;
+ },
+
+ clone: function () {
+ var clone = Hasher.clone.call(this);
+ clone._hash = this._hash.clone();
+
+ return clone;
+ },
+
+ blockSize: 1024/32
+ });
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.SHA512('message');
+ * var hash = CryptoJS.SHA512(wordArray);
+ */
+ C.SHA512 = Hasher._createHelper(SHA512);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacSHA512(message, key);
+ */
+ C.HmacSHA512 = Hasher._createHmacHelper(SHA512);
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_x64 = C.x64;
+ var X64Word = C_x64.Word;
+ var X64WordArray = C_x64.WordArray;
+ var C_algo = C.algo;
+ var SHA512 = C_algo.SHA512;
+
+ /**
+ * SHA-384 hash algorithm.
+ */
+ var SHA384 = C_algo.SHA384 = SHA512.extend({
+ _doReset: function () {
+ this._hash = new X64WordArray.init([
+ new X64Word.init(0xcbbb9d5d, 0xc1059ed8), new X64Word.init(0x629a292a, 0x367cd507),
+ new X64Word.init(0x9159015a, 0x3070dd17), new X64Word.init(0x152fecd8, 0xf70e5939),
+ new X64Word.init(0x67332667, 0xffc00b31), new X64Word.init(0x8eb44a87, 0x68581511),
+ new X64Word.init(0xdb0c2e0d, 0x64f98fa7), new X64Word.init(0x47b5481d, 0xbefa4fa4)
+ ]);
+ },
+
+ _doFinalize: function () {
+ var hash = SHA512._doFinalize.call(this);
+
+ hash.sigBytes -= 16;
+
+ return hash;
+ }
+ });
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.SHA384('message');
+ * var hash = CryptoJS.SHA384(wordArray);
+ */
+ C.SHA384 = SHA512._createHelper(SHA384);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacSHA384(message, key);
+ */
+ C.HmacSHA384 = SHA512._createHmacHelper(SHA384);
+ }());
+
+
+ (function (Math) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var Hasher = C_lib.Hasher;
+ var C_x64 = C.x64;
+ var X64Word = C_x64.Word;
+ var C_algo = C.algo;
+
+ // Constants tables
+ var RHO_OFFSETS = [];
+ var PI_INDEXES = [];
+ var ROUND_CONSTANTS = [];
+
+ // Compute Constants
+ (function () {
+ // Compute rho offset constants
+ var x = 1, y = 0;
+ for (var t = 0; t < 24; t++) {
+ RHO_OFFSETS[x + 5 * y] = ((t + 1) * (t + 2) / 2) % 64;
+
+ var newX = y % 5;
+ var newY = (2 * x + 3 * y) % 5;
+ x = newX;
+ y = newY;
+ }
+
+ // Compute pi index constants
+ for (var x = 0; x < 5; x++) {
+ for (var y = 0; y < 5; y++) {
+ PI_INDEXES[x + 5 * y] = y + ((2 * x + 3 * y) % 5) * 5;
+ }
+ }
+
+ // Compute round constants
+ var LFSR = 0x01;
+ for (var i = 0; i < 24; i++) {
+ var roundConstantMsw = 0;
+ var roundConstantLsw = 0;
+
+ for (var j = 0; j < 7; j++) {
+ if (LFSR & 0x01) {
+ var bitPosition = (1 << j) - 1;
+ if (bitPosition < 32) {
+ roundConstantLsw ^= 1 << bitPosition;
+ } else /* if (bitPosition >= 32) */ {
+ roundConstantMsw ^= 1 << (bitPosition - 32);
+ }
+ }
+
+ // Compute next LFSR
+ if (LFSR & 0x80) {
+ // Primitive polynomial over GF(2): x^8 + x^6 + x^5 + x^4 + 1
+ LFSR = (LFSR << 1) ^ 0x71;
+ } else {
+ LFSR <<= 1;
+ }
+ }
+
+ ROUND_CONSTANTS[i] = X64Word.create(roundConstantMsw, roundConstantLsw);
+ }
+ }());
+
+ // Reusable objects for temporary values
+ var T = [];
+ (function () {
+ for (var i = 0; i < 25; i++) {
+ T[i] = X64Word.create();
+ }
+ }());
+
+ /**
+ * SHA-3 hash algorithm.
+ */
+ var SHA3 = C_algo.SHA3 = Hasher.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {number} outputLength
+ * The desired number of bits in the output hash.
+ * Only values permitted are: 224, 256, 384, 512.
+ * Default: 512
+ */
+ cfg: Hasher.cfg.extend({
+ outputLength: 512
+ }),
+
+ _doReset: function () {
+ var state = this._state = []
+ for (var i = 0; i < 25; i++) {
+ state[i] = new X64Word.init();
+ }
+
+ this.blockSize = (1600 - 2 * this.cfg.outputLength) / 32;
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Shortcuts
+ var state = this._state;
+ var nBlockSizeLanes = this.blockSize / 2;
+
+ // Absorb
+ for (var i = 0; i < nBlockSizeLanes; i++) {
+ // Shortcuts
+ var M2i = M[offset + 2 * i];
+ var M2i1 = M[offset + 2 * i + 1];
+
+ // Swap endian
+ M2i = (
+ (((M2i << 8) | (M2i >>> 24)) & 0x00ff00ff) |
+ (((M2i << 24) | (M2i >>> 8)) & 0xff00ff00)
+ );
+ M2i1 = (
+ (((M2i1 << 8) | (M2i1 >>> 24)) & 0x00ff00ff) |
+ (((M2i1 << 24) | (M2i1 >>> 8)) & 0xff00ff00)
+ );
+
+ // Absorb message into state
+ var lane = state[i];
+ lane.high ^= M2i1;
+ lane.low ^= M2i;
+ }
+
+ // Rounds
+ for (var round = 0; round < 24; round++) {
+ // Theta
+ for (var x = 0; x < 5; x++) {
+ // Mix column lanes
+ var tMsw = 0, tLsw = 0;
+ for (var y = 0; y < 5; y++) {
+ var lane = state[x + 5 * y];
+ tMsw ^= lane.high;
+ tLsw ^= lane.low;
+ }
+
+ // Temporary values
+ var Tx = T[x];
+ Tx.high = tMsw;
+ Tx.low = tLsw;
+ }
+ for (var x = 0; x < 5; x++) {
+ // Shortcuts
+ var Tx4 = T[(x + 4) % 5];
+ var Tx1 = T[(x + 1) % 5];
+ var Tx1Msw = Tx1.high;
+ var Tx1Lsw = Tx1.low;
+
+ // Mix surrounding columns
+ var tMsw = Tx4.high ^ ((Tx1Msw << 1) | (Tx1Lsw >>> 31));
+ var tLsw = Tx4.low ^ ((Tx1Lsw << 1) | (Tx1Msw >>> 31));
+ for (var y = 0; y < 5; y++) {
+ var lane = state[x + 5 * y];
+ lane.high ^= tMsw;
+ lane.low ^= tLsw;
+ }
+ }
+
+ // Rho Pi
+ for (var laneIndex = 1; laneIndex < 25; laneIndex++) {
+ var tMsw;
+ var tLsw;
+
+ // Shortcuts
+ var lane = state[laneIndex];
+ var laneMsw = lane.high;
+ var laneLsw = lane.low;
+ var rhoOffset = RHO_OFFSETS[laneIndex];
+
+ // Rotate lanes
+ if (rhoOffset < 32) {
+ tMsw = (laneMsw << rhoOffset) | (laneLsw >>> (32 - rhoOffset));
+ tLsw = (laneLsw << rhoOffset) | (laneMsw >>> (32 - rhoOffset));
+ } else /* if (rhoOffset >= 32) */ {
+ tMsw = (laneLsw << (rhoOffset - 32)) | (laneMsw >>> (64 - rhoOffset));
+ tLsw = (laneMsw << (rhoOffset - 32)) | (laneLsw >>> (64 - rhoOffset));
+ }
+
+ // Transpose lanes
+ var TPiLane = T[PI_INDEXES[laneIndex]];
+ TPiLane.high = tMsw;
+ TPiLane.low = tLsw;
+ }
+
+ // Rho pi at x = y = 0
+ var T0 = T[0];
+ var state0 = state[0];
+ T0.high = state0.high;
+ T0.low = state0.low;
+
+ // Chi
+ for (var x = 0; x < 5; x++) {
+ for (var y = 0; y < 5; y++) {
+ // Shortcuts
+ var laneIndex = x + 5 * y;
+ var lane = state[laneIndex];
+ var TLane = T[laneIndex];
+ var Tx1Lane = T[((x + 1) % 5) + 5 * y];
+ var Tx2Lane = T[((x + 2) % 5) + 5 * y];
+
+ // Mix rows
+ lane.high = TLane.high ^ (~Tx1Lane.high & Tx2Lane.high);
+ lane.low = TLane.low ^ (~Tx1Lane.low & Tx2Lane.low);
+ }
+ }
+
+ // Iota
+ var lane = state[0];
+ var roundConstant = ROUND_CONSTANTS[round];
+ lane.high ^= roundConstant.high;
+ lane.low ^= roundConstant.low;
+ }
+ },
+
+ _doFinalize: function () {
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+ var nBitsTotal = this._nDataBytes * 8;
+ var nBitsLeft = data.sigBytes * 8;
+ var blockSizeBits = this.blockSize * 32;
+
+ // Add padding
+ dataWords[nBitsLeft >>> 5] |= 0x1 << (24 - nBitsLeft % 32);
+ dataWords[((Math.ceil((nBitsLeft + 1) / blockSizeBits) * blockSizeBits) >>> 5) - 1] |= 0x80;
+ data.sigBytes = dataWords.length * 4;
+
+ // Hash final blocks
+ this._process();
+
+ // Shortcuts
+ var state = this._state;
+ var outputLengthBytes = this.cfg.outputLength / 8;
+ var outputLengthLanes = outputLengthBytes / 8;
+
+ // Squeeze
+ var hashWords = [];
+ for (var i = 0; i < outputLengthLanes; i++) {
+ // Shortcuts
+ var lane = state[i];
+ var laneMsw = lane.high;
+ var laneLsw = lane.low;
+
+ // Swap endian
+ laneMsw = (
+ (((laneMsw << 8) | (laneMsw >>> 24)) & 0x00ff00ff) |
+ (((laneMsw << 24) | (laneMsw >>> 8)) & 0xff00ff00)
+ );
+ laneLsw = (
+ (((laneLsw << 8) | (laneLsw >>> 24)) & 0x00ff00ff) |
+ (((laneLsw << 24) | (laneLsw >>> 8)) & 0xff00ff00)
+ );
+
+ // Squeeze state to retrieve hash
+ hashWords.push(laneLsw);
+ hashWords.push(laneMsw);
+ }
+
+ // Return final computed hash
+ return new WordArray.init(hashWords, outputLengthBytes);
+ },
+
+ clone: function () {
+ var clone = Hasher.clone.call(this);
+
+ var state = clone._state = this._state.slice(0);
+ for (var i = 0; i < 25; i++) {
+ state[i] = state[i].clone();
+ }
+
+ return clone;
+ }
+ });
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.SHA3('message');
+ * var hash = CryptoJS.SHA3(wordArray);
+ */
+ C.SHA3 = Hasher._createHelper(SHA3);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacSHA3(message, key);
+ */
+ C.HmacSHA3 = Hasher._createHmacHelper(SHA3);
+ }(Math));
+
+
+ /** @preserve
+ (c) 2012 by Cédric Mesnil. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+ (function (Math) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var Hasher = C_lib.Hasher;
+ var C_algo = C.algo;
+
+ // Constants table
+ var _zl = WordArray.create([
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
+ 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
+ 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
+ 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
+ 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]);
+ var _zr = WordArray.create([
+ 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
+ 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
+ 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
+ 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
+ 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]);
+ var _sl = WordArray.create([
+ 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
+ 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
+ 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
+ 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
+ 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 ]);
+ var _sr = WordArray.create([
+ 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
+ 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
+ 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
+ 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
+ 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 ]);
+
+ var _hl = WordArray.create([ 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]);
+ var _hr = WordArray.create([ 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]);
+
+ /**
+ * RIPEMD160 hash algorithm.
+ */
+ var RIPEMD160 = C_algo.RIPEMD160 = Hasher.extend({
+ _doReset: function () {
+ this._hash = WordArray.create([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]);
+ },
+
+ _doProcessBlock: function (M, offset) {
+
+ // Swap endian
+ for (var i = 0; i < 16; i++) {
+ // Shortcuts
+ var offset_i = offset + i;
+ var M_offset_i = M[offset_i];
+
+ // Swap
+ M[offset_i] = (
+ (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
+ (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
+ );
+ }
+ // Shortcut
+ var H = this._hash.words;
+ var hl = _hl.words;
+ var hr = _hr.words;
+ var zl = _zl.words;
+ var zr = _zr.words;
+ var sl = _sl.words;
+ var sr = _sr.words;
+
+ // Working variables
+ var al, bl, cl, dl, el;
+ var ar, br, cr, dr, er;
+
+ ar = al = H[0];
+ br = bl = H[1];
+ cr = cl = H[2];
+ dr = dl = H[3];
+ er = el = H[4];
+ // Computation
+ var t;
+ for (var i = 0; i < 80; i += 1) {
+ t = (al + M[offset+zl[i]])|0;
+ if (i<16){
+ t += f1(bl,cl,dl) + hl[0];
+ } else if (i<32) {
+ t += f2(bl,cl,dl) + hl[1];
+ } else if (i<48) {
+ t += f3(bl,cl,dl) + hl[2];
+ } else if (i<64) {
+ t += f4(bl,cl,dl) + hl[3];
+ } else {// if (i<80) {
+ t += f5(bl,cl,dl) + hl[4];
+ }
+ t = t|0;
+ t = rotl(t,sl[i]);
+ t = (t+el)|0;
+ al = el;
+ el = dl;
+ dl = rotl(cl, 10);
+ cl = bl;
+ bl = t;
+
+ t = (ar + M[offset+zr[i]])|0;
+ if (i<16){
+ t += f5(br,cr,dr) + hr[0];
+ } else if (i<32) {
+ t += f4(br,cr,dr) + hr[1];
+ } else if (i<48) {
+ t += f3(br,cr,dr) + hr[2];
+ } else if (i<64) {
+ t += f2(br,cr,dr) + hr[3];
+ } else {// if (i<80) {
+ t += f1(br,cr,dr) + hr[4];
+ }
+ t = t|0;
+ t = rotl(t,sr[i]) ;
+ t = (t+er)|0;
+ ar = er;
+ er = dr;
+ dr = rotl(cr, 10);
+ cr = br;
+ br = t;
+ }
+ // Intermediate hash value
+ t = (H[1] + cl + dr)|0;
+ H[1] = (H[2] + dl + er)|0;
+ H[2] = (H[3] + el + ar)|0;
+ H[3] = (H[4] + al + br)|0;
+ H[4] = (H[0] + bl + cr)|0;
+ H[0] = t;
+ },
+
+ _doFinalize: function () {
+ // Shortcuts
+ var data = this._data;
+ var dataWords = data.words;
+
+ var nBitsTotal = this._nDataBytes * 8;
+ var nBitsLeft = data.sigBytes * 8;
+
+ // Add padding
+ dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
+ dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
+ (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
+ (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
+ );
+ data.sigBytes = (dataWords.length + 1) * 4;
+
+ // Hash final blocks
+ this._process();
+
+ // Shortcuts
+ var hash = this._hash;
+ var H = hash.words;
+
+ // Swap endian
+ for (var i = 0; i < 5; i++) {
+ // Shortcut
+ var H_i = H[i];
+
+ // Swap
+ H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
+ (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
+ }
+
+ // Return final computed hash
+ return hash;
+ },
+
+ clone: function () {
+ var clone = Hasher.clone.call(this);
+ clone._hash = this._hash.clone();
+
+ return clone;
+ }
+ });
+
+
+ function f1(x, y, z) {
+ return ((x) ^ (y) ^ (z));
+
+ }
+
+ function f2(x, y, z) {
+ return (((x)&(y)) | ((~x)&(z)));
+ }
+
+ function f3(x, y, z) {
+ return (((x) | (~(y))) ^ (z));
+ }
+
+ function f4(x, y, z) {
+ return (((x) & (z)) | ((y)&(~(z))));
+ }
+
+ function f5(x, y, z) {
+ return ((x) ^ ((y) |(~(z))));
+
+ }
+
+ function rotl(x,n) {
+ return (x<>>(32-n));
+ }
+
+
+ /**
+ * Shortcut function to the hasher's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ *
+ * @return {WordArray} The hash.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hash = CryptoJS.RIPEMD160('message');
+ * var hash = CryptoJS.RIPEMD160(wordArray);
+ */
+ C.RIPEMD160 = Hasher._createHelper(RIPEMD160);
+
+ /**
+ * Shortcut function to the HMAC's object interface.
+ *
+ * @param {WordArray|string} message The message to hash.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hmac = CryptoJS.HmacRIPEMD160(message, key);
+ */
+ C.HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160);
+ }(Math));
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var Base = C_lib.Base;
+ var C_enc = C.enc;
+ var Utf8 = C_enc.Utf8;
+ var C_algo = C.algo;
+
+ /**
+ * HMAC algorithm.
+ */
+ var HMAC = C_algo.HMAC = Base.extend({
+ /**
+ * Initializes a newly created HMAC.
+ *
+ * @param {Hasher} hasher The hash algorithm to use.
+ * @param {WordArray|string} key The secret key.
+ *
+ * @example
+ *
+ * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
+ */
+ init: function (hasher, key) {
+ // Init hasher
+ hasher = this._hasher = new hasher.init();
+
+ // Convert string to WordArray, else assume WordArray already
+ if (typeof key == 'string') {
+ key = Utf8.parse(key);
+ }
+
+ // Shortcuts
+ var hasherBlockSize = hasher.blockSize;
+ var hasherBlockSizeBytes = hasherBlockSize * 4;
+
+ // Allow arbitrary length keys
+ if (key.sigBytes > hasherBlockSizeBytes) {
+ key = hasher.finalize(key);
+ }
+
+ // Clamp excess bits
+ key.clamp();
+
+ // Clone key for inner and outer pads
+ var oKey = this._oKey = key.clone();
+ var iKey = this._iKey = key.clone();
+
+ // Shortcuts
+ var oKeyWords = oKey.words;
+ var iKeyWords = iKey.words;
+
+ // XOR keys with pad constants
+ for (var i = 0; i < hasherBlockSize; i++) {
+ oKeyWords[i] ^= 0x5c5c5c5c;
+ iKeyWords[i] ^= 0x36363636;
+ }
+ oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
+
+ // Set initial values
+ this.reset();
+ },
+
+ /**
+ * Resets this HMAC to its initial state.
+ *
+ * @example
+ *
+ * hmacHasher.reset();
+ */
+ reset: function () {
+ // Shortcut
+ var hasher = this._hasher;
+
+ // Reset
+ hasher.reset();
+ hasher.update(this._iKey);
+ },
+
+ /**
+ * Updates this HMAC with a message.
+ *
+ * @param {WordArray|string} messageUpdate The message to append.
+ *
+ * @return {HMAC} This HMAC instance.
+ *
+ * @example
+ *
+ * hmacHasher.update('message');
+ * hmacHasher.update(wordArray);
+ */
+ update: function (messageUpdate) {
+ this._hasher.update(messageUpdate);
+
+ // Chainable
+ return this;
+ },
+
+ /**
+ * Finalizes the HMAC computation.
+ * Note that the finalize operation is effectively a destructive, read-once operation.
+ *
+ * @param {WordArray|string} messageUpdate (Optional) A final message update.
+ *
+ * @return {WordArray} The HMAC.
+ *
+ * @example
+ *
+ * var hmac = hmacHasher.finalize();
+ * var hmac = hmacHasher.finalize('message');
+ * var hmac = hmacHasher.finalize(wordArray);
+ */
+ finalize: function (messageUpdate) {
+ // Shortcut
+ var hasher = this._hasher;
+
+ // Compute HMAC
+ var innerHash = hasher.finalize(messageUpdate);
+ hasher.reset();
+ var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
+
+ return hmac;
+ }
+ });
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var Base = C_lib.Base;
+ var WordArray = C_lib.WordArray;
+ var C_algo = C.algo;
+ var SHA1 = C_algo.SHA1;
+ var HMAC = C_algo.HMAC;
+
+ /**
+ * Password-Based Key Derivation Function 2 algorithm.
+ */
+ var PBKDF2 = C_algo.PBKDF2 = Base.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
+ * @property {Hasher} hasher The hasher to use. Default: SHA1
+ * @property {number} iterations The number of iterations to perform. Default: 1
+ */
+ cfg: Base.extend({
+ keySize: 128/32,
+ hasher: SHA1,
+ iterations: 1
+ }),
+
+ /**
+ * Initializes a newly created key derivation function.
+ *
+ * @param {Object} cfg (Optional) The configuration options to use for the derivation.
+ *
+ * @example
+ *
+ * var kdf = CryptoJS.algo.PBKDF2.create();
+ * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
+ * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
+ */
+ init: function (cfg) {
+ this.cfg = this.cfg.extend(cfg);
+ },
+
+ /**
+ * Computes the Password-Based Key Derivation Function 2.
+ *
+ * @param {WordArray|string} password The password.
+ * @param {WordArray|string} salt A salt.
+ *
+ * @return {WordArray} The derived key.
+ *
+ * @example
+ *
+ * var key = kdf.compute(password, salt);
+ */
+ compute: function (password, salt) {
+ // Shortcut
+ var cfg = this.cfg;
+
+ // Init HMAC
+ var hmac = HMAC.create(cfg.hasher, password);
+
+ // Initial values
+ var derivedKey = WordArray.create();
+ var blockIndex = WordArray.create([0x00000001]);
+
+ // Shortcuts
+ var derivedKeyWords = derivedKey.words;
+ var blockIndexWords = blockIndex.words;
+ var keySize = cfg.keySize;
+ var iterations = cfg.iterations;
+
+ // Generate key
+ while (derivedKeyWords.length < keySize) {
+ var block = hmac.update(salt).finalize(blockIndex);
+ hmac.reset();
+
+ // Shortcuts
+ var blockWords = block.words;
+ var blockWordsLength = blockWords.length;
+
+ // Iterations
+ var intermediate = block;
+ for (var i = 1; i < iterations; i++) {
+ intermediate = hmac.finalize(intermediate);
+ hmac.reset();
+
+ // Shortcut
+ var intermediateWords = intermediate.words;
+
+ // XOR intermediate with block
+ for (var j = 0; j < blockWordsLength; j++) {
+ blockWords[j] ^= intermediateWords[j];
+ }
+ }
+
+ derivedKey.concat(block);
+ blockIndexWords[0]++;
+ }
+ derivedKey.sigBytes = keySize * 4;
+
+ return derivedKey;
+ }
+ });
+
+ /**
+ * Computes the Password-Based Key Derivation Function 2.
+ *
+ * @param {WordArray|string} password The password.
+ * @param {WordArray|string} salt A salt.
+ * @param {Object} cfg (Optional) The configuration options to use for this computation.
+ *
+ * @return {WordArray} The derived key.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var key = CryptoJS.PBKDF2(password, salt);
+ * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
+ * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
+ */
+ C.PBKDF2 = function (password, salt, cfg) {
+ return PBKDF2.create(cfg).compute(password, salt);
+ };
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var Base = C_lib.Base;
+ var WordArray = C_lib.WordArray;
+ var C_algo = C.algo;
+ var MD5 = C_algo.MD5;
+
+ /**
+ * This key derivation function is meant to conform with EVP_BytesToKey.
+ * www.openssl.org/docs/crypto/EVP_BytesToKey.html
+ */
+ var EvpKDF = C_algo.EvpKDF = Base.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
+ * @property {Hasher} hasher The hash algorithm to use. Default: MD5
+ * @property {number} iterations The number of iterations to perform. Default: 1
+ */
+ cfg: Base.extend({
+ keySize: 128/32,
+ hasher: MD5,
+ iterations: 1
+ }),
+
+ /**
+ * Initializes a newly created key derivation function.
+ *
+ * @param {Object} cfg (Optional) The configuration options to use for the derivation.
+ *
+ * @example
+ *
+ * var kdf = CryptoJS.algo.EvpKDF.create();
+ * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
+ * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
+ */
+ init: function (cfg) {
+ this.cfg = this.cfg.extend(cfg);
+ },
+
+ /**
+ * Derives a key from a password.
+ *
+ * @param {WordArray|string} password The password.
+ * @param {WordArray|string} salt A salt.
+ *
+ * @return {WordArray} The derived key.
+ *
+ * @example
+ *
+ * var key = kdf.compute(password, salt);
+ */
+ compute: function (password, salt) {
+ var block;
+
+ // Shortcut
+ var cfg = this.cfg;
+
+ // Init hasher
+ var hasher = cfg.hasher.create();
+
+ // Initial values
+ var derivedKey = WordArray.create();
+
+ // Shortcuts
+ var derivedKeyWords = derivedKey.words;
+ var keySize = cfg.keySize;
+ var iterations = cfg.iterations;
+
+ // Generate key
+ while (derivedKeyWords.length < keySize) {
+ if (block) {
+ hasher.update(block);
+ }
+ block = hasher.update(password).finalize(salt);
+ hasher.reset();
+
+ // Iterations
+ for (var i = 1; i < iterations; i++) {
+ block = hasher.finalize(block);
+ hasher.reset();
+ }
+
+ derivedKey.concat(block);
+ }
+ derivedKey.sigBytes = keySize * 4;
+
+ return derivedKey;
+ }
+ });
+
+ /**
+ * Derives a key from a password.
+ *
+ * @param {WordArray|string} password The password.
+ * @param {WordArray|string} salt A salt.
+ * @param {Object} cfg (Optional) The configuration options to use for this computation.
+ *
+ * @return {WordArray} The derived key.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var key = CryptoJS.EvpKDF(password, salt);
+ * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
+ * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
+ */
+ C.EvpKDF = function (password, salt, cfg) {
+ return EvpKDF.create(cfg).compute(password, salt);
+ };
+ }());
+
+
+ /**
+ * Cipher core components.
+ */
+ CryptoJS.lib.Cipher || (function (undefined) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var Base = C_lib.Base;
+ var WordArray = C_lib.WordArray;
+ var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
+ var C_enc = C.enc;
+ var Utf8 = C_enc.Utf8;
+ var Base64 = C_enc.Base64;
+ var C_algo = C.algo;
+ var EvpKDF = C_algo.EvpKDF;
+
+ /**
+ * Abstract base cipher template.
+ *
+ * @property {number} keySize This cipher's key size. Default: 4 (128 bits)
+ * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
+ * @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
+ * @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
+ */
+ var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {WordArray} iv The IV to use for this operation.
+ */
+ cfg: Base.extend(),
+
+ /**
+ * Creates this cipher in encryption mode.
+ *
+ * @param {WordArray} key The key.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @return {Cipher} A cipher instance.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
+ */
+ createEncryptor: function (key, cfg) {
+ return this.create(this._ENC_XFORM_MODE, key, cfg);
+ },
+
+ /**
+ * Creates this cipher in decryption mode.
+ *
+ * @param {WordArray} key The key.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @return {Cipher} A cipher instance.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
+ */
+ createDecryptor: function (key, cfg) {
+ return this.create(this._DEC_XFORM_MODE, key, cfg);
+ },
+
+ /**
+ * Initializes a newly created cipher.
+ *
+ * @param {number} xformMode Either the encryption or decryption transormation mode constant.
+ * @param {WordArray} key The key.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @example
+ *
+ * var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
+ */
+ init: function (xformMode, key, cfg) {
+ // Apply config defaults
+ this.cfg = this.cfg.extend(cfg);
+
+ // Store transform mode and key
+ this._xformMode = xformMode;
+ this._key = key;
+
+ // Set initial values
+ this.reset();
+ },
+
+ /**
+ * Resets this cipher to its initial state.
+ *
+ * @example
+ *
+ * cipher.reset();
+ */
+ reset: function () {
+ // Reset data buffer
+ BufferedBlockAlgorithm.reset.call(this);
+
+ // Perform concrete-cipher logic
+ this._doReset();
+ },
+
+ /**
+ * Adds data to be encrypted or decrypted.
+ *
+ * @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
+ *
+ * @return {WordArray} The data after processing.
+ *
+ * @example
+ *
+ * var encrypted = cipher.process('data');
+ * var encrypted = cipher.process(wordArray);
+ */
+ process: function (dataUpdate) {
+ // Append
+ this._append(dataUpdate);
+
+ // Process available blocks
+ return this._process();
+ },
+
+ /**
+ * Finalizes the encryption or decryption process.
+ * Note that the finalize operation is effectively a destructive, read-once operation.
+ *
+ * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
+ *
+ * @return {WordArray} The data after final processing.
+ *
+ * @example
+ *
+ * var encrypted = cipher.finalize();
+ * var encrypted = cipher.finalize('data');
+ * var encrypted = cipher.finalize(wordArray);
+ */
+ finalize: function (dataUpdate) {
+ // Final data update
+ if (dataUpdate) {
+ this._append(dataUpdate);
+ }
+
+ // Perform concrete-cipher logic
+ var finalProcessedData = this._doFinalize();
+
+ return finalProcessedData;
+ },
+
+ keySize: 128/32,
+
+ ivSize: 128/32,
+
+ _ENC_XFORM_MODE: 1,
+
+ _DEC_XFORM_MODE: 2,
+
+ /**
+ * Creates shortcut functions to a cipher's object interface.
+ *
+ * @param {Cipher} cipher The cipher to create a helper for.
+ *
+ * @return {Object} An object with encrypt and decrypt shortcut functions.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
+ */
+ _createHelper: (function () {
+ function selectCipherStrategy(key) {
+ if (typeof key == 'string') {
+ return PasswordBasedCipher;
+ } else {
+ return SerializableCipher;
+ }
+ }
+
+ return function (cipher) {
+ return {
+ encrypt: function (message, key, cfg) {
+ return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
+ },
+
+ decrypt: function (ciphertext, key, cfg) {
+ return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
+ }
+ };
+ };
+ }())
+ });
+
+ /**
+ * Abstract base stream cipher template.
+ *
+ * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
+ */
+ var StreamCipher = C_lib.StreamCipher = Cipher.extend({
+ _doFinalize: function () {
+ // Process partial blocks
+ var finalProcessedBlocks = this._process(!!'flush');
+
+ return finalProcessedBlocks;
+ },
+
+ blockSize: 1
+ });
+
+ /**
+ * Mode namespace.
+ */
+ var C_mode = C.mode = {};
+
+ /**
+ * Abstract base block cipher mode template.
+ */
+ var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
+ /**
+ * Creates this mode for encryption.
+ *
+ * @param {Cipher} cipher A block cipher instance.
+ * @param {Array} iv The IV words.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
+ */
+ createEncryptor: function (cipher, iv) {
+ return this.Encryptor.create(cipher, iv);
+ },
+
+ /**
+ * Creates this mode for decryption.
+ *
+ * @param {Cipher} cipher A block cipher instance.
+ * @param {Array} iv The IV words.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
+ */
+ createDecryptor: function (cipher, iv) {
+ return this.Decryptor.create(cipher, iv);
+ },
+
+ /**
+ * Initializes a newly created mode.
+ *
+ * @param {Cipher} cipher A block cipher instance.
+ * @param {Array} iv The IV words.
+ *
+ * @example
+ *
+ * var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
+ */
+ init: function (cipher, iv) {
+ this._cipher = cipher;
+ this._iv = iv;
+ }
+ });
+
+ /**
+ * Cipher Block Chaining mode.
+ */
+ var CBC = C_mode.CBC = (function () {
+ /**
+ * Abstract base CBC mode.
+ */
+ var CBC = BlockCipherMode.extend();
+
+ /**
+ * CBC encryptor.
+ */
+ CBC.Encryptor = CBC.extend({
+ /**
+ * Processes the data block at offset.
+ *
+ * @param {Array} words The data words to operate on.
+ * @param {number} offset The offset where the block starts.
+ *
+ * @example
+ *
+ * mode.processBlock(data.words, offset);
+ */
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher;
+ var blockSize = cipher.blockSize;
+
+ // XOR and encrypt
+ xorBlock.call(this, words, offset, blockSize);
+ cipher.encryptBlock(words, offset);
+
+ // Remember this block to use with next block
+ this._prevBlock = words.slice(offset, offset + blockSize);
+ }
+ });
+
+ /**
+ * CBC decryptor.
+ */
+ CBC.Decryptor = CBC.extend({
+ /**
+ * Processes the data block at offset.
+ *
+ * @param {Array} words The data words to operate on.
+ * @param {number} offset The offset where the block starts.
+ *
+ * @example
+ *
+ * mode.processBlock(data.words, offset);
+ */
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher;
+ var blockSize = cipher.blockSize;
+
+ // Remember this block to use with next block
+ var thisBlock = words.slice(offset, offset + blockSize);
+
+ // Decrypt and XOR
+ cipher.decryptBlock(words, offset);
+ xorBlock.call(this, words, offset, blockSize);
+
+ // This block becomes the previous block
+ this._prevBlock = thisBlock;
+ }
+ });
+
+ function xorBlock(words, offset, blockSize) {
+ var block;
+
+ // Shortcut
+ var iv = this._iv;
+
+ // Choose mixing block
+ if (iv) {
+ block = iv;
+
+ // Remove IV for subsequent blocks
+ this._iv = undefined;
+ } else {
+ block = this._prevBlock;
+ }
+
+ // XOR blocks
+ for (var i = 0; i < blockSize; i++) {
+ words[offset + i] ^= block[i];
+ }
+ }
+
+ return CBC;
+ }());
+
+ /**
+ * Padding namespace.
+ */
+ var C_pad = C.pad = {};
+
+ /**
+ * PKCS #5/7 padding strategy.
+ */
+ var Pkcs7 = C_pad.Pkcs7 = {
+ /**
+ * Pads data using the algorithm defined in PKCS #5/7.
+ *
+ * @param {WordArray} data The data to pad.
+ * @param {number} blockSize The multiple that the data should be padded to.
+ *
+ * @static
+ *
+ * @example
+ *
+ * CryptoJS.pad.Pkcs7.pad(wordArray, 4);
+ */
+ pad: function (data, blockSize) {
+ // Shortcut
+ var blockSizeBytes = blockSize * 4;
+
+ // Count padding bytes
+ var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
+
+ // Create padding word
+ var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
+
+ // Create padding
+ var paddingWords = [];
+ for (var i = 0; i < nPaddingBytes; i += 4) {
+ paddingWords.push(paddingWord);
+ }
+ var padding = WordArray.create(paddingWords, nPaddingBytes);
+
+ // Add padding
+ data.concat(padding);
+ },
+
+ /**
+ * Unpads data that had been padded using the algorithm defined in PKCS #5/7.
+ *
+ * @param {WordArray} data The data to unpad.
+ *
+ * @static
+ *
+ * @example
+ *
+ * CryptoJS.pad.Pkcs7.unpad(wordArray);
+ */
+ unpad: function (data) {
+ // Get number of padding bytes from last byte
+ var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
+
+ // Remove padding
+ data.sigBytes -= nPaddingBytes;
+ }
+ };
+
+ /**
+ * Abstract base block cipher template.
+ *
+ * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
+ */
+ var BlockCipher = C_lib.BlockCipher = Cipher.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {Mode} mode The block mode to use. Default: CBC
+ * @property {Padding} padding The padding strategy to use. Default: Pkcs7
+ */
+ cfg: Cipher.cfg.extend({
+ mode: CBC,
+ padding: Pkcs7
+ }),
+
+ reset: function () {
+ var modeCreator;
+
+ // Reset cipher
+ Cipher.reset.call(this);
+
+ // Shortcuts
+ var cfg = this.cfg;
+ var iv = cfg.iv;
+ var mode = cfg.mode;
+
+ // Reset block mode
+ if (this._xformMode == this._ENC_XFORM_MODE) {
+ modeCreator = mode.createEncryptor;
+ } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
+ modeCreator = mode.createDecryptor;
+ // Keep at least one block in the buffer for unpadding
+ this._minBufferSize = 1;
+ }
+
+ if (this._mode && this._mode.__creator == modeCreator) {
+ this._mode.init(this, iv && iv.words);
+ } else {
+ this._mode = modeCreator.call(mode, this, iv && iv.words);
+ this._mode.__creator = modeCreator;
+ }
+ },
+
+ _doProcessBlock: function (words, offset) {
+ this._mode.processBlock(words, offset);
+ },
+
+ _doFinalize: function () {
+ var finalProcessedBlocks;
+
+ // Shortcut
+ var padding = this.cfg.padding;
+
+ // Finalize
+ if (this._xformMode == this._ENC_XFORM_MODE) {
+ // Pad data
+ padding.pad(this._data, this.blockSize);
+
+ // Process final blocks
+ finalProcessedBlocks = this._process(!!'flush');
+ } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
+ // Process final blocks
+ finalProcessedBlocks = this._process(!!'flush');
+
+ // Unpad data
+ padding.unpad(finalProcessedBlocks);
+ }
+
+ return finalProcessedBlocks;
+ },
+
+ blockSize: 128/32
+ });
+
+ /**
+ * A collection of cipher parameters.
+ *
+ * @property {WordArray} ciphertext The raw ciphertext.
+ * @property {WordArray} key The key to this ciphertext.
+ * @property {WordArray} iv The IV used in the ciphering operation.
+ * @property {WordArray} salt The salt used with a key derivation function.
+ * @property {Cipher} algorithm The cipher algorithm.
+ * @property {Mode} mode The block mode used in the ciphering operation.
+ * @property {Padding} padding The padding scheme used in the ciphering operation.
+ * @property {number} blockSize The block size of the cipher.
+ * @property {Format} formatter The default formatting strategy to convert this cipher params object to a string.
+ */
+ var CipherParams = C_lib.CipherParams = Base.extend({
+ /**
+ * Initializes a newly created cipher params object.
+ *
+ * @param {Object} cipherParams An object with any of the possible cipher parameters.
+ *
+ * @example
+ *
+ * var cipherParams = CryptoJS.lib.CipherParams.create({
+ * ciphertext: ciphertextWordArray,
+ * key: keyWordArray,
+ * iv: ivWordArray,
+ * salt: saltWordArray,
+ * algorithm: CryptoJS.algo.AES,
+ * mode: CryptoJS.mode.CBC,
+ * padding: CryptoJS.pad.PKCS7,
+ * blockSize: 4,
+ * formatter: CryptoJS.format.OpenSSL
+ * });
+ */
+ init: function (cipherParams) {
+ this.mixIn(cipherParams);
+ },
+
+ /**
+ * Converts this cipher params object to a string.
+ *
+ * @param {Format} formatter (Optional) The formatting strategy to use.
+ *
+ * @return {string} The stringified cipher params.
+ *
+ * @throws Error If neither the formatter nor the default formatter is set.
+ *
+ * @example
+ *
+ * var string = cipherParams + '';
+ * var string = cipherParams.toString();
+ * var string = cipherParams.toString(CryptoJS.format.OpenSSL);
+ */
+ toString: function (formatter) {
+ return (formatter || this.formatter).stringify(this);
+ }
+ });
+
+ /**
+ * Format namespace.
+ */
+ var C_format = C.format = {};
+
+ /**
+ * OpenSSL formatting strategy.
+ */
+ var OpenSSLFormatter = C_format.OpenSSL = {
+ /**
+ * Converts a cipher params object to an OpenSSL-compatible string.
+ *
+ * @param {CipherParams} cipherParams The cipher params object.
+ *
+ * @return {string} The OpenSSL-compatible string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
+ */
+ stringify: function (cipherParams) {
+ var wordArray;
+
+ // Shortcuts
+ var ciphertext = cipherParams.ciphertext;
+ var salt = cipherParams.salt;
+
+ // Format
+ if (salt) {
+ wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
+ } else {
+ wordArray = ciphertext;
+ }
+
+ return wordArray.toString(Base64);
+ },
+
+ /**
+ * Converts an OpenSSL-compatible string to a cipher params object.
+ *
+ * @param {string} openSSLStr The OpenSSL-compatible string.
+ *
+ * @return {CipherParams} The cipher params object.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
+ */
+ parse: function (openSSLStr) {
+ var salt;
+
+ // Parse base64
+ var ciphertext = Base64.parse(openSSLStr);
+
+ // Shortcut
+ var ciphertextWords = ciphertext.words;
+
+ // Test for salt
+ if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
+ // Extract salt
+ salt = WordArray.create(ciphertextWords.slice(2, 4));
+
+ // Remove salt from ciphertext
+ ciphertextWords.splice(0, 4);
+ ciphertext.sigBytes -= 16;
+ }
+
+ return CipherParams.create({ ciphertext: ciphertext, salt: salt });
+ }
+ };
+
+ /**
+ * A cipher wrapper that returns ciphertext as a serializable cipher params object.
+ */
+ var SerializableCipher = C_lib.SerializableCipher = Base.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL
+ */
+ cfg: Base.extend({
+ format: OpenSSLFormatter
+ }),
+
+ /**
+ * Encrypts a message.
+ *
+ * @param {Cipher} cipher The cipher algorithm to use.
+ * @param {WordArray|string} message The message to encrypt.
+ * @param {WordArray} key The key.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @return {CipherParams} A cipher params object.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
+ */
+ encrypt: function (cipher, message, key, cfg) {
+ // Apply config defaults
+ cfg = this.cfg.extend(cfg);
+
+ // Encrypt
+ var encryptor = cipher.createEncryptor(key, cfg);
+ var ciphertext = encryptor.finalize(message);
+
+ // Shortcut
+ var cipherCfg = encryptor.cfg;
+
+ // Create and return serializable cipher params
+ return CipherParams.create({
+ ciphertext: ciphertext,
+ key: key,
+ iv: cipherCfg.iv,
+ algorithm: cipher,
+ mode: cipherCfg.mode,
+ padding: cipherCfg.padding,
+ blockSize: cipher.blockSize,
+ formatter: cfg.format
+ });
+ },
+
+ /**
+ * Decrypts serialized ciphertext.
+ *
+ * @param {Cipher} cipher The cipher algorithm to use.
+ * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
+ * @param {WordArray} key The key.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @return {WordArray} The plaintext.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL });
+ * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL });
+ */
+ decrypt: function (cipher, ciphertext, key, cfg) {
+ // Apply config defaults
+ cfg = this.cfg.extend(cfg);
+
+ // Convert string to CipherParams
+ ciphertext = this._parse(ciphertext, cfg.format);
+
+ // Decrypt
+ var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
+
+ return plaintext;
+ },
+
+ /**
+ * Converts serialized ciphertext to CipherParams,
+ * else assumed CipherParams already and returns ciphertext unchanged.
+ *
+ * @param {CipherParams|string} ciphertext The ciphertext.
+ * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
+ *
+ * @return {CipherParams} The unserialized ciphertext.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
+ */
+ _parse: function (ciphertext, format) {
+ if (typeof ciphertext == 'string') {
+ return format.parse(ciphertext, this);
+ } else {
+ return ciphertext;
+ }
+ }
+ });
+
+ /**
+ * Key derivation function namespace.
+ */
+ var C_kdf = C.kdf = {};
+
+ /**
+ * OpenSSL key derivation function.
+ */
+ var OpenSSLKdf = C_kdf.OpenSSL = {
+ /**
+ * Derives a key and IV from a password.
+ *
+ * @param {string} password The password to derive from.
+ * @param {number} keySize The size in words of the key to generate.
+ * @param {number} ivSize The size in words of the IV to generate.
+ * @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
+ *
+ * @return {CipherParams} A cipher params object with the key, IV, and salt.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
+ * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
+ */
+ execute: function (password, keySize, ivSize, salt) {
+ // Generate random salt
+ if (!salt) {
+ salt = WordArray.random(64/8);
+ }
+
+ // Derive key and IV
+ var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
+
+ // Separate key and IV
+ var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
+ key.sigBytes = keySize * 4;
+
+ // Return params
+ return CipherParams.create({ key: key, iv: iv, salt: salt });
+ }
+ };
+
+ /**
+ * A serializable cipher wrapper that derives the key from a password,
+ * and returns ciphertext as a serializable cipher params object.
+ */
+ var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL
+ */
+ cfg: SerializableCipher.cfg.extend({
+ kdf: OpenSSLKdf
+ }),
+
+ /**
+ * Encrypts a message using a password.
+ *
+ * @param {Cipher} cipher The cipher algorithm to use.
+ * @param {WordArray|string} message The message to encrypt.
+ * @param {string} password The password.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @return {CipherParams} A cipher params object.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password');
+ * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
+ */
+ encrypt: function (cipher, message, password, cfg) {
+ // Apply config defaults
+ cfg = this.cfg.extend(cfg);
+
+ // Derive key and other params
+ var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize);
+
+ // Add IV to config
+ cfg.iv = derivedParams.iv;
+
+ // Encrypt
+ var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
+
+ // Mix in derived params
+ ciphertext.mixIn(derivedParams);
+
+ return ciphertext;
+ },
+
+ /**
+ * Decrypts serialized ciphertext using a password.
+ *
+ * @param {Cipher} cipher The cipher algorithm to use.
+ * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
+ * @param {string} password The password.
+ * @param {Object} cfg (Optional) The configuration options to use for this operation.
+ *
+ * @return {WordArray} The plaintext.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL });
+ * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL });
+ */
+ decrypt: function (cipher, ciphertext, password, cfg) {
+ // Apply config defaults
+ cfg = this.cfg.extend(cfg);
+
+ // Convert string to CipherParams
+ ciphertext = this._parse(ciphertext, cfg.format);
+
+ // Derive key and other params
+ var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
+
+ // Add IV to config
+ cfg.iv = derivedParams.iv;
+
+ // Decrypt
+ var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
+
+ return plaintext;
+ }
+ });
+ }());
+
+
+ /**
+ * Cipher Feedback block mode.
+ */
+ CryptoJS.mode.CFB = (function () {
+ var CFB = CryptoJS.lib.BlockCipherMode.extend();
+
+ CFB.Encryptor = CFB.extend({
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher;
+ var blockSize = cipher.blockSize;
+
+ generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
+
+ // Remember this block to use with next block
+ this._prevBlock = words.slice(offset, offset + blockSize);
+ }
+ });
+
+ CFB.Decryptor = CFB.extend({
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher;
+ var blockSize = cipher.blockSize;
+
+ // Remember this block to use with next block
+ var thisBlock = words.slice(offset, offset + blockSize);
+
+ generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
+
+ // This block becomes the previous block
+ this._prevBlock = thisBlock;
+ }
+ });
+
+ function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) {
+ var keystream;
+
+ // Shortcut
+ var iv = this._iv;
+
+ // Generate keystream
+ if (iv) {
+ keystream = iv.slice(0);
+
+ // Remove IV for subsequent blocks
+ this._iv = undefined;
+ } else {
+ keystream = this._prevBlock;
+ }
+ cipher.encryptBlock(keystream, 0);
+
+ // Encrypt
+ for (var i = 0; i < blockSize; i++) {
+ words[offset + i] ^= keystream[i];
+ }
+ }
+
+ return CFB;
+ }());
+
+
+ /**
+ * Counter block mode.
+ */
+ CryptoJS.mode.CTR = (function () {
+ var CTR = CryptoJS.lib.BlockCipherMode.extend();
+
+ var Encryptor = CTR.Encryptor = CTR.extend({
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher
+ var blockSize = cipher.blockSize;
+ var iv = this._iv;
+ var counter = this._counter;
+
+ // Generate keystream
+ if (iv) {
+ counter = this._counter = iv.slice(0);
+
+ // Remove IV for subsequent blocks
+ this._iv = undefined;
+ }
+ var keystream = counter.slice(0);
+ cipher.encryptBlock(keystream, 0);
+
+ // Increment counter
+ counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
+
+ // Encrypt
+ for (var i = 0; i < blockSize; i++) {
+ words[offset + i] ^= keystream[i];
+ }
+ }
+ });
+
+ CTR.Decryptor = Encryptor;
+
+ return CTR;
+ }());
+
+
+ /** @preserve
+ * Counter block mode compatible with Dr Brian Gladman fileenc.c
+ * derived from CryptoJS.mode.CTR
+ * Jan Hruby jhruby.web@gmail.com
+ */
+ CryptoJS.mode.CTRGladman = (function () {
+ var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
+
+ function incWord(word)
+ {
+ if (((word >> 24) & 0xff) === 0xff) { //overflow
+ var b1 = (word >> 16)&0xff;
+ var b2 = (word >> 8)&0xff;
+ var b3 = word & 0xff;
+
+ if (b1 === 0xff) // overflow b1
+ {
+ b1 = 0;
+ if (b2 === 0xff)
+ {
+ b2 = 0;
+ if (b3 === 0xff)
+ {
+ b3 = 0;
+ }
+ else
+ {
+ ++b3;
+ }
+ }
+ else
+ {
+ ++b2;
+ }
+ }
+ else
+ {
+ ++b1;
+ }
+
+ word = 0;
+ word += (b1 << 16);
+ word += (b2 << 8);
+ word += b3;
+ }
+ else
+ {
+ word += (0x01 << 24);
+ }
+ return word;
+ }
+
+ function incCounter(counter)
+ {
+ if ((counter[0] = incWord(counter[0])) === 0)
+ {
+ // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
+ counter[1] = incWord(counter[1]);
+ }
+ return counter;
+ }
+
+ var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher
+ var blockSize = cipher.blockSize;
+ var iv = this._iv;
+ var counter = this._counter;
+
+ // Generate keystream
+ if (iv) {
+ counter = this._counter = iv.slice(0);
+
+ // Remove IV for subsequent blocks
+ this._iv = undefined;
+ }
+
+ incCounter(counter);
+
+ var keystream = counter.slice(0);
+ cipher.encryptBlock(keystream, 0);
+
+ // Encrypt
+ for (var i = 0; i < blockSize; i++) {
+ words[offset + i] ^= keystream[i];
+ }
+ }
+ });
+
+ CTRGladman.Decryptor = Encryptor;
+
+ return CTRGladman;
+ }());
+
+
+
+
+ /**
+ * Output Feedback block mode.
+ */
+ CryptoJS.mode.OFB = (function () {
+ var OFB = CryptoJS.lib.BlockCipherMode.extend();
+
+ var Encryptor = OFB.Encryptor = OFB.extend({
+ processBlock: function (words, offset) {
+ // Shortcuts
+ var cipher = this._cipher
+ var blockSize = cipher.blockSize;
+ var iv = this._iv;
+ var keystream = this._keystream;
+
+ // Generate keystream
+ if (iv) {
+ keystream = this._keystream = iv.slice(0);
+
+ // Remove IV for subsequent blocks
+ this._iv = undefined;
+ }
+ cipher.encryptBlock(keystream, 0);
+
+ // Encrypt
+ for (var i = 0; i < blockSize; i++) {
+ words[offset + i] ^= keystream[i];
+ }
+ }
+ });
+
+ OFB.Decryptor = Encryptor;
+
+ return OFB;
+ }());
+
+
+ /**
+ * Electronic Codebook block mode.
+ */
+ CryptoJS.mode.ECB = (function () {
+ var ECB = CryptoJS.lib.BlockCipherMode.extend();
+
+ ECB.Encryptor = ECB.extend({
+ processBlock: function (words, offset) {
+ this._cipher.encryptBlock(words, offset);
+ }
+ });
+
+ ECB.Decryptor = ECB.extend({
+ processBlock: function (words, offset) {
+ this._cipher.decryptBlock(words, offset);
+ }
+ });
+
+ return ECB;
+ }());
+
+
+ /**
+ * ANSI X.923 padding strategy.
+ */
+ CryptoJS.pad.AnsiX923 = {
+ pad: function (data, blockSize) {
+ // Shortcuts
+ var dataSigBytes = data.sigBytes;
+ var blockSizeBytes = blockSize * 4;
+
+ // Count padding bytes
+ var nPaddingBytes = blockSizeBytes - dataSigBytes % blockSizeBytes;
+
+ // Compute last byte position
+ var lastBytePos = dataSigBytes + nPaddingBytes - 1;
+
+ // Pad
+ data.clamp();
+ data.words[lastBytePos >>> 2] |= nPaddingBytes << (24 - (lastBytePos % 4) * 8);
+ data.sigBytes += nPaddingBytes;
+ },
+
+ unpad: function (data) {
+ // Get number of padding bytes from last byte
+ var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
+
+ // Remove padding
+ data.sigBytes -= nPaddingBytes;
+ }
+ };
+
+
+ /**
+ * ISO 10126 padding strategy.
+ */
+ CryptoJS.pad.Iso10126 = {
+ pad: function (data, blockSize) {
+ // Shortcut
+ var blockSizeBytes = blockSize * 4;
+
+ // Count padding bytes
+ var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
+
+ // Pad
+ data.concat(CryptoJS.lib.WordArray.random(nPaddingBytes - 1)).
+ concat(CryptoJS.lib.WordArray.create([nPaddingBytes << 24], 1));
+ },
+
+ unpad: function (data) {
+ // Get number of padding bytes from last byte
+ var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
+
+ // Remove padding
+ data.sigBytes -= nPaddingBytes;
+ }
+ };
+
+
+ /**
+ * ISO/IEC 9797-1 Padding Method 2.
+ */
+ CryptoJS.pad.Iso97971 = {
+ pad: function (data, blockSize) {
+ // Add 0x80 byte
+ data.concat(CryptoJS.lib.WordArray.create([0x80000000], 1));
+
+ // Zero pad the rest
+ CryptoJS.pad.ZeroPadding.pad(data, blockSize);
+ },
+
+ unpad: function (data) {
+ // Remove zero padding
+ CryptoJS.pad.ZeroPadding.unpad(data);
+
+ // Remove one more byte -- the 0x80 byte
+ data.sigBytes--;
+ }
+ };
+
+
+ /**
+ * Zero padding strategy.
+ */
+ CryptoJS.pad.ZeroPadding = {
+ pad: function (data, blockSize) {
+ // Shortcut
+ var blockSizeBytes = blockSize * 4;
+
+ // Pad
+ data.clamp();
+ data.sigBytes += blockSizeBytes - ((data.sigBytes % blockSizeBytes) || blockSizeBytes);
+ },
+
+ unpad: function (data) {
+ // Shortcut
+ var dataWords = data.words;
+
+ // Unpad
+ var i = data.sigBytes - 1;
+ for (var i = data.sigBytes - 1; i >= 0; i--) {
+ if (((dataWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff)) {
+ data.sigBytes = i + 1;
+ break;
+ }
+ }
+ }
+ };
+
+
+ /**
+ * A noop padding strategy.
+ */
+ CryptoJS.pad.NoPadding = {
+ pad: function () {
+ },
+
+ unpad: function () {
+ }
+ };
+
+
+ (function (undefined) {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var CipherParams = C_lib.CipherParams;
+ var C_enc = C.enc;
+ var Hex = C_enc.Hex;
+ var C_format = C.format;
+
+ var HexFormatter = C_format.Hex = {
+ /**
+ * Converts the ciphertext of a cipher params object to a hexadecimally encoded string.
+ *
+ * @param {CipherParams} cipherParams The cipher params object.
+ *
+ * @return {string} The hexadecimally encoded string.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var hexString = CryptoJS.format.Hex.stringify(cipherParams);
+ */
+ stringify: function (cipherParams) {
+ return cipherParams.ciphertext.toString(Hex);
+ },
+
+ /**
+ * Converts a hexadecimally encoded ciphertext string to a cipher params object.
+ *
+ * @param {string} input The hexadecimally encoded string.
+ *
+ * @return {CipherParams} The cipher params object.
+ *
+ * @static
+ *
+ * @example
+ *
+ * var cipherParams = CryptoJS.format.Hex.parse(hexString);
+ */
+ parse: function (input) {
+ var ciphertext = Hex.parse(input);
+ return CipherParams.create({ ciphertext: ciphertext });
+ }
+ };
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var BlockCipher = C_lib.BlockCipher;
+ var C_algo = C.algo;
+
+ // Lookup tables
+ var SBOX = [];
+ var INV_SBOX = [];
+ var SUB_MIX_0 = [];
+ var SUB_MIX_1 = [];
+ var SUB_MIX_2 = [];
+ var SUB_MIX_3 = [];
+ var INV_SUB_MIX_0 = [];
+ var INV_SUB_MIX_1 = [];
+ var INV_SUB_MIX_2 = [];
+ var INV_SUB_MIX_3 = [];
+
+ // Compute lookup tables
+ (function () {
+ // Compute double table
+ var d = [];
+ for (var i = 0; i < 256; i++) {
+ if (i < 128) {
+ d[i] = i << 1;
+ } else {
+ d[i] = (i << 1) ^ 0x11b;
+ }
+ }
+
+ // Walk GF(2^8)
+ var x = 0;
+ var xi = 0;
+ for (var i = 0; i < 256; i++) {
+ // Compute sbox
+ var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
+ sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
+ SBOX[x] = sx;
+ INV_SBOX[sx] = x;
+
+ // Compute multiplication
+ var x2 = d[x];
+ var x4 = d[x2];
+ var x8 = d[x4];
+
+ // Compute sub bytes, mix columns tables
+ var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
+ SUB_MIX_0[x] = (t << 24) | (t >>> 8);
+ SUB_MIX_1[x] = (t << 16) | (t >>> 16);
+ SUB_MIX_2[x] = (t << 8) | (t >>> 24);
+ SUB_MIX_3[x] = t;
+
+ // Compute inv sub bytes, inv mix columns tables
+ var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
+ INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
+ INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
+ INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
+ INV_SUB_MIX_3[sx] = t;
+
+ // Compute next counter
+ if (!x) {
+ x = xi = 1;
+ } else {
+ x = x2 ^ d[d[d[x8 ^ x2]]];
+ xi ^= d[d[xi]];
+ }
+ }
+ }());
+
+ // Precomputed Rcon lookup
+ var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
+
+ /**
+ * AES block cipher algorithm.
+ */
+ var AES = C_algo.AES = BlockCipher.extend({
+ _doReset: function () {
+ var t;
+
+ // Skip reset of nRounds has been set before and key did not change
+ if (this._nRounds && this._keyPriorReset === this._key) {
+ return;
+ }
+
+ // Shortcuts
+ var key = this._keyPriorReset = this._key;
+ var keyWords = key.words;
+ var keySize = key.sigBytes / 4;
+
+ // Compute number of rounds
+ var nRounds = this._nRounds = keySize + 6;
+
+ // Compute number of key schedule rows
+ var ksRows = (nRounds + 1) * 4;
+
+ // Compute key schedule
+ var keySchedule = this._keySchedule = [];
+ for (var ksRow = 0; ksRow < ksRows; ksRow++) {
+ if (ksRow < keySize) {
+ keySchedule[ksRow] = keyWords[ksRow];
+ } else {
+ t = keySchedule[ksRow - 1];
+
+ if (!(ksRow % keySize)) {
+ // Rot word
+ t = (t << 8) | (t >>> 24);
+
+ // Sub word
+ t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
+
+ // Mix Rcon
+ t ^= RCON[(ksRow / keySize) | 0] << 24;
+ } else if (keySize > 6 && ksRow % keySize == 4) {
+ // Sub word
+ t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
+ }
+
+ keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
+ }
+ }
+
+ // Compute inv key schedule
+ var invKeySchedule = this._invKeySchedule = [];
+ for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
+ var ksRow = ksRows - invKsRow;
+
+ if (invKsRow % 4) {
+ var t = keySchedule[ksRow];
+ } else {
+ var t = keySchedule[ksRow - 4];
+ }
+
+ if (invKsRow < 4 || ksRow <= 4) {
+ invKeySchedule[invKsRow] = t;
+ } else {
+ invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
+ INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
+ }
+ }
+ },
+
+ encryptBlock: function (M, offset) {
+ this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
+ },
+
+ decryptBlock: function (M, offset) {
+ // Swap 2nd and 4th rows
+ var t = M[offset + 1];
+ M[offset + 1] = M[offset + 3];
+ M[offset + 3] = t;
+
+ this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
+
+ // Inv swap 2nd and 4th rows
+ var t = M[offset + 1];
+ M[offset + 1] = M[offset + 3];
+ M[offset + 3] = t;
+ },
+
+ _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
+ // Shortcut
+ var nRounds = this._nRounds;
+
+ // Get input, add round key
+ var s0 = M[offset] ^ keySchedule[0];
+ var s1 = M[offset + 1] ^ keySchedule[1];
+ var s2 = M[offset + 2] ^ keySchedule[2];
+ var s3 = M[offset + 3] ^ keySchedule[3];
+
+ // Key schedule row counter
+ var ksRow = 4;
+
+ // Rounds
+ for (var round = 1; round < nRounds; round++) {
+ // Shift rows, sub bytes, mix columns, add round key
+ var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
+ var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
+ var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
+ var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];
+
+ // Update state
+ s0 = t0;
+ s1 = t1;
+ s2 = t2;
+ s3 = t3;
+ }
+
+ // Shift rows, sub bytes, add round key
+ var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
+ var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
+ var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
+ var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
+
+ // Set output
+ M[offset] = t0;
+ M[offset + 1] = t1;
+ M[offset + 2] = t2;
+ M[offset + 3] = t3;
+ },
+
+ keySize: 256/32
+ });
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
+ */
+ C.AES = BlockCipher._createHelper(AES);
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var WordArray = C_lib.WordArray;
+ var BlockCipher = C_lib.BlockCipher;
+ var C_algo = C.algo;
+
+ // Permuted Choice 1 constants
+ var PC1 = [
+ 57, 49, 41, 33, 25, 17, 9, 1,
+ 58, 50, 42, 34, 26, 18, 10, 2,
+ 59, 51, 43, 35, 27, 19, 11, 3,
+ 60, 52, 44, 36, 63, 55, 47, 39,
+ 31, 23, 15, 7, 62, 54, 46, 38,
+ 30, 22, 14, 6, 61, 53, 45, 37,
+ 29, 21, 13, 5, 28, 20, 12, 4
+ ];
+
+ // Permuted Choice 2 constants
+ var PC2 = [
+ 14, 17, 11, 24, 1, 5,
+ 3, 28, 15, 6, 21, 10,
+ 23, 19, 12, 4, 26, 8,
+ 16, 7, 27, 20, 13, 2,
+ 41, 52, 31, 37, 47, 55,
+ 30, 40, 51, 45, 33, 48,
+ 44, 49, 39, 56, 34, 53,
+ 46, 42, 50, 36, 29, 32
+ ];
+
+ // Cumulative bit shift constants
+ var BIT_SHIFTS = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28];
+
+ // SBOXes and round permutation constants
+ var SBOX_P = [
+ {
+ 0x0: 0x808200,
+ 0x10000000: 0x8000,
+ 0x20000000: 0x808002,
+ 0x30000000: 0x2,
+ 0x40000000: 0x200,
+ 0x50000000: 0x808202,
+ 0x60000000: 0x800202,
+ 0x70000000: 0x800000,
+ 0x80000000: 0x202,
+ 0x90000000: 0x800200,
+ 0xa0000000: 0x8200,
+ 0xb0000000: 0x808000,
+ 0xc0000000: 0x8002,
+ 0xd0000000: 0x800002,
+ 0xe0000000: 0x0,
+ 0xf0000000: 0x8202,
+ 0x8000000: 0x0,
+ 0x18000000: 0x808202,
+ 0x28000000: 0x8202,
+ 0x38000000: 0x8000,
+ 0x48000000: 0x808200,
+ 0x58000000: 0x200,
+ 0x68000000: 0x808002,
+ 0x78000000: 0x2,
+ 0x88000000: 0x800200,
+ 0x98000000: 0x8200,
+ 0xa8000000: 0x808000,
+ 0xb8000000: 0x800202,
+ 0xc8000000: 0x800002,
+ 0xd8000000: 0x8002,
+ 0xe8000000: 0x202,
+ 0xf8000000: 0x800000,
+ 0x1: 0x8000,
+ 0x10000001: 0x2,
+ 0x20000001: 0x808200,
+ 0x30000001: 0x800000,
+ 0x40000001: 0x808002,
+ 0x50000001: 0x8200,
+ 0x60000001: 0x200,
+ 0x70000001: 0x800202,
+ 0x80000001: 0x808202,
+ 0x90000001: 0x808000,
+ 0xa0000001: 0x800002,
+ 0xb0000001: 0x8202,
+ 0xc0000001: 0x202,
+ 0xd0000001: 0x800200,
+ 0xe0000001: 0x8002,
+ 0xf0000001: 0x0,
+ 0x8000001: 0x808202,
+ 0x18000001: 0x808000,
+ 0x28000001: 0x800000,
+ 0x38000001: 0x200,
+ 0x48000001: 0x8000,
+ 0x58000001: 0x800002,
+ 0x68000001: 0x2,
+ 0x78000001: 0x8202,
+ 0x88000001: 0x8002,
+ 0x98000001: 0x800202,
+ 0xa8000001: 0x202,
+ 0xb8000001: 0x808200,
+ 0xc8000001: 0x800200,
+ 0xd8000001: 0x0,
+ 0xe8000001: 0x8200,
+ 0xf8000001: 0x808002
+ },
+ {
+ 0x0: 0x40084010,
+ 0x1000000: 0x4000,
+ 0x2000000: 0x80000,
+ 0x3000000: 0x40080010,
+ 0x4000000: 0x40000010,
+ 0x5000000: 0x40084000,
+ 0x6000000: 0x40004000,
+ 0x7000000: 0x10,
+ 0x8000000: 0x84000,
+ 0x9000000: 0x40004010,
+ 0xa000000: 0x40000000,
+ 0xb000000: 0x84010,
+ 0xc000000: 0x80010,
+ 0xd000000: 0x0,
+ 0xe000000: 0x4010,
+ 0xf000000: 0x40080000,
+ 0x800000: 0x40004000,
+ 0x1800000: 0x84010,
+ 0x2800000: 0x10,
+ 0x3800000: 0x40004010,
+ 0x4800000: 0x40084010,
+ 0x5800000: 0x40000000,
+ 0x6800000: 0x80000,
+ 0x7800000: 0x40080010,
+ 0x8800000: 0x80010,
+ 0x9800000: 0x0,
+ 0xa800000: 0x4000,
+ 0xb800000: 0x40080000,
+ 0xc800000: 0x40000010,
+ 0xd800000: 0x84000,
+ 0xe800000: 0x40084000,
+ 0xf800000: 0x4010,
+ 0x10000000: 0x0,
+ 0x11000000: 0x40080010,
+ 0x12000000: 0x40004010,
+ 0x13000000: 0x40084000,
+ 0x14000000: 0x40080000,
+ 0x15000000: 0x10,
+ 0x16000000: 0x84010,
+ 0x17000000: 0x4000,
+ 0x18000000: 0x4010,
+ 0x19000000: 0x80000,
+ 0x1a000000: 0x80010,
+ 0x1b000000: 0x40000010,
+ 0x1c000000: 0x84000,
+ 0x1d000000: 0x40004000,
+ 0x1e000000: 0x40000000,
+ 0x1f000000: 0x40084010,
+ 0x10800000: 0x84010,
+ 0x11800000: 0x80000,
+ 0x12800000: 0x40080000,
+ 0x13800000: 0x4000,
+ 0x14800000: 0x40004000,
+ 0x15800000: 0x40084010,
+ 0x16800000: 0x10,
+ 0x17800000: 0x40000000,
+ 0x18800000: 0x40084000,
+ 0x19800000: 0x40000010,
+ 0x1a800000: 0x40004010,
+ 0x1b800000: 0x80010,
+ 0x1c800000: 0x0,
+ 0x1d800000: 0x4010,
+ 0x1e800000: 0x40080010,
+ 0x1f800000: 0x84000
+ },
+ {
+ 0x0: 0x104,
+ 0x100000: 0x0,
+ 0x200000: 0x4000100,
+ 0x300000: 0x10104,
+ 0x400000: 0x10004,
+ 0x500000: 0x4000004,
+ 0x600000: 0x4010104,
+ 0x700000: 0x4010000,
+ 0x800000: 0x4000000,
+ 0x900000: 0x4010100,
+ 0xa00000: 0x10100,
+ 0xb00000: 0x4010004,
+ 0xc00000: 0x4000104,
+ 0xd00000: 0x10000,
+ 0xe00000: 0x4,
+ 0xf00000: 0x100,
+ 0x80000: 0x4010100,
+ 0x180000: 0x4010004,
+ 0x280000: 0x0,
+ 0x380000: 0x4000100,
+ 0x480000: 0x4000004,
+ 0x580000: 0x10000,
+ 0x680000: 0x10004,
+ 0x780000: 0x104,
+ 0x880000: 0x4,
+ 0x980000: 0x100,
+ 0xa80000: 0x4010000,
+ 0xb80000: 0x10104,
+ 0xc80000: 0x10100,
+ 0xd80000: 0x4000104,
+ 0xe80000: 0x4010104,
+ 0xf80000: 0x4000000,
+ 0x1000000: 0x4010100,
+ 0x1100000: 0x10004,
+ 0x1200000: 0x10000,
+ 0x1300000: 0x4000100,
+ 0x1400000: 0x100,
+ 0x1500000: 0x4010104,
+ 0x1600000: 0x4000004,
+ 0x1700000: 0x0,
+ 0x1800000: 0x4000104,
+ 0x1900000: 0x4000000,
+ 0x1a00000: 0x4,
+ 0x1b00000: 0x10100,
+ 0x1c00000: 0x4010000,
+ 0x1d00000: 0x104,
+ 0x1e00000: 0x10104,
+ 0x1f00000: 0x4010004,
+ 0x1080000: 0x4000000,
+ 0x1180000: 0x104,
+ 0x1280000: 0x4010100,
+ 0x1380000: 0x0,
+ 0x1480000: 0x10004,
+ 0x1580000: 0x4000100,
+ 0x1680000: 0x100,
+ 0x1780000: 0x4010004,
+ 0x1880000: 0x10000,
+ 0x1980000: 0x4010104,
+ 0x1a80000: 0x10104,
+ 0x1b80000: 0x4000004,
+ 0x1c80000: 0x4000104,
+ 0x1d80000: 0x4010000,
+ 0x1e80000: 0x4,
+ 0x1f80000: 0x10100
+ },
+ {
+ 0x0: 0x80401000,
+ 0x10000: 0x80001040,
+ 0x20000: 0x401040,
+ 0x30000: 0x80400000,
+ 0x40000: 0x0,
+ 0x50000: 0x401000,
+ 0x60000: 0x80000040,
+ 0x70000: 0x400040,
+ 0x80000: 0x80000000,
+ 0x90000: 0x400000,
+ 0xa0000: 0x40,
+ 0xb0000: 0x80001000,
+ 0xc0000: 0x80400040,
+ 0xd0000: 0x1040,
+ 0xe0000: 0x1000,
+ 0xf0000: 0x80401040,
+ 0x8000: 0x80001040,
+ 0x18000: 0x40,
+ 0x28000: 0x80400040,
+ 0x38000: 0x80001000,
+ 0x48000: 0x401000,
+ 0x58000: 0x80401040,
+ 0x68000: 0x0,
+ 0x78000: 0x80400000,
+ 0x88000: 0x1000,
+ 0x98000: 0x80401000,
+ 0xa8000: 0x400000,
+ 0xb8000: 0x1040,
+ 0xc8000: 0x80000000,
+ 0xd8000: 0x400040,
+ 0xe8000: 0x401040,
+ 0xf8000: 0x80000040,
+ 0x100000: 0x400040,
+ 0x110000: 0x401000,
+ 0x120000: 0x80000040,
+ 0x130000: 0x0,
+ 0x140000: 0x1040,
+ 0x150000: 0x80400040,
+ 0x160000: 0x80401000,
+ 0x170000: 0x80001040,
+ 0x180000: 0x80401040,
+ 0x190000: 0x80000000,
+ 0x1a0000: 0x80400000,
+ 0x1b0000: 0x401040,
+ 0x1c0000: 0x80001000,
+ 0x1d0000: 0x400000,
+ 0x1e0000: 0x40,
+ 0x1f0000: 0x1000,
+ 0x108000: 0x80400000,
+ 0x118000: 0x80401040,
+ 0x128000: 0x0,
+ 0x138000: 0x401000,
+ 0x148000: 0x400040,
+ 0x158000: 0x80000000,
+ 0x168000: 0x80001040,
+ 0x178000: 0x40,
+ 0x188000: 0x80000040,
+ 0x198000: 0x1000,
+ 0x1a8000: 0x80001000,
+ 0x1b8000: 0x80400040,
+ 0x1c8000: 0x1040,
+ 0x1d8000: 0x80401000,
+ 0x1e8000: 0x400000,
+ 0x1f8000: 0x401040
+ },
+ {
+ 0x0: 0x80,
+ 0x1000: 0x1040000,
+ 0x2000: 0x40000,
+ 0x3000: 0x20000000,
+ 0x4000: 0x20040080,
+ 0x5000: 0x1000080,
+ 0x6000: 0x21000080,
+ 0x7000: 0x40080,
+ 0x8000: 0x1000000,
+ 0x9000: 0x20040000,
+ 0xa000: 0x20000080,
+ 0xb000: 0x21040080,
+ 0xc000: 0x21040000,
+ 0xd000: 0x0,
+ 0xe000: 0x1040080,
+ 0xf000: 0x21000000,
+ 0x800: 0x1040080,
+ 0x1800: 0x21000080,
+ 0x2800: 0x80,
+ 0x3800: 0x1040000,
+ 0x4800: 0x40000,
+ 0x5800: 0x20040080,
+ 0x6800: 0x21040000,
+ 0x7800: 0x20000000,
+ 0x8800: 0x20040000,
+ 0x9800: 0x0,
+ 0xa800: 0x21040080,
+ 0xb800: 0x1000080,
+ 0xc800: 0x20000080,
+ 0xd800: 0x21000000,
+ 0xe800: 0x1000000,
+ 0xf800: 0x40080,
+ 0x10000: 0x40000,
+ 0x11000: 0x80,
+ 0x12000: 0x20000000,
+ 0x13000: 0x21000080,
+ 0x14000: 0x1000080,
+ 0x15000: 0x21040000,
+ 0x16000: 0x20040080,
+ 0x17000: 0x1000000,
+ 0x18000: 0x21040080,
+ 0x19000: 0x21000000,
+ 0x1a000: 0x1040000,
+ 0x1b000: 0x20040000,
+ 0x1c000: 0x40080,
+ 0x1d000: 0x20000080,
+ 0x1e000: 0x0,
+ 0x1f000: 0x1040080,
+ 0x10800: 0x21000080,
+ 0x11800: 0x1000000,
+ 0x12800: 0x1040000,
+ 0x13800: 0x20040080,
+ 0x14800: 0x20000000,
+ 0x15800: 0x1040080,
+ 0x16800: 0x80,
+ 0x17800: 0x21040000,
+ 0x18800: 0x40080,
+ 0x19800: 0x21040080,
+ 0x1a800: 0x0,
+ 0x1b800: 0x21000000,
+ 0x1c800: 0x1000080,
+ 0x1d800: 0x40000,
+ 0x1e800: 0x20040000,
+ 0x1f800: 0x20000080
+ },
+ {
+ 0x0: 0x10000008,
+ 0x100: 0x2000,
+ 0x200: 0x10200000,
+ 0x300: 0x10202008,
+ 0x400: 0x10002000,
+ 0x500: 0x200000,
+ 0x600: 0x200008,
+ 0x700: 0x10000000,
+ 0x800: 0x0,
+ 0x900: 0x10002008,
+ 0xa00: 0x202000,
+ 0xb00: 0x8,
+ 0xc00: 0x10200008,
+ 0xd00: 0x202008,
+ 0xe00: 0x2008,
+ 0xf00: 0x10202000,
+ 0x80: 0x10200000,
+ 0x180: 0x10202008,
+ 0x280: 0x8,
+ 0x380: 0x200000,
+ 0x480: 0x202008,
+ 0x580: 0x10000008,
+ 0x680: 0x10002000,
+ 0x780: 0x2008,
+ 0x880: 0x200008,
+ 0x980: 0x2000,
+ 0xa80: 0x10002008,
+ 0xb80: 0x10200008,
+ 0xc80: 0x0,
+ 0xd80: 0x10202000,
+ 0xe80: 0x202000,
+ 0xf80: 0x10000000,
+ 0x1000: 0x10002000,
+ 0x1100: 0x10200008,
+ 0x1200: 0x10202008,
+ 0x1300: 0x2008,
+ 0x1400: 0x200000,
+ 0x1500: 0x10000000,
+ 0x1600: 0x10000008,
+ 0x1700: 0x202000,
+ 0x1800: 0x202008,
+ 0x1900: 0x0,
+ 0x1a00: 0x8,
+ 0x1b00: 0x10200000,
+ 0x1c00: 0x2000,
+ 0x1d00: 0x10002008,
+ 0x1e00: 0x10202000,
+ 0x1f00: 0x200008,
+ 0x1080: 0x8,
+ 0x1180: 0x202000,
+ 0x1280: 0x200000,
+ 0x1380: 0x10000008,
+ 0x1480: 0x10002000,
+ 0x1580: 0x2008,
+ 0x1680: 0x10202008,
+ 0x1780: 0x10200000,
+ 0x1880: 0x10202000,
+ 0x1980: 0x10200008,
+ 0x1a80: 0x2000,
+ 0x1b80: 0x202008,
+ 0x1c80: 0x200008,
+ 0x1d80: 0x0,
+ 0x1e80: 0x10000000,
+ 0x1f80: 0x10002008
+ },
+ {
+ 0x0: 0x100000,
+ 0x10: 0x2000401,
+ 0x20: 0x400,
+ 0x30: 0x100401,
+ 0x40: 0x2100401,
+ 0x50: 0x0,
+ 0x60: 0x1,
+ 0x70: 0x2100001,
+ 0x80: 0x2000400,
+ 0x90: 0x100001,
+ 0xa0: 0x2000001,
+ 0xb0: 0x2100400,
+ 0xc0: 0x2100000,
+ 0xd0: 0x401,
+ 0xe0: 0x100400,
+ 0xf0: 0x2000000,
+ 0x8: 0x2100001,
+ 0x18: 0x0,
+ 0x28: 0x2000401,
+ 0x38: 0x2100400,
+ 0x48: 0x100000,
+ 0x58: 0x2000001,
+ 0x68: 0x2000000,
+ 0x78: 0x401,
+ 0x88: 0x100401,
+ 0x98: 0x2000400,
+ 0xa8: 0x2100000,
+ 0xb8: 0x100001,
+ 0xc8: 0x400,
+ 0xd8: 0x2100401,
+ 0xe8: 0x1,
+ 0xf8: 0x100400,
+ 0x100: 0x2000000,
+ 0x110: 0x100000,
+ 0x120: 0x2000401,
+ 0x130: 0x2100001,
+ 0x140: 0x100001,
+ 0x150: 0x2000400,
+ 0x160: 0x2100400,
+ 0x170: 0x100401,
+ 0x180: 0x401,
+ 0x190: 0x2100401,
+ 0x1a0: 0x100400,
+ 0x1b0: 0x1,
+ 0x1c0: 0x0,
+ 0x1d0: 0x2100000,
+ 0x1e0: 0x2000001,
+ 0x1f0: 0x400,
+ 0x108: 0x100400,
+ 0x118: 0x2000401,
+ 0x128: 0x2100001,
+ 0x138: 0x1,
+ 0x148: 0x2000000,
+ 0x158: 0x100000,
+ 0x168: 0x401,
+ 0x178: 0x2100400,
+ 0x188: 0x2000001,
+ 0x198: 0x2100000,
+ 0x1a8: 0x0,
+ 0x1b8: 0x2100401,
+ 0x1c8: 0x100401,
+ 0x1d8: 0x400,
+ 0x1e8: 0x2000400,
+ 0x1f8: 0x100001
+ },
+ {
+ 0x0: 0x8000820,
+ 0x1: 0x20000,
+ 0x2: 0x8000000,
+ 0x3: 0x20,
+ 0x4: 0x20020,
+ 0x5: 0x8020820,
+ 0x6: 0x8020800,
+ 0x7: 0x800,
+ 0x8: 0x8020000,
+ 0x9: 0x8000800,
+ 0xa: 0x20800,
+ 0xb: 0x8020020,
+ 0xc: 0x820,
+ 0xd: 0x0,
+ 0xe: 0x8000020,
+ 0xf: 0x20820,
+ 0x80000000: 0x800,
+ 0x80000001: 0x8020820,
+ 0x80000002: 0x8000820,
+ 0x80000003: 0x8000000,
+ 0x80000004: 0x8020000,
+ 0x80000005: 0x20800,
+ 0x80000006: 0x20820,
+ 0x80000007: 0x20,
+ 0x80000008: 0x8000020,
+ 0x80000009: 0x820,
+ 0x8000000a: 0x20020,
+ 0x8000000b: 0x8020800,
+ 0x8000000c: 0x0,
+ 0x8000000d: 0x8020020,
+ 0x8000000e: 0x8000800,
+ 0x8000000f: 0x20000,
+ 0x10: 0x20820,
+ 0x11: 0x8020800,
+ 0x12: 0x20,
+ 0x13: 0x800,
+ 0x14: 0x8000800,
+ 0x15: 0x8000020,
+ 0x16: 0x8020020,
+ 0x17: 0x20000,
+ 0x18: 0x0,
+ 0x19: 0x20020,
+ 0x1a: 0x8020000,
+ 0x1b: 0x8000820,
+ 0x1c: 0x8020820,
+ 0x1d: 0x20800,
+ 0x1e: 0x820,
+ 0x1f: 0x8000000,
+ 0x80000010: 0x20000,
+ 0x80000011: 0x800,
+ 0x80000012: 0x8020020,
+ 0x80000013: 0x20820,
+ 0x80000014: 0x20,
+ 0x80000015: 0x8020000,
+ 0x80000016: 0x8000000,
+ 0x80000017: 0x8000820,
+ 0x80000018: 0x8020820,
+ 0x80000019: 0x8000020,
+ 0x8000001a: 0x8000800,
+ 0x8000001b: 0x0,
+ 0x8000001c: 0x20800,
+ 0x8000001d: 0x820,
+ 0x8000001e: 0x20020,
+ 0x8000001f: 0x8020800
+ }
+ ];
+
+ // Masks that select the SBOX input
+ var SBOX_MASK = [
+ 0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000,
+ 0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f
+ ];
+
+ /**
+ * DES block cipher algorithm.
+ */
+ var DES = C_algo.DES = BlockCipher.extend({
+ _doReset: function () {
+ // Shortcuts
+ var key = this._key;
+ var keyWords = key.words;
+
+ // Select 56 bits according to PC1
+ var keyBits = [];
+ for (var i = 0; i < 56; i++) {
+ var keyBitPos = PC1[i] - 1;
+ keyBits[i] = (keyWords[keyBitPos >>> 5] >>> (31 - keyBitPos % 32)) & 1;
+ }
+
+ // Assemble 16 subkeys
+ var subKeys = this._subKeys = [];
+ for (var nSubKey = 0; nSubKey < 16; nSubKey++) {
+ // Create subkey
+ var subKey = subKeys[nSubKey] = [];
+
+ // Shortcut
+ var bitShift = BIT_SHIFTS[nSubKey];
+
+ // Select 48 bits according to PC2
+ for (var i = 0; i < 24; i++) {
+ // Select from the left 28 key bits
+ subKey[(i / 6) | 0] |= keyBits[((PC2[i] - 1) + bitShift) % 28] << (31 - i % 6);
+
+ // Select from the right 28 key bits
+ subKey[4 + ((i / 6) | 0)] |= keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)] << (31 - i % 6);
+ }
+
+ // Since each subkey is applied to an expanded 32-bit input,
+ // the subkey can be broken into 8 values scaled to 32-bits,
+ // which allows the key to be used without expansion
+ subKey[0] = (subKey[0] << 1) | (subKey[0] >>> 31);
+ for (var i = 1; i < 7; i++) {
+ subKey[i] = subKey[i] >>> ((i - 1) * 4 + 3);
+ }
+ subKey[7] = (subKey[7] << 5) | (subKey[7] >>> 27);
+ }
+
+ // Compute inverse subkeys
+ var invSubKeys = this._invSubKeys = [];
+ for (var i = 0; i < 16; i++) {
+ invSubKeys[i] = subKeys[15 - i];
+ }
+ },
+
+ encryptBlock: function (M, offset) {
+ this._doCryptBlock(M, offset, this._subKeys);
+ },
+
+ decryptBlock: function (M, offset) {
+ this._doCryptBlock(M, offset, this._invSubKeys);
+ },
+
+ _doCryptBlock: function (M, offset, subKeys) {
+ // Get input
+ this._lBlock = M[offset];
+ this._rBlock = M[offset + 1];
+
+ // Initial permutation
+ exchangeLR.call(this, 4, 0x0f0f0f0f);
+ exchangeLR.call(this, 16, 0x0000ffff);
+ exchangeRL.call(this, 2, 0x33333333);
+ exchangeRL.call(this, 8, 0x00ff00ff);
+ exchangeLR.call(this, 1, 0x55555555);
+
+ // Rounds
+ for (var round = 0; round < 16; round++) {
+ // Shortcuts
+ var subKey = subKeys[round];
+ var lBlock = this._lBlock;
+ var rBlock = this._rBlock;
+
+ // Feistel function
+ var f = 0;
+ for (var i = 0; i < 8; i++) {
+ f |= SBOX_P[i][((rBlock ^ subKey[i]) & SBOX_MASK[i]) >>> 0];
+ }
+ this._lBlock = rBlock;
+ this._rBlock = lBlock ^ f;
+ }
+
+ // Undo swap from last round
+ var t = this._lBlock;
+ this._lBlock = this._rBlock;
+ this._rBlock = t;
+
+ // Final permutation
+ exchangeLR.call(this, 1, 0x55555555);
+ exchangeRL.call(this, 8, 0x00ff00ff);
+ exchangeRL.call(this, 2, 0x33333333);
+ exchangeLR.call(this, 16, 0x0000ffff);
+ exchangeLR.call(this, 4, 0x0f0f0f0f);
+
+ // Set output
+ M[offset] = this._lBlock;
+ M[offset + 1] = this._rBlock;
+ },
+
+ keySize: 64/32,
+
+ ivSize: 64/32,
+
+ blockSize: 64/32
+ });
+
+ // Swap bits across the left and right words
+ function exchangeLR(offset, mask) {
+ var t = ((this._lBlock >>> offset) ^ this._rBlock) & mask;
+ this._rBlock ^= t;
+ this._lBlock ^= t << offset;
+ }
+
+ function exchangeRL(offset, mask) {
+ var t = ((this._rBlock >>> offset) ^ this._lBlock) & mask;
+ this._lBlock ^= t;
+ this._rBlock ^= t << offset;
+ }
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.DES.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.DES.decrypt(ciphertext, key, cfg);
+ */
+ C.DES = BlockCipher._createHelper(DES);
+
+ /**
+ * Triple-DES block cipher algorithm.
+ */
+ var TripleDES = C_algo.TripleDES = BlockCipher.extend({
+ _doReset: function () {
+ // Shortcuts
+ var key = this._key;
+ var keyWords = key.words;
+ // Make sure the key length is valid (64, 128 or >= 192 bit)
+ if (keyWords.length !== 2 && keyWords.length !== 4 && keyWords.length < 6) {
+ throw new Error('Invalid key length - 3DES requires the key length to be 64, 128, 192 or >192.');
+ }
+
+ // Extend the key according to the keying options defined in 3DES standard
+ var key1 = keyWords.slice(0, 2);
+ var key2 = keyWords.length < 4 ? keyWords.slice(0, 2) : keyWords.slice(2, 4);
+ var key3 = keyWords.length < 6 ? keyWords.slice(0, 2) : keyWords.slice(4, 6);
+
+ // Create DES instances
+ this._des1 = DES.createEncryptor(WordArray.create(key1));
+ this._des2 = DES.createEncryptor(WordArray.create(key2));
+ this._des3 = DES.createEncryptor(WordArray.create(key3));
+ },
+
+ encryptBlock: function (M, offset) {
+ this._des1.encryptBlock(M, offset);
+ this._des2.decryptBlock(M, offset);
+ this._des3.encryptBlock(M, offset);
+ },
+
+ decryptBlock: function (M, offset) {
+ this._des3.decryptBlock(M, offset);
+ this._des2.encryptBlock(M, offset);
+ this._des1.decryptBlock(M, offset);
+ },
+
+ keySize: 192/32,
+
+ ivSize: 64/32,
+
+ blockSize: 64/32
+ });
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.TripleDES.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.TripleDES.decrypt(ciphertext, key, cfg);
+ */
+ C.TripleDES = BlockCipher._createHelper(TripleDES);
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var StreamCipher = C_lib.StreamCipher;
+ var C_algo = C.algo;
+
+ /**
+ * RC4 stream cipher algorithm.
+ */
+ var RC4 = C_algo.RC4 = StreamCipher.extend({
+ _doReset: function () {
+ // Shortcuts
+ var key = this._key;
+ var keyWords = key.words;
+ var keySigBytes = key.sigBytes;
+
+ // Init sbox
+ var S = this._S = [];
+ for (var i = 0; i < 256; i++) {
+ S[i] = i;
+ }
+
+ // Key setup
+ for (var i = 0, j = 0; i < 256; i++) {
+ var keyByteIndex = i % keySigBytes;
+ var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
+
+ j = (j + S[i] + keyByte) % 256;
+
+ // Swap
+ var t = S[i];
+ S[i] = S[j];
+ S[j] = t;
+ }
+
+ // Counters
+ this._i = this._j = 0;
+ },
+
+ _doProcessBlock: function (M, offset) {
+ M[offset] ^= generateKeystreamWord.call(this);
+ },
+
+ keySize: 256/32,
+
+ ivSize: 0
+ });
+
+ function generateKeystreamWord() {
+ // Shortcuts
+ var S = this._S;
+ var i = this._i;
+ var j = this._j;
+
+ // Generate keystream word
+ var keystreamWord = 0;
+ for (var n = 0; n < 4; n++) {
+ i = (i + 1) % 256;
+ j = (j + S[i]) % 256;
+
+ // Swap
+ var t = S[i];
+ S[i] = S[j];
+ S[j] = t;
+
+ keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
+ }
+
+ // Update counters
+ this._i = i;
+ this._j = j;
+
+ return keystreamWord;
+ }
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
+ */
+ C.RC4 = StreamCipher._createHelper(RC4);
+
+ /**
+ * Modified RC4 stream cipher algorithm.
+ */
+ var RC4Drop = C_algo.RC4Drop = RC4.extend({
+ /**
+ * Configuration options.
+ *
+ * @property {number} drop The number of keystream words to drop. Default 192
+ */
+ cfg: RC4.cfg.extend({
+ drop: 192
+ }),
+
+ _doReset: function () {
+ RC4._doReset.call(this);
+
+ // Drop
+ for (var i = this.cfg.drop; i > 0; i--) {
+ generateKeystreamWord.call(this);
+ }
+ }
+ });
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
+ */
+ C.RC4Drop = StreamCipher._createHelper(RC4Drop);
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var StreamCipher = C_lib.StreamCipher;
+ var C_algo = C.algo;
+
+ // Reusable objects
+ var S = [];
+ var C_ = [];
+ var G = [];
+
+ /**
+ * Rabbit stream cipher algorithm
+ */
+ var Rabbit = C_algo.Rabbit = StreamCipher.extend({
+ _doReset: function () {
+ // Shortcuts
+ var K = this._key.words;
+ var iv = this.cfg.iv;
+
+ // Swap endian
+ for (var i = 0; i < 4; i++) {
+ K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) |
+ (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00);
+ }
+
+ // Generate initial state values
+ var X = this._X = [
+ K[0], (K[3] << 16) | (K[2] >>> 16),
+ K[1], (K[0] << 16) | (K[3] >>> 16),
+ K[2], (K[1] << 16) | (K[0] >>> 16),
+ K[3], (K[2] << 16) | (K[1] >>> 16)
+ ];
+
+ // Generate initial counter values
+ var C = this._C = [
+ (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
+ (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
+ (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
+ (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
+ ];
+
+ // Carry bit
+ this._b = 0;
+
+ // Iterate the system four times
+ for (var i = 0; i < 4; i++) {
+ nextState.call(this);
+ }
+
+ // Modify the counters
+ for (var i = 0; i < 8; i++) {
+ C[i] ^= X[(i + 4) & 7];
+ }
+
+ // IV setup
+ if (iv) {
+ // Shortcuts
+ var IV = iv.words;
+ var IV_0 = IV[0];
+ var IV_1 = IV[1];
+
+ // Generate four subvectors
+ var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
+ var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
+ var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
+ var i3 = (i2 << 16) | (i0 & 0x0000ffff);
+
+ // Modify counter values
+ C[0] ^= i0;
+ C[1] ^= i1;
+ C[2] ^= i2;
+ C[3] ^= i3;
+ C[4] ^= i0;
+ C[5] ^= i1;
+ C[6] ^= i2;
+ C[7] ^= i3;
+
+ // Iterate the system four times
+ for (var i = 0; i < 4; i++) {
+ nextState.call(this);
+ }
+ }
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Shortcut
+ var X = this._X;
+
+ // Iterate the system
+ nextState.call(this);
+
+ // Generate four keystream words
+ S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
+ S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
+ S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
+ S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
+
+ for (var i = 0; i < 4; i++) {
+ // Swap endian
+ S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
+ (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
+
+ // Encrypt
+ M[offset + i] ^= S[i];
+ }
+ },
+
+ blockSize: 128/32,
+
+ ivSize: 64/32
+ });
+
+ function nextState() {
+ // Shortcuts
+ var X = this._X;
+ var C = this._C;
+
+ // Save old counter values
+ for (var i = 0; i < 8; i++) {
+ C_[i] = C[i];
+ }
+
+ // Calculate new counter values
+ C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
+ C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
+ C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
+ C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
+ C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
+ C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
+ C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
+ C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
+ this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
+
+ // Calculate the g-values
+ for (var i = 0; i < 8; i++) {
+ var gx = X[i] + C[i];
+
+ // Construct high and low argument for squaring
+ var ga = gx & 0xffff;
+ var gb = gx >>> 16;
+
+ // Calculate high and low result of squaring
+ var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
+ var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
+
+ // High XOR low
+ G[i] = gh ^ gl;
+ }
+
+ // Calculate new state values
+ X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
+ X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
+ X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
+ X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
+ X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
+ X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
+ X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
+ X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
+ }
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg);
+ */
+ C.Rabbit = StreamCipher._createHelper(Rabbit);
+ }());
+
+
+ (function () {
+ // Shortcuts
+ var C = CryptoJS;
+ var C_lib = C.lib;
+ var StreamCipher = C_lib.StreamCipher;
+ var C_algo = C.algo;
+
+ // Reusable objects
+ var S = [];
+ var C_ = [];
+ var G = [];
+
+ /**
+ * Rabbit stream cipher algorithm.
+ *
+ * This is a legacy version that neglected to convert the key to little-endian.
+ * This error doesn't affect the cipher's security,
+ * but it does affect its compatibility with other implementations.
+ */
+ var RabbitLegacy = C_algo.RabbitLegacy = StreamCipher.extend({
+ _doReset: function () {
+ // Shortcuts
+ var K = this._key.words;
+ var iv = this.cfg.iv;
+
+ // Generate initial state values
+ var X = this._X = [
+ K[0], (K[3] << 16) | (K[2] >>> 16),
+ K[1], (K[0] << 16) | (K[3] >>> 16),
+ K[2], (K[1] << 16) | (K[0] >>> 16),
+ K[3], (K[2] << 16) | (K[1] >>> 16)
+ ];
+
+ // Generate initial counter values
+ var C = this._C = [
+ (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
+ (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
+ (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
+ (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
+ ];
+
+ // Carry bit
+ this._b = 0;
+
+ // Iterate the system four times
+ for (var i = 0; i < 4; i++) {
+ nextState.call(this);
+ }
+
+ // Modify the counters
+ for (var i = 0; i < 8; i++) {
+ C[i] ^= X[(i + 4) & 7];
+ }
+
+ // IV setup
+ if (iv) {
+ // Shortcuts
+ var IV = iv.words;
+ var IV_0 = IV[0];
+ var IV_1 = IV[1];
+
+ // Generate four subvectors
+ var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
+ var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
+ var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
+ var i3 = (i2 << 16) | (i0 & 0x0000ffff);
+
+ // Modify counter values
+ C[0] ^= i0;
+ C[1] ^= i1;
+ C[2] ^= i2;
+ C[3] ^= i3;
+ C[4] ^= i0;
+ C[5] ^= i1;
+ C[6] ^= i2;
+ C[7] ^= i3;
+
+ // Iterate the system four times
+ for (var i = 0; i < 4; i++) {
+ nextState.call(this);
+ }
+ }
+ },
+
+ _doProcessBlock: function (M, offset) {
+ // Shortcut
+ var X = this._X;
+
+ // Iterate the system
+ nextState.call(this);
+
+ // Generate four keystream words
+ S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
+ S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
+ S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
+ S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
+
+ for (var i = 0; i < 4; i++) {
+ // Swap endian
+ S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
+ (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
+
+ // Encrypt
+ M[offset + i] ^= S[i];
+ }
+ },
+
+ blockSize: 128/32,
+
+ ivSize: 64/32
+ });
+
+ function nextState() {
+ // Shortcuts
+ var X = this._X;
+ var C = this._C;
+
+ // Save old counter values
+ for (var i = 0; i < 8; i++) {
+ C_[i] = C[i];
+ }
+
+ // Calculate new counter values
+ C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
+ C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
+ C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
+ C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
+ C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
+ C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
+ C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
+ C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
+ this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
+
+ // Calculate the g-values
+ for (var i = 0; i < 8; i++) {
+ var gx = X[i] + C[i];
+
+ // Construct high and low argument for squaring
+ var ga = gx & 0xffff;
+ var gb = gx >>> 16;
+
+ // Calculate high and low result of squaring
+ var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
+ var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
+
+ // High XOR low
+ G[i] = gh ^ gl;
+ }
+
+ // Calculate new state values
+ X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
+ X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
+ X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
+ X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
+ X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
+ X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
+ X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
+ X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
+ }
+
+ /**
+ * Shortcut functions to the cipher's object interface.
+ *
+ * @example
+ *
+ * var ciphertext = CryptoJS.RabbitLegacy.encrypt(message, key, cfg);
+ * var plaintext = CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg);
+ */
+ C.RabbitLegacy = StreamCipher._createHelper(RabbitLegacy);
+ }());
+
+
+ return CryptoJS;
+
+}));
\ No newline at end of file
diff --git a/g4f/Provider/retry_provider.py b/g4f/Provider/retry_provider.py
new file mode 100644
index 0000000000000000000000000000000000000000..94b9b90a4ae7edd717d08a451c8f7a73bc98f707
--- /dev/null
+++ b/g4f/Provider/retry_provider.py
@@ -0,0 +1,85 @@
+from __future__ import annotations
+
+import random
+from typing import List, Type, Dict
+from ..typing import CreateResult, Messages
+from .base_provider import BaseProvider, AsyncProvider
+from ..debug import logging
+
+
+class RetryProvider(AsyncProvider):
+ __name__: str = "RetryProvider"
+ working: bool = True
+ supports_stream: bool = True
+
+ def __init__(
+ self,
+ providers: List[Type[BaseProvider]],
+ shuffle: bool = True
+ ) -> None:
+ self.providers: List[Type[BaseProvider]] = providers
+ self.shuffle: bool = shuffle
+
+
+ def create_completion(
+ self,
+ model: str,
+ messages: Messages,
+ stream: bool = False,
+ **kwargs
+ ) -> CreateResult:
+ if stream:
+ providers = [provider for provider in self.providers if provider.supports_stream]
+ else:
+ providers = self.providers
+ if self.shuffle:
+ random.shuffle(providers)
+
+ self.exceptions: Dict[str, Exception] = {}
+ started: bool = False
+ for provider in providers:
+ try:
+ if logging:
+ print(f"Using {provider.__name__} provider")
+ for token in provider.create_completion(model, messages, stream, **kwargs):
+ yield token
+ started = True
+ if started:
+ return
+ except Exception as e:
+ self.exceptions[provider.__name__] = e
+ if logging:
+ print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
+ if started:
+ raise e
+
+ self.raise_exceptions()
+
+ async def create_async(
+ self,
+ model: str,
+ messages: Messages,
+ **kwargs
+ ) -> str:
+ providers = self.providers
+ if self.shuffle:
+ random.shuffle(providers)
+
+ self.exceptions: Dict[str, Exception] = {}
+ for provider in providers:
+ try:
+ return await provider.create_async(model, messages, **kwargs)
+ except Exception as e:
+ self.exceptions[provider.__name__] = e
+ if logging:
+ print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
+
+ self.raise_exceptions()
+
+ def raise_exceptions(self) -> None:
+ if self.exceptions:
+ raise RuntimeError("\n".join(["All providers failed:"] + [
+ f"{p}: {self.exceptions[p].__class__.__name__}: {self.exceptions[p]}" for p in self.exceptions
+ ]))
+
+ raise RuntimeError("No provider found")
\ No newline at end of file
diff --git a/g4f/Provider/unfinished/Komo.py b/g4f/Provider/unfinished/Komo.py
new file mode 100644
index 0000000000000000000000000000000000000000..84d8d634bc65cdbe265f28aae925456b694e329b
--- /dev/null
+++ b/g4f/Provider/unfinished/Komo.py
@@ -0,0 +1,44 @@
+from __future__ import annotations
+
+import json
+
+from ...requests import StreamSession
+from ...typing import AsyncGenerator
+from ..base_provider import AsyncGeneratorProvider, format_prompt
+
+class Komo(AsyncGeneratorProvider):
+ url = "https://komo.ai/api/ask"
+ supports_gpt_35_turbo = True
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ **kwargs
+ ) -> AsyncGenerator:
+ async with StreamSession(impersonate="chrome107") as session:
+ prompt = format_prompt(messages)
+ data = {
+ "query": prompt,
+ "FLAG_URLEXTRACT": "false",
+ "token": "",
+ "FLAG_MODELA": "1",
+ }
+ headers = {
+ 'authority': 'komo.ai',
+ 'accept': 'text/event-stream',
+ 'cache-control': 'no-cache',
+ 'referer': 'https://komo.ai/',
+ }
+
+ async with session.get(cls.url, params=data, headers=headers) as response:
+ response.raise_for_status()
+ next = False
+ async for line in response.iter_lines():
+ if line == b"event: line":
+ next = True
+ elif next and line.startswith(b"data: "):
+ yield json.loads(line[6:])
+ next = False
+
diff --git a/g4f/Provider/unfinished/MikuChat.py b/g4f/Provider/unfinished/MikuChat.py
new file mode 100644
index 0000000000000000000000000000000000000000..bf19631f4b59d39fa1eebe9ca2c0bce8d0a19982
--- /dev/null
+++ b/g4f/Provider/unfinished/MikuChat.py
@@ -0,0 +1,97 @@
+from __future__ import annotations
+
+import random, json
+from datetime import datetime
+from ...requests import StreamSession
+
+from ...typing import AsyncGenerator
+from ..base_provider import AsyncGeneratorProvider
+
+
+class MikuChat(AsyncGeneratorProvider):
+ url = "https://ai.okmiku.com"
+ supports_gpt_35_turbo = True
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ **kwargs
+ ) -> AsyncGenerator:
+ if not model:
+ model = "gpt-3.5-turbo"
+ headers = {
+ "authority": "api.catgpt.cc",
+ "accept": "application/json",
+ "origin": cls.url,
+ "referer": f"{cls.url}/chat/",
+ 'x-app-version': 'undefined',
+ 'x-date': get_datetime(),
+ 'x-fingerprint': get_fingerprint(),
+ 'x-platform': 'web'
+ }
+ async with StreamSession(headers=headers, impersonate="chrome107") as session:
+ data = {
+ "model": model,
+ "top_p": 0.8,
+ "temperature": 0.5,
+ "presence_penalty": 1,
+ "frequency_penalty": 0,
+ "max_tokens": 2000,
+ "stream": True,
+ "messages": messages,
+ }
+ async with session.post("https://api.catgpt.cc/ai/v1/chat/completions", json=data) as response:
+ print(await response.text())
+ response.raise_for_status()
+ async for line in response.iter_lines():
+ if line.startswith(b"data: "):
+ line = json.loads(line[6:])
+ chunk = line["choices"][0]["delta"].get("content")
+ if chunk:
+ yield chunk
+
+def k(e: str, t: int):
+ a = len(e) & 3
+ s = len(e) - a
+ i = t
+ c = 3432918353
+ o = 461845907
+ n = 0
+ r = 0
+ while n < s:
+ r = (ord(e[n]) & 255) | ((ord(e[n + 1]) & 255) << 8) | ((ord(e[n + 2]) & 255) << 16) | ((ord(e[n + 3]) & 255) << 24)
+ n += 4
+ r = (r & 65535) * c + (((r >> 16) * c & 65535) << 16) & 4294967295
+ r = (r << 15) | (r >> 17)
+ r = (r & 65535) * o + (((r >> 16) * o & 65535) << 16) & 4294967295
+ i ^= r
+ i = (i << 13) | (i >> 19)
+ l = (i & 65535) * 5 + (((i >> 16) * 5 & 65535) << 16) & 4294967295
+ i = (l & 65535) + 27492 + (((l >> 16) + 58964 & 65535) << 16)
+
+ if a == 3:
+ r ^= (ord(e[n + 2]) & 255) << 16
+ elif a == 2:
+ r ^= (ord(e[n + 1]) & 255) << 8
+ elif a == 1:
+ r ^= ord(e[n]) & 255
+ r = (r & 65535) * c + (((r >> 16) * c & 65535) << 16) & 4294967295
+ r = (r << 15) | (r >> 17)
+ r = (r & 65535) * o + (((r >> 16) * o & 65535) << 16) & 4294967295
+ i ^= r
+
+ i ^= len(e)
+ i ^= i >> 16
+ i = (i & 65535) * 2246822507 + (((i >> 16) * 2246822507 & 65535) << 16) & 4294967295
+ i ^= i >> 13
+ i = (i & 65535) * 3266489909 + (((i >> 16) * 3266489909 & 65535) << 16) & 4294967295
+ i ^= i >> 16
+ return i & 0xFFFFFFFF
+
+def get_fingerprint() -> str:
+ return str(k(str(int(random.random() * 100000)), 256))
+
+def get_datetime() -> str:
+ return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
\ No newline at end of file
diff --git a/g4f/Provider/unfinished/PerplexityAi.py b/g4f/Provider/unfinished/PerplexityAi.py
new file mode 100644
index 0000000000000000000000000000000000000000..3e0968084ea65f46a30d8f7527c63ae4efd1b146
--- /dev/null
+++ b/g4f/Provider/unfinished/PerplexityAi.py
@@ -0,0 +1,100 @@
+from __future__ import annotations
+
+import json
+import time
+import base64
+from curl_cffi.requests import AsyncSession
+
+from ..base_provider import AsyncProvider, format_prompt, get_cookies
+
+
+class PerplexityAi(AsyncProvider):
+ url = "https://www.perplexity.ai"
+ supports_gpt_35_turbo = True
+ _sources = []
+
+ @classmethod
+ async def create_async(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ proxy: str = None,
+ **kwargs
+ ) -> str:
+ url = cls.url + "/socket.io/?EIO=4&transport=polling"
+ headers = {
+ "Referer": f"{cls.url}/"
+ }
+ async with AsyncSession(headers=headers, proxies={"https": proxy}, impersonate="chrome107") as session:
+ url_session = "https://www.perplexity.ai/api/auth/session"
+ response = await session.get(url_session)
+ response.raise_for_status()
+
+ url_session = "https://www.perplexity.ai/api/auth/session"
+ response = await session.get(url_session)
+ response.raise_for_status()
+
+ response = await session.get(url, params={"t": timestamp()})
+ response.raise_for_status()
+ sid = json.loads(response.text[1:])["sid"]
+
+ response = await session.get(url, params={"t": timestamp(), "sid": sid})
+ response.raise_for_status()
+
+ data = '40{"jwt":"anonymous-ask-user"}'
+ response = await session.post(url, params={"t": timestamp(), "sid": sid}, data=data)
+ response.raise_for_status()
+
+ response = await session.get(url, params={"t": timestamp(), "sid": sid})
+ response.raise_for_status()
+
+ data = "424" + json.dumps([
+ "perplexity_ask",
+ format_prompt(messages),
+ {
+ "version":"2.1",
+ "source":"default",
+ "language":"en",
+ "timezone": time.tzname[0],
+ "search_focus":"internet",
+ "mode":"concise"
+ }
+ ])
+ response = await session.post(url, params={"t": timestamp(), "sid": sid}, data=data)
+ response.raise_for_status()
+
+ while True:
+ response = await session.get(url, params={"t": timestamp(), "sid": sid})
+ response.raise_for_status()
+ for line in response.text.splitlines():
+ if line.startswith("434"):
+ result = json.loads(json.loads(line[3:])[0]["text"])
+
+ cls._sources = [{
+ "title": source["name"],
+ "url": source["url"],
+ "snippet": source["snippet"]
+ } for source in result["web_results"]]
+
+ return result["answer"]
+
+ @classmethod
+ def get_sources(cls):
+ return cls._sources
+
+
+ @classmethod
+ @property
+ def params(cls):
+ params = [
+ ("model", "str"),
+ ("messages", "list[dict[str, str]]"),
+ ("stream", "bool"),
+ ("proxy", "str"),
+ ]
+ param = ", ".join([": ".join(p) for p in params])
+ return f"g4f.provider.{cls.__name__} supports: ({param})"
+
+
+def timestamp() -> str:
+ return base64.urlsafe_b64encode(int(time.time()-1407782612).to_bytes(4, 'big')).decode()
\ No newline at end of file
diff --git a/g4f/Provider/unfinished/__init__.py b/g4f/Provider/unfinished/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..8330b5e48860a882ac0b7e1f42197b03444d1d52
--- /dev/null
+++ b/g4f/Provider/unfinished/__init__.py
@@ -0,0 +1,3 @@
+from .MikuChat import MikuChat
+from .PerplexityAi import PerplexityAi
+from .Komo import Komo
\ No newline at end of file
diff --git a/g4f/__init__.py b/g4f/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..4d3c41c654067f5893b5eabeb0f83a1d1c5ab95a
--- /dev/null
+++ b/g4f/__init__.py
@@ -0,0 +1,112 @@
+from __future__ import annotations
+from requests import get
+from g4f.models import Model, ModelUtils
+from .Provider import BaseProvider
+from .typing import Messages, CreateResult, Union
+from .debug import logging
+
+version = '0.1.6.2'
+version_check = False
+
+def check_pypi_version() -> None:
+ try:
+ response = get("https://pypi.org/pypi/g4f/json").json()
+ latest_version = response["info"]["version"]
+
+ if version != latest_version:
+ print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
+
+ except Exception as e:
+ print(f'Failed to check g4f pypi version: {e}')
+
+def get_model_and_provider(model : Union[Model, str],
+ provider : Union[type[BaseProvider], None],
+ stream : bool) -> tuple[Model, type[BaseProvider]]:
+
+ if isinstance(model, str):
+ if model in ModelUtils.convert:
+ model = ModelUtils.convert[model]
+ else:
+ raise ValueError(f'The model: {model} does not exist')
+
+ if not provider:
+ provider = model.best_provider
+
+ if not provider:
+ raise RuntimeError(f'No provider found for model: {model}')
+
+ if not provider.working:
+ raise RuntimeError(f'{provider.__name__} is not working')
+
+ if not provider.supports_stream and stream:
+ raise ValueError(f'{provider.__name__} does not support "stream" argument')
+
+ if logging:
+ print(f'Using {provider.__name__} provider')
+
+ return model, provider
+
+class ChatCompletion:
+ @staticmethod
+ def create(model: Union[Model, str],
+ messages : Messages,
+ provider : Union[type[BaseProvider], None] = None,
+ stream : bool = False,
+ auth : Union[str, None] = None, **kwargs) -> Union[CreateResult, str]:
+
+ model, provider = get_model_and_provider(model, provider, stream)
+
+ if provider.needs_auth and not auth:
+ raise ValueError(
+ f'{provider.__name__} requires authentication (use auth=\'cookie or token or jwt ...\' param)')
+
+ if provider.needs_auth:
+ kwargs['auth'] = auth
+
+ result = provider.create_completion(model.name, messages, stream, **kwargs)
+ return result if stream else ''.join(result)
+
+ @staticmethod
+ async def create_async(
+ model : Union[Model, str],
+ messages: Messages,
+ provider: Union[type[BaseProvider], None] = None,
+ stream : bool = False, **kwargs) -> str:
+
+ if stream:
+ raise ValueError(f'"create_async" does not support "stream" argument')
+
+ model, provider = get_model_and_provider(model, provider, False)
+
+ return await provider.create_async(model.name, messages, **kwargs)
+
+class Completion:
+ @staticmethod
+ def create(
+ model: str,
+ prompt: str,
+ provider: Union[type[BaseProvider], None] = None,
+ stream: bool = False,
+ **kwargs
+ ) -> Union[CreateResult, str]:
+
+ allowed_models = [
+ 'code-davinci-002',
+ 'text-ada-001',
+ 'text-babbage-001',
+ 'text-curie-001',
+ 'text-davinci-002',
+ 'text-davinci-003'
+ ]
+
+ if model not in allowed_models:
+ raise Exception(f'ValueError: Can\'t use {model} with Completion.create()')
+
+ model, provider = get_model_and_provider(model, provider, stream)
+
+ result = provider.create_completion(model.name, [{"role": "user", "content": prompt}], stream, **kwargs)
+
+ return result if stream else ''.join(result)
+
+if version_check:
+ check_pypi_version()
\ No newline at end of file
diff --git a/g4f/debug.py b/g4f/debug.py
new file mode 100644
index 0000000000000000000000000000000000000000..28b1182b9052e632ac7100c79249afc89fa4c215
--- /dev/null
+++ b/g4f/debug.py
@@ -0,0 +1 @@
+logging = True
\ No newline at end of file
diff --git a/g4f/models.py b/g4f/models.py
new file mode 100644
index 0000000000000000000000000000000000000000..7f4de8ba6f25ca6b4de9af603ad9646092117b87
--- /dev/null
+++ b/g4f/models.py
@@ -0,0 +1,234 @@
+from __future__ import annotations
+from dataclasses import dataclass
+from .typing import Union
+from .Provider import BaseProvider, RetryProvider
+from .Provider import *
+
+@dataclass(unsafe_hash=True)
+class Model:
+ name: str
+ base_provider: str
+ best_provider: Union[type[BaseProvider], RetryProvider] = None
+
+default = Model(
+ name = "",
+ base_provider = "",
+ best_provider = RetryProvider([
+ ChatgptDuo, # Include search results
+ Aibn, Aichat, ChatgptAi, ChatgptLogin, FreeGpt, GptGo, Myshell, Ylokh,
+ ])
+)
+
+# GPT-3.5 too, but all providers supports long responses and a custom timeouts
+gpt_35_long = Model(
+ name = 'gpt-3.5-turbo',
+ base_provider = 'openai',
+ best_provider = RetryProvider([
+ AiAsk, Aibn, Aichat, ChatgptAi, ChatgptDemo, ChatgptDuo,
+ FreeGpt, GptGo, Liaobots, Myshell, Vitalentum, Ylokh, You, Yqcloud,
+ GPTalk, GptGod
+ ])
+)
+
+# GPT-3.5 / GPT-4
+gpt_35_turbo = Model(
+ name = 'gpt-3.5-turbo',
+ base_provider = 'openai',
+ best_provider = RetryProvider([
+ ChatgptLogin, ChatgptAi, GptGo, AItianhu, Aichat, AItianhuSpace, Myshell, Aibn, FreeGpt, Ylokh
+ ])
+)
+gpt_35_turbo_0613 = Model(
+ name = 'gpt-3.5-turbo-0613',
+ base_provider = 'openai',
+ best_provider = NeuroGPT)
+
+gpt_4 = Model(
+ name = 'gpt-4',
+ base_provider = 'openai',
+ best_provider = NeuroGPT)
+
+# Bard
+palm = Model(
+ name = 'palm',
+ base_provider = 'google',
+ best_provider = Bard)
+
+# H2o
+falcon_7b = Model(
+ name = 'h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v3',
+ base_provider = 'huggingface',
+ best_provider = H2o)
+
+falcon_40b = Model(
+ name = 'h2oai/h2ogpt-gm-oasst1-en-2048-falcon-40b-v1',
+ base_provider = 'huggingface',
+ best_provider = H2o)
+
+llama_13b = Model(
+ name = 'h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-13b',
+ base_provider = 'huggingface',
+ best_provider = H2o)
+
+# Vercel
+claude_instant_v1 = Model(
+ name = 'claude-instant-v1',
+ base_provider = 'anthropic',
+ best_provider = Vercel)
+
+claude_v1 = Model(
+ name = 'claude-v1',
+ base_provider = 'anthropic',
+ best_provider = Vercel)
+
+claude_v2 = Model(
+ name = 'claude-v2',
+ base_provider = 'anthropic',
+ best_provider = Vercel)
+
+command_light_nightly = Model(
+ name = 'command-light-nightly',
+ base_provider = 'cohere',
+ best_provider = Vercel)
+
+command_nightly = Model(
+ name = 'command-nightly',
+ base_provider = 'cohere',
+ best_provider = Vercel)
+
+gpt_neox_20b = Model(
+ name = 'EleutherAI/gpt-neox-20b',
+ base_provider = 'huggingface',
+ best_provider = Vercel)
+
+oasst_sft_1_pythia_12b = Model(
+ name = 'OpenAssistant/oasst-sft-1-pythia-12b',
+ base_provider = 'huggingface',
+ best_provider = Vercel)
+
+oasst_sft_4_pythia_12b_epoch_35 = Model(
+ name = 'OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5',
+ base_provider = 'huggingface',
+ best_provider = Vercel)
+
+santacoder = Model(
+ name = 'bigcode/santacoder',
+ base_provider = 'huggingface',
+ best_provider = Vercel)
+
+bloom = Model(
+ name = 'bigscience/bloom',
+ base_provider = 'huggingface',
+ best_provider = Vercel)
+
+flan_t5_xxl = Model(
+ name = 'google/flan-t5-xxl',
+ base_provider = 'huggingface',
+ best_provider = Vercel)
+
+code_davinci_002 = Model(
+ name = 'code-davinci-002',
+ base_provider = 'openai',
+ best_provider = Vercel)
+
+gpt_35_turbo_16k = Model(
+ name = 'gpt-3.5-turbo-16k',
+ base_provider = 'openai',
+ best_provider = NeuroGPT)
+
+gpt_35_turbo_16k_0613 = Model(
+ name = 'gpt-3.5-turbo-16k-0613',
+ base_provider = 'openai',
+ best_provider = NeuroGPT)
+
+gpt_35_turbo_0613 = Model(
+ name = 'gpt-3.5-turbo-0613',
+ base_provider = 'openai',
+ best_provider = NeuroGPT)
+
+gpt_4_0613 = Model(
+ name = 'gpt-4-0613',
+ base_provider = 'openai',
+ best_provider = NeuroGPT)
+
+text_ada_001 = Model(
+ name = 'text-ada-001',
+ base_provider = 'openai',
+ best_provider = Vercel)
+
+text_babbage_001 = Model(
+ name = 'text-babbage-001',
+ base_provider = 'openai',
+ best_provider = Vercel)
+
+text_curie_001 = Model(
+ name = 'text-curie-001',
+ base_provider = 'openai',
+ best_provider = Vercel)
+
+text_davinci_002 = Model(
+ name = 'text-davinci-002',
+ base_provider = 'openai',
+ best_provider = Vercel)
+
+text_davinci_003 = Model(
+ name = 'text-davinci-003',
+ base_provider = 'openai',
+ best_provider = Vercel)
+
+llama13b_v2_chat = Model(
+ name = 'replicate:a16z-infra/llama13b-v2-chat',
+ base_provider = 'replicate',
+ best_provider = Vercel)
+
+llama7b_v2_chat = Model(
+ name = 'replicate:a16z-infra/llama7b-v2-chat',
+ base_provider = 'replicate',
+ best_provider = Vercel)
+
+
+class ModelUtils:
+ convert: dict[str, Model] = {
+ # gpt-3.5 / gpt-4
+ 'gpt-3.5-turbo' : gpt_35_turbo,
+ 'gpt-3.5-turbo-0613' : gpt_35_turbo_0613,
+ 'gpt-3.5-turbo-16k' : gpt_35_turbo_16k,
+ 'gpt-4' : gpt_4,
+ 'gpt-4-0613' : gpt_4_0613,
+ 'gpt-3.5-turbo-16k-0613' : gpt_35_turbo_16k_0613,
+
+ # Bard
+ 'palm2' : palm,
+ 'palm' : palm,
+ 'google' : palm,
+ 'google-bard' : palm,
+ 'google-palm' : palm,
+ 'bard' : palm,
+
+ # H2o
+ 'falcon-40b' : falcon_40b,
+ 'falcon-7b' : falcon_7b,
+ 'llama-13b' : llama_13b,
+
+ # Vercel
+ 'claude-instant-v1' : claude_instant_v1,
+ 'claude-v1' : claude_v1,
+ 'claude-v2' : claude_v2,
+ 'command-nightly' : command_nightly,
+ 'gpt-neox-20b' : gpt_neox_20b,
+ 'santacoder' : santacoder,
+ 'bloom' : bloom,
+ 'flan-t5-xxl' : flan_t5_xxl,
+ 'code-davinci-002' : code_davinci_002,
+ 'text-ada-001' : text_ada_001,
+ 'text-babbage-001' : text_babbage_001,
+ 'text-curie-001' : text_curie_001,
+ 'text-davinci-002' : text_davinci_002,
+ 'text-davinci-003' : text_davinci_003,
+ 'llama13b-v2-chat' : llama13b_v2_chat,
+ 'llama7b-v2-chat' : llama7b_v2_chat,
+
+ 'oasst-sft-1-pythia-12b' : oasst_sft_1_pythia_12b,
+ 'oasst-sft-4-pythia-12b-epoch-3.5' : oasst_sft_4_pythia_12b_epoch_35,
+ 'command-light-nightly' : command_light_nightly,
+ }
\ No newline at end of file
diff --git a/g4f/requests.py b/g4f/requests.py
new file mode 100644
index 0000000000000000000000000000000000000000..f238062e3e3832d79a5e12b1d3f96c11d708cb0e
--- /dev/null
+++ b/g4f/requests.py
@@ -0,0 +1,181 @@
+from __future__ import annotations
+
+import warnings
+import json
+import asyncio
+from functools import partialmethod
+from asyncio import Future, Queue
+from typing import AsyncGenerator, Union, Optional
+
+from curl_cffi.requests import AsyncSession, Response
+import curl_cffi
+
+is_newer_0_5_8: bool = hasattr(AsyncSession, "_set_cookies") or hasattr(curl_cffi.requests.Cookies, "get_cookies_for_curl")
+is_newer_0_5_9: bool = hasattr(curl_cffi.AsyncCurl, "remove_handle")
+is_newer_0_5_10: bool = hasattr(AsyncSession, "release_curl")
+
+
+class StreamResponse:
+ def __init__(self, inner: Response, queue: Queue[bytes]) -> None:
+ self.inner: Response = inner
+ self.queue: Queue[bytes] = queue
+ self.request = inner.request
+ self.status_code: int = inner.status_code
+ self.reason: str = inner.reason
+ self.ok: bool = inner.ok
+ self.headers = inner.headers
+ self.cookies = inner.cookies
+
+ async def text(self) -> str:
+ content: bytes = await self.read()
+ return content.decode()
+
+ def raise_for_status(self) -> None:
+ if not self.ok:
+ raise RuntimeError(f"HTTP Error {self.status_code}: {self.reason}")
+
+ async def json(self, **kwargs) -> dict:
+ return json.loads(await self.read(), **kwargs)
+
+ async def iter_lines(
+ self, chunk_size: Optional[int] = None, decode_unicode: bool = False, delimiter: Optional[str] = None
+ ) -> AsyncGenerator[bytes, None]:
+ """
+ Copied from: https://requests.readthedocs.io/en/latest/_modules/requests/models/
+ which is under the License: Apache 2.0
+ """
+
+ pending: bytes = None
+
+ async for chunk in self.iter_content(
+ chunk_size=chunk_size, decode_unicode=decode_unicode
+ ):
+ if pending is not None:
+ chunk = pending + chunk
+ if delimiter:
+ lines = chunk.split(delimiter)
+ else:
+ lines = chunk.splitlines()
+ if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:
+ pending = lines.pop()
+ else:
+ pending = None
+
+ for line in lines:
+ yield line
+
+ if pending is not None:
+ yield pending
+
+ async def iter_content(
+ self, chunk_size: Optional[int] = None, decode_unicode: bool = False
+ ) -> AsyncGenerator[bytes, None]:
+ if chunk_size:
+ warnings.warn("chunk_size is ignored, there is no way to tell curl that.")
+ if decode_unicode:
+ raise NotImplementedError()
+ while True:
+ chunk = await self.queue.get()
+ if chunk is None:
+ return
+ yield chunk
+
+ async def read(self) -> bytes:
+ return b"".join([chunk async for chunk in self.iter_content()])
+
+
+class StreamRequest:
+ def __init__(self, session: AsyncSession, method: str, url: str, **kwargs: Union[bool, int, str]) -> None:
+ self.session: AsyncSession = session
+ self.loop: asyncio.AbstractEventLoop = session.loop if session.loop else asyncio.get_running_loop()
+ self.queue: Queue[bytes] = Queue()
+ self.method: str = method
+ self.url: str = url
+ self.options: dict = kwargs
+ self.handle: Optional[curl_cffi.AsyncCurl] = None
+
+ def _on_content(self, data: bytes) -> None:
+ if not self.enter.done():
+ self.enter.set_result(None)
+ self.queue.put_nowait(data)
+
+ def _on_done(self, task: Future) -> None:
+ if not self.enter.done():
+ self.enter.set_result(None)
+ self.queue.put_nowait(None)
+
+ self.loop.call_soon(self.release_curl)
+
+ async def fetch(self) -> StreamResponse:
+ if self.handle:
+ raise RuntimeError("Request already started")
+ self.curl: curl_cffi.AsyncCurl = await self.session.pop_curl()
+ self.enter: asyncio.Future = self.loop.create_future()
+ if is_newer_0_5_10:
+ request, _, header_buffer, _, _ = self.session._set_curl_options(
+ self.curl,
+ self.method,
+ self.url,
+ content_callback=self._on_content,
+ **self.options
+ )
+ else:
+ request, _, header_buffer = self.session._set_curl_options(
+ self.curl,
+ self.method,
+ self.url,
+ content_callback=self._on_content,
+ **self.options
+ )
+ if is_newer_0_5_9:
+ self.handle = self.session.acurl.add_handle(self.curl)
+ else:
+ await self.session.acurl.add_handle(self.curl, False)
+ self.handle = self.session.acurl._curl2future[self.curl]
+ self.handle.add_done_callback(self._on_done)
+ # Wait for headers
+ await self.enter
+ # Raise exceptions
+ if self.handle.done():
+ self.handle.result()
+ if is_newer_0_5_8:
+ response = self.session._parse_response(self.curl, _, header_buffer)
+ response.request = request
+ else:
+ response = self.session._parse_response(self.curl, request, _, header_buffer)
+ return StreamResponse(response, self.queue)
+
+ async def __aenter__(self) -> StreamResponse:
+ return await self.fetch()
+
+ async def __aexit__(self, *args) -> None:
+ self.release_curl()
+
+ def release_curl(self) -> None:
+ if is_newer_0_5_10:
+ self.session.release_curl(self.curl)
+ return
+ if not self.curl:
+ return
+ self.curl.clean_after_perform()
+ if is_newer_0_5_9:
+ self.session.acurl.remove_handle(self.curl)
+ elif not self.handle.done() and not self.handle.cancelled():
+ self.session.acurl.set_result(self.curl)
+ self.curl.reset()
+ self.session.push_curl(self.curl)
+ self.curl = None
+
+
+class StreamSession(AsyncSession):
+ def request(
+ self, method: str, url: str, **kwargs
+ ) -> StreamRequest:
+ return StreamRequest(self, method, url, **kwargs)
+
+ head = partialmethod(request, "HEAD")
+ get = partialmethod(request, "GET")
+ post = partialmethod(request, "POST")
+ put = partialmethod(request, "PUT")
+ patch = partialmethod(request, "PATCH")
+ delete = partialmethod(request, "DELETE")
\ No newline at end of file
diff --git a/g4f/typing.py b/g4f/typing.py
new file mode 100644
index 0000000000000000000000000000000000000000..cfddf4a82a0cb60f455fd2e775ea9c22132cb7b8
--- /dev/null
+++ b/g4f/typing.py
@@ -0,0 +1,22 @@
+import sys
+from typing import Any, AsyncGenerator, Generator, NewType, Tuple, Union, List, Dict
+
+if sys.version_info >= (3, 8):
+ from typing import TypedDict
+else:
+ from typing_extensions import TypedDict
+
+SHA256 = NewType('sha_256_hash', str)
+CreateResult = Generator[str, None, None]
+AsyncResult = AsyncGenerator[str, None]
+Messages = List[Dict[str, str]]
+
+__all__ = [
+ 'Any',
+ 'AsyncGenerator',
+ 'Generator',
+ 'Tuple',
+ 'TypedDict',
+ 'SHA256',
+ 'CreateResult',
+]
diff --git a/install_on_huggingface.md b/install_on_huggingface.md
new file mode 100644
index 0000000000000000000000000000000000000000..1f475a16b7d51b5dc48db9e7842058d137d5b6af
--- /dev/null
+++ b/install_on_huggingface.md
@@ -0,0 +1,84 @@
+# Инструкция для linux, но на windows не сильно должно отличаться (мб кто дополнит)
+
+### Клонируем репозиторий и переходим в папку проекта:
+
+```bash
+➜ ~/git/ git clone https://github.com/Em1tSan/NeuroGPT
+Клонирование в «NeuroGPT»...
+remote: Enumerating objects: 859, done.
+remote: Counting objects: 100% (262/262), done.
+remote: Compressing objects: 100% (207/207), done.
+remote: Total 859 (delta 133), reused 125 (delta 55), pack-reused 597
+Получение объектов: 100% (859/859), 1.33 МиБ | 5.67 МиБ/с, готово.
+Определение изменений: 100% (473/473), готово.
+
+➜ ~/git/ cd NeuroGPT
+```
+
+## ==Получаем ключ ChimeraApi по основной инструкции и вносим его в config.json.==
+
+### Создаем venv и устанавливаем gradio:
+
+```bash
+➜ ~/git/NeuroGPT/ python -m venv venv
+➜ ~/git/NeuroGPT/ source venv/bin/activate
+(venv) ➜ ~/git/NeuroGPT/ [main*] pip install gradio
+```
+
+* * *
+
+## Деплой проекта на Huggingface:
+
+Для этого регаемся на [huggingface](https://huggingface.co) и получаем токен по ссылке (нужен токен с правами write) [tokens](https://huggingface.co/settings/tokens)
+
+Теперь можно запустить деплой проекта, нам понадобиться токен когда его спросят, все остальные поля либо оставляем как есть, либо заполняем своими данными. Важно: бесплатные мощности идут только на **cpu-basic**, поэтому его не меняем.
+
+==Деплой запускается командой **gradio deploy** в активированом venv в корне проекта.==
+
+```bash
+(venv) ➜ NeuroGPT git:(main) gradio deploy
+Need 'write' access token to create a Spaces repo.
+
+ _| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
+ _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
+ _|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
+ _| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
+ _| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
+
+ A token is already saved on your machine. Run `huggingface-cli whoami` to get more information or `huggingface-cli logout` if you want to log out.
+ Setting a new token will erase the existing one.
+ To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
+Token:
+Add token as git credential? (Y/n) Y
+Token is valid (permission: write).
+Cannot authenticate through git-credential as no helper is defined on your machine.
+You might have to re-authenticate when pushing to the Hugging Face Hub.
+Run the following command in your terminal in case you want to set the 'store' credential helper as default.
+
+git config --global credential.helper store
+
+Read https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage for more details.
+Token has not been saved to git credential helper.
+Your token has been saved to /root/.cache/huggingface/token
+Login successful
+Creating new Spaces Repo in '/root/git/NeuroGPT'. Collecting metadata, press Enter to accept default value.
+Enter Spaces app title [NeuroGPT]:
+Enter Gradio app file [webui.py]:
+Enter Spaces hardware (cpu-basic, cpu-upgrade, t4-small, t4-medium, a10g-small, a10g-large, a100-large) [cpu-basic]:
+Any Spaces secrets (y/n) [n]:
+Create Github Action to automatically update Space on 'git push'? [n]:
+
+Space available at https://huggingface.co/spaces/<ваш ник>/<имя проекта>
+```
+
+### Деплой займет какое-то время, после чего ссылка на проект будет в конце работы как на примере выше или проект можно найти по пути:
+
+`https://huggingface.co/spaces/<ваш ник на huggingface>/<имя проекта заданное на этапе деплоя>`
+
+### Для ограничения доступа к проекту надо сделать его приватным, для этого открываем страницу проекта и в правом верхнем углу нажимаем Settings, после чего листаем вниз и переключаем с public на private.
+
+* * *
+
+## Для обновления:
+
+Переходим в каталог проекта, активируем venv, выполняем git pull и затем gradio deploy.
diff --git a/lang.txt b/lang.txt
new file mode 100644
index 0000000000000000000000000000000000000000..5417272c82a9b646eb6fd0f4b6fd5988548250d6
--- /dev/null
+++ b/lang.txt
@@ -0,0 +1 @@
+ru_RU
diff --git a/modules/__init__.py b/modules/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/modules/config.py b/modules/config.py
new file mode 100644
index 0000000000000000000000000000000000000000..e8fb9ed2294d91eca66f1b37d6d5a8653836ea4f
--- /dev/null
+++ b/modules/config.py
@@ -0,0 +1,218 @@
+from collections import defaultdict
+from contextlib import contextmanager
+import os
+import logging
+import sys
+import json
+
+from . import shared
+from . import presets
+
+
+__all__ = [
+ "my_api_key",
+ "authflag",
+ "auth_list",
+ "dockerflag",
+ "retrieve_proxy",
+ "log_level",
+ "advance_docs",
+ "update_doc_config",
+ "usage_limit",
+ "multi_api_key",
+ "server_name",
+ "server_port",
+ "share",
+ "check_update",
+ "latex_delimiters_set",
+ "hide_history_when_not_logged_in",
+ "default_chuanhu_assistant_model"
+]
+
+if os.path.exists("config.json"):
+ with open("config.json", "r") as f:
+ config = json.load(f)
+else:
+ config = {}
+
+lang_config = config.get("language", "auto")
+language = os.environ.get("LANGUAGE", lang_config)
+
+hide_history_when_not_logged_in = config.get("hide_history_when_not_logged_in", False)
+check_update = config.get("check_update", True)
+
+if os.path.exists("api_key.txt"):
+ logging.info("检测到api_key.txt文件,正在进行迁移...")
+ with open("api_key.txt", "r") as f:
+ config["openai_api_key"] = f.read().strip()
+ os.rename("api_key.txt", "api_key(deprecated).txt")
+ with open("config.json", "w") as f:
+ json.dump(config, f, indent=4, ensure_ascii=False)
+
+if os.path.exists("auth.json"):
+ logging.info("检测到auth.json文件,正在进行迁移...")
+ auth_list = []
+ with open("auth.json", "r") as f:
+ auth = json.load(f)
+ for _ in auth:
+ if auth[_]["username"] and auth[_]["password"]:
+ auth_list.append((auth[_]["username"], auth[_]["password"]))
+ else:
+ logging.error("请检查auth.json文件中的用户名和密码!")
+ sys.exit(1)
+ config["users"] = auth_list
+ os.rename("auth.json", "auth(deprecated).json")
+ with open("config.json", "w") as f:
+ json.dump(config, f, indent=4, ensure_ascii=False)
+
+## 处理docker if we are running in Docker
+dockerflag = config.get("dockerflag", False)
+if os.environ.get("dockerrun") == "yes":
+ dockerflag = True
+
+## 处理 api-key 以及 允许的用户列表
+my_api_key = config.get("openai_api_key", "")
+my_api_key = os.environ.get("OPENAI_API_KEY", my_api_key)
+
+usage_limit = os.environ.get("USAGE_LIMIT", config.get("usage_limit", 120))
+
+## 多账户机制
+multi_api_key = config.get("multi_api_key", False) # 是否开启多账户机制
+if multi_api_key:
+ api_key_list = config.get("api_key_list", [])
+ if len(api_key_list) == 0:
+ logging.error("多账号模式已开启,但api_key_list为空,请检查config.json")
+ sys.exit(1)
+ shared.state.set_api_key_queue(api_key_list)
+
+auth_list = config.get("users", []) # 实际上是使用者的列表
+authflag = len(auth_list) > 0 # 是否开启认证的状态值,改为判断auth_list长度
+
+# 处理自定义的api_host,优先读环境变量的配置,如果存在则自动装配
+api_host = os.environ.get("OPENAI_API_BASE", config.get("openai_api_base", None))
+if api_host is not None:
+ shared.state.set_api_host(api_host)
+
+default_chuanhu_assistant_model = config.get("default_chuanhu_assistant_model", "gpt-3.5-turbo")
+for x in ["GOOGLE_CSE_ID", "GOOGLE_API_KEY", "WOLFRAM_ALPHA_APPID", "SERPAPI_API_KEY"]:
+ if config.get(x, None) is not None:
+ os.environ[x] = config[x]
+
+@contextmanager
+def retrieve_openai_api(api_key = None):
+ old_api_key = os.environ.get("OPENAI_API_KEY", "")
+ if api_key is None:
+ os.environ["OPENAI_API_KEY"] = my_api_key
+ yield my_api_key
+ else:
+ os.environ["OPENAI_API_KEY"] = api_key
+ yield api_key
+ os.environ["OPENAI_API_KEY"] = old_api_key
+
+## 处理log
+log_level = config.get("log_level", "INFO")
+logging.basicConfig(
+ level=log_level,
+ format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
+)
+
+## 处理代理:
+http_proxy = os.environ.get("HTTP_PROXY", "")
+https_proxy = os.environ.get("HTTPS_PROXY", "")
+http_proxy = config.get("http_proxy", http_proxy)
+https_proxy = config.get("https_proxy", https_proxy)
+
+# 重置系统变量,在不需要设置的时候不设置环境变量,以免引起全局代理报错
+os.environ["HTTP_PROXY"] = ""
+os.environ["HTTPS_PROXY"] = ""
+
+local_embedding = config.get("local_embedding", False) # 是否使用本地embedding
+
+@contextmanager
+def retrieve_proxy(proxy=None):
+ """
+ 1, 如果proxy = NONE,设置环境变量,并返回最新设置的代理
+ 2,如果proxy != NONE,更新当前的代理配置,但是不更新环境变量
+ """
+ global http_proxy, https_proxy
+ if proxy is not None:
+ http_proxy = proxy
+ https_proxy = proxy
+ yield http_proxy, https_proxy
+ else:
+ old_var = os.environ["HTTP_PROXY"], os.environ["HTTPS_PROXY"]
+ os.environ["HTTP_PROXY"] = http_proxy
+ os.environ["HTTPS_PROXY"] = https_proxy
+ yield http_proxy, https_proxy # return new proxy
+
+ # return old proxy
+ os.environ["HTTP_PROXY"], os.environ["HTTPS_PROXY"] = old_var
+
+## 处理latex options
+user_latex_option = config.get("latex_option", "default")
+if user_latex_option == "default":
+ latex_delimiters_set = [
+ {"left": "$$", "right": "$$", "display": True},
+ {"left": "$", "right": "$", "display": False},
+ {"left": "\\(", "right": "\\)", "display": False},
+ {"left": "\\[", "right": "\\]", "display": True},
+ ]
+elif user_latex_option == "strict":
+ latex_delimiters_set = [
+ {"left": "$$", "right": "$$", "display": True},
+ {"left": "\\(", "right": "\\)", "display": False},
+ {"left": "\\[", "right": "\\]", "display": True},
+ ]
+elif user_latex_option == "all":
+ latex_delimiters_set = [
+ {"left": "$$", "right": "$$", "display": True},
+ {"left": "$", "right": "$", "display": False},
+ {"left": "\\(", "right": "\\)", "display": False},
+ {"left": "\\[", "right": "\\]", "display": True},
+ {"left": "\\begin{equation}", "right": "\\end{equation}", "display": True},
+ {"left": "\\begin{align}", "right": "\\end{align}", "display": True},
+ {"left": "\\begin{alignat}", "right": "\\end{alignat}", "display": True},
+ {"left": "\\begin{gather}", "right": "\\end{gather}", "display": True},
+ {"left": "\\begin{CD}", "right": "\\end{CD}", "display": True},
+ ]
+elif user_latex_option == "disabled":
+ latex_delimiters_set = []
+else:
+ latex_delimiters_set = [
+ {"left": "$$", "right": "$$", "display": True},
+ {"left": "$", "right": "$", "display": False},
+ {"left": "\\(", "right": "\\)", "display": False},
+ {"left": "\\[", "right": "\\]", "display": True},
+ ]
+
+## 处理advance docs
+advance_docs = defaultdict(lambda: defaultdict(dict))
+advance_docs.update(config.get("advance_docs", {}))
+def update_doc_config(two_column_pdf):
+ global advance_docs
+ advance_docs["pdf"]["two_column"] = two_column_pdf
+
+ logging.info(f"更新后的文件参数为:{advance_docs}")
+
+## 处理gradio.launch参数
+server_name = config.get("server_name", None)
+server_port = config.get("server_port", None)
+if server_name is None:
+ if dockerflag:
+ server_name = "0.0.0.0"
+ else:
+ server_name = "127.0.0.1"
+if server_port is None:
+ if dockerflag:
+ server_port = 7860
+
+assert server_port is None or type(server_port) == int, "要求port设置为int类型"
+
+# 设置默认model
+default_model = config.get("default_model", "")
+try:
+ presets.DEFAULT_MODEL = presets.MODELS.index(default_model)
+except ValueError:
+ pass
+
+share = config.get("share", False)
diff --git a/modules/index_func.py b/modules/index_func.py
new file mode 100644
index 0000000000000000000000000000000000000000..85e12641a30597abc6d7cce5f36118c53552acca
--- /dev/null
+++ b/modules/index_func.py
@@ -0,0 +1,149 @@
+import os
+import logging
+
+import colorama
+import PyPDF2
+from tqdm import tqdm
+
+from modules.presets import *
+from modules.utils import *
+from modules.config import local_embedding
+
+
+def get_index_name(file_src):
+ file_paths = [x.name for x in file_src]
+ file_paths.sort(key=lambda x: os.path.basename(x))
+
+ md5_hash = hashlib.md5()
+ for file_path in file_paths:
+ with open(file_path, "rb") as f:
+ while chunk := f.read(8192):
+ md5_hash.update(chunk)
+
+ return md5_hash.hexdigest()
+
+
+def get_documents(file_src):
+ from langchain.schema import Document
+ from langchain.text_splitter import TokenTextSplitter
+ text_splitter = TokenTextSplitter(chunk_size=500, chunk_overlap=30)
+
+ documents = []
+ logging.debug("Loading documents...")
+ logging.debug(f"file_src: {file_src}")
+ for file in file_src:
+ filepath = file.name
+ filename = os.path.basename(filepath)
+ file_type = os.path.splitext(filename)[1]
+ logging.info(f"loading file: {filename}")
+ try:
+ if file_type == ".pdf":
+ logging.debug("Loading PDF...")
+ try:
+ from modules.pdf_func import parse_pdf
+ from modules.config import advance_docs
+
+ two_column = advance_docs["pdf"].get("two_column", False)
+ pdftext = parse_pdf(filepath, two_column).text
+ except:
+ pdftext = ""
+ with open(filepath, "rb") as pdfFileObj:
+ pdfReader = PyPDF2.PdfReader(pdfFileObj)
+ for page in tqdm(pdfReader.pages):
+ pdftext += page.extract_text()
+ texts = [Document(page_content=pdftext, metadata={"source": filepath})]
+ elif file_type == ".docx":
+ logging.debug("Loading Word...")
+ from langchain.document_loaders import UnstructuredWordDocumentLoader
+ loader = UnstructuredWordDocumentLoader(filepath)
+ texts = loader.load()
+ elif file_type == ".pptx":
+ logging.debug("Loading PowerPoint...")
+ from langchain.document_loaders import UnstructuredPowerPointLoader
+ loader = UnstructuredPowerPointLoader(filepath)
+ texts = loader.load()
+ elif file_type == ".epub":
+ logging.debug("Loading EPUB...")
+ from langchain.document_loaders import UnstructuredEPubLoader
+ loader = UnstructuredEPubLoader(filepath)
+ texts = loader.load()
+ elif file_type == ".xlsx":
+ logging.debug("Loading Excel...")
+ text_list = excel_to_string(filepath)
+ texts = []
+ for elem in text_list:
+ texts.append(Document(page_content=elem, metadata={"source": filepath}))
+ else:
+ logging.debug("Loading text file...")
+ from langchain.document_loaders import TextLoader
+ loader = TextLoader(filepath)
+ texts = loader.load()
+ except Exception as e:
+ import traceback
+ logging.error(f"Error loading file: {filename}")
+ traceback.print_exc()
+
+ texts = text_splitter.split_documents(texts)
+ documents.extend(texts)
+ logging.debug("Documents loaded.")
+ return documents
+
+
+def construct_index(
+ api_key,
+ file_src,
+ max_input_size=4096,
+ num_outputs=5,
+ max_chunk_overlap=20,
+ chunk_size_limit=600,
+ embedding_limit=None,
+ separator=" ",
+):
+ from langchain.chat_models import ChatOpenAI
+ from langchain.vectorstores import FAISS
+
+ if api_key:
+ os.environ["OPENAI_API_KEY"] = api_key
+ else:
+ # 由于一个依赖的愚蠢的设计,这里必须要有一个API KEY
+ os.environ["OPENAI_API_KEY"] = "sk-xxxxxxx"
+ chunk_size_limit = None if chunk_size_limit == 0 else chunk_size_limit
+ embedding_limit = None if embedding_limit == 0 else embedding_limit
+ separator = " " if separator == "" else separator
+
+ index_name = get_index_name(file_src)
+ index_path = f"./index/{index_name}"
+ if local_embedding:
+ try:
+ import sentence_transformers
+ except ImportError:
+ logging.error(
+ colorama.Back.RED
+ + "\nВ вашей системе не найден модуль torch. Для работы с файлами, вам необходимо установить пакеты из файла requirements_advanced.txt"
+ + colorama.Style.RESET_ALL
+ )
+ from langchain.embeddings.huggingface import HuggingFaceEmbeddings
+ embeddings = HuggingFaceEmbeddings(model_name = "sentence-transformers/distiluse-base-multilingual-cased-v2")
+ else:
+ from langchain.embeddings import OpenAIEmbeddings
+ embeddings = OpenAIEmbeddings(openai_api_base=os.environ.get("OPENAI_API_BASE", "https://purgpt.xyz/v1/embeddings"), openai_api_key=os.environ.get("OPENAI_EMBEDDING_API_KEY", "purgpt-b2vrs9w13oiyf14a7v4lt"))
+ if os.path.exists(index_path):
+ logging.info("Найдена кешированная индексация, загружаю ...")
+ return FAISS.load_local(index_path, embeddings)
+ else:
+ try:
+ documents = get_documents(file_src)
+ logging.info("Создание индексации ...")
+ with retrieve_proxy():
+ index = FAISS.from_documents(documents, embeddings)
+ logging.debug("Индексация завершена!")
+ os.makedirs("./index", exist_ok=True)
+ index.save_local(index_path)
+ logging.debug("Индексация была сохранена локально!")
+ return index
+
+ except Exception as e:
+ import traceback
+ logging.error("Сбоц индексации!%s", e)
+ traceback.print_exc()
+ return None
diff --git a/modules/models/ChuanhuAgent.py b/modules/models/ChuanhuAgent.py
new file mode 100644
index 0000000000000000000000000000000000000000..34c3c60e84fad58a82e5c46e8ef88181e7e8009b
--- /dev/null
+++ b/modules/models/ChuanhuAgent.py
@@ -0,0 +1,240 @@
+from langchain.chains.summarize import load_summarize_chain
+from langchain import PromptTemplate, LLMChain
+from langchain.chat_models import ChatOpenAI
+from langchain.prompts import PromptTemplate
+from langchain.text_splitter import TokenTextSplitter
+from langchain.embeddings import OpenAIEmbeddings
+from langchain.vectorstores import FAISS
+from langchain.chains import RetrievalQA
+from langchain.agents import load_tools
+from langchain.agents import initialize_agent
+from langchain.agents import AgentType
+from langchain.docstore.document import Document
+from langchain.tools import BaseTool, StructuredTool, Tool, tool
+from langchain.callbacks.stdout import StdOutCallbackHandler
+from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
+from langchain.callbacks.manager import BaseCallbackManager
+from duckduckgo_search import DDGS
+from itertools import islice
+
+from typing import Any, Dict, List, Optional, Union
+
+from langchain.callbacks.base import BaseCallbackHandler
+from langchain.input import print_text
+from langchain.schema import AgentAction, AgentFinish, LLMResult
+
+from langchain.llms.base import LLM
+from langchain_g4f import G4FLLM
+
+from pydantic import BaseModel, Field
+
+import requests
+from bs4 import BeautifulSoup
+from threading import Thread, Condition
+from collections import deque
+
+from .base_model import BaseLLMModel, CallbackToIterator, ChuanhuCallbackHandler
+from ..config import default_chuanhu_assistant_model
+from ..presets import SUMMARIZE_PROMPT
+from ..index_func import construct_index
+
+from langchain.callbacks import get_openai_callback
+import os
+import gradio as gr
+import logging
+
+from g4f import Provider, models
+
+class GoogleSearchInput(BaseModel):
+ keywords: str = Field(description="keywords to search")
+
+class WebBrowsingInput(BaseModel):
+ url: str = Field(description="URL of a webpage")
+
+class WebAskingInput(BaseModel):
+ url: str = Field(description="URL of a webpage")
+ question: str = Field(description="Question that you want to know the answer to, based on the webpage's content.")
+
+
+class ChuanhuAgent_Client(BaseLLMModel):
+ def __init__(self, model_name, openai_api_key, user_name="") -> None:
+ super().__init__(model_name=model_name, user=user_name)
+ self.text_splitter = TokenTextSplitter(chunk_size=500, chunk_overlap=30)
+ self.api_key = 'sk-lVyIGFN0e4Il91M6VmIVEZbxEpuKoMMfUfRzT8IiKn8XzpMH'
+ self.llm: LLM = G4FLLM(temperature=0, model=models.gpt_35_turbo, provider=Provider.NeuroGPT)
+ self.cheap_llm: LLM = G4FLLM(temperature=0, model=models.gpt_35_turbo, provider=Provider.NeuroGPT)
+ PROMPT = PromptTemplate(template=SUMMARIZE_PROMPT, input_variables=["text"])
+ self.summarize_chain = load_summarize_chain(self.cheap_llm, chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT)
+ self.index_summary = None
+ self.index = None
+ if "Pro" in self.model_name:
+ self.tools = load_tools(["serpapi", "google-search-results-json", "llm-math", "arxiv", "wikipedia", "wolfram-alpha"], llm=self.llm)
+ else:
+ self.tools = load_tools(["ddg-search", "llm-math", "arxiv", "wikipedia"], llm=self.llm)
+ self.tools.append(
+ Tool.from_function(
+ func=self.google_search_simple,
+ name="Google Search JSON",
+ description="useful when you need to search the web.",
+ args_schema=GoogleSearchInput
+ )
+ )
+
+ self.tools.append(
+ Tool.from_function(
+ func=self.summary_url,
+ name="Summary Webpage",
+ description="useful when you need to know the overall content of a webpage.",
+ args_schema=WebBrowsingInput
+ )
+ )
+
+ self.tools.append(
+ StructuredTool.from_function(
+ func=self.ask_url,
+ name="Ask Webpage",
+ description="useful when you need to ask detailed questions about a webpage.",
+ args_schema=WebAskingInput
+ )
+ )
+
+ def google_search_simple(self, query):
+ results = []
+ with DDGS() as ddgs:
+ ddgs_gen = ddgs.text("notes from a dead house", backend="api")
+ for r in islice(ddgs_gen, 10):
+ results.append({
+ "title": r["title"],
+ "link": r["href"],
+ "snippet": r["body"]
+ })
+ return str(results)
+
+ def handle_file_upload(self, files, chatbot, language):
+ """if the model accepts multi modal input, implement this function"""
+ status = gr.Markdown.update()
+ if files:
+ index = construct_index(file_src=files)
+ assert index is not None, "Сбой получения индексации"
+ self.index = index
+ status = "Создание индексации завершено"
+ # Summarize the document
+ logging.info("Генерирация краткого изложения контента……")
+ with get_openai_callback() as cb:
+ os.environ["OPENAI_API_KEY"] = self.api_key
+ from langchain.chains.summarize import load_summarize_chain
+ from langchain.prompts import PromptTemplate
+ from langchain.chat_models import ChatOpenAI
+ prompt_template = "Write a concise summary of the following:\n\n{text}\n\nCONCISE SUMMARY IN " + language + ":"
+ PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
+ llm = G4FLLM(temperature=0, model=models.gpt_35_turbo, provider=Provider.NeuroGPT)
+ chain = load_summarize_chain(llm, chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT)
+ summary = chain({"input_documents": list(index.docstore.__dict__["_dict"].values())}, return_only_outputs=True)["output_text"]
+ logging.info(f"Summary: {summary}")
+ self.index_summary = summary
+ chatbot.append((f"Uploaded {len(files)} files", summary))
+ logging.info(cb)
+ return gr.Files.update(), chatbot, status
+
+ # ChuanhuAgent.py
+
+ def handle_message(self, message):
+ words = message.split()
+
+ if words[0].lower() == '!search':
+ keywords = ' '.join(words[1:])
+ return self.google_search_simple({ 'keywords': keywords })
+
+ elif words[0].lower() == '!summarize':
+ url = words[1]
+ return self.summary_url({ 'url': url })
+
+ elif words[0].lower() == '!ask':
+ url, question = words[1], ' '.join(words[2:])
+ return self.ask_url({ 'url': url, 'question': question })
+
+ return f'Unknown command: {words[0]}'
+
+ def query_index(self, query):
+ if self.index is not None:
+ retriever = self.index.as_retriever()
+ qa = RetrievalQA.from_chain_type(llm=self.llm, chain_type="stuff", retriever=retriever)
+ return qa.run(query)
+ else:
+ "Error during query."
+
+ def summary(self, text):
+ texts = Document(page_content=text)
+ texts = self.text_splitter.split_documents([texts])
+ return self.summarize_chain({"input_documents": texts}, return_only_outputs=True)["output_text"]
+
+ def fetch_url_content(self, url):
+ response = requests.get(url)
+ soup = BeautifulSoup(response.text, 'html.parser')
+
+ # Извлеките весь текст
+ text = ''.join(s.getText() for s in soup.find_all('p'))
+ logging.info(f"Extracted text from {url}")
+ return text
+
+ def summary_url(self, url):
+ text = self.fetch_url_content(url)
+ if text == "":
+ return "URL unavailable."
+ text_summary = self.summary(text)
+ url_content = "webpage content summary:\n" + text_summary
+
+ return url_content
+
+ def ask_url(self, url, question):
+ text = self.fetch_url_content(url)
+ if text == "":
+ return "URL unavailable."
+ texts = Document(page_content=text)
+ texts = self.text_splitter.split_documents([texts])
+ # use embedding
+ embeddings = OpenAIEmbeddings(openai_api_key=self.api_key, openai_api_base=os.environ.get("OPENAI_API_BASE", None))
+
+ # create vectorstore
+ db = FAISS.from_documents(texts, embeddings)
+ retriever = db.as_retriever()
+ qa = RetrievalQA.from_chain_type(llm=self.cheap_llm, chain_type="stuff", retriever=retriever)
+ return qa.run(f"{question} Reply in Русский")
+
+ def get_answer_at_once(self):
+ question = self.history[-1]["content"]
+ # llm=ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
+ agent = initialize_agent(self.tools, self.llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
+ reply = agent.run(input=f"{question} Reply in Русский")
+ return reply, -1
+
+ def get_answer_stream_iter(self):
+ question = self.history[-1]["content"]
+ it = CallbackToIterator()
+ manager = BaseCallbackManager(handlers=[ChuanhuCallbackHandler(it.callback)])
+ def thread_func():
+ tools = self.tools
+ if self.index is not None:
+ tools.append(
+ Tool.from_function(
+ func=self.query_index,
+ name="Query Knowledge Base",
+ description=f"useful when you need to know about: {self.index_summary}",
+ args_schema=WebBrowsingInput
+ )
+ )
+ agent = initialize_agent(self.tools, self.llm, agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION, verbose=True, callback_manager=manager)
+ try:
+ reply = agent.run(input=f"{question} Reply in Русский")
+ except Exception as e:
+ import traceback
+ traceback.print_exc()
+ reply = str(e)
+ it.callback(reply)
+ it.finish()
+ t = Thread(target=thread_func)
+ t.start()
+ partial_text = ""
+ for value in it:
+ partial_text += value
+ yield partial_text
diff --git a/modules/models/__init__.py b/modules/models/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/modules/models/base_model.py b/modules/models/base_model.py
new file mode 100644
index 0000000000000000000000000000000000000000..16ac34c9ae2c771159b93d9a754a7ff5904fa4ad
--- /dev/null
+++ b/modules/models/base_model.py
@@ -0,0 +1,675 @@
+from __future__ import annotations
+
+import logging
+import json
+import os
+import urllib3
+import traceback
+import pathlib
+import re
+
+import colorama
+from duckduckgo_search import DDGS
+from itertools import islice
+from enum import Enum
+
+from typing import Any, Optional, Tuple, Generator
+
+from langchain.callbacks.base import BaseCallbackHandler
+from langchain.schema import AgentAction, AgentFinish
+
+from threading import Condition
+from collections import deque
+
+from ..presets import *
+from ..index_func import *
+from ..utils import *
+from .. import shared
+from ..config import retrieve_proxy
+
+class CallbackToIterator:
+ def __init__(self):
+ self.queue = deque()
+ self.cond = Condition()
+ self.finished = False
+
+ def callback(self, result: str) -> None:
+ with self.cond:
+ self.queue.append(result)
+ self.cond.notify()
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ with self.cond:
+ while not self.queue and not self.finished:
+ self.cond.wait()
+ if not self.queue:
+ raise StopIteration()
+ return self.queue.popleft()
+
+ def finish(self):
+ with self.cond:
+ self.finished = True
+ self.cond.notify()
+
+def get_action_description(text):
+ match = re.search('```(.*?)```', text, re.S)
+ json_text = match.group(1)
+ # Преобразование json в словарь Python
+ json_dict = json.loads(json_text)
+ # Извлечение значений 'action' и 'action_input'
+ action_name = json_dict['action']
+ action_input = json_dict['action_input']
+ if action_name != "Final Answer":
+ return f'{action_name}: {action_input}
'
+ else:
+ return ""
+
+class ChuanhuCallbackHandler(BaseCallbackHandler):
+
+ def __init__(self, callback) -> None:
+ self.callback = callback
+
+ def on_agent_action(
+ self, action: AgentAction, color: Optional[str] = None, **kwargs: Any
+ ) -> Any:
+ self.callback(get_action_description(action.log))
+
+ def on_tool_end(
+ self,
+ output: str,
+ color: Optional[str] = None,
+ observation_prefix: Optional[str] = None,
+ llm_prefix: Optional[str] = None,
+ **kwargs: Any,
+ ) -> None:
+ """If not the final action, print out observation."""
+ # if observation_prefix is not None:
+ # self.callback(f"\n\n{observation_prefix}")
+ # self.callback(output)
+ # if llm_prefix is not None:
+ # self.callback(f"\n\n{llm_prefix}")
+ if observation_prefix is not None:
+ logging.info(observation_prefix)
+ self.callback(output)
+ if llm_prefix is not None:
+ logging.info(llm_prefix)
+
+ def on_agent_finish(
+ self, finish: AgentFinish, color: Optional[str] = None, **kwargs: Any
+ ) -> None:
+ # self.callback(f"{finish.log}\n\n")
+ logging.info(finish.log)
+
+ def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
+ """Run on new LLM token. Only available when streaming is enabled."""
+ self.callback(token)
+
+
+class ModelType(Enum):
+ Unknown = -1
+ OpenAI = 0
+ ChuanhuAgent = 1
+
+ @classmethod
+ def get_type(cls, model_name: str):
+ model_type = None
+ model_name_lower = model_name.lower()
+ if "gpt" in model_name_lower:
+ model_type = ModelType.OpenAI
+ elif "agent" in model_name_lower:
+ model_type = ModelType.ChuanhuAgent
+ else:
+ model_type = ModelType.Unknown
+ return model_type
+
+
+class BaseLLMModel:
+ def __init__(
+ self,
+ model_name,
+ system_prompt="",
+ temperature=1.0,
+ top_p=1.0,
+ n_choices=1,
+ stop=None,
+ max_generation_token=None,
+ presence_penalty=0,
+ frequency_penalty=0,
+ logit_bias=None,
+ user="",
+ ) -> None:
+ self.history = []
+ self.all_token_counts = []
+ self.model_name = model_name
+ self.model_type = ModelType.get_type(model_name)
+ try:
+ self.token_upper_limit = MODEL_TOKEN_LIMIT[model_name]
+ except KeyError:
+ self.token_upper_limit = DEFAULT_TOKEN_LIMIT
+ self.interrupted = False
+ self.system_prompt = system_prompt
+ self.api_key = None
+ self.need_api_key = False
+ self.single_turn = False
+
+ self.temperature = temperature
+ self.top_p = top_p
+ self.n_choices = n_choices
+ self.stop_sequence = stop
+ self.max_generation_token = None
+ self.presence_penalty = presence_penalty
+ self.frequency_penalty = frequency_penalty
+ self.logit_bias = logit_bias
+ self.user_identifier = user
+
+ def get_answer_stream_iter(self):
+ """stream predict, need to be implemented
+ conversations are stored in self.history, with the most recent question, in OpenAI format
+ should return a generator, each time give the next word (str) in the answer
+ """
+ logging.warning("stream predict not implemented, using at once predict instead")
+ response, _ = self.get_answer_at_once()
+ yield response
+
+ def get_answer_at_once(self):
+ """predict at once, need to be implemented
+ conversations are stored in self.history, with the most recent question, in OpenAI format
+ Should return:
+ the answer (str)
+ total token count (int)
+ """
+ logging.warning("at once predict not implemented, using stream predict instead")
+ response_iter = self.get_answer_stream_iter()
+ count = 0
+ for response in response_iter:
+ count += 1
+ return response, sum(self.all_token_counts) + count
+
+ def billing_info(self):
+ """get billing infomation, inplement if needed"""
+ logging.warning("billing info not implemented, using default")
+ return BILLING_NOT_APPLICABLE_MSG
+
+ def count_token(self, user_input):
+ """get token count from input, implement if needed"""
+ # logging.warning("token count not implemented, using default")
+ return len(user_input)
+
+ def stream_next_chatbot(self, inputs, chatbot, fake_input=None, display_append=""):
+ def get_return_value():
+ return chatbot, status_text
+
+ partial_text = "" # Добавить это определение
+
+ if fake_input:
+ chatbot.append((fake_input, ""))
+ else:
+ chatbot.append((inputs, ""))
+
+ user_token_count = self.count_token(inputs)
+ self.all_token_counts.append(user_token_count)
+ logging.debug(f"Входной токен счет: {user_token_count}")
+
+ stream_iter = self.get_answer_stream_iter()
+
+ if display_append:
+ display_append = '\n\n ' + display_append
+ for partial_text in stream_iter:
+ chatbot[-1] = (chatbot[-1][0], partial_text + display_append)
+ self.all_token_counts[-1] += 1
+ status_text = self.token_message()
+ yield get_return_value()
+ if self.interrupted:
+ self.recover()
+ break
+ self.history.append(construct_assistant(partial_text))
+
+ def next_chatbot_at_once(self, inputs, chatbot, fake_input=None, display_append=""):
+ if fake_input:
+ chatbot.append((fake_input, ""))
+ else:
+ chatbot.append((inputs, ""))
+ if fake_input is not None:
+ user_token_count = self.count_token(fake_input)
+ else:
+ user_token_count = self.count_token(inputs)
+ self.all_token_counts.append(user_token_count)
+ ai_reply, total_token_count = self.get_answer_at_once()
+ self.history.append(construct_assistant(ai_reply))
+ if fake_input is not None:
+ self.history[-2] = construct_user(fake_input)
+ chatbot[-1] = (chatbot[-1][0], ai_reply + display_append)
+ if fake_input is not None:
+ self.all_token_counts[-1] += count_token(construct_assistant(ai_reply))
+ else:
+ self.all_token_counts[-1] = total_token_count - sum(self.all_token_counts)
+ status_text = self.token_message()
+ return chatbot, status_text
+
+ def handle_file_upload(self, files, chatbot, language):
+ """if the model accepts multi modal input, implement this function"""
+ status = gr.Markdown.update()
+ if files:
+ index = construct_index(self.api_key, file_src=files)
+ status = "Создание индекса завершено"
+ return gr.Files.update(), chatbot, status
+
+ def summarize_index(self, files, chatbot, language):
+ status = gr.Markdown.update()
+ if files:
+ index = construct_index(self.api_key, file_src=files)
+ status = "Полный"
+ logging.info("Создание сводки контента...")
+ os.environ["OPENAI_API_KEY"] = self.api_key
+ os.environ["OPENAI_API_BASE"] = "https://neuroapi.host"
+ from langchain.chains.summarize import load_summarize_chain
+ from langchain.prompts import PromptTemplate
+ from langchain.chat_models import ChatOpenAI
+ from langchain.callbacks import StdOutCallbackHandler
+ prompt_template = "Write a concise summary of the following:\n\n{text}\n\nCONCISE SUMMARY IN " + language + ":"
+ PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
+ llm = ChatOpenAI()
+ chain = load_summarize_chain(llm, chain_type="map_reduce", return_intermediate_steps=True, map_prompt=PROMPT, combine_prompt=PROMPT)
+ summary = chain({"input_documents": list(index.docstore.__dict__["_dict"].values())}, return_only_outputs=True)["output_text"]
+ print("Сводка:", summary)
+ chatbot.append([f"Загружено {len(files)} файлов", summary])
+ return chatbot, status
+
+ def prepare_inputs(self, real_inputs, use_websearch, files, reply_language, chatbot):
+ fake_inputs = None
+ display_append = []
+ limited_context = False
+ fake_inputs = real_inputs
+ if files:
+ from langchain.embeddings.huggingface import HuggingFaceEmbeddings
+ from langchain.vectorstores.base import VectorStoreRetriever
+ limited_context = True
+ msg = "Загрузка индекса..."
+ logging.info(msg)
+ index = construct_index(self.api_key, file_src=files)
+ assert index is not None, "Не удалось получить индекс"
+ msg = "Создание индекса завершено, генерация ответа..."
+ logging.info(msg)
+ with retrieve_proxy():
+ retriever = VectorStoreRetriever(vectorstore=index, search_type="similarity_score_threshold",search_kwargs={"k":6, "score_threshold": 0.5})
+ relevant_documents = retriever.get_relevant_documents(real_inputs)
+ reference_results = [[d.page_content.strip("�"), os.path.basename(d.metadata["source"])] for d in relevant_documents]
+ reference_results = add_source_numbers(reference_results)
+ display_append = add_details(reference_results)
+ display_append = "\n\n" + "".join(display_append)
+ real_inputs = (
+ replace_today(PROMPT_TEMPLATE)
+ .replace("{query_str}", real_inputs)
+ .replace("{context_str}", "\n\n".join(reference_results))
+ .replace("{reply_language}", reply_language)
+ )
+ elif use_websearch:
+ search_results = []
+ with DDGS() as ddgs:
+ ddgs_gen = ddgs.text(real_inputs, backend="lite")
+ for r in islice(ddgs_gen, 10):
+ search_results.append(r)
+ reference_results = []
+ for idx, result in enumerate(search_results):
+ logging.debug(f"Результат поиска {idx + 1}: {result}")
+ domain_name = urllib3.util.parse_url(result['href']).host
+ reference_results.append([result['body'], result['href']])
+ display_append.append(
+ # f"{idx+1}. [{domain_name}]({result['href']})\n"
+ f"{idx+1}. {result['title']} "
+ )
+ reference_results = add_source_numbers(reference_results)
+ # display_append = "\n\n" + "".join(display_append) + " "
+ display_append = '' + "".join(display_append) + '
'
+ real_inputs = (
+ replace_today(WEBSEARCH_PTOMPT_TEMPLATE)
+ .replace("{query}", real_inputs)
+ .replace("{web_results}", "\n\n".join(reference_results))
+ .replace("{reply_language}", reply_language)
+ )
+ else:
+ display_append = ""
+ return limited_context, fake_inputs, display_append, real_inputs, chatbot
+
+ def predict(
+ self,
+ inputs,
+ chatbot,
+ stream=False,
+ use_websearch=False,
+ files=None,
+ reply_language="Русский",
+ should_check_token_count=True,
+ ):
+
+ if inputs.strip().startswith("!"):
+ response_text = self.handle_message(inputs)
+ chatbot.append((inputs, response_text))
+ return chatbot, "Command handled"
+
+ status_text = "Отправка запроса ..."
+ logging.info(
+ "Ввод пользователя" + f"{self.user_identifier}" + " : " + colorama.Fore.BLUE + f"{inputs}" + colorama.Style.RESET_ALL
+ )
+ if should_check_token_count:
+ yield chatbot + [(inputs, "")], status_text
+ if reply_language == "Следуйте языку вопросов (нестабильно)":
+ reply_language = "такой же язык, как вопрос, например, английский, русский, японский, испанский, французский или немецкий."
+
+ limited_context, fake_inputs, display_append, inputs, chatbot = self.prepare_inputs(real_inputs=inputs, use_websearch=use_websearch, files=files, reply_language=reply_language, chatbot=chatbot)
+ yield chatbot + [(fake_inputs, "")], status_text
+
+ if (
+ self.need_api_key and
+ self.api_key is None
+ and not shared.state.multi_api_key
+ ):
+ status_text = STANDARD_ERROR_MSG + NO_APIKEY_MSG
+ logging.info(status_text)
+ chatbot.append((inputs, ""))
+ if len(self.history) == 0:
+ self.history.append(construct_user(inputs))
+ self.history.append("")
+ self.all_token_counts.append(0)
+ else:
+ self.history[-2] = construct_user(inputs)
+ yield chatbot + [(inputs, "")], status_text
+ return
+ elif len(inputs.strip()) == 0:
+ status_text = STANDARD_ERROR_MSG + NO_INPUT_MSG
+ logging.info(status_text)
+ yield chatbot + [(inputs, "")], status_text
+ return
+
+ if self.single_turn:
+ self.history = []
+ self.all_token_counts = []
+ self.history.append(construct_user(inputs))
+
+ try:
+ if stream:
+ logging.debug("Использование потоковой передачи")
+ iter = self.stream_next_chatbot(
+ inputs,
+ chatbot,
+ fake_input=fake_inputs,
+ display_append=display_append,
+ )
+ for chatbot, status_text in iter:
+ yield chatbot, status_text
+ else:
+ logging.debug("Не использовать потоковую передачу")
+ chatbot, status_text = self.next_chatbot_at_once(
+ inputs,
+ chatbot,
+ fake_input=fake_inputs,
+ display_append=display_append,
+ )
+ yield chatbot, status_text
+ except Exception as e:
+ traceback.print_exc()
+ yield chatbot, "Ошибка: " + str(e)
+
+ if len(self.history) > 1 and self.history[-1]["content"] != inputs:
+ logging.info(
+ "Ответ: "
+ + colorama.Fore.BLUE
+ + f"{self.history[-1]['content']}"
+ + colorama.Style.RESET_ALL
+ )
+
+ if limited_context:
+ self.history = []
+ self.all_token_counts = []
+
+ max_token = self.token_upper_limit - TOKEN_OFFSET
+
+ if sum(self.all_token_counts) > max_token and should_check_token_count:
+ count = 0
+ while (
+ sum(self.all_token_counts)
+ > self.token_upper_limit * REDUCE_TOKEN_FACTOR
+ and sum(self.all_token_counts) > 0
+ ):
+ count += 1
+ del self.all_token_counts[0]
+ del self.history[:2]
+ yield chatbot, f"Чтобы избежать превышения лимита токенов, модель забыла {count} предыдущих ответов"
+
+ self.auto_save(chatbot)
+
+ def retry(
+ self,
+ chatbot,
+ stream=False,
+ use_websearch=False,
+ files=None,
+ reply_language="Русский",
+ ):
+ logging.debug("Повторная попытка...")
+ if len(self.history) > 0:
+ inputs = self.history[-2]["content"]
+ del self.history[-2:]
+ if len(self.all_token_counts) > 0:
+ self.all_token_counts.pop()
+ elif len(chatbot) > 0:
+ inputs = chatbot[-1][0]
+ else:
+ yield chatbot, f"{STANDARD_ERROR_MSG}Контекст пустой"
+ return
+
+ iter = self.predict(
+ inputs,
+ chatbot,
+ stream=stream,
+ use_websearch=use_websearch,
+ files=files,
+ reply_language=reply_language,
+ )
+ for x in iter:
+ yield x
+ logging.debug("Повторная попытка завершена")
+
+ # def reduce_token_size(self, chatbot):
+ # logging.info("Начало уменьшения количества токенов...")
+ # chatbot, status_text = self.next_chatbot_at_once(
+ # summarize_prompt,
+ # chatbot
+ # )
+ # max_token_count = self.token_upper_limit * REDUCE_TOKEN_FACTOR
+ # num_chat = find_n(self.all_token_counts, max_token_count)
+ # logging.info(f"previous_token_count: {self.all_token_counts}, keeping {num_chat} chats")
+ # chatbot = chatbot[:-1]
+ # self.history = self.history[-2*num_chat:] if num_chat > 0 else []
+ # self.all_token_counts = self.all_token_counts[-num_chat:] if num_chat > 0 else []
+ # msg = f"Сохранено последних {num_chat} диалогов"
+ # logging.info(msg)
+ # logging.info("Уменьшение количества токенов завершено")
+ # return chatbot, msg + " и " + self.token_message(self.all_token_counts if len(self.all_token_counts) > 0 else [0])
+
+ def interrupt(self):
+ self.interrupted = True
+
+ def recover(self):
+ self.interrupted = False
+
+ def set_token_upper_limit(self, new_upper_limit):
+ self.token_upper_limit = new_upper_limit
+ print(f"Максимальный лимит токенов установлен на {new_upper_limit}")
+
+ def set_temperature(self, new_temperature):
+ self.temperature = new_temperature
+
+ def set_top_p(self, new_top_p):
+ self.top_p = new_top_p
+
+ def set_n_choices(self, new_n_choices):
+ self.n_choices = new_n_choices
+
+ def set_stop_sequence(self, new_stop_sequence: str):
+ new_stop_sequence = new_stop_sequence.split(",")
+ self.stop_sequence = new_stop_sequence
+
+ def set_max_tokens(self, new_max_tokens):
+ self.max_generation_token = new_max_tokens
+
+ def set_presence_penalty(self, new_presence_penalty):
+ self.presence_penalty = new_presence_penalty
+
+ def set_frequency_penalty(self, new_frequency_penalty):
+ self.frequency_penalty = new_frequency_penalty
+
+ def set_logit_bias(self, logit_bias):
+ logit_bias = logit_bias.split()
+ bias_map = {}
+ encoding = tiktoken.get_encoding("cl100k_base")
+ for line in logit_bias:
+ word, bias_amount = line.split(":")
+ if word:
+ for token in encoding.encode(word):
+ bias_map[token] = float(bias_amount)
+ self.logit_bias = bias_map
+
+ def set_user_identifier(self, new_user_identifier):
+ self.user_identifier = new_user_identifier
+
+ def set_system_prompt(self, new_system_prompt):
+ self.system_prompt = new_system_prompt
+
+ def set_key(self, new_access_key):
+ self.api_key = new_access_key.strip()
+ msg = "API-ключ изменен на " + hide_middle_chars(self.api_key)
+ logging.info(msg)
+ return self.api_key, msg
+
+ def set_single_turn(self, new_single_turn):
+ self.single_turn = new_single_turn
+
+ def reset(self):
+ self.history = []
+ self.all_token_counts = []
+ self.interrupted = False
+ pathlib.Path(os.path.join(HISTORY_DIR, self.user_identifier, new_auto_history_filename(os.path.join(HISTORY_DIR, self.user_identifier)))).touch()
+ return [], self.token_message([0])
+
+ def delete_first_conversation(self):
+ if self.history:
+ del self.history[:2]
+ del self.all_token_counts[0]
+ return self.token_message()
+
+ def delete_last_conversation(self, chatbot):
+ if len(chatbot) > 0 and STANDARD_ERROR_MSG in chatbot[-1][1]:
+ msg = "Удаление только записи chatbot, так как содержит сообщение об ошибке"
+ chatbot.pop()
+ return chatbot, self.history
+ if len(self.history) > 0:
+ self.history.pop()
+ self.history.pop()
+ if len(chatbot) > 0:
+ msg = "Удалена одна пара ответов chatbot"
+ chatbot.pop()
+ if len(self.all_token_counts) > 0:
+ msg = "Удалена одна пара ответов с подсчетом токенов"
+ self.all_token_counts.pop()
+ msg = "Удалена одна пара ответов"
+ return chatbot, msg
+
+ def token_message(self, token_lst=None):
+ if token_lst is None:
+ token_lst = self.all_token_counts
+ token_sum = 0
+ for i in range(len(token_lst)):
+ token_sum += sum(token_lst[: i + 1])
+ return "Количество токенов: " + f"{sum(token_lst)}" + " Текущий диалог использовал " + f"{token_sum} токенов"
+
+ def save_chat_history(self, filename, chatbot, user_name):
+ if filename == "":
+ return
+ if not filename.endswith(".json"):
+ filename += ".json"
+ return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
+
+ def auto_save(self, chatbot):
+ history_file_path = get_history_filepath(self.user_identifier)
+ save_file(history_file_path, self.system_prompt, self.history, chatbot, self.user_identifier)
+
+ def export_markdown(self, filename, chatbot, user_name):
+ if filename == "":
+ return
+ if not filename.endswith(".md"):
+ filename += ".md"
+ return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
+
+ def load_chat_history(self, filename, user_name):
+ logging.debug(f"{user_name} Загрузка истории чата...")
+ logging.info(f"filename: {filename}")
+ if type(filename) != str and filename is not None:
+ filename = filename.name
+ try:
+ if "/" not in filename:
+ history_file_path = os.path.join(HISTORY_DIR, user_name, filename)
+ else:
+ history_file_path = filename
+ with open(history_file_path, "r", encoding='utf8') as f:
+ json_s = json.load(f)
+ try:
+ if type(json_s["history"][0]) == str:
+ logging.info("Формат истории устарел, конвертация...")
+ new_history = []
+ for index, item in enumerate(json_s["history"]):
+ if index % 2 == 0:
+ new_history.append(construct_user(item))
+ else:
+ new_history.append(construct_assistant(item))
+ json_s["history"] = new_history
+ logging.info(new_history)
+ except:
+ pass
+ logging.debug(f"{user_name} Загрузка истории чата завершена")
+ self.history = json_s["history"]
+ return os.path.basename(filename), json_s["system"], json_s["chatbot"]
+ except:
+ # Нет истории чата или не удалось загрузить историю чата
+ logging.info(f"История чата не найдена {filename}")
+ return gr.update(), self.system_prompt, gr.update()
+
+ def delete_chat_history(self, filename, user_name):
+ if filename == "CANCELED":
+ return gr.update(), gr.update(), gr.update()
+ if filename == "":
+ return "Вы не выбрали историю чата", gr.update(), gr.update()
+ if not filename.endswith(".json"):
+ filename += ".json"
+ if "/" not in filename:
+ history_file_path = os.path.join(HISTORY_DIR, user_name, filename)
+ else:
+ history_file_path = filename
+ try:
+ os.remove(history_file_path)
+ return "История чата успешно удалена", get_history_names(False, user_name), []
+ except:
+ logging.info(f"Не удалось удалить историю чата {history_file_path}")
+ return "История чата " + filename + " была удалена", gr.update(), gr.update()
+
+ def auto_load(self):
+ if self.user_identifier == "":
+ self.reset()
+ return self.system_prompt, gr.update()
+ history_file_path = get_history_filepath(self.user_identifier)
+ filename, system_prompt, chatbot = self.load_chat_history(history_file_path, self.user_identifier)
+ return system_prompt, chatbot
+
+
+ def like(self):
+ """like the last response, implement if needed
+ """
+ return gr.update()
+
+ def dislike(self):
+ """dislike the last response, implement if needed
+ """
+ return gr.update()
\ No newline at end of file
diff --git a/modules/models/models.py b/modules/models/models.py
new file mode 100644
index 0000000000000000000000000000000000000000..d0677302a2e60b379d39812e75966929f3dd1909
--- /dev/null
+++ b/modules/models/models.py
@@ -0,0 +1,361 @@
+from __future__ import annotations
+
+import logging
+import json
+import json
+import requests
+
+import colorama
+
+
+from ..presets import *
+from ..index_func import *
+from ..utils import *
+from .. import shared
+from ..config import retrieve_proxy, usage_limit
+from modules import config
+from .base_model import BaseLLMModel, ModelType
+
+
+class OpenAIClient(BaseLLMModel):
+ def __init__(
+ self,
+ model_name,
+ api_key,
+ system_prompt=INITIAL_SYSTEM_PROMPT,
+ temperature=1.0,
+ top_p=1.0,
+ user_name=""
+ ) -> None:
+ super().__init__(
+ model_name=model_name,
+ temperature=temperature,
+ top_p=top_p,
+ system_prompt=system_prompt,
+ user=user_name
+ )
+ with open("config.json", "r") as f:
+ self.configuration_json = json.load(f)
+ self.api_key = api_key
+ self.need_api_key = True
+ self._refresh_header()
+
+ def get_answer_stream_iter(self):
+ response = self._get_response(stream=True)
+ if response is not None:
+ iter = self._decode_chat_response(response)
+ partial_text = ""
+ for i in iter:
+ partial_text += i
+ yield partial_text
+ else:
+ yield STANDARD_ERROR_MSG + GENERAL_ERROR_MSG
+
+
+ def get_answer_at_once(self):
+ response = self._get_response()
+ response = json.loads(response.text)
+ content = response["choices"][0]["message"]["content"]
+ total_token_count = response["usage"]["total_tokens"]
+ return content, total_token_count
+
+ def count_token(self, user_input):
+ input_token_count = count_token(construct_user(user_input))
+ if self.system_prompt is not None and len(self.all_token_counts) == 0:
+ system_prompt_token_count = count_token(
+ construct_system(self.system_prompt)
+ )
+ return input_token_count + system_prompt_token_count
+ return input_token_count
+
+ def billing_info(self):
+ try:
+ curr_time = datetime.datetime.now()
+ last_day_of_month = get_last_day_of_month(
+ curr_time).strftime("%Y-%m-%d")
+ first_day_of_month = curr_time.replace(day=1).strftime("%Y-%m-%d")
+ usage_url = f"{shared.state.usage_api_url}?start_date={first_day_of_month}&end_date={last_day_of_month}"
+ try:
+ usage_data = self._get_billing_data(usage_url)
+ except Exception as e:
+ None
+ rounded_usage = round(usage_data["total_usage"] / 100, 5)
+ usage_percent = round(usage_data["total_usage"] / usage_limit, 2)
+ return get_html("billing_info.html").format(
+ label = "Ежемесячное использование",
+ usage_percent = usage_percent,
+ rounded_usage = rounded_usage,
+ usage_limit = usage_limit
+ )
+ except requests.exceptions.ConnectTimeout:
+ None
+ except requests.exceptions.ReadTimeout:
+ None
+ except Exception as e:
+ None
+
+ def set_token_upper_limit(self, new_upper_limit):
+ pass
+
+ @shared.state.switching_api_key # 在不开启多账号模式的时候,这个装饰器不会起作用
+ def _get_response(self, stream=False):
+ headers = self._get_headers()
+ history = self._get_history()
+ payload = self._get_payload(history, stream)
+ shared.state.completion_url = self._get_api_url()
+ logging.info(f"Используется API URL: {shared.state.completion_url}")
+ with retrieve_proxy():
+ response = self._make_request(headers, payload, stream)
+ return response
+
+ def _get_api_url(self):
+ if "naga-gpt" in self.model_name or "naga-llama" in self.model_name or "naga-claude" in self.model_name:
+ url = "https://api.naga.ac/v1/chat/completions"
+ elif "naga-text" in self.model_name:
+ url = "https://api.naga.ac/v1/completions"
+ elif "chatty" in self.model_name:
+ url = "https://chattyapi.tech/v1/chat/completions"
+ elif "daku" in self.model_name:
+ url = "https://api.daku.tech/v1/chat/completions"
+ elif self.model_name.startswith('gpt-4') or self.model_name.startswith('gpt-4-'):
+ url = "https://neuroapi.host/gpt4/v1/chat/completions"
+ else:
+ url = "https://neuroapi.host/v1/chat/completions"
+ return url
+
+ def _get_headers(self):
+ if self.model_name == "purgpt":
+ purgpt_api_key = self.configuration_json["purgpt_api_key"]
+ headers = {
+ 'Content-Type': 'application/json',
+ 'Authorization': f'Bearer {purgpt_api_key or self.api_key}',
+ }
+ elif "chatty" in self.model_name:
+ chatty_api_key = self.configuration_json["chatty_api_key"]
+ headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {chatty_api_key or self.api_key}",
+ }
+ elif "daku" in self.model_name:
+ daku_api_key = self.configuration_json["daku_api_key"]
+ headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {daku_api_key or self.api_key}",
+ }
+ else:
+ headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {self.api_key}",
+ }
+ return headers
+
+ def _get_history(self):
+ system_prompt = self.system_prompt
+ history = self.history
+ logging.debug(colorama.Fore.YELLOW + f"{history}" + colorama.Fore.RESET)
+ if system_prompt is not None:
+ history = [construct_system(system_prompt), *history]
+ return history
+
+ def _get_payload(self, history, stream):
+ model = self.model_name.replace("naga-", "").replace("chatty-", "").replace("neuro-", "").replace("daku-", "")
+ if "naga-text" in self.model_name:
+ last_msg = self.history[-1]
+ last_user_input = last_msg["role"] == "user"
+ if last_user_input:
+ last_text = last_msg["content"]
+ payload = {
+ "model": model,
+ "prompt": last_text,
+ "stream": stream,
+ }
+ return payload
+ else:
+ payload = {
+ "model": model,
+ "messages": history,
+ "temperature": self.temperature,
+ "top_p": self.top_p,
+ "n": self.n_choices,
+ "stream": stream,
+ "presence_penalty": self.presence_penalty,
+ "frequency_penalty": self.frequency_penalty,
+ }
+ if self.max_generation_token is not None:
+ payload["max_tokens"] = self.max_generation_token
+ if self.stop_sequence is not None:
+ payload["stop"] = self.stop_sequence
+ if self.logit_bias is not None:
+ payload["logit_bias"] = self.logit_bias
+ if self.user_identifier:
+ payload["user"] = self.user_identifier
+ return payload
+
+ def _make_request(self, headers, payload, stream):
+ if stream:
+ timeout = TIMEOUT_STREAMING
+ else:
+ timeout = TIMEOUT_ALL
+ try: #Заготовочка для переписания системы отправки запросов
+ if any(substring in self.model_name for substring in ["purgpt", "naga", "chatty"]):
+ response = requests.post(
+ shared.state.completion_url,
+ headers = headers,
+ json=payload,
+ stream=stream,
+ )
+ else:
+ response = requests.post(
+ shared.state.completion_url,
+ headers=headers,
+ json=payload,
+ stream=stream,
+ timeout=timeout,
+ )
+ except:
+ return None
+ return response
+
+ def _refresh_header(self):
+ self.headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"Bearer {self.api_key}",
+ }
+
+ def _get_billing_data(self, billing_url):
+ with retrieve_proxy():
+ response = requests.get(
+ billing_url,
+ headers=self.headers,
+ timeout=TIMEOUT_ALL,
+ )
+ if response.status_code == 200:
+ data = response.json()
+ return data
+ else:
+ raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
+
+ def _decode_chat_response(self, response):
+ error_msg = ""
+ for chunk in response.iter_lines():
+ if chunk:
+ chunk = chunk.decode()
+ chunk_length = len(chunk)
+ try:
+ chunk = json.loads(chunk[6:])
+ except json.JSONDecodeError:
+ error_msg += chunk
+ continue
+ if chunk_length > 6 and "delta" in chunk["choices"][0]:
+ if chunk["choices"][0]["finish_reason"] == "stop":
+ break
+ try:
+ yield chunk["choices"][0]["delta"]["content"]
+ except Exception as e:
+ continue
+ if error_msg:
+ if "Not authenticated" in error_msg:
+ yield 'Провайдер API ответил ошибкой: Ключ ChimeraAPI не обнаружен. Убедитесь что ввели его.'
+ elif "Invalid API key" in error_msg:
+ yield 'Провайдер API ответил ошибкой: Неверный ключ ChimeraAPI. Возможно вы ввели его неправильно либо он деактивирован. Вы можете сгенерировать его заново в Discord: https://discord.gg/chimeragpt'
+ elif "Reverse engineered site does not respond" in error_msg:
+ yield 'Провайдер API ответил ошибкой: На данный момент, все сайты-провайдеры недоступны. Попробуйте позже.'
+ elif "one_api_error" in error_msg:
+ yield 'Провайдер API ответил ошибкой: Сервер Chatty API недоступен. Попробуйте позднее.'
+ else:
+ yield 'Ошибка: ' + error_msg
+
+ def set_key(self, new_access_key):
+ ret = super().set_key(new_access_key)
+ self._refresh_header()
+ return ret
+
+def get_model(
+ model_name,
+ lora_model_path=None,
+ access_key=None,
+ temperature=None,
+ top_p=None,
+ system_prompt=None,
+ user_name=""
+) -> BaseLLMModel:
+ msg = "Модель установлена на: " + f" {model_name}"
+ model_type = ModelType.get_type(model_name)
+ lora_selector_visibility = False
+ lora_choices = []
+ dont_change_lora_selector = False
+ if model_type != ModelType.OpenAI:
+ config.local_embedding = True
+ # del current_model.model
+ model = None
+ chatbot = gr.Chatbot.update(label=model_name)
+ try:
+ if model_type == ModelType.OpenAI:
+ logging.info(f"Загрузка модели OpenAI: {model_name}")
+ model = OpenAIClient(
+ model_name=model_name,
+ api_key=access_key,
+ system_prompt=system_prompt,
+ temperature=temperature,
+ top_p=top_p,
+ user_name=user_name,
+ )
+ elif model_type == ModelType.ChuanhuAgent:
+ from .ChuanhuAgent import ChuanhuAgent_Client
+ model = ChuanhuAgent_Client(model_name, access_key, user_name=user_name)
+ msg = "Доступные инструменты:" + ", ".join([i.name for i in model.tools])
+ elif model_type == ModelType.Unknown:
+ logging.info(f"正在加载OpenAI模型: {model_name}")
+ model = OpenAIClient(
+ model_name=model_name,
+ api_key=access_key,
+ system_prompt=system_prompt,
+ temperature=temperature,
+ top_p=top_p,
+ user_name=user_name,
+ )
+ logging.info(msg)
+ except Exception as e:
+ import traceback
+ traceback.print_exc()
+ msg = f"{STANDARD_ERROR_MSG}: {e}"
+ if dont_change_lora_selector:
+ return model, msg, chatbot
+ else:
+ return model, msg, chatbot, gr.Dropdown.update(choices=lora_choices, visible=lora_selector_visibility)
+
+
+if __name__ == "__main__":
+ with open("config.json", "r") as f:
+ openai_api_key = json.load(f)["openai_api_key"]
+ # set logging level to debug
+ logging.basicConfig(level=logging.DEBUG)
+ # client = ModelManager(model_name="gpt-3.5-turbo", access_key=openai_api_key)
+ client = get_model(model_name="chatglm-6b-int4")
+ chatbot = []
+ stream = False
+ # 测试账单功能
+ logging.info(colorama.Back.GREEN + "测试账单功能" + colorama.Back.RESET)
+ logging.info(client.billing_info())
+ # 测试问答
+ logging.info(colorama.Back.GREEN + "测试问答" + colorama.Back.RESET)
+ question = "巴黎是中国的首都吗?"
+ for i in client.predict(inputs=question, chatbot=chatbot, stream=stream):
+ logging.info(i)
+ logging.info(f"测试问答后history : {client.history}")
+ # 测试记忆力
+ logging.info(colorama.Back.GREEN + "测试记忆力" + colorama.Back.RESET)
+ question = "我刚刚问了你什么问题?"
+ for i in client.predict(inputs=question, chatbot=chatbot, stream=stream):
+ logging.info(i)
+ logging.info(f"测试记忆力后history : {client.history}")
+ # 测试重试功能
+ logging.info(colorama.Back.GREEN + "测试重试功能" + colorama.Back.RESET)
+ for i in client.retry(chatbot=chatbot, stream=stream):
+ logging.info(i)
+ logging.info(f"重试后history : {client.history}")
+ # # 测试总结功能
+ # print(colorama.Back.GREEN + "测试总结功能" + colorama.Back.RESET)
+ # chatbot, msg = client.reduce_token_size(chatbot=chatbot)
+ # print(chatbot, msg)
+ # print(f"总结后history: {client.history}")
diff --git a/modules/overwrites.py b/modules/overwrites.py
new file mode 100644
index 0000000000000000000000000000000000000000..d75f809caeb9398a3ad7b56b51c4b43bf2dc93c0
--- /dev/null
+++ b/modules/overwrites.py
@@ -0,0 +1,123 @@
+from __future__ import annotations
+import logging
+
+from typing import List, Tuple
+from gradio_client import utils as client_utils
+from gradio import utils
+import inspect
+
+from modules.presets import *
+from modules.index_func import *
+
+
+def postprocess(
+ self,
+ y: List[List[str | Tuple[str] | Tuple[str, str] | None] | Tuple],
+ ) -> List[List[str | Dict | None]]:
+ """
+ Parameters:
+ y: List of lists representing the message and response pairs. Each message and response should be a string, which may be in Markdown format. It can also be a tuple whose first element is a string filepath or URL to an image/video/audio, and second (optional) element is the alt text, in which case the media file is displayed. It can also be None, in which case that message is not displayed.
+ Returns:
+ List of lists representing the message and response. Each message and response will be a string of HTML, or a dictionary with media information. Or None if the message is not to be displayed.
+ """
+ if y is None:
+ return []
+ processed_messages = []
+ for message_pair in y:
+ assert isinstance(
+ message_pair, (tuple, list)
+ ), f"Expected a list of lists or list of tuples. Received: {message_pair}"
+ assert (
+ len(message_pair) == 2
+ ), f"Expected a list of lists of length 2 or list of tuples of length 2. Received: {message_pair}"
+
+ processed_messages.append(
+ [
+ self._postprocess_chat_messages(message_pair[0], "user"),
+ self._postprocess_chat_messages(message_pair[1], "bot"),
+ ]
+ )
+ return processed_messages
+
+def postprocess_chat_messages(
+ self, chat_message: str | tuple | list | None, role: str
+ ) -> str | dict | None:
+ if chat_message is None:
+ return None
+ elif isinstance(chat_message, (tuple, list)):
+ file_uri = chat_message[0]
+ if utils.validate_url(file_uri):
+ filepath = file_uri
+ else:
+ filepath = self.make_temp_copy_if_needed(file_uri)
+
+ mime_type = client_utils.get_mimetype(filepath)
+ return {
+ "name": filepath,
+ "mime_type": mime_type,
+ "alt_text": chat_message[1] if len(chat_message) > 1 else None,
+ "data": None, # These last two fields are filled in by the frontend
+ "is_file": True,
+ }
+ elif isinstance(chat_message, str):
+ # chat_message = inspect.cleandoc(chat_message)
+ # escape html spaces
+ # chat_message = chat_message.replace(" ", " ")
+ if role == "bot":
+ chat_message = convert_bot_before_marked(chat_message)
+ elif role == "user":
+ chat_message = convert_user_before_marked(chat_message)
+ return chat_message
+ else:
+ raise ValueError(f"Invalid message for Chatbot component: {chat_message}")
+
+def add_classes_to_gradio_component(comp):
+ """
+ this adds gradio-* to the component for css styling (ie gradio-button to gr.Button), as well as some others
+ code from stable-diffusion-webui
+ """
+
+ comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(comp.elem_classes or [])]
+
+ if getattr(comp, 'multiselect', False):
+ comp.elem_classes.append('multiselect')
+
+
+def IOComponent_init(self, *args, **kwargs):
+ res = original_IOComponent_init(self, *args, **kwargs)
+ add_classes_to_gradio_component(self)
+
+ return res
+
+original_IOComponent_init = gr.components.IOComponent.__init__
+gr.components.IOComponent.__init__ = IOComponent_init
+
+
+def BlockContext_init(self, *args, **kwargs):
+ res = original_BlockContext_init(self, *args, **kwargs)
+ add_classes_to_gradio_component(self)
+
+ return res
+
+original_BlockContext_init = gr.blocks.BlockContext.__init__
+gr.blocks.BlockContext.__init__ = BlockContext_init
+
+with open("./assets/custom.js", "r", encoding="utf-8") as f, \
+ open("./assets/external-scripts.js", "r", encoding="utf-8") as f1:
+ customJS = f.read()
+ externalScripts = f1.read()
+
+
+def reload_javascript():
+ print("Reloading javascript...")
+ js = f''
+ js += ''
+ def template_response(*args, **kwargs):
+ res = GradioTemplateResponseOriginal(*args, **kwargs)
+ res.body = res.body.replace(b'