Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 2 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 3 |
+
from langchain.vectorstores import FAISS
|
| 4 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 5 |
+
from langchain.llms import HuggingFacePipeline
|
| 6 |
+
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
import pandas as pd
|
| 8 |
+
df = pd.read_csv('NLP.csv')
|
| 9 |
+
corpus = df['text']
|
| 10 |
+
#Chunking
|
| 11 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=200,chunk_overlap = 10)
|
| 12 |
+
texts = sum([splitter.split_text(doc) for doc in corpus], [])
|
| 13 |
+
# Embeddings
|
| 14 |
+
embeddings = HuggingFaceEmbeddings(model_name='all-MiniLM-L6-v2')
|
| 15 |
+
# Indexing
|
| 16 |
+
db = FAISS.from_texts(texts[:300],embeddings)
|
| 17 |
+
retriever = db.as_retriever(search_kwargs={'k':2})
|
| 18 |
+
# Model
|
| 19 |
+
llm = HuggingFacePipeline.from_model_id(model_id='google/flan-t5-large',task='text2text-generation')
|
| 20 |
+
# Memory
|
| 21 |
+
memory = ConversationBufferMemory(memory_key='chat_history',return_messages=True)
|
| 22 |
+
# Combine previous steps
|
| 23 |
+
qa = ConversationalRetrievalChain.from_llm(llm=llm,retriever=retriever,memory=memory)
|
| 24 |
+
def ans_ques(ques):
|
| 25 |
+
result = qa({'question':ques})
|
| 26 |
+
return result['answer']
|
| 27 |
+
import gradio as gr
|
| 28 |
+
|
| 29 |
+
demo = gr.Interface(ans_ques,inputs='text',outputs='text')
|
| 30 |
+
demo.launch()
|