AFischer1985 commited on
Commit
8489876
1 Parent(s): 50734e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -23
app.py CHANGED
@@ -1,33 +1,74 @@
1
- import json
2
- import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import requests
4
- from llama_cpp import Llama
5
- import gradio as gr
6
 
7
- #url="https://huggingface.co/TheBloke/WizardLM-13B-V1.2-GGUF/resolve/main/wizardlm-13b-v1.2.Q4_0.gguf"
8
- #url="https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/resolve/main/mixtral-8x7b-instruct-v0.1.Q4_0.gguf?download=true"
9
- url="https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_0.gguf?download=true"
10
- response = requests.get(url)
11
- with open("./model.gguf", mode="wb") as file:
12
- file.write(response.content)
13
- print("Model downloaded")
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- command = ["python3", "-m", "llama_cpp.server", "--model", "./model.gguf", "--host", "0.0.0.0", "--port", "2600", "--n_threads", "2"]
 
 
 
 
16
  subprocess.Popen(command)
17
- print("Model ready!")
 
 
18
 
19
- #llm = Llama(model_path="./model.gguf")
20
- #def response(input_text, history):
21
- # output = llm(f"Q: {input_text} A:", max_tokens=256, stop=["Q:", "\n"], echo=True)
22
- # return output['choices'][0]['text']
23
 
 
 
 
24
  def response(message, history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  #url="https://afischer1985-wizardlm-13b-v1-2-q4-0-gguf.hf.space/v1/completions"
26
  url="http://0.0.0.0:2600/v1/completions"
27
- #body={"prompt":"Im Folgenden findest du eine Instruktion, die eine Aufgabe bescheibt. Schreibe eine Antwort, um die Aufgabe zu lösen.\n\n### Instruktion:\n"+message+"\n\n### Antwort:","max_tokens":500, "echo":"False","stream":"True"}
28
- #body={"prompt":" chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.\n\nUSER:\n"+message+"\n\nASSISTANT:","max_tokens":500, "echo":"False","stream":"True"}
29
- #body={"prompt":system+"### Instruktion:\n"+message+"\n\n### Antwort:","max_tokens":500, "echo":"False","stream":"True"} #e.g. SauerkrautLM
30
- body={"prompt":"[INST]"+message+"[/INST]","max_tokens":500, "echo":"False","stream":"True"} #e.g. Mixtral-Instruct
31
  response=""
32
  buffer=""
33
  print("URL: "+url)
@@ -56,5 +97,5 @@ def response(message, history):
56
  pass
57
  yield response
58
 
59
- gr.ChatInterface(response,title="Mistral-7B-Instruct-v0.2-GGUF Chatbot").queue().launch(share=True) #False, server_name="0.0.0.0", server_port=7864)
60
- print("Interface up and running!")
 
1
+ #########################################################################################
2
+ # Title: Gradio Interface to LLM-chatbot with on premises
3
+ # Author: Andreas Fischer
4
+ # Date: October 15th, 2023
5
+ # Last update: January 21st, 2024
6
+ ##########################################################################################
7
+
8
+ #https://github.com/abetlen/llama-cpp-python/issues/306
9
+ #sudo apt install libclblast-dev
10
+ #CMAKE_ARGS="-DLLAMA_CLBLAST=on" FORCE_CMAKE=1 pip install llama-cpp-python --force-reinstall --upgrade --no-cache-dir -v
11
+
12
+ # Get model
13
+ #-----------
14
+
15
+ import os
16
  import requests
 
 
17
 
18
+ ##modelPath="/home/af/gguf/models/phi-2.Q4_0.gguf"
19
+ #modelPath="/home/af/gguf/models/openchat-3.5-0106.Q4_0.gguf"
20
+ modelPath="/home/af/gguf/models/SauerkrautLM-7b-HerO-q8_0.gguf"
21
+ #modelPath="/home/af/gguf/models/sauerkrautlm-una-solar-instruct.Q4_0.gguf"
22
+ #modelPath="/home/af/gguf/models/decilm-7b-uniform-gqa-q8_0.gguf"
23
+ #modelPath="/home/af/gguf/models/mixtral-8x7b-instruct-v0.1.Q4_0.gguf"
24
+ if(os.path.exists(modelPath)==False):
25
+ #url="https://huggingface.co/TheBloke/WizardLM-13B-V1.2-GGUF/resolve/main/wizardlm-13b-v1.2.Q4_0.gguf"
26
+ #url="https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/resolve/main/mixtral-8x7b-instruct-v0.1.Q4_0.gguf?download=true"
27
+ url="https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_0.gguf?download=true"
28
+ response = requests.get(url)
29
+ with open("./model.gguf", mode="wb") as file:
30
+ file.write(response.content)
31
+ print("Model downloaded")
32
+ modelPath="./model.gguf"
33
+
34
+ print(modelPath)
35
+
36
 
37
+ # Llama-cpp-Server
38
+ #------------------
39
+
40
+ import subprocess
41
+ command = ["python3", "-m", "llama_cpp.server", "--model", modelPath, "--host", "0.0.0.0", "--port", "2600", "--n_threads", "4", "--n_gpu_layers","20"]
42
  subprocess.Popen(command)
43
+ print("Server ready!")
44
+
45
+
46
 
47
+ # Gradio-GUI
48
+ #------------
 
 
49
 
50
+ import gradio as gr
51
+ import requests
52
+ import json
53
  def response(message, history):
54
+ prompt=message
55
+ system="Du bist ein KI-basiertes Assistenzsystem."
56
+ if("mixtral-8x7b-instruct" in modelPath):
57
+ prompt=f"[INST] {prompt} [/INST]"
58
+ if("mixtral-8x7b-instruct" in modelPath):
59
+ prompt=f"[INST] {prompt} [/INST]"
60
+ if("openchat-3.5" in modelPath):
61
+ prompt=f"GPT4 Correct User: {system} {prompt}<|end_of_turn|>GPT4 Correct Assistant:"
62
+ if("SauerkrautLM-7b-HerO" in modelPath):
63
+ prompt=f"<|im_start|>system\n{system}<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
64
+ if("WizardLM-13B-V1.2" in modelPath):
65
+ prompt=f"{system} USER: {prompt} ASSISTANT: "
66
+ if("phi-2" in modelPath):
67
+ prompt=f"Instruct: {prompt}\nOutput:"
68
+ print(prompt)
69
  #url="https://afischer1985-wizardlm-13b-v1-2-q4-0-gguf.hf.space/v1/completions"
70
  url="http://0.0.0.0:2600/v1/completions"
71
+ body={"prompt":prompt,"max_tokens":1000, "echo":"False","stream":"True"} #e.g. Mixtral-Instruct
 
 
 
72
  response=""
73
  buffer=""
74
  print("URL: "+url)
 
97
  pass
98
  yield response
99
 
100
+ gr.ChatInterface(response, chatbot=gr.Chatbot(render_markdown=True),title="AI-Interface").queue().launch(share=True) #False, server_name="0.0.0.0", server_port=7864)
101
+ print("Interface up and running!")