Spaces:
Running
Running
File size: 18,218 Bytes
c87bf64 77253fa c680fd3 8864f7b 77253fa 6812c83 77253fa c680fd3 77253fa 0daf636 77253fa 8114680 a099ec5 ee0ded9 6812c83 77253fa 60499c7 77253fa 6812c83 77253fa 6812c83 77253fa 6812c83 77253fa 6812c83 77253fa 5016eca 77253fa c87bf64 23b35f5 c87bf64 8114680 75b6d0b e12828c fc0fae1 6812c83 28ed593 77253fa 185f5c0 77253fa 185f5c0 77253fa 14d5927 da482df 14d5927 7f71963 14d5927 77253fa cee37a9 77253fa 72f2043 77253fa 72f2043 77253fa 72f2043 77253fa 72f2043 77253fa 72f2043 77253fa 72f2043 77253fa cee37a9 77253fa 14d5927 77253fa 72f2043 77253fa 5efd55c 77253fa 5efd55c 77253fa 72f2043 77253fa 6812c83 77253fa 4e99304 c87bf64 cee37a9 4e99304 cee37a9 4e99304 cee37a9 4e99304 c87bf64 cee37a9 4e99304 cee37a9 4e99304 c87bf64 4e99304 cee37a9 4e99304 1fb6cb1 4e99304 a5bc1a5 4e99304 1fb6cb1 4e99304 1fb6cb1 4e99304 1fb6cb1 eb95dcd 28ed593 b0fa9f5 ba70895 8234d03 b0fa9f5 77253fa b0fa9f5 4892445 23b35f5 4892445 bb45e35 4892445 cb022f9 fc0fae1 cb022f9 75b6d0b 8234d03 b0fa9f5 ba70895 8234d03 b0fa9f5 ba70895 b0fa9f5 ba70895 b0fa9f5 8234d03 07cac5d 8234d03 07cac5d 8234d03 b0fa9f5 ba70895 9bfdbb2 b0fa9f5 ba70895 b0fa9f5 8234d03 75b6d0b fc0fae1 d1fa96d 40ec004 75b6d0b fc0fae1 75b6d0b 8234d03 ba70895 4892445 8234d03 07cac5d 4892445 8234d03 4892445 d129c65 4892445 8234d03 4892445 d129c65 4892445 8234d03 4892445 ba70895 8234d03 4892445 8234d03 9584128 4892445 482d60c da482df 8234d03 4892445 c680fd3 482d60c 4892445 e85f5f5 8234d03 77253fa |
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
from huggingface_hub import HfApi
import tempfile
import streamlit as st
import pandas as pd
import io, os
import subprocess
import pdfplumber
from lxml import etree
from bs4 import BeautifulSoup
from PyPDF2 import PdfReader
from langchain_community.vectorstores import FAISS
from langchain.embeddings.base import Embeddings
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from langchain.agents import Tool
from langchain.memory import ConversationBufferMemory
from langchain.text_splitter import CharacterTextSplitter
from dotenv import load_dotenv
import google.generativeai as genai
from typing import List
from langchain_core.language_models import BaseLanguageModel
from langchain_core.runnables import Runnable
import google.generativeai as genai
from datetime import datetime
load_dotenv()
def load_environment():
# Ensure HF_TOKEN is available
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ and "HF_TOKEN" in os.environ:
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.environ["HF_TOKEN"]
if "GOOGLE_API_KEY" not in os.environ:
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
from keybert import KeyBERT
from sentence_transformers import CrossEncoder
from sentence_transformers import SentenceTransformer
class GeminiLLM(Runnable):
def __init__(self, model_name="models/gemini-1.5-pro-latest", api_key=None):
self.api_key = api_key or os.environ["GOOGLE_API_KEY"]
if not self.api_key:
raise ValueError("GOOGLE_API_KEY not found.")
genai.configure(api_key=self.api_key)
self.model = genai.GenerativeModel(model_name)
def _call(self, prompt: str, stop=None) -> str:
response = self.model.generate_content(prompt)
return response.text
@property
def _llm_type(self) -> str:
return "custom_gemini"
def invoke(self, input, config=None):
response = self.model.generate_content(input)
return response.text.strip()
class GeminiEmbeddings(Embeddings):
def __init__(self, model_name="models/embedding-001", api_key=None):
api_key = "AIzaSyBIfGJRoet_wzzYXIiWXxStkIigEOzSR2o"
if not api_key:
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
os.environ["GOOGLE_API_KEY"] = api_key
genai.configure(api_key=api_key)
self.model_name = model_name
def embed_documents(self, texts: List[str]) -> List[List[float]]:
return [
genai.embed_content(
model=self.model_name,
content=text,
task_type="retrieval_document"
)["embedding"]
for text in texts
]
def embed_query(self, text: str) -> List[float]:
return genai.embed_content(
model=self.model_name,
content=text,
task_type="retrieval_query"
)["embedding"]
vectorstore_global = None
if "feedback_log" not in st.session_state:
st.session_state["feedback_log"] = []
def preload_modtran_document():
global vectorstore_global
embeddings = GeminiEmbeddings()
st.session_state.vectorstore = FAISS.load_local("monte_vectorstore", embeddings, allow_dangerous_deserialization=True)
set_global_vectorstore(st.session_state.vectorstore)
st.session_state.chat_ready = True
def convert_pdf_to_xml(pdf_file, xml_path):
os.makedirs("temp", exist_ok=True)
pdf_path = os.path.join("temp", pdf_file.name)
with open(pdf_path, 'wb') as f:
f.write(pdf_file.getbuffer())
subprocess.run(["pdftohtml", "-xml", pdf_path, xml_path], check=True)
return xml_path
def extract_text_from_xml(xml_path, document_name):
tree = etree.parse(xml_path)
text_chunks = []
for page in tree.xpath("//page"):
page_num = int(page.get("number", 0))
texts = [text.text for text in page.xpath('.//text') if text.text]
combined_text = '\n'.join(texts)
text_chunks.append({"text": combined_text, "page": page_num, "document": document_name})
return text_chunks
def extract_text_from_pdf(pdf_file, document_name):
text_chunks = []
with pdfplumber.open(pdf_file) as pdf:
for i, page in enumerate(pdf.pages):
text = page.extract_text()
if text:
text_chunks.append({"text": text, "page": i + 1, "document": document_name})
return text_chunks
def get_uploaded_text(uploaded_files):
raw_text = []
for uploaded_file in uploaded_files:
document_name = uploaded_file.name
if document_name.endswith(".pdf"):
text_chunks = extract_text_from_pdf(uploaded_file, document_name)
raw_text.extend(text_chunks)
elif uploaded_file.name.endswith((".html", ".htm")):
soup = BeautifulSoup(uploaded_file.getvalue(), 'lxml')
raw_text.append({"text": soup.get_text(), "page": None, "document": document_name})
elif uploaded_file.name.endswith((".txt")):
content = uploaded_file.getvalue().decode("utf-8")
raw_text.append({"text": content, "page": None, "document": document_name})
return raw_text
def get_text_chunks(raw_text):
splitter = CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=100)
final_chunks = []
for chunk in raw_text:
for split_text in splitter.split_text(chunk["text"]):
final_chunks.append({"text": split_text, "page": chunk["page"], "document": chunk["document"]})
return final_chunks
def get_vectorstore(text_chunks):
if not text_chunks:
raise ValueError("text_chunks is empty. Cannot initialize FAISS vectorstore.")
embeddings = GeminiEmbeddings()
texts = [chunk["text"] for chunk in text_chunks]
metadatas = [{"page": chunk["page"], "document": chunk["document"]} for chunk in text_chunks]
return FAISS.from_texts(texts, embedding=embeddings, metadatas=metadatas)
def set_global_vectorstore(vectorstore):
global vectorstore_global
vectorstore_global = vectorstore
kw_model = None
reranker = None
def get_kw_model():
global kw_model
if kw_model is None:
# Load sentence transformer with HF token explicitly
model = SentenceTransformer(
'sentence-transformers/all-MiniLM-L6-v2',
use_auth_token=os.environ.get("HF_TOKEN")
)
kw_model = KeyBERT(model=model)
return kw_model
def self_reasoning(query, context):
print("π§ͺ self_reasoning received context of length:", len(context))
llm = GeminiLLM()
reasoning_prompt = f"""
You are an AI assistant that analyzes the context provided to answer the user's query comprehensively and clearly.
Answer in a concise, factual way using the terminology from the context. Avoid extra explanation unless explicitly asked.
YOU MUST mention the document file name (e.g., tools.html, refguide.html) in your answer.
### Example 1:
**Question:** What is the purpose of the Monte GUI?
**Context:**
[From `tools.html`] The Monte GUI provides interfaces for setting up trajectory parameters and viewing output results.
**Answer:** The Monte GUI helps users configure trajectory parameters and visualize results. (From `tools.html`)
### Example 2:
**Question:** How do you perform covariance analysis in Monte?
**Context:**
[From `designEdition.html`] The Monte Design Edition includes support for statistical maneuver and covariance analysis during the design phase.
**Answer:** Monte supports covariance analysis through the Design Edition. (From `designEdition.html`)
### Now answer:
**Question:** {query}
**Context:**
{context}
**Answer:**
"""
try:
result = llm._call(reasoning_prompt)
print("β
Gemini returned a result.")
return result
except Exception as e:
print("β Error in self_reasoning:", e)
return f"β οΈ Gemini failed: {e}"
def faiss_search_with_keywords(query):
global vectorstore_global
if vectorstore_global is None:
raise ValueError("FAISS vectorstore is not initialized.")
kw_model = get_kw_model()
keywords = kw_model.extract_keywords(query, keyphrase_ngram_range=(1,2), stop_words='english', top_n=5)
refined_query = " ".join([keyword[0] for keyword in keywords])
retriever = vectorstore_global.as_retriever(search_kwargs={"k": 13})
docs = retriever.get_relevant_documents(refined_query)
context = '\n\n'.join([f"[From `{doc.metadata.get('document', 'unknown.html')}`] {doc.page_content}" for doc in docs])
return self_reasoning(query, context)
def get_reranker():
global reranker
if reranker is None:
reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
return reranker
def faiss_search_with_reasoning(query):
global vectorstore_global
if vectorstore_global is None:
raise ValueError("FAISS vectorstore is not initialized.")
reranker = get_reranker()
retriever = vectorstore_global.as_retriever(search_kwargs={"k": 13})
docs = retriever.get_relevant_documents(query)
pairs = [(query, doc.page_content) for doc in docs]
scores = reranker.predict(pairs)
reranked_docs = sorted(zip(scores, docs), key=lambda x: x[0], reverse=True)
top_docs = [doc for _, doc in reranked_docs[:5]]
context = '\n\n'.join([f"[From `{doc.metadata.get('document', 'unknown.html')}`] {doc.page_content.strip()}" for doc in top_docs])
return self_reasoning(query, context)
faiss_keyword_tool = Tool(
name="FAISS Keyword Search",
func=faiss_search_with_keywords,
description="Searches FAISS with a keyword-based approach to retrieve context."
)
faiss_reasoning_tool = Tool(
name="FAISS Reasoning Search",
func=faiss_search_with_reasoning,
description="Searches FAISS with detailed reasoning to retrieve context."
)
def initialize_chatbot_agent():
llm = GeminiLLM()
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
tools = [faiss_keyword_tool, faiss_reasoning_tool]
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
memory=memory,
verbose=False,
handle_parsing_errors=True
)
return agent
def handle_user_query(query):
try:
global vectorstore_global
if vectorstore_global is None:
raise ValueError("Vectorstore is not initialized.")
print("π Starting handle_user_query with:", query)
if "how" in query.lower():
print("π§ Routing to: faiss_search_with_reasoning")
context = faiss_search_with_reasoning(query)
else:
print("π§ Routing to: faiss_search_with_keywords")
context = faiss_search_with_keywords(query)
print("π Context length:", len(context))
print("βοΈ Calling self_reasoning...")
answer = self_reasoning(query, context)
print("β
Answer generated.")
return answer
except Exception as e:
print("β Error in handle_user_query:", e)
return f"β οΈ Error: {e}"
def save_feedback_to_huggingface():
try:
if not st.session_state.feedback_log:
print("β οΈ No feedbacks collected yet.")
return
feedback_df = pd.DataFrame(st.session_state.feedback_log)
now = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"feedback_{now}.csv"
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, filename)
feedback_df.to_csv(filepath, index=False)
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
if not token:
raise ValueError("β Hugging Face token not found!")
print(f"π€ Attempting upload to repo: ZarinT/chatbot-feedback as {filename}")
print("π Feedback data:", feedback_df)
api = HfApi(token=token)
api.upload_file(
path_or_fileobj=filepath,
path_in_repo=filename,
repo_id="ZarinT/chatbot-feedback",
repo_type="dataset"
)
print("β
Feedback uploaded successfully.")
st.session_state.feedback_log.clear()
except Exception as e:
print("β Feedback upload failed:", e)
def clear_user_input():
st.session_state["user_input"] = ""
from datetime import datetime
def clear_user_input():
st.session_state.user_input = ""
from datetime import datetime
def clear_user_input():
st.session_state.user_input = ""
from datetime import datetime
def clear_user_input():
st.session_state.user_input = ""
def main():
load_environment()
# Initialize session state
if "chat_ready" not in st.session_state:
st.session_state.chat_ready = False
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "vectorstore" not in st.session_state:
st.session_state.vectorstore = None
if "feedback_log" not in st.session_state:
st.session_state.feedback_log = []
if "feedback_submitted" not in st.session_state:
st.session_state.feedback_submitted = False
if "last_answered_question" not in st.session_state:
st.session_state.last_answered_question = ""
st.markdown("""
<h2>Chat with MONTE Documents π</h2>
""", unsafe_allow_html=True)
# Inject custom CSS for chat bubbles
st.markdown("""
<style>
.chat-container {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.bot-bubble {
align-self: flex-start;
background-color: #e8f4fd;
color: #111;
padding: 1rem;
border-radius: 0.5rem 1rem 1rem 1rem;
max-width: 80%;
}
.user-bubble {
align-self: flex-end;
background-color: rgba(0, 0, 0, 0.85);
color: white;
padding: 1rem;
border-radius: 1rem 0.5rem 1rem 1rem;
max-width: 80%;
text-align: right;
}
.rating-line {
font-weight: bold;
color: #ffaa00;
font-size: 0.95rem;
max-width: 80%;
align-self: center;
text-align: center;
}
.feedback-counter {
font-style: italic;
color: #666;
font-size: 0.9rem;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
# Load vectorstore and chatbot agent
if not st.session_state.chat_ready:
with st.spinner("Loading Monte documents..."):
preload_modtran_document()
st.session_state.agent = initialize_chatbot_agent()
st.session_state.chat_ready = True
st.success("Monte Docuemnts loaded successfully!")
# Render all previous Q&A in chat format
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
for i, exchange in enumerate(st.session_state.chat_history):
# User's question (right side)
st.markdown(f'<div class="user-bubble"><strong>You:</strong> {exchange["user"]}</div>', unsafe_allow_html=True)
# MODTRAN Bot's answer (left side)
st.markdown(f'<div class="bot-bubble"><strong>MODTRAN Bot:</strong> {exchange["bot"]}</div>', unsafe_allow_html=True)
# If already rated
if "rating" in exchange:
st.markdown(f'<div class="rating-line">βοΈ You rated this: {exchange["rating"]}</div>', unsafe_allow_html=True)
# If not rated yet and it's the last message, show form
elif i == len(st.session_state.chat_history) - 1:
with st.form(key=f"feedback_form_{i}"):
rating = st.radio(
"Rate this response:",
options=["Not helpful", "Somewhat helpful", "Neutral", "Helpful", "Very helpful"],
key=f"rating_{i}",
horizontal=True
)
submitted = st.form_submit_button("Submit Rating")
if submitted:
st.session_state.chat_history[i]["rating"] = rating
st.session_state.feedback_log.append({
"question": exchange["user"],
"response": exchange["bot"],
"rating": rating,
"timestamp": datetime.now().isoformat()
})
if len(st.session_state.feedback_log) >= 2:
print("π¦ Upload threshold reached β saving feedback to Hugging Face.")
save_feedback_to_huggingface()
st.session_state.feedback_submitted = True
st.rerun()
st.markdown('</div>', unsafe_allow_html=True)
# Show current feedback progress
#st.markdown(f'<div class="feedback-counter">π Feedbacks collected: <strong>{len(st.session_state.feedback_log)} / 5</strong></div>', unsafe_allow_html=True)
# Input for next question
user_question = st.chat_input("Ask your next question:")
if user_question and user_question != st.session_state.last_answered_question:
with st.spinner("Generating answer..."):
try:
set_global_vectorstore(st.session_state.vectorstore)
response = handle_user_query(user_question)
except Exception as e:
response = f"β οΈ Something went wrong: {e}"
# Save new Q&A
st.session_state.chat_history.append({
"user": user_question,
"bot": response
})
st.session_state.last_answered_question = user_question
st.rerun()
if __name__ == "__main__":
load_environment()
main()
|