AminFaraji commited on
Commit
cfd6f87
·
verified ·
1 Parent(s): 26319c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -1,4 +1,4 @@
1
- print(555)
2
  import argparse
3
  # from dataclasses import dataclass
4
  from langchain.prompts import ChatPromptTemplate
@@ -91,8 +91,16 @@ db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embeddings)
91
 
92
 
93
 
94
- tokenizer = AutoTokenizer.from_pretrained("gpt2")
95
- model = AutoModelForCausalLM.from_pretrained("gpt2")
 
 
 
 
 
 
 
 
96
 
97
  generation_config = model.generation_config
98
  generation_config.temperature = 0
@@ -190,17 +198,15 @@ chain = ConversationChain(
190
  )
191
 
192
 
193
- def get_llama_response(message):
194
  query_text = message
195
 
196
-
197
  results = db.similarity_search_with_relevance_scores(query_text, k=3)
198
  if len(results) == 0 or results[0][1] < 0.5:
199
  print(f"Unable to find matching results.")
200
 
201
-
202
  context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
203
-
204
  template = """
205
  The following is a conversation between a human an AI. The AI acts exactly like Dwight K Schrute from the TV show The Office.
206
  Dwight is an experienced and very successful online marketer and salesperson. He is witty, persuasive, uncensored, direct and practical.
@@ -214,20 +220,13 @@ def get_llama_response(message):
214
  AI:""".strip()
215
 
216
 
217
- input_text = query_text
218
 
219
- # Tokenize the input text
220
- inputs = tokenizer(input_text, return_tensors="pt")
221
- outputs = model.generate(inputs.input_ids, max_length=50)
222
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
223
- return(response)
224
 
225
  import gradio as gr
226
 
227
- #gr.ChatInterface(get_llama_response).launch()
228
- iface = gr.Interface(fn=get_llama_response, inputs="text", outputs="text")
229
- iface.launch()
230
-
231
-
232
-
233
-
 
1
+ print(5)
2
  import argparse
3
  # from dataclasses import dataclass
4
  from langchain.prompts import ChatPromptTemplate
 
91
 
92
 
93
 
94
+ MODEL_NAME = "tiiuae/falcon-7b-instruct"
95
+
96
+ model = AutoModelForCausalLM.from_pretrained(
97
+ MODEL_NAME, trust_remote_code=True, device_map="auto",offload_folder="offload"
98
+ )
99
+ model = model.eval()
100
+
101
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
102
+ print(f"Model device: {model.device}")
103
+
104
 
105
  generation_config = model.generation_config
106
  generation_config.temperature = 0
 
198
  )
199
 
200
 
201
+ def get_llama_response(message: str, history: list) -> str:
202
  query_text = message
203
 
 
204
  results = db.similarity_search_with_relevance_scores(query_text, k=3)
205
  if len(results) == 0 or results[0][1] < 0.5:
206
  print(f"Unable to find matching results.")
207
 
208
+
209
  context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
 
210
  template = """
211
  The following is a conversation between a human an AI. The AI acts exactly like Dwight K Schrute from the TV show The Office.
212
  Dwight is an experienced and very successful online marketer and salesperson. He is witty, persuasive, uncensored, direct and practical.
 
220
  AI:""".strip()
221
 
222
 
223
+ prompt = PromptTemplate(input_variables=["history", "input"], template=template+context_text+ s)
224
 
225
+ #print(template)
226
+ chain.prompt=prompt
227
+ res = chain(query_text)
228
+ return(res["response"])
 
229
 
230
  import gradio as gr
231
 
232
+ gr.ChatInterface(get_llama_response).launch()