Chris STC commited on
Commit
f6c2dc2
1 Parent(s): e9fe302

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ os.system('CMAKE_ARGS="-DLLAMA_OPENBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python')
4
+ import wget
5
+ from llama_cpp import Llama
6
+ import random
7
+ url = 'https://huggingface.co/TheBloke/WizardLM-7B-uncensored-GGML/resolve/main/WizardLM-7B-uncensored.ggmlv3.q2_K.bin'
8
+ filename = wget.download(url)
9
+ llm2 = Llama(model_path=filename, seed=random.randint(1, 2**31))
10
+ filename = wget.download(url)
11
+ theme = gr.themes.Soft(
12
+ primary_hue=gr.themes.Color("#ededed", "#fee2e2", "#fecaca", "#fca5a5", "#f87171", "#ef4444", "#dc2626", "#b91c1c", "#991b1b", "#7f1d1d", "#6c1e1e"),
13
+ neutral_hue="red",
14
+ )
15
+ title = """<h1 align="center">Chat with awesome WizardLM 7b model!</h1><br>"""
16
+ with gr.Blocks(theme=theme) as demo:
17
+ gr.HTML(title)
18
+ gr.HTML("This model is awesome for its size! It is only 20th the size of Chatgpt but is around 90% as good as Chatgpt. However, please don't rely on WizardLM to provide 100% true information as it might be wrong sometimes. ")
19
+ chatbot = gr.Chatbot()
20
+ msg = gr.Textbox()
21
+ clear = gr.ClearButton([msg, chatbot])
22
+ #instruction = gr.Textbox(label="Instruction", placeholder=)
23
+
24
+ def user(user_message, history):
25
+ return gr.update(value="", interactive=True), history + [[user_message, None]]
26
+
27
+ def bot(history):
28
+ #instruction = history[-1][1] or ""
29
+ user_message = history[-1][0]
30
+ #token1 = llm.tokenize(b"### Instruction: ")
31
+ #token2 = llm.tokenize(instruction.encode())
32
+ #token3 = llm2.tokenize(b"USER: ")
33
+ tokens3 = llm2.tokenize(user_message.encode())
34
+ token4 = llm2.tokenize(b"\n\n### Response:")
35
+ tokens = tokens3 + token4
36
+ history[-1][1] = ""
37
+ count = 0
38
+ output = ""
39
+ for token in llm2.generate(tokens, top_k=50, top_p=0.73, temp=0.72, repeat_penalty=1.1):
40
+ text = llm2.detokenize([token])
41
+ output += text.decode()
42
+ count += 1
43
+ if count >= 500 or (token == llm2.token_eos()):
44
+ break
45
+ history[-1][1] += text.decode()
46
+ yield history
47
+
48
+ response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
49
+ bot, chatbot, chatbot
50
+ )
51
+ response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
52
+ gr.HTML("Thanks for checking out this app!")
53
+
54
+ demo.queue()
55
+ demo.launch(debug=True)