Spaces:
Sleeping
Sleeping
File size: 3,858 Bytes
dc8bd9f 1071b0b 1876716 b43d116 1071b0b 9ca09c4 1876716 bfd67d4 3074fbe 1876716 9ca09c4 1071b0b b43d116 9ca09c4 1876716 9ca09c4 1071b0b b43d116 9ca09c4 1876716 9ca09c4 1071b0b ebfddcb 9ca09c4 1071b0b b43d116 9ca09c4 1876716 9ca09c4 1071b0b 9ca09c4 1071b0b b43d116 9ca09c4 1876716 9ca09c4 ebfddcb 1071b0b 9ca09c4 1071b0b 9ca09c4 1876716 9ca09c4 ca169fc 9ca09c4 1876716 9ca09c4 1876716 9ca09c4 1071b0b 9ca09c4 6fecdb5 9ca09c4 1876716 9ca09c4 1071b0b 71d0903 1071b0b b43d116 dc8bd9f 1071b0b 9ca09c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
import gradio as gr
import os
import google.generativeai as genai
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
# -------------------------------
# 1. Setup Gemini
# -------------------------------
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
gemini_model = genai.GenerativeModel("gemini-2.5-flash")
# -------------------------------
# 2. Ensure about_me.txt exists
# -------------------------------
if not os.path.exists("about_me.txt"):
with open("about_me.txt", "w") as f:
f.write("""
Hello! I am a portfolio chatbot. I can help answer questions about projects, skills, and experience.
This is a sample portfolio text. Please replace this with your actual portfolio content.
""")
# -------------------------------
# 3. Load data
# -------------------------------
try:
loader = TextLoader("about_me.txt")
docs = loader.load()
except Exception as e:
print(f"Error loading document: {e}")
from langchain.schema import Document
docs = [Document(page_content="Hello! I am a portfolio chatbot ready to help you.")]
# -------------------------------
# 4. Split documents
# -------------------------------
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=50
)
split_docs = text_splitter.split_documents(docs)
# -------------------------------
# 5. Create embeddings & FAISS
# -------------------------------
print("Loading embeddings...")
embedding_model = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'}
)
print("Creating vector database...")
db = FAISS.from_documents(split_docs, embedding_model)
# -------------------------------
# 6. Ask function with Gemini refinement
# -------------------------------
def ask_bot_alternative(question: str):
try:
if not question.strip():
return "Please ask me a question about the portfolio!"
# Retrieve top documents
retriever = db.as_retriever(search_kwargs={"k": 2})
context_docs = retriever.get_relevant_documents(question)
if not context_docs:
return "I could not find an answer in the portfolio content."
# Combine retrieved docs into context
context = "\n".join([doc.page_content for doc in context_docs])
# Send to Gemini for refinement
prompt = f"""
You are a helpful assistant.
Answer the following question using only the given context.
If the answer is not present, say "I don’t know".
Question: {question}
Context:
{context}
Final Answer:
"""
response = gemini_model.generate_content(prompt)
return response.text.strip()
except Exception as e:
return f"Sorry, I encountered an error: {str(e)[:200]}"
# -------------------------------
# 7. Gradio Interface
# -------------------------------
iface = gr.Interface(
fn=ask_bot_alternative,
inputs=gr.Textbox(
label="Ask me about the portfolio",
placeholder="What would you like to know?",
max_lines=3
),
outputs=gr.Textbox(
label="Response",
max_lines=10
),
title="Portfolio Chatbot",
description="Ask me questions about skills, projects, and experience!",
examples=[
"What are your technical skills?",
"Tell me about your projects",
"What is your background?"
],
cache_examples=False,
allow_flagging="never"
)
if __name__ == "__main__":
print("Launching chatbot...")
iface.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
)
|