Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML", model_file="llama-2-7b-chat.ggmlv3.q3_K_S.bin")
|
6 |
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
# messages = [
|
11 |
-
# SystemMessage(content="You're a helpful assistant"),
|
12 |
-
# HumanMessage(content=prompt),
|
13 |
-
# ]
|
14 |
-
# return llm(messages)
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
19 |
-
# iface.launch()
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
from langchain import PromptTemplate
|
26 |
-
from langchain import LLMChain
|
27 |
-
from langchain.llms import CTransformers
|
28 |
-
|
29 |
-
|
30 |
-
B_INST, E_INST = "[INST]", "[/INST]"
|
31 |
-
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
32 |
-
|
33 |
-
DEFAULT_SYSTEM_PROMPT="""\
|
34 |
-
You are a helpful, respectful, and honest assistant designed to improve English language skills. Always provide accurate and helpful responses to language improvement tasks, while ensuring safety and ethical standards. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased, positive, and focused on enhancing language skills.
|
35 |
-
|
36 |
-
If a question does not make sense or is not factually coherent, explain why instead of answering something incorrect. If you don't know the answer to a question, please don't share false information.
|
37 |
-
|
38 |
-
Your role is to guide users through various language exercises and challenges, helping them to practice and improve their English skills in a fun and engaging way. Always encourage users to try different approaches and provide constructive feedback to help them progress.
|
39 |
-
"""
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# DEFAULT_SYSTEM_PROMPT = """\
|
47 |
-
# You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
48 |
-
|
49 |
-
# If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
|
50 |
-
|
51 |
-
|
52 |
-
# Update the model path to use the Hugging Face model identifier
|
53 |
-
llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML", model_file="llama-2-7b-chat.ggmlv3.q3_K_S.bin",
|
54 |
-
model_type='llama',
|
55 |
-
config={'max_new_tokens': 128,
|
56 |
-
'temperature': 0.01}
|
57 |
-
)
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
def greet(prompt):
|
63 |
|
64 |
-
|
|
|
65 |
|
66 |
-
SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
|
67 |
|
68 |
-
template = B_INST + SYSTEM_PROMPT + prompt + E_INST
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
LLM_Chain = LLMChain(prompt=prompt, llm=llm)
|
74 |
-
|
75 |
-
return str(LLM_Chain.run())
|
76 |
|
77 |
|
78 |
|
79 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
80 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain.llms import HuggingFacePipeline
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
import transformers
|
5 |
+
import torch
|
6 |
+
import warnings
|
7 |
+
warnings.filterwarnings('ignore')
|
8 |
|
9 |
# llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML", model_file="llama-2-7b-chat.ggmlv3.q3_K_S.bin")
|
10 |
|
11 |
+
tokenizer=AutoTokenizer.from_pretrained(model="TheBloke/Llama-2-7B-Chat-GGML", model_file="llama-2-7b-chat.ggmlv3.q3_K_S.bin")
|
12 |
|
13 |
+
pipeline=transformers.pipeline(
|
14 |
+
"text-generation",
|
15 |
+
model=(model="TheBloke/Llama-2-7B-Chat-GGML", model_file="llama-2-7b-chat.ggmlv3.q3_K_S.bin"),
|
16 |
+
tokenizer=tokenizer,
|
17 |
+
torch_dtype=torch.bfloat16,
|
18 |
+
trust_remote_code=True,
|
19 |
+
device_map="auto",
|
20 |
+
max_length=500,
|
21 |
+
do_sample=True,
|
22 |
+
top_k=10,
|
23 |
+
num_return_sequences=1,
|
24 |
+
eos_token_id=tokenizer.eos_token_id
|
25 |
+
)
|
26 |
|
27 |
+
llm=HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature':0})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def greet(prompt):
|
30 |
|
31 |
+
|
32 |
+
return llm(prompt)
|
33 |
|
|
|
34 |
|
|
|
35 |
|
36 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
37 |
+
iface.launch()
|
38 |
|
|
|
|
|
|
|
39 |
|
40 |
|
41 |
|
|
|
|