Spaces:
No application file
No application file
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,47 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import PyPDF2
|
| 3 |
-
import os
|
| 4 |
-
from langchain.embeddings import CohereEmbeddings
|
| 5 |
-
from langchain.vectorstores.faiss import FAISS
|
| 6 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
-
from langchain.llms.fireworks import Fireworks
|
| 8 |
-
from langchain.chains import VectorDBQA
|
| 9 |
-
|
| 10 |
-
FIREWORKS_API_KEY = os.getenv("FIREWORKS_API_KEY")
|
| 11 |
-
|
| 12 |
-
def pdf_to_text(pdf_file, query):
|
| 13 |
-
# Open the PDF file in binary mode
|
| 14 |
-
with open(pdf_file.name, 'rb') as pdf_file:
|
| 15 |
-
# Create a PDF reader object
|
| 16 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 17 |
-
|
| 18 |
-
# Create an empty string to store the text
|
| 19 |
-
text = ""
|
| 20 |
-
|
| 21 |
-
# Loop through each page of the PDF
|
| 22 |
-
for page_num in range(len(pdf_reader.pages)):
|
| 23 |
-
# Get the page object
|
| 24 |
-
page = pdf_reader.pages[page_num]
|
| 25 |
-
# Extract the texst from the page and add it to the text variable
|
| 26 |
-
text += page.extract_text()
|
| 27 |
-
#embedding step
|
| 28 |
-
llm = Fireworks(model="accounts/fireworks/models/llama-v2-13b-chat", model_kwargs={"temperature": 0, "max_tokens": 500, "top_p": 1.0})
|
| 29 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
| 30 |
-
texts = text_splitter.split_text(text)
|
| 31 |
-
|
| 32 |
-
embeddings = CohereEmbeddings(cohere_api_key="Ev0v9wwQPa90xDucdHTyFsllXGVHXouakUMObkNb")
|
| 33 |
-
#vector store
|
| 34 |
-
vectorstore = FAISS.from_texts(texts, embeddings)
|
| 35 |
-
|
| 36 |
-
#inference
|
| 37 |
-
qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=vectorstore)
|
| 38 |
-
return qa.run(query)
|
| 39 |
-
|
| 40 |
-
# Define the Gradio interface
|
| 41 |
-
pdf_input = gr.inputs.File(label="PDF File")
|
| 42 |
-
query_input = gr.inputs.Textbox(label="Query")
|
| 43 |
-
outputs = gr.outputs.Textbox(label="Chatbot Response")
|
| 44 |
-
interface = gr.Interface(fn=pdf_to_text, inputs=[pdf_input, query_input], outputs=outputs)
|
| 45 |
-
|
| 46 |
-
# Run the interface
|
| 47 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|