Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +119 -104
- requirements.txt +0 -5
app.py
CHANGED
|
@@ -1,69 +1,93 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from langchain_community.vectorstores import Chroma
|
| 7 |
-
from langchain_huggingface import HuggingFaceEndpoint
|
| 8 |
-
from langchain.chains import RetrievalQA
|
| 9 |
-
from langchain_core.prompts import PromptTemplate
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
-
# You can set your Hugging Face Token here or as an environment variable
|
| 13 |
-
# os.environ["HUGGINGFACEHUB_API_TOKEN"] = "your_token_here"
|
| 14 |
-
|
| 15 |
KNOWLEDGE_BASE_DIR = "knowledge_base"
|
| 16 |
-
|
| 17 |
|
|
|
|
| 18 |
def load_documents():
|
| 19 |
-
"""Loads
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# simple cleanup for fresh clear start (optional for production but good for dev)
|
| 36 |
-
import shutil
|
| 37 |
-
shutil.rmtree(PERSIST_DIRECTORY)
|
| 38 |
-
except:
|
| 39 |
-
pass
|
| 40 |
-
|
| 41 |
-
vector_store = Chroma.from_documents(texts, embeddings, persist_directory=PERSIST_DIRECTORY)
|
| 42 |
-
return vector_store
|
| 43 |
-
|
| 44 |
-
def setup_rag_chain(vector_store):
|
| 45 |
-
"""Sets up the RAG chain with a retrieval capability."""
|
| 46 |
-
# Using a free endpoint model.
|
| 47 |
-
# 'mistralai/Mistral-7B-Instruct-v0.2' is a good choice, but requires a token.
|
| 48 |
-
# 'google/flan-t5-large' is another option.
|
| 49 |
-
# We'll use a generic reliable one or let the user input their token/model in a real scenario.
|
| 50 |
-
# For the assignment, let's try to use a model that might work with the free tier or a locally downloadable one if needed.
|
| 51 |
-
# However, running local LLM is heavy.
|
| 52 |
-
# Let's assume the user has a token or we use a very small model.
|
| 53 |
-
# If no token is found, this might fail or warn.
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
Context:
|
| 69 |
{context}
|
|
@@ -71,74 +95,65 @@ Context:
|
|
| 71 |
Question: {question}
|
| 72 |
|
| 73 |
Answer:"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
# --- Global Initialization ---
|
| 88 |
print("Loading documents...")
|
| 89 |
-
docs = load_documents()
|
| 90 |
-
print(f"Loaded {len(docs)} documents
|
| 91 |
-
|
| 92 |
-
print("
|
| 93 |
-
|
| 94 |
-
print("Vector store
|
| 95 |
-
|
| 96 |
-
print("
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
except Exception as e:
|
| 101 |
-
print(f"Error setting up RAG chain (likely missing HF Token): {e}")
|
| 102 |
-
rag_chain = None
|
| 103 |
-
|
| 104 |
-
def ask_ai_twin(question):
|
| 105 |
-
if not rag_chain:
|
| 106 |
-
return "Error: RAG Chain not initialized. Please check your Hugging Face Token."
|
| 107 |
-
|
| 108 |
-
result = rag_chain.invoke({"query": question})
|
| 109 |
-
return result["result"]
|
| 110 |
|
| 111 |
# --- Gradio UI ---
|
| 112 |
def load_profile_summary():
|
| 113 |
try:
|
| 114 |
-
with open(os.path.join(KNOWLEDGE_BASE_DIR, "profile.txt"), "r") as f:
|
| 115 |
return f.read()
|
| 116 |
except FileNotFoundError:
|
| 117 |
return "Profile not found."
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
gr.Markdown("Ask me anything about my professional background, skills, and projects!")
|
| 122 |
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column(scale=1):
|
| 125 |
-
gr.Markdown("### Profile Summary")
|
| 126 |
profile_content = load_profile_summary()
|
| 127 |
-
gr.Textbox(value=profile_content, label="About Me", interactive=False, lines=
|
| 128 |
|
| 129 |
with gr.Column(scale=2):
|
| 130 |
-
chatbot = gr.Chatbot(label="Conversation")
|
| 131 |
-
msg = gr.Textbox(label="Ask a question")
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return "", chat_history
|
| 139 |
-
|
| 140 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 141 |
-
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 142 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import chromadb
|
| 5 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
|
|
|
|
|
|
|
|
|
| 8 |
KNOWLEDGE_BASE_DIR = "knowledge_base"
|
| 9 |
+
COLLECTION_NAME = "ai_twin_kb"
|
| 10 |
|
| 11 |
+
# --- Step 1: Load documents from knowledge_base/ ---
|
| 12 |
def load_documents():
|
| 13 |
+
"""Loads all .txt files from the knowledge base directory."""
|
| 14 |
+
documents = []
|
| 15 |
+
filenames = []
|
| 16 |
+
for filename in os.listdir(KNOWLEDGE_BASE_DIR):
|
| 17 |
+
if filename.endswith(".txt"):
|
| 18 |
+
filepath = os.path.join(KNOWLEDGE_BASE_DIR, filename)
|
| 19 |
+
with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
|
| 20 |
+
content = f.read().strip()
|
| 21 |
+
if content:
|
| 22 |
+
documents.append(content)
|
| 23 |
+
filenames.append(filename)
|
| 24 |
+
return documents, filenames
|
| 25 |
+
|
| 26 |
+
# --- Step 2: Chunk documents ---
|
| 27 |
+
def chunk_text(text, chunk_size=500, overlap=100):
|
| 28 |
+
"""Splits text into overlapping chunks."""
|
| 29 |
+
chunks = []
|
| 30 |
+
start = 0
|
| 31 |
+
while start < len(text):
|
| 32 |
+
end = start + chunk_size
|
| 33 |
+
chunks.append(text[start:end])
|
| 34 |
+
start += chunk_size - overlap
|
| 35 |
+
return chunks
|
| 36 |
+
|
| 37 |
+
# --- Step 3: Build vector store ---
|
| 38 |
+
def build_vector_store(documents, filenames):
|
| 39 |
+
"""Creates embeddings and stores them in ChromaDB."""
|
| 40 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 41 |
+
|
| 42 |
+
client = chromadb.Client()
|
| 43 |
+
# Delete existing collection if it exists
|
| 44 |
+
try:
|
| 45 |
+
client.delete_collection(COLLECTION_NAME)
|
| 46 |
+
except:
|
| 47 |
+
pass
|
| 48 |
+
collection = client.create_collection(name=COLLECTION_NAME)
|
| 49 |
|
| 50 |
+
all_chunks = []
|
| 51 |
+
all_ids = []
|
| 52 |
+
all_metadata = []
|
| 53 |
+
chunk_id = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
for doc, fname in zip(documents, filenames):
|
| 56 |
+
chunks = chunk_text(doc)
|
| 57 |
+
for chunk in chunks:
|
| 58 |
+
all_chunks.append(chunk)
|
| 59 |
+
all_ids.append(f"chunk_{chunk_id}")
|
| 60 |
+
all_metadata.append({"source": fname})
|
| 61 |
+
chunk_id += 1
|
| 62 |
+
|
| 63 |
+
# Generate embeddings
|
| 64 |
+
embeddings = model.encode(all_chunks).tolist()
|
| 65 |
+
|
| 66 |
+
# Add to ChromaDB
|
| 67 |
+
collection.add(
|
| 68 |
+
documents=all_chunks,
|
| 69 |
+
embeddings=embeddings,
|
| 70 |
+
ids=all_ids,
|
| 71 |
+
metadatas=all_metadata
|
| 72 |
)
|
| 73 |
|
| 74 |
+
return collection, model
|
| 75 |
+
|
| 76 |
+
# --- Step 4: RAG query function ---
|
| 77 |
+
def query_rag(question, collection, embed_model, llm_client):
|
| 78 |
+
"""Retrieves relevant chunks and generates an answer."""
|
| 79 |
+
# Embed the question
|
| 80 |
+
q_embedding = embed_model.encode([question]).tolist()
|
| 81 |
+
|
| 82 |
+
# Retrieve top 3 relevant chunks
|
| 83 |
+
results = collection.query(query_embeddings=q_embedding, n_results=3)
|
| 84 |
+
|
| 85 |
+
# Build context from retrieved documents
|
| 86 |
+
context = "\n\n".join(results["documents"][0])
|
| 87 |
|
| 88 |
+
# Create prompt
|
| 89 |
+
prompt = f"""You are an AI Twin that represents a person. Use ONLY the following context to answer the question.
|
| 90 |
+
If you don't know the answer from the context, say "I don't have that information in my profile."
|
| 91 |
|
| 92 |
Context:
|
| 93 |
{context}
|
|
|
|
| 95 |
Question: {question}
|
| 96 |
|
| 97 |
Answer:"""
|
| 98 |
+
|
| 99 |
+
# Generate response using Hugging Face Inference API
|
| 100 |
+
try:
|
| 101 |
+
response = llm_client.text_generation(
|
| 102 |
+
prompt,
|
| 103 |
+
max_new_tokens=512,
|
| 104 |
+
temperature=0.3,
|
| 105 |
+
repetition_penalty=1.1
|
| 106 |
+
)
|
| 107 |
+
return response.strip()
|
| 108 |
+
except Exception as e:
|
| 109 |
+
return f"Error generating response: {str(e)}"
|
| 110 |
|
| 111 |
# --- Global Initialization ---
|
| 112 |
print("Loading documents...")
|
| 113 |
+
docs, fnames = load_documents()
|
| 114 |
+
print(f"Loaded {len(docs)} documents: {fnames}")
|
| 115 |
+
|
| 116 |
+
print("Building vector store...")
|
| 117 |
+
kb_collection, embedding_model = build_vector_store(docs, fnames)
|
| 118 |
+
print("Vector store ready.")
|
| 119 |
+
|
| 120 |
+
print("Initializing LLM client...")
|
| 121 |
+
hf_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN", None)
|
| 122 |
+
llm = InferenceClient(model="mistralai/Mistral-7B-Instruct-v0.2", token=hf_token)
|
| 123 |
+
print("LLM client ready.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# --- Gradio UI ---
|
| 126 |
def load_profile_summary():
|
| 127 |
try:
|
| 128 |
+
with open(os.path.join(KNOWLEDGE_BASE_DIR, "profile.txt"), "r", encoding="utf-8") as f:
|
| 129 |
return f.read()
|
| 130 |
except FileNotFoundError:
|
| 131 |
return "Profile not found."
|
| 132 |
|
| 133 |
+
def ask_ai_twin(message, chat_history):
|
| 134 |
+
answer = query_rag(message, kb_collection, embedding_model, llm)
|
| 135 |
+
chat_history.append((message, answer))
|
| 136 |
+
return "", chat_history
|
| 137 |
+
|
| 138 |
+
with gr.Blocks(title="My AI Twin", theme=gr.themes.Soft()) as demo:
|
| 139 |
+
gr.Markdown("# 🤖 My AI Twin")
|
| 140 |
gr.Markdown("Ask me anything about my professional background, skills, and projects!")
|
| 141 |
|
| 142 |
with gr.Row():
|
| 143 |
with gr.Column(scale=1):
|
| 144 |
+
gr.Markdown("### 📋 Profile Summary")
|
| 145 |
profile_content = load_profile_summary()
|
| 146 |
+
gr.Textbox(value=profile_content, label="About Me", interactive=False, lines=15)
|
| 147 |
|
| 148 |
with gr.Column(scale=2):
|
| 149 |
+
chatbot = gr.Chatbot(label="Conversation", height=400)
|
| 150 |
+
msg = gr.Textbox(label="Ask a question", placeholder="e.g. What are my skills?")
|
| 151 |
+
with gr.Row():
|
| 152 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 153 |
+
clear = gr.Button("Clear")
|
| 154 |
+
|
| 155 |
+
msg.submit(ask_ai_twin, [msg, chatbot], [msg, chatbot])
|
| 156 |
+
submit_btn.click(ask_ai_twin, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
langchain
|
| 2 |
-
langchain-community
|
| 3 |
-
langchain-huggingface
|
| 4 |
-
langchain-text-splitters
|
| 5 |
-
langchain-core
|
| 6 |
chromadb
|
| 7 |
sentence-transformers
|
| 8 |
gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
chromadb
|
| 2 |
sentence-transformers
|
| 3 |
gradio
|