Spaces:
Sleeping
Sleeping
Updated Libraries
Browse files
app.py
CHANGED
|
@@ -1,126 +1,294 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
from google import genai
|
|
|
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_community.document_loaders import TextLoader
|
| 7 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# 1. Setup Gemini
|
| 11 |
-
# -------------------------------
|
| 12 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
-
gemini_model = genai.GenerativeModel("gemini-2.5-flash")
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
# 2. Ensure about_me.txt exists
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
loader = TextLoader(
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
print(f"Error loading
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# -------------------------------
|
| 37 |
-
# 4. Split documents
|
| 38 |
-
# -------------------------------
|
| 39 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 40 |
chunk_size=500,
|
| 41 |
chunk_overlap=50
|
| 42 |
)
|
| 43 |
-
split_docs = text_splitter.split_documents(docs)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
embedding_model = HuggingFaceEmbeddings(
|
| 50 |
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 51 |
-
model_kwargs={
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
print("Creating vector database...")
|
| 55 |
-
db = FAISS.from_documents(split_docs, embedding_model)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
#
|
| 59 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
-
if not question.strip():
|
| 63 |
-
return "Please ask me a question about the portfolio!"
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
if not
|
| 70 |
-
return
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
# Combine retrieved docs into context
|
| 73 |
-
context = "\n".join([doc.page_content for doc in context_docs])
|
| 74 |
|
| 75 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
prompt = f"""
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
Question: {question}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
response = gemini_model.generate_content(prompt)
|
| 90 |
-
return response.text.strip()
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
-
return f"Sorry, I encountered an error: {str(e)[:200]}"
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
inputs=gr.Textbox(
|
| 101 |
label="Ask me about the portfolio",
|
| 102 |
placeholder="What would you like to know?",
|
| 103 |
-
|
| 104 |
),
|
|
|
|
| 105 |
outputs=gr.Textbox(
|
| 106 |
label="Response",
|
| 107 |
-
|
| 108 |
),
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
examples=[
|
| 112 |
"What are your technical skills?",
|
| 113 |
-
"Tell me about your projects",
|
| 114 |
-
"What is your background?"
|
|
|
|
|
|
|
| 115 |
],
|
| 116 |
-
|
| 117 |
-
|
| 118 |
)
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
if __name__ == "__main__":
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
server_name="0.0.0.0",
|
| 124 |
-
server_port=7860,
|
| 125 |
show_error=True
|
| 126 |
)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
from google import genai
|
| 5 |
+
|
| 6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
from langchain_community.vectorstores import FAISS
|
| 8 |
from langchain_community.document_loaders import TextLoader
|
| 9 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# ============================================================
|
| 13 |
+
# 1. Gemini Setup
|
| 14 |
+
# ============================================================
|
| 15 |
+
|
| 16 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 17 |
+
|
| 18 |
+
if not GOOGLE_API_KEY:
|
| 19 |
+
raise RuntimeError(
|
| 20 |
+
"GOOGLE_API_KEY is not set. "
|
| 21 |
+
"Add it to your Hugging Face Space Secrets."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 25 |
|
| 26 |
+
GEMINI_MODEL = "gemini-2.5-flash"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
|
| 29 |
+
# ============================================================
|
| 30 |
# 2. Ensure about_me.txt exists
|
| 31 |
+
# ============================================================
|
| 32 |
+
|
| 33 |
+
ABOUT_FILE = "about_me.txt"
|
| 34 |
+
|
| 35 |
+
if not os.path.exists(ABOUT_FILE):
|
| 36 |
+
with open(ABOUT_FILE, "w", encoding="utf-8") as f:
|
| 37 |
+
f.write(
|
| 38 |
+
"""
|
| 39 |
+
Hello! I am Abdullah Tahir.
|
| 40 |
+
|
| 41 |
+
I am an AI Developer and AI Engineer with experience in Python,
|
| 42 |
+
backend development, AI applications, RAG systems, and AI integrations.
|
| 43 |
+
|
| 44 |
+
I work with technologies such as Python, FastAPI, Django,
|
| 45 |
+
LangChain, LangGraph, Hugging Face, TensorFlow, scikit-learn,
|
| 46 |
+
n8n, and LLM APIs.
|
| 47 |
+
|
| 48 |
+
This chatbot is connected to my portfolio and can answer questions
|
| 49 |
+
about my background, skills, projects, and experience.
|
| 50 |
+
"""
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# ============================================================
|
| 55 |
+
# 3. Load Portfolio Data
|
| 56 |
+
# ============================================================
|
| 57 |
+
|
| 58 |
try:
|
| 59 |
+
loader = TextLoader(
|
| 60 |
+
ABOUT_FILE,
|
| 61 |
+
encoding="utf-8"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
documents = loader.load()
|
| 65 |
+
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"Error loading {ABOUT_FILE}: {e}")
|
| 68 |
+
|
| 69 |
+
from langchain_core.documents import Document
|
| 70 |
+
|
| 71 |
+
documents = [
|
| 72 |
+
Document(
|
| 73 |
+
page_content=(
|
| 74 |
+
"This is Abdullah Tahir's portfolio chatbot. "
|
| 75 |
+
"The portfolio information could not be loaded."
|
| 76 |
+
)
|
| 77 |
+
)
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# ============================================================
|
| 82 |
+
# 4. Split Documents
|
| 83 |
+
# ============================================================
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 86 |
chunk_size=500,
|
| 87 |
chunk_overlap=50
|
| 88 |
)
|
|
|
|
| 89 |
|
| 90 |
+
split_documents = text_splitter.split_documents(documents)
|
| 91 |
+
|
| 92 |
+
print(f"Loaded {len(split_documents)} document chunks.")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ============================================================
|
| 96 |
+
# 5. Create Embedding Model
|
| 97 |
+
# ============================================================
|
| 98 |
+
|
| 99 |
+
print("Loading embedding model...")
|
| 100 |
+
|
| 101 |
embedding_model = HuggingFaceEmbeddings(
|
| 102 |
model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 103 |
+
model_kwargs={
|
| 104 |
+
"device": "cpu"
|
| 105 |
+
},
|
| 106 |
+
encode_kwargs={
|
| 107 |
+
"normalize_embeddings": True
|
| 108 |
+
}
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
print("Embedding model loaded.")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# ============================================================
|
| 115 |
+
# 6. Create FAISS Vector Database
|
| 116 |
+
# ============================================================
|
| 117 |
+
|
| 118 |
+
print("Creating FAISS vector database...")
|
| 119 |
+
|
| 120 |
+
vector_db = FAISS.from_documents(
|
| 121 |
+
split_documents,
|
| 122 |
+
embedding_model
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
print("FAISS database ready.")
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# ============================================================
|
| 129 |
+
# 7. Create Retriever
|
| 130 |
+
# ============================================================
|
| 131 |
+
|
| 132 |
+
retriever = vector_db.as_retriever(
|
| 133 |
+
search_kwargs={
|
| 134 |
+
"k": 3
|
| 135 |
+
}
|
| 136 |
)
|
| 137 |
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
# ============================================================
|
| 140 |
+
# 8. Ask Chatbot
|
| 141 |
+
# ============================================================
|
| 142 |
+
|
| 143 |
+
def ask_bot(question: str) -> str:
|
| 144 |
+
|
| 145 |
try:
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
# --------------------------------------------
|
| 148 |
+
# Validate question
|
| 149 |
+
# --------------------------------------------
|
| 150 |
+
|
| 151 |
+
if not question or not question.strip():
|
| 152 |
+
return "Please ask me a question about my portfolio."
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
question = question.strip()
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# --------------------------------------------
|
| 159 |
+
# Retrieve relevant documents
|
| 160 |
+
# --------------------------------------------
|
| 161 |
+
|
| 162 |
+
context_documents = retriever.invoke(question)
|
| 163 |
|
| 164 |
+
if not context_documents:
|
| 165 |
+
return (
|
| 166 |
+
"I couldn't find relevant information "
|
| 167 |
+
"in the portfolio."
|
| 168 |
+
)
|
| 169 |
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
# --------------------------------------------
|
| 172 |
+
# Build context
|
| 173 |
+
# --------------------------------------------
|
| 174 |
+
|
| 175 |
+
context = "\n\n".join(
|
| 176 |
+
document.page_content
|
| 177 |
+
for document in context_documents
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
# --------------------------------------------
|
| 182 |
+
# Gemini Prompt
|
| 183 |
+
# --------------------------------------------
|
| 184 |
+
|
| 185 |
prompt = f"""
|
| 186 |
+
You are Abdullah Tahir's portfolio assistant.
|
| 187 |
+
|
| 188 |
+
Your job is to answer questions about Abdullah using ONLY
|
| 189 |
+
the portfolio information provided below.
|
| 190 |
+
|
| 191 |
+
Rules:
|
| 192 |
+
|
| 193 |
+
1. Use only the provided context.
|
| 194 |
+
2. Do not invent information.
|
| 195 |
+
3. If the answer is not available in the context,
|
| 196 |
+
clearly say that the information is not available.
|
| 197 |
+
4. Keep answers concise and professional.
|
| 198 |
+
5. Speak naturally.
|
| 199 |
+
6. When appropriate, mention relevant technologies,
|
| 200 |
+
projects, education, or experience from the context.
|
| 201 |
+
|
| 202 |
+
Portfolio Context:
|
| 203 |
+
------------------
|
| 204 |
+
{context}
|
| 205 |
+
------------------
|
| 206 |
+
|
| 207 |
+
User Question:
|
| 208 |
+
{question}
|
| 209 |
+
|
| 210 |
+
Answer:
|
| 211 |
+
"""
|
| 212 |
|
|
|
|
| 213 |
|
| 214 |
+
# --------------------------------------------
|
| 215 |
+
# Call Gemini
|
| 216 |
+
# --------------------------------------------
|
| 217 |
|
| 218 |
+
response = client.models.generate_content(
|
| 219 |
+
model=GEMINI_MODEL,
|
| 220 |
+
contents=prompt
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
# --------------------------------------------
|
| 225 |
+
# Return response
|
| 226 |
+
# --------------------------------------------
|
| 227 |
+
|
| 228 |
+
if response and response.text:
|
| 229 |
+
return response.text.strip()
|
| 230 |
+
|
| 231 |
+
return "I couldn't generate a response."
|
| 232 |
|
|
|
|
|
|
|
| 233 |
|
| 234 |
except Exception as e:
|
|
|
|
| 235 |
|
| 236 |
+
print(f"Chatbot error: {e}")
|
| 237 |
+
|
| 238 |
+
return (
|
| 239 |
+
"Sorry, I encountered an error while processing "
|
| 240 |
+
"your question. Please try again."
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
# ============================================================
|
| 245 |
+
# 9. Gradio Interface
|
| 246 |
+
# ============================================================
|
| 247 |
+
|
| 248 |
+
interface = gr.Interface(
|
| 249 |
+
|
| 250 |
+
fn=ask_bot,
|
| 251 |
+
|
| 252 |
inputs=gr.Textbox(
|
| 253 |
label="Ask me about the portfolio",
|
| 254 |
placeholder="What would you like to know?",
|
| 255 |
+
lines=3
|
| 256 |
),
|
| 257 |
+
|
| 258 |
outputs=gr.Textbox(
|
| 259 |
label="Response",
|
| 260 |
+
lines=10
|
| 261 |
),
|
| 262 |
+
|
| 263 |
+
title="Abdullah Tahir - Portfolio Chatbot",
|
| 264 |
+
|
| 265 |
+
description=(
|
| 266 |
+
"Ask questions about my background, "
|
| 267 |
+
"skills, projects, education, and experience."
|
| 268 |
+
),
|
| 269 |
+
|
| 270 |
examples=[
|
| 271 |
"What are your technical skills?",
|
| 272 |
+
"Tell me about your projects.",
|
| 273 |
+
"What is your educational background?",
|
| 274 |
+
"What technologies do you work with?",
|
| 275 |
+
"Tell me about your AI experience."
|
| 276 |
],
|
| 277 |
+
|
| 278 |
+
cache_examples=False
|
| 279 |
)
|
| 280 |
|
| 281 |
+
|
| 282 |
+
# ============================================================
|
| 283 |
+
# 10. Launch
|
| 284 |
+
# ============================================================
|
| 285 |
+
|
| 286 |
if __name__ == "__main__":
|
| 287 |
+
|
| 288 |
+
print("Starting Portfolio Chatbot...")
|
| 289 |
+
|
| 290 |
+
interface.launch(
|
| 291 |
server_name="0.0.0.0",
|
| 292 |
+
server_port=int(os.getenv("PORT", "7860")),
|
| 293 |
show_error=True
|
| 294 |
)
|