Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,23 @@ import gradio as gr
|
|
| 5 |
import fitz # PyMuPDF
|
| 6 |
import easyocr
|
| 7 |
from PIL import Image
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
# Function to extract text from PDF
|
| 18 |
def extract_text_from_pdf(pdf_path):
|
|
@@ -31,31 +40,66 @@ def extract_text_from_image(image_path):
|
|
| 31 |
|
| 32 |
# Function to generate a response
|
| 33 |
def generate_response(prompt):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
return response
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Chatbot function to handle text, PDF, and image inputs
|
| 40 |
def chatbot(input_type, text_input, pdf_input, image_input):
|
| 41 |
if input_type == "Text":
|
| 42 |
if not text_input:
|
| 43 |
return "Please enter some text."
|
| 44 |
-
|
| 45 |
elif input_type == "PDF":
|
| 46 |
if pdf_input is None:
|
| 47 |
return "Please upload a PDF file."
|
| 48 |
pdf_text = extract_text_from_pdf(pdf_input)
|
| 49 |
-
|
| 50 |
elif input_type == "Image":
|
| 51 |
if image_input is None:
|
| 52 |
return "Please upload an image file."
|
| 53 |
image_text = extract_text_from_image(image_input)
|
| 54 |
-
|
| 55 |
else:
|
| 56 |
return "Invalid input type."
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Generate response using the model
|
|
|
|
| 59 |
response = generate_response(prompt)
|
| 60 |
return response
|
| 61 |
|
|
@@ -72,7 +116,7 @@ interface = gr.Interface(
|
|
| 72 |
fn=chatbot,
|
| 73 |
inputs=input_components,
|
| 74 |
outputs="text",
|
| 75 |
-
title="
|
| 76 |
description="Select the input type (Text, PDF, or Image) and provide your input."
|
| 77 |
)
|
| 78 |
|
|
|
|
| 5 |
import fitz # PyMuPDF
|
| 6 |
import easyocr
|
| 7 |
from PIL import Image
|
| 8 |
+
from sentence_transformers import SentenceTransformer
|
| 9 |
+
from chromadb import Client, Settings
|
| 10 |
|
| 11 |
+
# Load Zephyr 7B (fine-tuned for chat)
|
| 12 |
+
zephyr_tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
|
| 13 |
+
zephyr_model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
"HuggingFaceH4/zephyr-7b-alpha",
|
| 15 |
+
torch_dtype=torch.float16, # Use half-precision for faster inference
|
| 16 |
+
device_map="auto" # Automatically loads the model on GPU if available
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Load a sentence transformer model for embeddings
|
| 20 |
+
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 21 |
|
| 22 |
+
# Initialize Chroma client for RAG
|
| 23 |
+
chroma_client = Client(Settings())
|
| 24 |
+
collection = chroma_client.create_collection(name="knowledge_base")
|
| 25 |
|
| 26 |
# Function to extract text from PDF
|
| 27 |
def extract_text_from_pdf(pdf_path):
|
|
|
|
| 40 |
|
| 41 |
# Function to generate a response
|
| 42 |
def generate_response(prompt):
|
| 43 |
+
# Structure the input prompt for chat
|
| 44 |
+
formatted_prompt = f"<|user|>\n{prompt}\n<|assistant|>\n"
|
| 45 |
+
|
| 46 |
+
# Tokenize the input prompt
|
| 47 |
+
inputs = zephyr_tokenizer(formatted_prompt, return_tensors="pt").to(zephyr_model.device)
|
| 48 |
+
|
| 49 |
+
# Generate the response
|
| 50 |
+
outputs = zephyr_model.generate(**inputs, max_length=200)
|
| 51 |
+
|
| 52 |
+
# Decode the response
|
| 53 |
+
response = zephyr_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
# Extract only the assistant's response
|
| 56 |
+
response = response.split("<|assistant|>")[-1].strip()
|
| 57 |
return response
|
| 58 |
|
| 59 |
+
# Function to add documents to the knowledge base
|
| 60 |
+
def add_to_knowledge_base(text_chunks):
|
| 61 |
+
embeddings = embedding_model.encode(text_chunks)
|
| 62 |
+
for idx, (chunk, embedding) in enumerate(zip(text_chunks, embeddings)):
|
| 63 |
+
collection.add(
|
| 64 |
+
documents=[chunk],
|
| 65 |
+
embeddings=[embedding.tolist()],
|
| 66 |
+
ids=[str(idx)]
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Function to retrieve relevant chunks
|
| 70 |
+
def retrieve_relevant_chunks(query, top_k=3):
|
| 71 |
+
query_embedding = embedding_model.encode(query)
|
| 72 |
+
results = collection.query(
|
| 73 |
+
query_embeddings=[query_embedding.tolist()],
|
| 74 |
+
n_results=top_k
|
| 75 |
+
)
|
| 76 |
+
return results["documents"][0]
|
| 77 |
+
|
| 78 |
# Chatbot function to handle text, PDF, and image inputs
|
| 79 |
def chatbot(input_type, text_input, pdf_input, image_input):
|
| 80 |
if input_type == "Text":
|
| 81 |
if not text_input:
|
| 82 |
return "Please enter some text."
|
| 83 |
+
query = text_input
|
| 84 |
elif input_type == "PDF":
|
| 85 |
if pdf_input is None:
|
| 86 |
return "Please upload a PDF file."
|
| 87 |
pdf_text = extract_text_from_pdf(pdf_input)
|
| 88 |
+
query = f"Extracted text from PDF:\n{pdf_text}\n\nQuestion: {text_input}"
|
| 89 |
elif input_type == "Image":
|
| 90 |
if image_input is None:
|
| 91 |
return "Please upload an image file."
|
| 92 |
image_text = extract_text_from_image(image_input)
|
| 93 |
+
query = f"Extracted text from image:\n{image_text}\n\nQuestion: {text_input}"
|
| 94 |
else:
|
| 95 |
return "Invalid input type."
|
| 96 |
|
| 97 |
+
# Retrieve relevant chunks from the knowledge base
|
| 98 |
+
relevant_chunks = retrieve_relevant_chunks(query)
|
| 99 |
+
context = "\n\n".join(relevant_chunks)
|
| 100 |
+
|
| 101 |
# Generate response using the model
|
| 102 |
+
prompt = f"Context:\n{context}\n\nQuestion: {query}\n\nAnswer:"
|
| 103 |
response = generate_response(prompt)
|
| 104 |
return response
|
| 105 |
|
|
|
|
| 116 |
fn=chatbot,
|
| 117 |
inputs=input_components,
|
| 118 |
outputs="text",
|
| 119 |
+
title="RAG Chatbot with PDF and Image Support",
|
| 120 |
description="Select the input type (Text, PDF, or Image) and provide your input."
|
| 121 |
)
|
| 122 |
|