Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from langchain.chains import RetrievalQA
|
| 4 |
from langchain_community.vectorstores import Chroma
|
| 5 |
-
from langchain_community.
|
| 6 |
-
from langchain_huggingface import
|
| 7 |
-
from
|
| 8 |
-
import time
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
os.system("pip install -U huggingface_hub langchain_huggingface langchain_core langchain")
|
| 12 |
|
| 13 |
# Define paths for cybersecurity training PDFs
|
| 14 |
PDF_FILES = [
|
|
@@ -20,7 +20,7 @@ PDF_FILES = [
|
|
| 20 |
|
| 21 |
# Fetch Hugging Face API token securely from environment variables
|
| 22 |
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 23 |
-
if
|
| 24 |
raise ValueError("β Hugging Face API token is missing! Set it in Hugging Face Spaces Secrets.")
|
| 25 |
|
| 26 |
# Load PDFs into ChromaDB
|
|
@@ -28,37 +28,37 @@ def load_data():
|
|
| 28 |
"""Loads multiple PDFs and stores embeddings in ChromaDB"""
|
| 29 |
all_docs = []
|
| 30 |
for pdf in PDF_FILES:
|
| 31 |
-
if os.path.exists(pdf): # Ensure the PDF exists
|
| 32 |
loader = PyPDFLoader(pdf)
|
| 33 |
all_docs.extend(loader.load())
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
return Chroma.from_documents(all_docs, embeddings)
|
| 38 |
|
| 39 |
# Load the knowledge base
|
| 40 |
vector_db = load_data()
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
max_new_tokens=250, # β
Ensure API limit is followed
|
| 47 |
-
huggingfacehub_api_token=HUGGINGFACE_API_KEY
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Create Retrieval QA chain
|
| 51 |
-
qa_chain = RetrievalQA.from_chain_type(llm=
|
| 52 |
|
| 53 |
# Function to simulate futuristic typing effect
|
| 54 |
def chatbot_response(question):
|
| 55 |
"""Handles chatbot queries with a typing effect"""
|
| 56 |
response = qa_chain.invoke(question) # β
Use `invoke` instead of deprecated `run`
|
| 57 |
-
|
| 58 |
-
# Ensure response is a string to avoid errors
|
| 59 |
-
if not isinstance(response, str):
|
| 60 |
-
response = str(response)
|
| 61 |
-
|
| 62 |
displayed_response = ""
|
| 63 |
for char in response:
|
| 64 |
displayed_response += char
|
|
@@ -86,5 +86,5 @@ iface = gr.Interface(
|
|
| 86 |
live=True, # Enables real-time updates for typing effect
|
| 87 |
)
|
| 88 |
|
| 89 |
-
# Launch chatbot
|
| 90 |
-
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import time
|
| 4 |
from langchain.chains import RetrievalQA
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 7 |
+
from langchain_huggingface import HuggingFaceEmbeddings # β
Fixed Import
|
| 8 |
+
from huggingface_hub import InferenceClient # β
New method for querying Hugging Face LLM
|
|
|
|
| 9 |
|
| 10 |
+
# Install required dependencies (ensure latest versions)
|
| 11 |
+
os.system("pip install -U huggingface_hub langchain_huggingface langchain_core langchain gradio")
|
| 12 |
|
| 13 |
# Define paths for cybersecurity training PDFs
|
| 14 |
PDF_FILES = [
|
|
|
|
| 20 |
|
| 21 |
# Fetch Hugging Face API token securely from environment variables
|
| 22 |
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 23 |
+
if HUGGINGFACE_API_KEY is None:
|
| 24 |
raise ValueError("β Hugging Face API token is missing! Set it in Hugging Face Spaces Secrets.")
|
| 25 |
|
| 26 |
# Load PDFs into ChromaDB
|
|
|
|
| 28 |
"""Loads multiple PDFs and stores embeddings in ChromaDB"""
|
| 29 |
all_docs = []
|
| 30 |
for pdf in PDF_FILES:
|
| 31 |
+
if os.path.exists(pdf): # Ensure the PDF exists
|
| 32 |
loader = PyPDFLoader(pdf)
|
| 33 |
all_docs.extend(loader.load())
|
| 34 |
|
| 35 |
+
# Use updated embedding model
|
| 36 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 37 |
|
| 38 |
return Chroma.from_documents(all_docs, embeddings)
|
| 39 |
|
| 40 |
# Load the knowledge base
|
| 41 |
vector_db = load_data()
|
| 42 |
|
| 43 |
+
# Initialize Hugging Face Inference Client (new recommended method)
|
| 44 |
+
client = InferenceClient(
|
| 45 |
+
"https://api-inference.huggingface.co/models/google/flan-t5-large",
|
| 46 |
+
token=HUGGINGFACE_API_KEY
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Function to interact with the Hugging Face model
|
| 50 |
+
def query_llm(prompt):
|
| 51 |
+
"""Send query to Hugging Face API and return response"""
|
| 52 |
+
response = client.chat_completion(messages=[{"role": "user", "content": prompt}])
|
| 53 |
+
return response["choices"][0]["message"]["content"]
|
| 54 |
+
|
| 55 |
# Create Retrieval QA chain
|
| 56 |
+
qa_chain = RetrievalQA.from_chain_type(llm=query_llm, retriever=vector_db.as_retriever())
|
| 57 |
|
| 58 |
# Function to simulate futuristic typing effect
|
| 59 |
def chatbot_response(question):
|
| 60 |
"""Handles chatbot queries with a typing effect"""
|
| 61 |
response = qa_chain.invoke(question) # β
Use `invoke` instead of deprecated `run`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
displayed_response = ""
|
| 63 |
for char in response:
|
| 64 |
displayed_response += char
|
|
|
|
| 86 |
live=True, # Enables real-time updates for typing effect
|
| 87 |
)
|
| 88 |
|
| 89 |
+
# Launch chatbot with public link
|
| 90 |
+
iface.launch(share=True) # β
Now launches with a public link
|