AFischer1985 commited on
Commit
caaa800
1 Parent(s): 2fcdaa5

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +48 -27
run.py CHANGED
@@ -1,58 +1,79 @@
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
  import random
4
  import json
5
- def response(message, history, model, prompt_type):
6
- url="https://afischer1985-OpenHermes-2-GGUF-API.hf.space/v1/completions"
7
- endstr="<|im_end|>"
8
- print(model)
9
- if(model=="WizardLM-13B"):
10
- url="https://wizardlm-13b-v1-2-q4-0-gguf.hf.space/v1/completions"
11
- if(prompt_type=="Default"): prompt_type="Vicuna"
 
12
  if(model=="SauerkrautLM-7B"):
13
- url="https://SauerkrautLM-GGUF-API.hf.space/v1/completions"
14
- if(prompt_type=="Default"): prompt_type="Vicuna (German)"
 
 
 
15
  if(model=="OpenHermes2-7B"):
16
- url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions"
17
- if(prompt_type=="Default"): prompt_type="ChatML"
18
  if(model=="CollectiveCognition-7B"):
19
- url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions"
20
- if(prompt_type=="Default"): prompt_type="ChatML"
21
- print(prompt_type)
22
  if(prompt_type=="ChatML"):
23
- body={"prompt":"<|im_start|>system\nYou are a helpful AI-Assistant.<|im_end|>\n<|im_start|>user\n"+message+"<|im_end|>\n<|im_start|>assistant","max_tokens":1000,"stop":"<|im_end|>","stream":True}
24
  if(prompt_type=="ChatML (German)"):
25
- body={"prompt":"<|im_start|>system\nDu bist ein KI-basiertes deutschsprachiges Assistenzsystem.<|im_end|>\n<|im_start|>user"+message+"<|im_end|>\n<|im_start|>assistant","max_tokens":1000,"stop":"<|im_end|>","stream":True}
26
  if(prompt_type=="Alpaca"):
27
- body={"prompt":"###Instruction:\n"+message+"\n\n###Resonse:\n","max_tokens":1000,"stop":"###","stream":True}
28
  if(prompt_type=="Vicuna"):
29
- body={"prompt":"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: "+message+" ASSISTANT:","max_tokens":1000,"stop":"USER:","stream":True}
30
  if(prompt_type=="Vicuna (German)"):
31
- body={"prompt":"Ein Chat zwischen einem Benutzer und einem KI-Assistenten. Der KI-Assistent gibt hilfreiche, detaillierte und höfliche Antworten.\nUser: "+message+"\nAssistant: ","max_tokens":1000,"stop":"User:","stream":True}
 
 
 
 
 
 
 
32
  response=""
33
  buffer=""
34
- print(str(body))
35
  print("User: "+message+"\nAI: ")
36
  for text in requests.post(url, json=body, stream=True):
37
- #print("*** Raw String: "+str(text)+"\n***\n")
38
  text=text.decode('utf-8')
39
  if(text.startswith(": ping -")==False):buffer=str(buffer)+str(text)
40
- #if(text.startswith(": ping -")): print("\n*** PIacNG!\n***\n")
41
- #print("\n*** Buffer: "+str(buffer)+"\n***\n")
42
- buffer=buffer.split('"finish_reason"')
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.startswith("data: ")):part=part.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:
55
  pass
56
  yield response
57
 
58
- gr.ChatInterface(response,additional_inputs=[gr.Dropdown(["CollectiveCognition-7B", "OpenHermes2-7B"],value="OpenHermes2-7B",label="Model"),gr.Dropdown(["Default", "ChatML","ChatML (German)","Vicuna","Vicuna (German)","Alpaca"],value="Default",label="Prompt Type")]).queue().launch(share=True)
 
 
 
 
 
 
 
1
+ #############################################################################
2
+ # Title: Gradio Interface to AI hosted on Huggingface-Space
3
+ # Author: Andreas Fischer
4
+ # Date: October 7th, 2023
5
+ # Last update: December 8th, 2023
6
+ #############################################################################
7
+
8
  import gradio as gr
9
  import requests
10
  import random
11
  import json
12
+
13
+ def specifications(message, model,prompt_type,verbose=False):
14
+ url="http://0.0.0.0:2600/v1/completions"
15
+ body=""
16
+
17
+ if(model=="Local"):
18
+ url="http://0.0.0.0:2600/v1/completions"
19
+ if(prompt_type=="Default"): prompt_type="ChatML (German)"
20
  if(model=="SauerkrautLM-7B"):
