ancebuc commited on
Commit
571e5fc
1 Parent(s): b74c351

Añado cosas, ni idea

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py CHANGED
@@ -7,6 +7,36 @@ with gr.Blocks() as demo:
7
  msg = gr.Textbox()
8
  clear = gr.ClearButton([msg, chatbot])
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def respond(message, chat_history):
11
  bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
12
  chat_history.append((message, bot_message))
 
7
  msg = gr.Textbox()
8
  clear = gr.ClearButton([msg, chatbot])
9
 
10
+ model = AutoModelForSequenceClassification.from_pretrained("/path/to/serialized/model/")
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained("/path/to/serialized/tokenizer/")
13
+
14
+ query_pipeline = transformers.pipeline(
15
+ "text-generation",
16
+ model=model,
17
+ tokenizer=tokenizer,
18
+ torch_dtype=torch.float16,
19
+ device_map="auto", max_new_tokens=200)
20
+
21
+ vectordb = Chroma.from_documents(documents=all_splits, embedding=embeddings, persist_directory="chroma_db")
22
+
23
+ def test_rag(pipeline, query):
24
+ docs = vectordb.similarity_search_with_score(query)
25
+ context = []
26
+ for doc,score in docs:
27
+ if(score<7):
28
+ doc_details = doc.to_json()['kwargs']
29
+ context.append( doc_details['page_content'])
30
+ if(len(context)!=0):
31
+ messages = [{"role": "user", "content": "Basándote en la siguiente información: " + "\n".join(context) + "\n Responde en castellano a la pregunta: " + query}]
32
+ prompt = pipeline.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
33
+ outputs = pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
34
+ answer = outputs[0]["generated_text"]
35
+ return answer[answer.rfind("[/INST]")+8:],docs
36
+ else:
37
+ return "No tengo información para responder a esta pregunta",docs
38
+
39
+
40
  def respond(message, chat_history):
41
  bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
42
  chat_history.append((message, bot_message))