sithumonline commited on
Commit
be4916e
·
verified ·
1 Parent(s): 97ebfb3

Call via llama

Browse files
Files changed (1) hide show
  1. app.py +53 -29
app.py CHANGED
@@ -1,13 +1,21 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- 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
6
- """
7
- client = InferenceClient("TheBloke/phi-2-GGUF")
8
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
 
11
  message,
12
  history: list[tuple[str, str]],
13
  system_message,
@@ -15,35 +23,51 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- response += token
40
- yield response
41
 
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
  demo = gr.ChatInterface(
46
- respond,
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  additional_inputs=[
48
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -60,4 +84,4 @@ demo = gr.ChatInterface(
60
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ import copy
4
+ from llama_cpp import Llama
5
+ from huggingface_hub import hf_hub_download
6
 
 
 
 
 
7
 
8
+ llm = Llama(
9
+ model_path=hf_hub_download(
10
+ repo_id=os.environ.get("REPO_ID", "TheBloke/phi-2-GGUF"),
11
+ filename=os.environ.get("MODEL_FILE", "phi-2.Q4_K_M.gguf"),
12
+ ),
13
+ n_ctx=2048,
14
+ n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM
15
+ )
16
 
17
+
18
+ def generate_text(
19
  message,
20
  history: list[tuple[str, str]],
21
  system_message,
 
23
  temperature,
24
  top_p,
25
  ):
26
+ temp = ""
27
+ input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
28
+ for interaction in history:
29
+ input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
 
 
 
30
 
31
+ input_prompt = input_prompt + str(message) + " [/INST] "
32
 
33
+ output = llm(
34
+ input_prompt,
 
 
 
 
35
  temperature=temperature,
36
  top_p=top_p,
37
+ top_k=40,
38
+ repeat_penalty=1.1,
39
+ max_tokens=max_tokens,
40
+ stop=[
41
+ "<|prompter|>",
42
+ "<|endoftext|>",
43
+ "<|endoftext|> \n",
44
+ "ASSISTANT:",
45
+ "USER:",
46
+ "SYSTEM:",
47
+ ],
48
+ stream=True,
49
+ )
50
+ for out in output:
51
+ stream = copy.deepcopy(out)
52
+ temp += stream["choices"][0]["text"]
53
+ yield temp
54
 
 
 
55
 
 
 
 
56
  demo = gr.ChatInterface(
57
+ generate_text,
58
+ title="llama-cpp-python on GPU",
59
+ description="Running LLM with https://github.com/abetlen/llama-cpp-python",
60
+ examples=[
61
+ ['How to setup a human base on Mars? Give short answer.'],
62
+ ['Explain theory of relativity to me like I’m 8 years old.'],
63
+ ['What is 9,000 * 9,000?'],
64
+ ['Write a pun-filled happy birthday message to my friend Alex.'],
65
+ ['Justify why a penguin might make a good king of the jungle.']
66
+ ],
67
+ cache_examples=False,
68
+ retry_btn=None,
69
+ undo_btn="Delete Previous",
70
+ clear_btn="Clear",
71
  additional_inputs=[
72
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
73
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
84
 
85
 
86
  if __name__ == "__main__":
87
+ demo.launch()