adarshzolekar commited on
Commit
43b9dec
·
verified ·
1 Parent(s): 42e3b98

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
+ import faiss
4
+ import numpy as np
5
+ from transformers import pipeline
6
+
7
+ docs = [
8
+ "RAG stands for Retrieval Augmented Generation.",
9
+ "This chatbot runs on Hugging Face Spaces.",
10
+ "FAISS is a vector database."
11
+ ]
12
+
13
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
14
+ doc_embeddings = embedder.encode(docs)
15
+
16
+ index = faiss.IndexFlatL2(doc_embeddings.shape[1])
17
+ index.add(np.array(doc_embeddings))
18
+
19
+ llm = pipeline("text2text-generation", model="google/flan-t5-base")
20
+
21
+ def chat(q):
22
+ q_emb = embedder.encode([q])
23
+ _, I = index.search(np.array(q_emb), k=2)
24
+ context = " ".join([docs[i] for i in I[0]])
25
+ prompt = f"Context: {context}\nQuestion: {q}\nAnswer:"
26
+ return llm(prompt, max_length=120)[0]["generated_text"]
27
+
28
+ gr.Interface(chat, "text", "text", title="Mobile RAG Bot").launch()