Spaces:
Runtime error
Runtime error
Prashant Shukla commited on
Commit ·
5a7dc20
1
Parent(s): d2f50dc
First deployment of bot
Browse files- app.py +36 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import faiss,pickle
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
|
| 6 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 7 |
+
|
| 8 |
+
def rag_qa(query:str,top_k = 1) -> str:
|
| 9 |
+
if model.tokenizer.pad_token is None:
|
| 10 |
+
model.tokenizer.pad_token = model.tokenizer.eos_token
|
| 11 |
+
logging.info("Loading FAISS index")
|
| 12 |
+
loaded_index = faiss.read_index('.\\faiss_index.idx')
|
| 13 |
+
with open(".\\faiss_documents.pk1","rb") as f:
|
| 14 |
+
docs = pickle.load(f)
|
| 15 |
+
f.close()
|
| 16 |
+
logging.debug("FAISS index loaded from E:")
|
| 17 |
+
q_emb = model.encode([query],convert_to_numpy = True)
|
| 18 |
+
D,I = loaded_index.search(q_emb, top_k)
|
| 19 |
+
logging.debug("FAISS index search completed")
|
| 20 |
+
results = []
|
| 21 |
+
for idx in I[0]:
|
| 22 |
+
results.append(docs[idx])
|
| 23 |
+
logging.debug(f"The length of result is {len(results)}")
|
| 24 |
+
split_resp = results[0].split('A:')
|
| 25 |
+
output = split_resp[1].strip()
|
| 26 |
+
return output
|
| 27 |
+
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn = rag_qa,
|
| 30 |
+
inputs = [gr.Textbox(label = "Question", lines = 4, max_lines = 8)],
|
| 31 |
+
outputs = [gr.Textbox(label = "Answer", lines = 4, max_lines = 8)],
|
| 32 |
+
title = "DSCM KNOWLEDGE BOT"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
faiss-cpu
|