Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,115 +1,96 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
-
|
4 |
-
|
5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
-
from langchain_community.document_loaders import TextLoader, PyPDFLoader
|
7 |
-
from langchain.chains import RetrievalQA
|
8 |
-
from langchain_community.llms import HuggingFaceHub
|
9 |
-
import tempfile
|
10 |
-
import shutil
|
11 |
-
from langchain.prompts import PromptTemplate
|
12 |
-
|
13 |
-
# Define a proper prompt template
|
14 |
-
prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
15 |
-
{context}
|
16 |
-
Question: {question}
|
17 |
-
Answer:"""
|
18 |
-
PROMPT = PromptTemplate(
|
19 |
-
template=prompt_template, input_variables=["context", "question"]
|
20 |
-
)
|
21 |
-
|
22 |
# Load environment variables
|
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 |
-
if file is None:
|
49 |
-
return "No file uploaded."
|
50 |
-
try:
|
51 |
-
# Create a temporary directory
|
52 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
53 |
-
# Create a path for the temporary file
|
54 |
-
temp_file_path = os.path.join(temp_dir, os.path.basename(file.name))
|
55 |
-
# Save the uploaded file to the temporary path
|
56 |
-
with open(temp_file_path, 'wb') as temp_file:
|
57 |
-
temp_file.write(file.read())
|
58 |
-
# Determine file type and load accordingly
|
59 |
-
if file.name.endswith('.pdf'):
|
60 |
-
loader = PyPDFLoader(temp_file_path)
|
61 |
-
else:
|
62 |
-
loader = TextLoader(temp_file_path)
|
63 |
-
|
64 |
-
documents = loader.load()
|
65 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
66 |
-
texts = text_splitter.split_documents(documents)
|
67 |
-
|
68 |
-
# Update the vector store with new documents
|
69 |
-
vectorstore.add_documents(texts)
|
70 |
-
vectorstore.persist()
|
71 |
-
|
72 |
-
return f"File processed and added to the knowledge base. {len(texts)} chunks created."
|
73 |
-
except Exception as e:
|
74 |
-
return f"An error occurred while processing the file: {str(e)}"
|
75 |
-
|
76 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
77 |
-
full_prompt = f"{system_message}\n\nHuman: {message}"
|
78 |
-
# Use the RetrievalQA chain to get the answer
|
79 |
-
result = qa_chain({"query": full_prompt})
|
80 |
-
answer = result['result']
|
81 |
-
# Return only the answer
|
82 |
-
yield answer
|
83 |
-
|
84 |
-
with gr.Blocks() as demo:
|
85 |
-
gr.Markdown("# RAG Chatbot with Content Upload")
|
86 |
-
with gr.Row():
|
87 |
-
with gr.Column(scale=3):
|
88 |
-
chatbot = gr.Chatbot()
|
89 |
-
msg = gr.Textbox()
|
90 |
-
clear = gr.Button("Clear")
|
91 |
-
with gr.Column(scale=1):
|
92 |
-
file_upload = gr.File(label="Upload Content for RAG (TXT or PDF)")
|
93 |
-
upload_button = gr.Button("Process Uploaded File")
|
94 |
-
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
|
95 |
-
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
96 |
-
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
97 |
-
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
98 |
-
|
99 |
-
def user(user_message, history):
|
100 |
-
return "", history + [[user_message, None]]
|
101 |
-
|
102 |
-
def bot(history, system_message, max_tokens, temperature, top_p):
|
103 |
-
user_message = history[-1][0]
|
104 |
-
bot_message = next(respond(user_message, history[:-1], system_message, max_tokens, temperature, top_p))
|
105 |
-
history[-1][1] = bot_message
|
106 |
-
return history
|
107 |
-
|
108 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
109 |
-
bot, [chatbot, system_message, max_tokens, temperature, top_p], chatbot
|
110 |
-
)
|
111 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
112 |
-
upload_button.click(process_uploaded_file, inputs=[file_upload], outputs=[gr.Textbox()])
|
113 |
-
|
114 |
-
if __name__ == "__main__":
|
115 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate
|
3 |
+
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
6 |
+
from llama_index.core import Settings
|
7 |
import os
|
8 |
+
import base64
|
9 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# Configure the Llama index settings
|
14 |
+
Settings.llm = HuggingFaceInferenceAPI(
|
15 |
+
model_name="nltpt/Llama-3.2-3B-Instruct",
|
16 |
+
tokenizer_name="nltpt/Llama-3.2-3B-Instruct",
|
17 |
+
context_window=3000,
|
18 |
+
token=os.getenv("HF_TOKEN"),
|
19 |
+
max_new_tokens=512,
|
20 |
+
generate_kwargs={"temperature": 0.1},
|
21 |
+
)
|
22 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
23 |
+
model_name="BAAI/bge-small-en-v1.5"
|
24 |
)
|
25 |
+
|
26 |
+
# Define the directory for persistent storage and data
|
27 |
+
PERSIST_DIR = "./db"
|
28 |
+
DATA_DIR = "data"
|
29 |
+
|
30 |
+
# Ensure data directory exists
|
31 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
32 |
+
os.makedirs(PERSIST_DIR, exist_ok=True)
|
33 |
+
|
34 |
+
def displayPDF(file):
|
35 |
+
with open(file, "rb") as f:
|
36 |
+
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
|
37 |
+
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
|
38 |
+
return pdf_display
|
39 |
+
|
40 |
+
def data_ingestion(files):
|
41 |
+
for file in files:
|
42 |
+
filepath = os.path.join(DATA_DIR, file.name)
|
43 |
+
with open(filepath, "wb") as f:
|
44 |
+
f.write(file.getbuffer())
|
45 |
+
documents = SimpleDirectoryReader(DATA_DIR).load_data()
|
46 |
+
storage_context = StorageContext.from_defaults()
|
47 |
+
index = VectorStoreIndex.from_documents(documents)
|
48 |
+
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
49 |
+
|
50 |
+
def handle_query(query):
|
51 |
+
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
52 |
+
index = load_index_from_storage(storage_context)
|
53 |
+
chat_text_qa_msgs = [
|
54 |
+
(
|
55 |
+
"user",
|
56 |
+
"""You are a Q&A assistant. Your main goal is to provide answers as accurately as possible, based on the context of the document provided. If the question does not match the context or is outside the scope of the document, advise the user to ask questions that are relevant to the document.
|
57 |
+
Context:
|
58 |
+
{context_str}
|
59 |
+
Question:
|
60 |
+
{query_str}
|
61 |
+
"""
|
62 |
+
)
|
63 |
+
]
|
64 |
+
|
65 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
66 |
+
|
67 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template)
|
68 |
+
answer = query_engine.query(query)
|
69 |
+
|
70 |
+
if hasattr(answer, 'response'):
|
71 |
+
return answer.response
|
72 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
73 |
+
return answer['response']
|
74 |
+
else:
|
75 |
+
return "Sorry, I couldn't find an answer."
|
76 |
+
|
77 |
+
# Gradio app setup
|
78 |
+
def gradio_app(files, user_query):
|
79 |
+
if files:
|
80 |
+
data_ingestion(files) # Process PDFs after they are uploaded
|
81 |
+
response = handle_query(user_query)
|
82 |
+
return response
|
83 |
+
return "Please upload at least one PDF file."
|
84 |
+
|
85 |
+
interface = gr.Interface(
|
86 |
+
fn=gradio_app,
|
87 |
+
inputs=[
|
88 |
+
gr.File(label="Upload PDF Files", type="file", file_count="multiple"),
|
89 |
+
gr.Textbox(label="Ask me anything about the content of the PDF(s):")
|
90 |
+
],
|
91 |
+
outputs="text",
|
92 |
+
title="(PDF) Information and Inference🗞️",
|
93 |
+
description="Retrieval-Augmented Generation. Start chat ...🚀"
|
94 |
)
|
95 |
+
|
96 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|