Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
34 |
+
print("User: "+message+"\nAI: ")
|
35 |
+
for text in requests.post(url, json=body, stream=True): #-H 'accept: application/json' -H 'Content-Type: application/json'
|
36 |
+
if buffer is None: buffer=""
|
37 |
+
buffer=str("".join(buffer))
|
38 |
+
#print("*** Raw String: "+str(text)+"\n***\n")
|
39 |
+
text=text.decode('utf-8')
|
40 |
+
if((text.startswith(": ping -")==False) & (len(text.strip("\n\r"))>0)): buffer=buffer+str(text)
|
41 |
+
#print("\n*** Buffer: "+str(buffer)+"\n***\n")
|
42 |
+
buffer=buffer.split('"finish_reason": null}]}')
|
43 |
+
if(len(buffer)==1):
|
44 |
+
buffer="".join(buffer)
|
45 |
+
pass
|
46 |
+
if(len(buffer)==2):
|
47 |
+
part=buffer[0]+'"finish_reason": null}]}'
|
48 |
+
if(part.lstrip('\n\r').startswith("data: ")): part=part.lstrip('\n\r').replace("data: ", "")
|
49 |
+
try:
|
50 |
+
part = str(json.loads(part)["choices"][0]["text"])
|
51 |
+
print(part, end="", flush=True)
|
52 |
+
response=response+part
|
53 |
+
buffer="" # reset buffer
|
54 |
+
except Exception as e:
|
55 |
+
print("Exception:"+str(e))
|
56 |
+
pass
|
57 |
+
yield response
|
58 |
+
|
59 |
+
gr.ChatInterface(
|
60 |
+
fn=response,
|
61 |
+
title="Mistral-7B-Instruct-v0.2-GGUF Chatbot",
|
62 |
+
theme='syddharth/gray-minimal'
|
63 |
+
).queue().launch(share=True)
|