Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
|
|
3 |
from threading import Thread
|
4 |
import PyPDF2
|
5 |
import pandas as pd
|
@@ -12,17 +13,22 @@ st.set_page_config(
|
|
12 |
layout="centered"
|
13 |
)
|
14 |
|
15 |
-
#
|
16 |
-
MODEL_NAME = "amiguel/optimizedModelListing6.1"
|
17 |
|
18 |
# Title with rocket emojis
|
19 |
st.title("π WizNerd Insp π")
|
20 |
|
21 |
# Sidebar configuration
|
22 |
with st.sidebar:
|
23 |
-
st.header("
|
24 |
-
hf_token = st.text_input("HuggingFace Token", type="password"
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
uploaded_file = st.file_uploader(
|
27 |
"Choose a PDF or XLSX file",
|
28 |
type=["pdf", "xlsx"],
|
@@ -45,29 +51,68 @@ def process_file(uploaded_file):
|
|
45 |
df = pd.read_excel(uploaded_file)
|
46 |
file_content = df.to_markdown()
|
47 |
except Exception as e:
|
48 |
-
st.error(f"Error processing file: {str(e)}")
|
49 |
return file_content
|
50 |
|
51 |
# Load model and tokenizer with authentication
|
52 |
@st.cache_resource
|
53 |
-
def load_model():
|
54 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
tokenizer = AutoTokenizer.from_pretrained(
|
56 |
MODEL_NAME,
|
57 |
-
token=hf_token
|
58 |
)
|
59 |
model = AutoModelForCausalLM.from_pretrained(
|
60 |
MODEL_NAME,
|
61 |
device_map="auto",
|
62 |
torch_dtype=torch.float16,
|
63 |
-
token=hf_token
|
64 |
)
|
65 |
return model, tokenizer
|
66 |
except Exception as e:
|
67 |
-
st.error(f"Model loading failed: {str(e)}")
|
68 |
return None, None
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Display chat messages
|
73 |
for message in st.session_state.messages:
|
@@ -76,51 +121,32 @@ for message in st.session_state.messages:
|
|
76 |
|
77 |
# Chat input
|
78 |
if prompt := st.chat_input("Ask your inspection question..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Add user message to chat history
|
80 |
with st.chat_message("user", avatar="π§π»"):
|
81 |
st.markdown(prompt)
|
82 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
83 |
|
84 |
-
#
|
85 |
file_context = process_file(uploaded_file) if uploaded_file else ""
|
86 |
|
87 |
-
# Generate response
|
88 |
if model and tokenizer:
|
89 |
with st.chat_message("assistant", avatar="π€"):
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
# Create streamer
|
98 |
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
99 |
-
|
100 |
-
# Tokenize input
|
101 |
-
inputs = tokenizer(
|
102 |
-
full_prompt,
|
103 |
-
return_tensors="pt",
|
104 |
-
max_length=4096,
|
105 |
-
truncation=True
|
106 |
-
).to(model.device)
|
107 |
-
|
108 |
-
# Start generation thread
|
109 |
-
generation_kwargs = dict(
|
110 |
-
inputs,
|
111 |
-
streamer=streamer,
|
112 |
-
max_new_tokens=1024,
|
113 |
-
temperature=0.7,
|
114 |
-
top_p=0.9,
|
115 |
-
repetition_penalty=1.1
|
116 |
-
)
|
117 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
118 |
-
thread.start()
|
119 |
-
|
120 |
-
# Stream response
|
121 |
-
response = st.write_stream(streamer)
|
122 |
-
|
123 |
-
# Add to chat history
|
124 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
125 |
else:
|
126 |
-
st.error("Model not loaded - check
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
3 |
+
from huggingface_hub import login
|
4 |
from threading import Thread
|
5 |
import PyPDF2
|
6 |
import pandas as pd
|
|
|
13 |
layout="centered"
|
14 |
)
|
15 |
|
16 |
+
# Correct model name
|
17 |
+
MODEL_NAME = "amiguel/optimizedModelListing6.1"
|
18 |
|
19 |
# Title with rocket emojis
|
20 |
st.title("π WizNerd Insp π")
|
21 |
|
22 |
# Sidebar configuration
|
23 |
with st.sidebar:
|
24 |
+
st.header("Authentication π")
|
25 |
+
hf_token = st.text_input("HuggingFace Token", type="password",
|
26 |
+
help="Get your token from https://huggingface.co/settings/tokens")
|
27 |
+
|
28 |
+
if not hf_token:
|
29 |
+
st.warning("π Token required for private model access!", icon="β οΈ")
|
30 |
+
|
31 |
+
st.header("Upload Documents π")
|
32 |
uploaded_file = st.file_uploader(
|
33 |
"Choose a PDF or XLSX file",
|
34 |
type=["pdf", "xlsx"],
|
|
|
51 |
df = pd.read_excel(uploaded_file)
|
52 |
file_content = df.to_markdown()
|
53 |
except Exception as e:
|
54 |
+
st.error(f"π Error processing file: {str(e)}")
|
55 |
return file_content
|
56 |
|
57 |
# Load model and tokenizer with authentication
|
58 |
@st.cache_resource
|
59 |
+
def load_model(hf_token):
|
60 |
try:
|
61 |
+
# Login to Hugging Face Hub
|
62 |
+
if hf_token:
|
63 |
+
login(token=hf_token)
|
64 |
+
else:
|
65 |
+
st.error("π Authentication required! Please provide a Hugging Face token.")
|
66 |
+
return None, None
|
67 |
+
|
68 |
tokenizer = AutoTokenizer.from_pretrained(
|
69 |
MODEL_NAME,
|
70 |
+
token=hf_token
|
71 |
)
|
72 |
model = AutoModelForCausalLM.from_pretrained(
|
73 |
MODEL_NAME,
|
74 |
device_map="auto",
|
75 |
torch_dtype=torch.float16,
|
76 |
+
token=hf_token
|
77 |
)
|
78 |
return model, tokenizer
|
79 |
except Exception as e:
|
80 |
+
st.error(f"π€ Model loading failed: {str(e)}")
|
81 |
return None, None
|
82 |
|
83 |
+
# Generate responses with streaming
|
84 |
+
def generate_response(prompt, file_context):
|
85 |
+
# Prepare prompt template
|
86 |
+
full_prompt = f"""You are an expert inspection engineer. Analyze this context:
|
87 |
+
{file_context}
|
88 |
+
|
89 |
+
Question: {prompt}
|
90 |
+
Answer:"""
|
91 |
+
|
92 |
+
# Create streamer
|
93 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
94 |
+
|
95 |
+
# Tokenize input
|
96 |
+
inputs = tokenizer(
|
97 |
+
full_prompt,
|
98 |
+
return_tensors="pt",
|
99 |
+
max_length=4096,
|
100 |
+
truncation=True
|
101 |
+
).to(model.device)
|
102 |
+
|
103 |
+
# Start generation thread
|
104 |
+
generation_kwargs = dict(
|
105 |
+
inputs,
|
106 |
+
streamer=streamer,
|
107 |
+
max_new_tokens=1024,
|
108 |
+
temperature=0.7,
|
109 |
+
top_p=0.9,
|
110 |
+
repetition_penalty=1.1
|
111 |
+
)
|
112 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
113 |
+
thread.start()
|
114 |
+
|
115 |
+
return streamer
|
116 |
|
117 |
# Display chat messages
|
118 |
for message in st.session_state.messages:
|
|
|
121 |
|
122 |
# Chat input
|
123 |
if prompt := st.chat_input("Ask your inspection question..."):
|
124 |
+
if not hf_token:
|
125 |
+
st.error("π Authentication required! Please enter your Hugging Face token in the sidebar.")
|
126 |
+
st.stop()
|
127 |
+
|
128 |
+
# Load model if not loaded
|
129 |
+
if "model" not in st.session_state:
|
130 |
+
st.session_state.model, st.session_state.tokenizer = load_model(hf_token)
|
131 |
+
model = st.session_state.model
|
132 |
+
tokenizer = st.session_state.tokenizer
|
133 |
+
|
134 |
# Add user message to chat history
|
135 |
with st.chat_message("user", avatar="π§π»"):
|
136 |
st.markdown(prompt)
|
137 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
138 |
|
139 |
+
# Process file
|
140 |
file_context = process_file(uploaded_file) if uploaded_file else ""
|
141 |
|
142 |
+
# Generate and stream response
|
143 |
if model and tokenizer:
|
144 |
with st.chat_message("assistant", avatar="π€"):
|
145 |
+
try:
|
146 |
+
streamer = generate_response(prompt, file_context)
|
147 |
+
response = st.write_stream(streamer)
|
148 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
149 |
+
except Exception as e:
|
150 |
+
st.error(f"β‘ Generation error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
else:
|
152 |
+
st.error("π€ Model not loaded - check your token and connection!")
|