Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,84 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
|
| 9 |
+
# Folder to store the merged vector index
|
| 10 |
+
INDEX_DIR = "rag_multi_pdf_index"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Load documents from multiple PDFs and build a knowledge base
|
| 13 |
+
def create_knowledge_base(pdf_files):
|
| 14 |
+
all_chunks = []
|
| 15 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
| 16 |
|
| 17 |
+
for pdf_file in pdf_files:
|
| 18 |
+
loader = PyPDFLoader(pdf_file.name)
|
| 19 |
+
docs = loader.load()
|
| 20 |
+
chunks = splitter.split_documents(docs)
|
| 21 |
+
all_chunks.extend(chunks)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Create embeddings and save vector index
|
| 24 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 25 |
+
vectorstore = FAISS.from_documents(all_chunks, embeddings)
|
| 26 |
+
vectorstore.save_local(INDEX_DIR)
|
|
|
|
| 27 |
|
| 28 |
+
return f"β
Knowledge base created from {len(pdf_files)} PDFs and saved."
|
| 29 |
|
| 30 |
+
# Load existing vectorstore
|
| 31 |
+
def load_vectorstore():
|
| 32 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 33 |
+
return FAISS.load_local(INDEX_DIR, embeddings)
|
| 34 |
|
| 35 |
+
# Ask a question using Gemini + FAISS
|
| 36 |
+
def chat_with_rag(api_key, question):
|
| 37 |
+
if not api_key or not api_key.startswith("AI"):
|
| 38 |
+
return "β Please provide a valid Gemini API Key starting with 'AI'."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Set up Gemini
|
| 41 |
+
try:
|
| 42 |
+
genai.configure(api_key=api_key)
|
| 43 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"β Gemini API setup failed: {str(e)}"
|
| 46 |
|
| 47 |
+
try:
|
| 48 |
+
vs = load_vectorstore()
|
| 49 |
+
top_docs = vs.similarity_search(question, k=3)
|
| 50 |
+
context = "\n\n".join([doc.page_content for doc in top_docs])
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"β Error retrieving from vector store: {str(e)}"
|
| 53 |
|
| 54 |
+
prompt = f"""Use the following context to answer the question:\n\n{context}\n\nQuestion: {question}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
try:
|
| 57 |
+
response = model.generate_content(prompt)
|
| 58 |
+
return response.text
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"β Gemini Error: {str(e)}"
|
| 61 |
+
|
| 62 |
+
# Gradio UI
|
| 63 |
+
with gr.Blocks(title="π Multi-PDF RAG Chat with Gemini") as demo:
|
| 64 |
+
gr.Markdown("## π§ Upload multiple PDFs and ask questions using Gemini")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
api_key = gr.Textbox(label="π Gemini API Key", placeholder="Paste your Gemini API Key here", type="password")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
pdfs = gr.File(label="π Upload PDFs", file_types=[".pdf"], file_count="multiple")
|
| 71 |
+
create_btn = gr.Button("π Create Knowledge Base")
|
| 72 |
+
kb_status = gr.Textbox(label="Knowledge Base Status", interactive=False)
|
| 73 |
+
|
| 74 |
+
create_btn.click(fn=create_knowledge_base, inputs=[pdfs], outputs=[kb_status])
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
question = gr.Textbox(label="β Your Question")
|
| 78 |
+
answer = gr.Textbox(label="π¬ Gemini Answer", lines=10)
|
| 79 |
+
ask_btn = gr.Button("π Ask")
|
| 80 |
+
|
| 81 |
+
ask_btn.click(fn=chat_with_rag, inputs=[api_key, question], outputs=[answer])
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch()
|