pabloce commited on
Commit
a2e6c05
1 Parent(s): 5416372

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -29
app.py CHANGED
@@ -1,11 +1,30 @@
1
  import spaces
 
 
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  """
6
- 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
7
- """
8
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
 
10
  @spaces.GPU(duration=120)
11
  def respond(
@@ -15,39 +34,58 @@ def respond(
15
  max_tokens,
16
  temperature,
17
  top_p,
 
18
  ):
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
20
 
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
 
27
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
28
 
29
- response = ""
30
 
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
 
39
 
40
- response += token
41
- yield response
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
53
  minimum=0.1,
@@ -56,9 +94,24 @@ demo = gr.ChatInterface(
56
  step=0.05,
57
  label="Top-p (nucleus sampling)",
58
  ),
 
59
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import spaces
2
+ import json
3
+ import subprocess
4
  import gradio as gr
5
+ from huggingface_hub import hf_hub_download
6
 
7
+ subprocess.run('pip install llama-cpp-python==0.2.75 --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124', shell=True)
8
+ subprocess.run('pip install llama-cpp-agent==0.2.10', shell=True)
9
+
10
+ hf_hub_download(repo_id="bartowski/dolphin-2.9.1-yi-1.5-34b-GGUF", filename="dolphin-2.9.1-yi-1.5-34b-Q6_K.gguf", local_dir = "./models")
11
+ hf_hub_download(repo_id="bartowski/dolphin-2.9.1-yi-1.5-9b-GGUF", filename="dolphin-2.9.1-yi-1.5-9b-f32.gguf", local_dir = "./models")
12
+
13
+ css = """
14
+ .message-row {
15
+ justify-content: space-evenly !important;
16
+ }
17
+ .message-bubble-border {
18
+ border-radius: 6px !important;
19
+ border-color: #343140 !important;
20
+ }
21
+ .user {
22
+ background: #1e1c26 !important;
23
+ }
24
+ .assistant, .pending {
25
+ background: #16141c !important;
26
+ }
27
  """
 
 
 
28
 
29
  @spaces.GPU(duration=120)
30
  def respond(
 
34
  max_tokens,
35
  temperature,
36
  top_p,
37
+ model,
38
  ):
39
+ from llama_cpp import Llama
40
+ from llama_cpp_agent import LlamaCppAgent
41
+ from llama_cpp_agent import MessagesFormatterType
42
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
43
+ from llama_cpp_agent.chat_history import BasicChatHistory
44
+ from llama_cpp_agent.chat_history.messages import Roles
45
 
46
+ llm = Llama(
47
+ model_path=f"models/{model}",
48
+ n_gpu_layers=81,
49
+ )
50
+ provider = LlamaCppPythonProvider(llm)
51
 
52
+ agent = LlamaCppAgent(
53
+ provider,
54
+ system_prompt="You are a helpful assistant.",
55
+ predefined_messages_formatter_type=MessagesFormatterType.CHATML,
56
+ debug_output=True
57
+ )
58
+
59
+ settings = provider.get_provider_default_settings()
60
+ settings.max_tokens = max_tokens
61
+ settings.stream = True
62
 
63
+ messages = BasicChatHistory()
64
 
65
+ for msn in history:
66
+ user = {
67
+ 'role': Roles.user,
68
+ 'content': msn[0]
69
+ }
70
+ assistant = {
71
+ 'role': Roles.assistant,
72
+ 'content': msn[1]
73
+ }
74
 
75
+ messages.add_message(user)
76
+ messages.add_message(assistant)
77
+
78
+ stream = agent.get_chat_response(message, llm_sampling_settings=settings, chat_history=messages, returns_streaming_generator=True, print_output=False)
79
+
80
+ outputs = ""
81
+ for output in stream:
82
+ outputs += output
83
+ yield outputs
84
 
 
 
 
85
  demo = gr.ChatInterface(
86
  respond,
87
  additional_inputs=[
88
+ gr.Slider(minimum=1, maximum=8192, value=8192, step=1, label="Max new tokens"),
 
89
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
90
  gr.Slider(
91
  minimum=0.1,
 
94
  step=0.05,
95
  label="Top-p (nucleus sampling)",
96
  ),
97
+ gr.Dropdown(['dolphin-2.9.1-yi-1.5-34b-Q6_K.gguf', 'dolphin-2.9.1-yi-1.5-9b-f32.gguf'], value="dolphin-2.9.1-yi-1.5-34b-Q6_K.gguf", label="Model"),
98
  ],
99
+ theme=gr.themes.Soft(primary_hue="violet", secondary_hue="violet", neutral_hue="gray",font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]).set(
100
+ body_background_fill_dark="#16141c",
101
+ block_background_fill_dark="#16141c",
102
+ block_title_background_fill_dark="#1e1c26",
103
+ input_background_fill_dark="#292733",
104
+ button_secondary_background_fill_dark="#24212b",
105
+ border_color_primary_dark="#343140",
106
+ background_fill_secondary_dark="#16141c"
107
+ ),
108
+ css=css,
109
+ retry_btn="Retry",
110
+ undo_btn="Undo",
111
+ clear_btn="Clear",
112
+ submit_btn="Send",
113
+ description="Llama-cpp-agent: Chat multi llm selection"
114
  )
115
 
 
116
  if __name__ == "__main__":
117
+ demo.launch()