Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,72 +5,82 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
-
from
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
HF_REPO_ID = "Shami96/7solar-documentation"
|
| 12 |
-
HF_PDF_NAME = "7solar_documentation.pdf"
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
def create_vector_db():
|
| 27 |
-
docs = load_pdf_from_hf()
|
| 28 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 29 |
-
chunk_size=
|
| 30 |
-
chunk_overlap=
|
| 31 |
)
|
| 32 |
-
chunks = text_splitter.split_documents(
|
| 33 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 34 |
return Chroma.from_documents(chunks, embeddings)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 9 |
|
| 10 |
+
# Configuration
|
| 11 |
+
HF_REPO_ID = "Shami96/7solar-documentation"
|
| 12 |
+
HF_PDF_NAME = "7solar_documentation.pdf"
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
|
| 15 |
+
# Initialize components
|
| 16 |
+
def initialize_components():
|
| 17 |
+
print("⚙️ Initializing components...")
|
| 18 |
+
|
| 19 |
+
# Load PDF
|
| 20 |
+
try:
|
| 21 |
+
pdf_path = hf_hub_download(
|
| 22 |
+
repo_id=HF_REPO_ID,
|
| 23 |
+
filename=HF_PDF_NAME,
|
| 24 |
+
repo_type="dataset",
|
| 25 |
+
token=HF_TOKEN
|
| 26 |
+
)
|
| 27 |
+
loader = PyPDFLoader(pdf_path)
|
| 28 |
+
documents = loader.load()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
raise RuntimeError(f"Failed to load PDF: {str(e)}")
|
| 31 |
|
| 32 |
+
# Create vector store
|
|
|
|
|
|
|
| 33 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 34 |
+
chunk_size=1000,
|
| 35 |
+
chunk_overlap=200
|
| 36 |
)
|
| 37 |
+
chunks = text_splitter.split_documents(documents)
|
| 38 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 39 |
return Chroma.from_documents(chunks, embeddings)
|
| 40 |
|
| 41 |
+
# Chat function
|
| 42 |
+
def respond(message, history):
|
| 43 |
+
try:
|
| 44 |
+
# Initialize if not already done
|
| 45 |
+
if 'vector_db' not in globals():
|
| 46 |
+
global vector_db
|
| 47 |
+
vector_db = initialize_components()
|
| 48 |
+
|
| 49 |
+
# Handle greetings
|
| 50 |
+
if message.lower() in ["hi", "hello", "hey"]:
|
| 51 |
+
return "Hello! I'm your 7Solar assistant. How can I help you today?"
|
| 52 |
+
|
| 53 |
+
# Search documents
|
| 54 |
+
docs = vector_db.similarity_search(message, k=3)
|
| 55 |
+
if not docs:
|
| 56 |
+
return "I couldn't find relevant information. Please try another question about 7Solar."
|
| 57 |
+
|
| 58 |
+
# Generate response
|
| 59 |
+
llm = ChatGroq(
|
| 60 |
+
model_name="llama3-70b-8192",
|
| 61 |
+
temperature=0.3
|
| 62 |
+
)
|
| 63 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 64 |
+
response = llm.invoke(
|
| 65 |
+
f"Using only this context:\n{context}\n\nQuestion: {message}\nAnswer:"
|
| 66 |
+
)
|
| 67 |
+
return response.content
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"An error occurred: {str(e)}"
|
| 70 |
|
| 71 |
+
# Create Gradio interface
|
| 72 |
+
demo = gr.ChatInterface(
|
| 73 |
+
fn=respond,
|
| 74 |
+
title="☀️ 7Solar Assistant",
|
| 75 |
+
description="Ask me anything about 7Solar's services and documentation",
|
| 76 |
+
examples=["What solar packages do you offer?", "How does the registration process work?"],
|
| 77 |
+
cache_examples=False
|
| 78 |
+
)
|
| 79 |
|
| 80 |
+
# Launch with error handling
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
try:
|
| 83 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Failed to launch: {str(e)}")
|
| 86 |
+
raise
|