Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,34 @@
|
|
|
|
|
|
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 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
32 |
for text in llm(prompt=prompt):
|
33 |
-
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from threading import Thread
|
3 |
import os
|
|
|
4 |
from ctransformers import AutoModelForCausalLM
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
llm = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GGUF",
|
8 |
+
model_file="mistral-7b-instruct-v0.1.Q4_K_M.gguf",
|
9 |
+
model_type="mistral",
|
10 |
+
temperature=0.7,
|
11 |
+
gpu_layers=0,
|
12 |
+
stream=True,
|
13 |
+
threads=int(os.cpu_count() / 2),
|
14 |
+
max_new_tokens=10000)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Function to generate model predictions.
|
18 |
+
def predict(message, history):
|
19 |
+
history_transformer_format = history + [[message, ""]]
|
20 |
|
21 |
+
# Formatting the input for the model.
|
22 |
+
messages = "</s>".join(["</s>".join(["\n<|user|>:" + item[0], "\n<|assistant|>:" + item[1]])
|
23 |
+
for item in history_transformer_format])
|
24 |
+
|
25 |
+
prompt = f"[INST]{messages.content}[/INST]"
|
26 |
for text in llm(prompt=prompt):
|
27 |
+
yield text
|
28 |
|
29 |
+
# Setting up the Gradio chat interface.
|
30 |
+
gr.ChatInterface(predict,
|
31 |
+
title="Test Mistral 7B",
|
32 |
+
description="Ask Mistral any questions",
|
33 |
+
examples=['How to cook a fish?', 'Who is the president of US now?']
|
34 |
+
).launch() # Launching the web interface.
|