dkdaniz commited on
Commit
b531b23
1 Parent(s): 7eef381

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import copy
4
+ import time
5
+ import llama_cpp
6
+ from llama_cpp import Llama
7
+ from huggingface_hub import hf_hub_download
8
+
9
+ from constants import (
10
+ CHROMA_SETTINGS,
11
+ DOCUMENT_MAP,
12
+ EMBEDDING_MODEL_NAME,
13
+ INGEST_THREADS,
14
+ PERSIST_DIRECTORY,
15
+ SOURCE_DIRECTORY,
16
+ )
17
+
18
+ with open("ingest.py", "r") as file:
19
+ code = file.read()
20
+ exec(code)
21
+
22
+
23
+ llm = Llama(
24
+ model_path=hf_hub_download(
25
+ repo_id=os.environ.get("REPO_ID", "TheBloke/Llama-2-7b-Chat-GGUF"),
26
+ filename=os.environ.get("MODEL_FILE", "llama-2-7b-chat.Q5_0.gguf"),
27
+ ),
28
+ n_ctx=2048,
29
+ n_gpu_layers=50, # change n_gpu_layers if you have more or less VRAM
30
+ )
31
+
32
+ history = []
33
+
34
+ system_message = """
35
+ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
36
+ If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
37
+ """
38
+
39
+
40
+ def generate_text(message, history):
41
+ temp = ""
42
+ input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
43
+ for interaction in history:
44
+ input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
45
+
46
+ input_prompt = input_prompt + str(message) + " [/INST] "
47
+
48
+ output = llm(
49
+ input_prompt,
50
+ temperature=0.15,
51
+ top_p=0.1,
52
+ top_k=40,
53
+ repeat_penalty=1.1,
54
+ max_tokens=1024,
55
+ stop=[
56
+ "<|prompter|>",
57
+ "<|endoftext|>",
58
+ "<|endoftext|> \n",
59
+ "ASSISTANT:",
60
+ "USER:",
61
+ "SYSTEM:",
62
+ ],
63
+ stream=True,
64
+ )
65
+ for out in output:
66
+ stream = copy.deepcopy(out)
67
+ temp += stream["choices"][0]["text"]
68
+ yield temp
69
+
70
+ history = ["init", input_prompt]
71
+
72
+
73
+ demo = gr.ChatInterface(
74
+ generate_text,
75
+ title="llama-cpp-python on GPU",
76
+ description="Running LLM with https://github.com/abetlen/llama-cpp-python",
77
+ examples=["tell me everything about llamas"],
78
+ cache_examples=True,
79
+ retry_btn=None,
80
+ undo_btn="Delete Previous",
81
+ clear_btn="Clear",
82
+ )
83
+ demo.queue(concurrency_count=1, max_size=5)
84
+ demo.launch()