Mahmoud Aboliel commited on
Commit
04e8cf9
1 Parent(s): 3f12e0f

custom the model for chat

Browse files
Files changed (1) hide show
  1. app.py +70 -4
app.py CHANGED
@@ -1,12 +1,78 @@
1
- import gradio as gr
2
- from langchain_community.llms import CTransformers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- llm = CTransformers(model="TheBloke/Llama-2-7B-Chat-GGML", model_file="llama-2-7b-chat.ggmlv3.q3_K_S.bin")
5
 
6
 
7
 
8
  def greet(prompt):
9
- return llm(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
 
12
 
 
1
+ # import gradio as gr
2
+ # from langchain_community.llms import CTransformers
3
+ # from langchain_core.messages import HumanMessage, SystemMessage
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
+ # def greet(prompt):
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
+ # instruction = "Convert the following text from English to French: \n\n {text}"
65
+
66
+ SYSTEM_PROMPT = B_SYS + DEFAULT_SYSTEM_PROMPT + E_SYS
67
+
68
+ template = B_INST + SYSTEM_PROMPT + prompt + E_INST
69
+
70
+ # print(template)
71
+ prompt = PromptTemplate(template=template, input_variables=["text"])
72
+
73
+ LLM_Chain = LLMChain(prompt=prompt, llm=llm)
74
+
75
+ return LLM_Chain.run()
76
 
77
 
78