Spaces:
Runtime error
Runtime error
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419"""Filters that accept a `Application` as argument."""from __future__ import annotationsfrom typing import TYPE_CHECKING, castfrom prompt_toolkit.application.current import get_appfrom prompt_toolkit.cache import memoizedfrom prompt_toolkit.enums import EditingModefrom .base import Conditionif TYPE_CHECKING:from prompt_toolkit.layout.layout import FocusableElement__all__ = ["has_arg","has_completions","completion_is_selected","has_focus","buffer_has_focus","has_selection","has_suggestion","has_validation_error","is_done","is_read_only","is_multiline","renderer_height_is_known","in_editing_mode","in_paste_mode","vi_mode","vi_navigation_mode","vi_insert_mode","vi_insert_multiple_mode","vi_replace_mode","vi_selection_mode","vi_waiting_for_text_object_mode","vi_digraph_mode","vi_recording_macro","emacs_mode","emacs_insert_mode","emacs_selection_mode","shift_selection_mode","is_searching","control_is_searchable","vi_search_direction_reversed",]# NOTE: `has_focus` below should *not* be `memoized`. It can reference any user# control. For instance, if we would continuously create new# `PromptSession` instances, then previous instances won't be released,# because this memoize (which caches results in the global scope) will# still refer to each instance.def has_focus(value: FocusableElement) -> Condition:"""Enable when this buffer has the focus."""from prompt_toolkit.buffer import Bufferfrom prompt_toolkit.layout import walkfrom prompt_toolkit.layout.containers import Container, Window, to_containerfrom prompt_toolkit.layout.controls import UIControlif isinstance(value, str):def test() -> bool:return get_app().current_buffer.name == valueelif isinstance(value, Buffer):def test() -> bool:return get_app().current_buffer == valueelif isinstance(value, UIControl):def test() -> bool:return get_app().layout.current_control == valueelse:value = to_container(value)if isinstance(value, Window):def test() -> bool:return get_app().layout.current_window == valueelse:def test() -> bool:# Consider focused when any window inside this container is# focused.current_window = get_app().layout.current_windowfor c in walk(cast(Container, value)):if isinstance(c, Window) and c == current_window:return Truereturn False@Conditiondef has_focus_filter() -> bool:return test()return has_focus_filter@Conditiondef buffer_has_focus() -> bool:"""Enabled when the currently focused control is a `BufferControl`."""return get_app().layout.buffer_has_focus@Conditiondef has_selection() -> bool:"""Enable when the current buffer has a selection."""return bool(get_app().current_buffer.selection_state)@Conditiondef has_suggestion() -> bool:"""Enable when the current buffer has a suggestion."""buffer = get_app().current_bufferreturn buffer.suggestion is not None and buffer.suggestion.text != ""@Conditiondef has_completions() -> bool:"""Enable when the current buffer has completions."""state = get_app().current_buffer.complete_statereturn state is not None and len(state.completions) > 0@Conditiondef completion_is_selected() -> bool:"""True when the user selected a completion."""complete_state = get_app().current_buffer.complete_statereturn complete_state is not None and complete_state.current_completion is not None@Conditiondef is_read_only() -> bool:"""True when the current buffer is read only."""return get_app().current_buffer.read_only()@Conditiondef is_multiline() -> bool:"""True when the current buffer has been marked as multiline."""return get_app().current_buffer.multiline()@Conditiondef has_validation_error() -> bool:"Current buffer has validation error."return get_app().current_buffer.validation_error is not None@Conditiondef has_arg() -> bool:"Enable when the input processor has an 'arg'."return get_app().key_processor.arg is not None@Conditiondef is_done() -> bool:"""True when the CLI is returning, aborting or exiting."""return get_app().is_done@Conditiondef renderer_height_is_known() -> bool:"""Only True when the renderer knows it's real height.(On VT100 terminals, we have to wait for a CPR response, before we can besure of the available height between the cursor position and the bottom ofthe terminal. And usually it's nicer to wait with drawing bottom toolbarsuntil we receive the height, in order to avoid flickering -- first drawingsomewhere in the middle, and then again at the bottom.)"""return get_app().renderer.height_is_known@memoized()def in_editing_mode(editing_mode: EditingMode) -> Condition:"""Check whether a given editing mode is active. (Vi or Emacs.)"""@Conditiondef in_editing_mode_filter() -> bool:return get_app().editing_mode == editing_modereturn in_editing_mode_filter@Conditiondef in_paste_mode() -> bool:return get_app().paste_mode()@Conditiondef vi_mode() -> bool:return get_app().editing_mode == EditingMode.VI@Conditiondef vi_navigation_mode() -> bool:"""Active when the set for Vi navigation key bindings are active."""from prompt_toolkit.key_binding.vi_state import InputModeapp = get_app()if (app.editing_mode != EditingMode.VIor app.vi_state.operator_funcor app.vi_state.waiting_for_digraphor app.current_buffer.selection_state):return Falsereturn (app.vi_state.input_mode == InputMode.NAVIGATIONor app.vi_state.temporary_navigation_modeor app.current_buffer.read_only())@Conditiondef vi_insert_mode() -> bool:from prompt_toolkit.key_binding.vi_state import InputModeapp = get_app()if (app.editing_mode != EditingMode.VIor app.vi_state.operator_funcor app.vi_state.waiting_for_digraphor app.current_buffer.selection_stateor app.vi_state.temporary_navigation_modeor app.current_buffer.read_only()):return Falsereturn app.vi_state.input_mode == InputMode.INSERT@Conditiondef vi_insert_multiple_mode() -> bool:from prompt_toolkit.key_binding.vi_state import InputModeapp = get_app()if (app.editing_mode != EditingMode.VIor app.vi_state.operator_funcor app.vi_state.waiting_for_digraphor app.current_buffer.selection_stateor app.vi_state.temporary_navigation_modeor app.current_buffer.read_only()):return Falsereturn app.vi_state.input_mode == InputMode.INSERT_MULTIPLE@Conditiondef vi_replace_mode() -> bool:from prompt_toolkit.key_binding.vi_state import InputModeapp = get_app()if (app.editing_mode != EditingMode.VIor app.vi_state.operator_funcor app.vi_state.waiting_for_digraphor app.current_buffer.selection_stateor app.vi_state.temporary_navigation_modeor app.current_buffer.read_only()):return Falsereturn app.vi_state.input_mode == InputMode.REPLACE@Conditiondef vi_replace_single_mode() -> bool:from prompt_toolkit.key_binding.vi_state import InputModeapp = get_app()if (app.editing_mode != EditingMode.VIor app.vi_state.operator_funcor app.vi_state.waiting_for_digraphor app.current_buffer.selection_stateor app.vi_state.temporary_navigation_modeor app.current_buffer.read_only()):return Falsereturn app.vi_state.input_mode == InputMode.REPLACE_SINGLE@Conditiondef vi_selection_mode() -> bool:app = get_app()if app.editing_mode != EditingMode.VI:return Falsereturn bool(app.current_buffer.selection_state)@Conditiondef vi_waiting_for_text_object_mode() -> bool:app = get_app()if app.editing_mode != EditingMode.VI:return Falsereturn app.vi_state.operator_func is not None@Conditiondef vi_digraph_mode() -> bool:app = get_app()if app.editing_mode != EditingMode.VI:return Falsereturn app.vi_state.waiting_for_digraph@Conditiondef vi_recording_macro() -> bool:"When recording a Vi macro."app = get_app()if app.editing_mode != EditingMode.VI:return Falsereturn app.vi_state.recording_register is not None@Conditiondef emacs_mode() -> bool:"When the Emacs bindings are active."return get_app().editing_mode == EditingMode.EMACS@Conditiondef emacs_insert_mode() -> bool:app = get_app()if (app.editing_mode != EditingMode.EMACSor app.current_buffer.selection_stateor app.current_buffer.read_only()):return Falsereturn True@Conditiondef emacs_selection_mode() -> bool:app = get_app()return bool(app.editing_mode == EditingMode.EMACS and app.current_buffer.selection_state)@Conditiondef shift_selection_mode() -> bool:app = get_app()return bool(app.current_buffer.selection_stateand app.current_buffer.selection_state.shift_mode)@Conditiondef is_searching() -> bool:"When we are searching."app = get_app()return app.layout.is_searching@Conditiondef control_is_searchable() -> bool:"When the current UIControl is searchable."from prompt_toolkit.layout.controls import BufferControlcontrol = get_app().layout.current_controlreturn (isinstance(control, BufferControl) and control.search_buffer_control is not None)@Conditiondef vi_search_direction_reversed() -> bool:"When the '/' and '?' key bindings for Vi-style searching have been reversed."return get_app().reverse_vi_search_direction()
app.py+413-58