File size: 2,237 Bytes
9045a87
308dd86
 
 
57817e9
b91d77f
57817e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db8595
57817e9
b91d77f
8db8595
00d1f95
8db8595
 
 
 
308dd86
 
57817e9
308dd86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9045a87
57817e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import requests
import random
import json
def response(message, history, model):
  url="https://afischer1985-OpenHermes-2-GGUF-API.hf.space/v1/completions"
  prompt_type="ChatML"
  endstr="<|im_end|>"
  print(model)
  if(model=="WizardLM-13B"): 
      url="https://wizardlm-13b-v1-2-q4-0-gguf.hf.space/v1/completions"  
      prompt_type="Alpaca"
  if(model=="SauerkrautLM-7B"): 
      url="https://SauerkrautLM-GGUF-API.hf.space/v1/completions"  
      prompt_type="Alpaca"
  if(model=="OpenHermes2-7B"): 
      url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions"
      prompt_type="ChatML"
  if(model=="CollectiveCognition-7B"): 
      url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions"  
      prompt_type="ChatML" 
  print(prompt_type)
  if(prompt_type=="Alpaca"):
      body={"prompt":"###Frage: "+message+" ###Antwort:","max_tokens":1000,"stop":"###","stream":True}
  if(prompt_type!="Alpaca"):
      body={"prompt":"""<|im_start|>system
You are a helpful AI-Assistant.<|im_end|>
<|im_start|>user
"""+message+"""<|im_end|>
<|im_start|>assistant
""","max_tokens":1000,"stop":"<|im_end|>","stream":True}
  response=""
  buffer=""
  print(str(body))
  print("User: "+message+"\nAI: ")
  for text in requests.post(url, json=body, stream=True):  
    #print("*** Raw String: "+str(text)+"\n***\n")
    text=text.decode('utf-8')
    if(text.startswith(": ping -")==False):buffer=str(buffer)+str(text)
    #if(text.startswith(": ping -")): print("\n*** PING!\n***\n")
    #print("\n*** Buffer: "+str(buffer)+"\n***\n") 
    buffer=buffer.split('"finish_reason"')
    if(len(buffer)==1):
      buffer="".join(buffer)
      pass
    if(len(buffer)==2):
      part=buffer[0]+'"finish_reason": null}]}'  
      if(part.startswith("data: ")):part=part.replace("data: ", "")
      try: 
        part = str(json.loads(part)["choices"][0]["text"])
        print(part, end="", flush=True)
        response=response+part
        buffer="" # reset buffer
      except:
        pass
    yield response 

gr.ChatInterface(response,additional_inputs=[gr.Dropdown(["CollectiveCognition-7B", "OpenHermes2-7B"],value="OpenHermes2-7B",label="Model")]).queue().launch(share=True)