Update app.py
Browse files
app.py
CHANGED
|
@@ -336,6 +336,7 @@ if "pdf_name" not in st.session_state: st.session_state.pdf_name = None
|
|
| 336 |
if "pdf_pages" not in st.session_state: st.session_state.pdf_pages = 0
|
| 337 |
if "pdf_chunks" not in st.session_state: st.session_state.pdf_chunks = 0
|
| 338 |
if "q_count" not in st.session_state: st.session_state.q_count = 0
|
|
|
|
| 339 |
|
| 340 |
# ─── MODEL LOADERS ────────────────────────────────────────────────────────────
|
| 341 |
@st.cache_resource(show_spinner=False)
|
|
@@ -349,9 +350,13 @@ def load_llm():
|
|
| 349 |
model_id = "TinyLlama/TinyLlama-1.1B-chat-v1.0"
|
| 350 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 351 |
model = AutoModelForCausalLM.from_pretrained(
|
| 352 |
-
model_id,
|
| 353 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
| 354 |
)
|
|
|
|
|
|
|
| 355 |
pipe = pipeline(
|
| 356 |
"text-generation", model=model, tokenizer=tokenizer,
|
| 357 |
max_new_tokens=512, temperature=0.3, do_sample=True,
|
|
@@ -421,9 +426,9 @@ with st.sidebar:
|
|
| 421 |
<div class="profile-card">
|
| 422 |
<div class="profile-avatar">{avatar}</div>
|
| 423 |
<div class="profile-name">SRIRAM SAI</div>
|
| 424 |
-
<div class="profile-role">
|
| 425 |
<div class="profile-links">
|
| 426 |
-
<a class="p-link" href="https://github.com/sriramsai18" target="_blank">💻
|
| 427 |
<a class="p-link" href="https://www.linkedin.com/in/sriram-sai-laggisetti/" target="_blank">💼 LinkedIn</a>
|
| 428 |
</div>
|
| 429 |
</div>
|
|
@@ -502,7 +507,7 @@ st.markdown("""
|
|
| 502 |
<div class="app-header">
|
| 503 |
<div>
|
| 504 |
<div class="app-title">QUERY<span>DOCS</span> AI 📚</div>
|
| 505 |
-
<div class="app-sub">
|
| 506 |
</div>
|
| 507 |
</div>
|
| 508 |
""", unsafe_allow_html=True)
|
|
@@ -530,7 +535,7 @@ if not st.session_state.vectorstore:
|
|
| 530 |
<div class="wc-title">WELCOME TO QUERYDOCS AI</div>
|
| 531 |
<div class="wc-sub">
|
| 532 |
Upload any PDF document from the sidebar and start asking questions.<br>
|
| 533 |
-
Powered by RAG pipeline
|
| 534 |
</div>
|
| 535 |
<br>
|
| 536 |
<span class="tip-chip">📋 Legal documents</span>
|
|
@@ -596,7 +601,7 @@ else:
|
|
| 596 |
|
| 597 |
with col_q:
|
| 598 |
question = st.text_input(
|
| 599 |
-
"", placeholder="
|
| 600 |
label_visibility="collapsed", key="question_input"
|
| 601 |
)
|
| 602 |
with col_btn:
|
|
@@ -604,8 +609,8 @@ else:
|
|
| 604 |
ask_btn = st.button("▶ ASK", use_container_width=True)
|
| 605 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 606 |
|
| 607 |
-
# Generate answer
|
| 608 |
-
if (ask_btn or question) and question.strip():
|
| 609 |
ts = time.strftime("%H:%M")
|
| 610 |
st.session_state.messages.append({
|
| 611 |
"role": "user", "content": question.strip(), "time": ts
|
|
@@ -644,4 +649,5 @@ else:
|
|
| 644 |
})
|
| 645 |
|
| 646 |
typing_slot.empty()
|
|
|
|
| 647 |
st.rerun()
|
|
|
|
| 336 |
if "pdf_pages" not in st.session_state: st.session_state.pdf_pages = 0
|
| 337 |
if "pdf_chunks" not in st.session_state: st.session_state.pdf_chunks = 0
|
| 338 |
if "q_count" not in st.session_state: st.session_state.q_count = 0
|
| 339 |
+
if "last_q" not in st.session_state: st.session_state.last_q = ""
|
| 340 |
|
| 341 |
# ─── MODEL LOADERS ────────────────────────────────────────────────────────────
|
| 342 |
@st.cache_resource(show_spinner=False)
|
|
|
|
| 350 |
model_id = "TinyLlama/TinyLlama-1.1B-chat-v1.0"
|
| 351 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 352 |
model = AutoModelForCausalLM.from_pretrained(
|
| 353 |
+
model_id,
|
| 354 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 355 |
+
low_cpu_mem_usage=True,
|
| 356 |
+
device_map="cuda" if torch.cuda.is_available() else None
|
| 357 |
)
|
| 358 |
+
if not torch.cuda.is_available():
|
| 359 |
+
model = model.to("cpu")
|
| 360 |
pipe = pipeline(
|
| 361 |
"text-generation", model=model, tokenizer=tokenizer,
|
| 362 |
max_new_tokens=512, temperature=0.3, do_sample=True,
|
|
|
|
| 426 |
<div class="profile-card">
|
| 427 |
<div class="profile-avatar">{avatar}</div>
|
| 428 |
<div class="profile-name">SRIRAM SAI</div>
|
| 429 |
+
<div class="profile-role">AI & ML ENGINEER</div>
|
| 430 |
<div class="profile-links">
|
| 431 |
+
<a class="p-link" href="https://github.com/sriramsai18" target="_blank">💻 Github</a>
|
| 432 |
<a class="p-link" href="https://www.linkedin.com/in/sriram-sai-laggisetti/" target="_blank">💼 LinkedIn</a>
|
| 433 |
</div>
|
| 434 |
</div>
|
|
|
|
| 507 |
<div class="app-header">
|
| 508 |
<div>
|
| 509 |
<div class="app-title">QUERY<span>DOCS</span> AI 📚</div>
|
| 510 |
+
<div class="app-sub">INTELLIGENT DOCUMENT Q&A · RAG PIPELINE </div>
|
| 511 |
</div>
|
| 512 |
</div>
|
| 513 |
""", unsafe_allow_html=True)
|
|
|
|
| 535 |
<div class="wc-title">WELCOME TO QUERYDOCS AI</div>
|
| 536 |
<div class="wc-sub">
|
| 537 |
Upload any PDF document from the sidebar and start asking questions.<br>
|
| 538 |
+
Powered by RAG pipeline , context-aware answers.
|
| 539 |
</div>
|
| 540 |
<br>
|
| 541 |
<span class="tip-chip">📋 Legal documents</span>
|
|
|
|
| 601 |
|
| 602 |
with col_q:
|
| 603 |
question = st.text_input(
|
| 604 |
+
"", placeholder="ask a question about your document...",
|
| 605 |
label_visibility="collapsed", key="question_input"
|
| 606 |
)
|
| 607 |
with col_btn:
|
|
|
|
| 609 |
ask_btn = st.button("▶ ASK", use_container_width=True)
|
| 610 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 611 |
|
| 612 |
+
# Generate answer — guard against infinite loop
|
| 613 |
+
if (ask_btn or question) and question.strip() and question.strip() != st.session_state.last_q:
|
| 614 |
ts = time.strftime("%H:%M")
|
| 615 |
st.session_state.messages.append({
|
| 616 |
"role": "user", "content": question.strip(), "time": ts
|
|
|
|
| 649 |
})
|
| 650 |
|
| 651 |
typing_slot.empty()
|
| 652 |
+
st.session_state.last_q = question.strip()
|
| 653 |
st.rerun()
|