Spaces:
Runtime error
Runtime error
File size: 1,718 Bytes
690f23e afe4d44 c0486d0 0042971 690f23e c1be932 690f23e c1be932 6f3c118 690f23e c1be932 690f23e c1be932 0b1afc6 690f23e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
import os
OPENAI_API_KEY="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"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
pdfreader = PdfReader('csp.pdf')
from typing_extensions import Concatenate
# read text from pdf
raw_text = ''
for i, page in enumerate(pdfreader.pages):
content = page.extract_text()
if content:
raw_text += content
raw_text
text_splitter = CharacterTextSplitter(
separator = "\n",
chunk_size = 800,
chunk_overlap = 200,
length_function = len,
)
texts = text_splitter.split_text(raw_text)
len(texts)
embeddings = OpenAIEmbeddings()
document_search = FAISS.from_texts(texts, embeddings)
document_search
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
chain = load_qa_chain(OpenAI(), chain_type="stuff")
import gradio as gr
# Define function to process user query
def process_query(query):
# Perform similarity search to retrieve relevant documents
docs = document_search.similarity_search(query)
# Run question-answering process with retrieved documents and user query
response = chain.run(input_documents=docs, question=query)
# Return the response
return response
# Define Gradio interface
iface = gr.Interface(
fn=process_query,
inputs="text",
outputs="text",
title="Woxsenite",
description="Hi! How can I help you?"
)
# Launch the interface
iface.launch() |