Spaces:
Runtime error
Runtime error
File size: 5,476 Bytes
ac1af52 bd6d025 ac1af52 da69772 ac1af52 da69772 ac1af52 bd6d025 ac1af52 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
import llama_cpp
import llama_cpp.llama_tokenizer
from llama_cpp import Llama
import gradio as gr
from loguru import logger
import psutil
from ctransformers import AutoModelForCausalLM,AutoTokenizer
prompt_template = """[INST] <<SYS>>
You are a helpful assistant for a crowdfunding platform called GiveSendGo. Your goal is to gather essential information for campaign and generate a title and sample pitch of atleast 1000 words for the campaign.
<</SYS>>
{question} [/INST]
"""
model_loc = "models/llama-2-13b-chat.Q5_K_M.gguf"
model_loc = "TheBloke/Llama-2-13B-chat-GGUF"
llama = AutoModelForCausalLM.from_pretrained(
model_loc,
model_type="llama",
context_length=4096,
max_new_tokens=2048,
hf=True
# threads=cpu_count,
)
# llama = llama_cpp.Llama.from_pretrained(
# #repo_id="Qwen/Qwen1.5-0.5B-Chat-GGUF",
# #filename="*q8_0.gguf",
# mode_path=model_loc,
# model_type="llama",
# context_length=4096,
# max_new_tokens=2048,
# filename="llama-2-13b-chat.Q5_K_M.gguf",
# tokenizer=llama_cpp.llama_tokenizer.LlamaHFTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B"),
# verbose=False
# )
# llama = Llama(
# model_path=model_loc,
# max_tokens=4096,
# n_ctx=4096,
# verbose=False,
# )
_ = [elm for elm in prompt_template.splitlines() if elm.strip()]
stop_string = [elm.split(":")[0] + ":" for elm in _][-2]
logger.debug(f"{stop_string=}")
_ = psutil.cpu_count(logical=False) - 1
cpu_count: int = int(_) if _ else 1
logger.debug(f"{cpu_count=}")
model = "gpt-3.5-turbo"
def predict(message, history):
messages = []
prompt = prompt_template.format(question=message)
for user_message, assistant_message in history:
messages.append({"role": "system", "content": prompt},)
messages.append({"role": "user", "content": user_message})
messages.append({"role": "assistant", "content": assistant_message})
messages.append({"role": "user", "content": message})
response = llama.create_chat_completion_openai_v1(
model=model,
messages=messages,
response_format={
"type": "json_object",
"schema": {
"type": "object",
"properties": {"title": {"type": "string"},
#"description": {"type": "string"},
"sample_pitch": {"type": "string"},
"amount": {"type": "string"},
"location": {"type": "string"}},
"required": ["title","sample_pitch","amount","location"], #description
},
},
temperature=0.7,
stream=True
)
text = ""
for chunk in response:
content = chunk.choices[0].delta.content
if content:
text += content
yield text
def generate(message):
try:
messages = []
prompt = prompt_template.format(question=message)
#for user_message, assistant_message in history:
messages.append({"role": "system", "content": prompt},)
#messages.append({"role": "user", "content": user_message})
#messages.append({"role": "assistant", "content": assistant_message})
messages.append({"role": "user", "content": message})
response = llama.create_chat_completion_openai_v1(
model=model,
messages=messages,
response_format={
"type": "json_object",
"schema": {
"type": "object",
"properties": {"title": {"type": "string"},
#"description": {"type": "string"},
"sample_pitch": {"type": "string"},
"amount": {"type": "string"},
"location": {"type": "string"}},
"required": ["title","sample_pitch","amount","location"], #description
},
},
temperature=0.7,
stream=False)
# text = ""
# for chunk in response:
# content = chunk.choices[0].delta.content
# if content:
# text += content
# logger.debug(f"api: {content=}")
# yield text
logger.debug(f"{response}")
return response.choices[0].delta.content
except Exception as exc:
logger.error(exc)
response = f"{exc=}"
def predict_api(message):
logger.debug(f"{message=}")
text = generate(message)
logger.debug(f"text::{text=}")
return f"json: {text=}"
js = """function () {
gradioURL = window.location.href
if (!gradioURL.endsWith('?__theme=dark')) {
window.location.replace(gradioURL + '?__theme=dark');
}
}"""
css = """
footer {
visibility: hidden;
}
full-height {
height: 100%;
}
"""
with gr.Blocks(theme=gr.themes.Soft(), js=js, css=css) as demo:
gr.ChatInterface(predict, examples=["What is the capital of France?", "Who was the first person on the moon?"])
with gr.Accordion("For Chat/Translation API", open=False, visible=False):
input_text = gr.Text()
api_btn = gr.Button("Go", variant="primary")
out_text = gr.Text()
api_btn.click(
predict_api,
input_text,
out_text,
api_name="api",
)
if __name__ == "__main__":
demo.queue().launch(debug=True, share=True)
|