21
+ url="https://SauerkrautLM-GGUF-API.hf.space/v1/completions"
22
+ if(prompt_type=="Default"): prompt_type="Vicuna (German)"
23
+ if(model=="WizardLM-13B"):
24
+ url="https://afischer1985-wizardlm-13b-v1-2-q4-0-gguf.hf.space/v1/completions"
25
+ if(prompt_type=="Default"): prompt_type="Vicuna"
26
  if(model=="OpenHermes2-7B"):
27
+ url="https://AFischer1985-OpenHermes-2-GGUF-API.hf.space/v1/completions"
28
+ if(prompt_type=="Default"): prompt_type="ChatML"
29
  if(model=="CollectiveCognition-7B"):
30
+ url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions"
31
+ if(prompt_type=="Default"): prompt_type="ChatML"
32
+
33
  if(prompt_type=="ChatML"):
34
+ body={"prompt":"<|im_start|>system\nYou are a helpful AI-Assistant.<|im_end|>\n<|im_start|>user\n"+message+"<|im_end|>\n<|im_start|>assistant\n","max_tokens":1000,"stop":"<|im_end|>","echo":"False","stream":True}
35
  if(prompt_type=="ChatML (German)"):
36
+ body={"prompt":"<|im_start|>system\nu bist ein großes Sprachmodell, das höflich und kompetent antwortet. Schreibe deine Gedanken Schritt für Schritt auf, um Probleme sinnvoll zu lösen.<|im_end|>\n<|im_start|>user\n"+message+"<|im_end|>\n<|im_start|>assistant\n","max_tokens":1000,"stop":"User:","echo":"False","stream":True}
37
  if(prompt_type=="Alpaca"):
38
+ body={"prompt":"###Instruction:\n"+message+"\n\n###Response:\n","max_tokens":1000,"stop":"###","echo":"False","stream":True}
39
  if(prompt_type=="Vicuna"):
40
+ body={"prompt":"A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: "+message+" ASSISTANT:","max_tokens":1000,"stop":"USER:","echo":"False","stream":"True"}
41
  if(prompt_type=="Vicuna (German)"):
42
+ body={"prompt":"Ein Chat zwischen einem Benutzer und einem KI-Assistenten. Der KI-Assistent gibt hilfreiche, detaillierte und höfliche Antworten.\nUser: "+message+"\nAssistant: ","max_tokens":1000,"stop":"User:","echo":"False","stream":True}
43
+ if(verbose==True):
44
+ print("model: "+model+"\n"+"URL: "+url+"\n"+"prompt_type: "+prompt_type+"\n"+"message: "+message+"\n"+"body: "+str(body)+"\n")
45
+ return([url,body,model,prompt_type])
46
+
47
+ def response(message, history, model, prompt_type):
48
+ print(model)
49
+ [url,body,model,prompt_type]=specifications(message,model,prompt_type,verbose=True)
50
  response=""
51
  buffer=""
52
+ print("URL: "+url)
53
  print("User: "+message+"\nAI: ")
54
  for text in requests.post(url, json=body, stream=True):
 
55
  text=text.decode('utf-8')
56
  if(text.startswith(": ping -")==False):buffer=str(buffer)+str(text)
57
+ buffer=buffer.split('"finish_reason": null}]}')
 
 
58
  if(len(buffer)==1):
59
  buffer="".join(buffer)
60
  pass
61
  if(len(buffer)==2):
62
  part=buffer[0]+'"finish_reason": null}]}'
63
+ if(part.lstrip('\n\r').startswith("data: ")): part=part.lstrip('\n\r').replace("data: ", "")
64
  try:
65
  part = str(json.loads(part)["choices"][0]["text"])
66
  print(part, end="", flush=True)
67
  response=response+part
68
+ buffer=""
69
  except:
70
  pass
71
  yield response
72
 
73
+
74
+ gr.ChatInterface(response,additional_inputs=[gr.Dropdown(["Local","CollectiveCognition-7B", "OpenHermes2-7B","WizardLM-13B"],value="WizardLM-13B",label="Model"),gr.Dropdown(["Default", "ChatML","ChatML (German)","Vicuna","Vicuna (German)","Alpaca"],value="Default",label="Prompt Type")]).queue().launch(share=False, server_name="0.0.0.0", server_port=7864)
75
+
76
+ #import os
77
+ #os.system('python3 -m llama_cpp.server --model "/home/af/gguf/models/SauerkrautLM-7b-HerO-q8_0.gguf" --host 0.0.0.0 --port 2600')
78
+
79
+