Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import random | |
import json | |
def response(message, history, model, prompt_type): | |
url="https://afischer1985-OpenHermes-2-GGUF-API.hf.space/v1/completions" | |
endstr="<|im_end|>" | |
print(model) | |
if(model=="WizardLM-13B"): | |
url="https://wizardlm-13b-v1-2-q4-0-gguf.hf.space/v1/completions" | |
if(prompt_type=="Default"): prompt_type="Alpaca" | |
if(model=="SauerkrautLM-7B"): | |
url="https://SauerkrautLM-GGUF-API.hf.space/v1/completions" | |
if(prompt_type=="Default"): prompt_type="Alpaca" | |
if(model=="OpenHermes2-7B"): | |
url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions" | |
if(prompt_type=="Default"): prompt_type="ChatML" | |
if(model=="CollectiveCognition-7B"): | |
url="https://AFischer1985-CollectiveCognition-GGUF-API.hf.space/v1/completions" | |
if(prompt_type=="Default"): prompt_type="ChatML" | |
print(prompt_type) | |
if(prompt_type=="Alpaca"): | |
body={"prompt":"###Frage: "+message+" ###Antwort:","max_tokens":1000,"stop":"###","stream":True} | |
if(prompt_type=="ChatML"): | |
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"),gr.Dropdown(["Default", "Alpaca","ChatML"],value="Default",label="Prompt Type")]).queue().launch(share=True) |