Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,35 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from
|
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 |
-
history_transformer_format = history + [[message, ""]]
|
30 |
-
stop = StopOnTokens()
|
31 |
-
|
32 |
-
|
33 |
-
# Formatting the input for the model.
|
34 |
-
messages = "</s>".join(["</s>".join(["\n<|user|>:" + item[0], "\n<|assistant|>:" + item[1]])
|
35 |
-
for item in history_transformer_format])
|
36 |
-
model_inputs = tokenizer([messages], return_tensors="pt").to(device)
|
37 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
38 |
-
generate_kwargs = dict(
|
39 |
-
model_inputs,
|
40 |
-
streamer=streamer,
|
41 |
-
max_new_tokens=1024,
|
42 |
-
do_sample=True,
|
43 |
-
top_p=0.95,
|
44 |
-
top_k=50,
|
45 |
-
temperature=0.7,
|
46 |
-
num_beams=1,
|
47 |
-
stopping_criteria=StoppingCriteriaList([stop])
|
48 |
)
|
49 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
50 |
-
t.start() # Starting the generation in a separate thread.
|
51 |
-
partial_message = ""
|
52 |
-
for new_token in streamer:
|
53 |
-
partial_message += new_token
|
54 |
-
if '</s>' in partial_message: # Breaking the loop if the stop token is generated.
|
55 |
-
break
|
56 |
-
yield partial_message
|
57 |
-
|
58 |
-
|
59 |
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
gr.ChatInterface(predict,
|
63 |
-
title="Tinyllama_chatBot",
|
64 |
-
description="Ask Tiny llama any questions",
|
65 |
-
examples=['How to cook a fish?', 'Who is the president of US now?']
|
66 |
-
).launch() # Launching the web interface.
|
|
|
1 |
+
import os
|
2 |
+
import chainlit as cl
|
3 |
+
from ctransformers import AutoModelForCausalLM
|
4 |
+
|
5 |
+
# Runs when the chat starts
|
6 |
+
@cl.on_chat_start
|
7 |
+
def main():
|
8 |
+
# Create the llm
|
9 |
+
llm = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GGUF",
|
10 |
+
model_file="mistral-7b-instruct-v0.1.Q4_K_M.gguf",
|
11 |
+
model_type="mistral",
|
12 |
+
temperature=0.7,
|
13 |
+
gpu_layers=0,
|
14 |
+
stream=True,
|
15 |
+
threads=int(os.cpu_count() / 2),
|
16 |
+
max_new_tokens=10000)
|
17 |
+
|
18 |
+
# Store the llm in the user session
|
19 |
+
cl.user_session.set("llm", llm)
|
20 |
+
|
21 |
+
# Runs when a message is sent
|
22 |
+
@cl.on_message
|
23 |
+
async def main(message: cl.Message):
|
24 |
+
# Retrieve the chain from the user session
|
25 |
+
llm = cl.user_session.get("llm")
|
26 |
+
|
27 |
+
msg = cl.Message(
|
28 |
+
content="",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
prompt = f"[INST]{message.content}[/INST]"
|
32 |
+
for text in llm(prompt=prompt):
|
33 |
+
await msg.stream_token(text)
|
34 |
|
35 |
+
await msg.send()
|
|
|
|
|
|
|
|
|
|