|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer |
|
from huggingface_hub import login |
|
from threading import Thread |
|
import PyPDF2 |
|
import pandas as pd |
|
import torch |
|
import time |
|
|
|
|
|
st.set_page_config( |
|
page_title="WizNerd Insp", |
|
page_icon="π", |
|
layout="centered" |
|
) |
|
|
|
MODEL_NAME = "amiguel/optimizedModelListing6.1" |
|
|
|
|
|
st.title("π WizNerd Insp π") |
|
|
|
|
|
with st.sidebar: |
|
st.header("Authentication π") |
|
hf_token = st.text_input("Hugging Face Token", type="password", |
|
help="Get your token from https://huggingface.co/settings/tokens") |
|
|
|
st.header("Upload Documents π") |
|
uploaded_file = st.file_uploader( |
|
"Choose a PDF or XLSX file", |
|
type=["pdf", "xlsx"], |
|
label_visibility="collapsed" |
|
) |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
@st.cache_data |
|
def process_file(uploaded_file): |
|
|
|
pass |
|
|
|
@st.cache_resource |
|
def load_model(hf_token): |
|
try: |
|
if not hf_token: |
|
st.error("π Authentication required! Please provide a Hugging Face token.") |
|
return None |
|
|
|
|
|
login(token=hf_token) |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained( |
|
MODEL_NAME, |
|
token=hf_token |
|
) |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
MODEL_NAME, |
|
device_map="auto", |
|
torch_dtype=torch.float16, |
|
token=hf_token |
|
) |
|
|
|
return model, tokenizer |
|
|
|
except Exception as e: |
|
st.error(f"π€ Model loading failed: {str(e)}") |
|
return None |
|
|
|
|
|
if prompt := st.chat_input("Ask your inspection question..."): |
|
if not hf_token: |
|
st.error("π Authentication required!") |
|
st.stop() |
|
|
|
|
|
if "model" not in st.session_state: |
|
model_data = load_model(hf_token) |
|
if model_data is None: |
|
st.error("Failed to load model. Please check your token and try again.") |
|
st.stop() |
|
|
|
st.session_state.model, st.session_state.tokenizer = model_data |
|
|
|
model = st.session_state.model |
|
tokenizer = st.session_state.tokenizer |
|
|
|
|
|
|
|
with st.chat_message("user", avatar="π€"): |
|
st.markdown(prompt) |
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
file_context = process_file(uploaded_file) |
|
|
|
|
|
if model and tokenizer: |
|
try: |
|
with st.chat_message("assistant", avatar="π€"): |
|
start_time = time.time() |
|
streamer = generate_with_kv_cache(prompt, file_context, use_cache=True) |
|
|
|
response_container = st.empty() |
|
full_response = "" |
|
|
|
for chunk in streamer: |
|
cleaned_chunk = chunk.replace("<think>", "").replace("</think>", "").strip() |
|
full_response += cleaned_chunk + " " |
|
response_container.markdown(full_response + "β", unsafe_allow_html=True) |
|
|
|
|
|
end_time = time.time() |
|
st.caption(f"Generated in {end_time - start_time:.2f}s using KV caching") |
|
|
|
response_container.markdown(full_response) |
|
st.session_state.messages.append({"role": "assistant", "content": full_response}) |
|
|
|
except Exception as e: |
|
st.error(f"β‘ Generation error: {str(e)}") |
|
else: |
|
st.error("π€ Model not loaded!") |