Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
+
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 10 |
+
from langchain import HuggingFaceHub
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
reader = PdfReader("Faculty of Computer Science and Information Technology (1).pdf")
|
| 16 |
+
# read data from the file and put them into a variable called raw_text
|
| 17 |
+
raw_text = ''
|
| 18 |
+
for i, page in enumerate(reader.pages):
|
| 19 |
+
text = page.extract_text()
|
| 20 |
+
if text:
|
| 21 |
+
raw_text += text
|
| 22 |
+
text_splitter = CharacterTextSplitter(
|
| 23 |
+
separator = "\n",
|
| 24 |
+
chunk_size = 1000,
|
| 25 |
+
chunk_overlap = 200,
|
| 26 |
+
length_function = len,
|
| 27 |
+
)
|
| 28 |
+
texts = text_splitter.split_text(raw_text)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def main(prompt):
|
| 32 |
+
embeddings = HuggingFaceEmbeddings()
|
| 33 |
+
db = FAISS.from_texts(texts, embeddings)
|
| 34 |
+
llm=HuggingFaceHub(repo_id="google/flan-t5-xxl",model_kwargs={"temperature":1, "max_length":512})
|
| 35 |
+
chain=load_qa_chain(llm,chain_type="stuff")
|
| 36 |
+
query =prompt
|
| 37 |
+
docs = db.similarity_search(query)
|
| 38 |
+
return chain.run(input_documents=docs, question=query)
|
| 39 |
+
|
| 40 |
+
interface=gr.Interface(fn=main,inputs=[gr.components.Textbox(label="Type your Question")],
|
| 41 |
+
outputs=gr.components.Textbox(label="Answer.."),
|
| 42 |
+
)
|
| 43 |
+
interface.launch(debug=True)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|