Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,71 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# Title and emojis
|
7 |
+
st.title("🚀 WizNerd Insp 🚀")
|
8 |
+
|
9 |
+
# Sidebar for file uploads
|
10 |
+
st.sidebar.header("Upload Files")
|
11 |
+
uploaded_xlsx = st.sidebar.file_uploader("Upload XLSX File", type=["xlsx"])
|
12 |
+
uploaded_pdf = st.sidebar.file_uploader("Upload PDF File", type=["pdf"])
|
13 |
+
|
14 |
+
# Load the HuggingFace model and tokenizer
|
15 |
+
@st.cache_resource
|
16 |
+
def load_model():
|
17 |
+
model_name = "amiguel/optimizedModelLinsting6.1"
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
20 |
+
return tokenizer, model
|
21 |
+
|
22 |
+
tokenizer, model = load_model()
|
23 |
+
|
24 |
+
# Prompt style
|
25 |
+
prompt_style = """
|
26 |
+
Below is an instruction that describes a task, paired with an input that provides further context.
|
27 |
+
Write a response that appropriately completes the request.
|
28 |
+
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
|
29 |
+
### Instruction:
|
30 |
+
You are an experienced inspection methods engineer, a topside expert with advanced knowledge in scope definition, functional location determination, and inspection plan building.
|
31 |
+
Please answer the following inspection scope question.
|
32 |
+
### Instruction:
|
33 |
+
{}
|
34 |
+
### Output:
|
35 |
+
<think> {} </think> {}
|
36 |
+
"""
|
37 |
+
|
38 |
+
# Function to process user input and generate response
|
39 |
+
def generate_response(input_text):
|
40 |
+
# Format the input using the prompt style
|
41 |
+
formatted_input = prompt_style.format(input_text, "", "")
|
42 |
+
|
43 |
+
# Tokenize and generate response
|
44 |
+
inputs = tokenizer(formatted_input, return_tensors="pt", truncation=True, max_length=512)
|
45 |
+
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True)
|
46 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
47 |
+
|
48 |
+
return response
|
49 |
+
|
50 |
+
# Main chat interface
|
51 |
+
st.header("Chat with WizNerd Insp")
|
52 |
+
user_input = st.text_input("Ask a question:")
|
53 |
+
if st.button("Submit"):
|
54 |
+
if user_input.strip() != "":
|
55 |
+
response = generate_response(user_input)
|
56 |
+
st.write("Response:")
|
57 |
+
st.write(response)
|
58 |
+
|
59 |
+
# Process uploaded files
|
60 |
+
if uploaded_xlsx:
|
61 |
+
st.write("Processing XLSX file...")
|
62 |
+
df = pd.read_excel(uploaded_xlsx)
|
63 |
+
st.write(df)
|
64 |
+
|
65 |
+
if uploaded_pdf:
|
66 |
+
st.write("Processing PDF file...")
|
67 |
+
pdf_reader = PdfReader(uploaded_pdf)
|
68 |
+
text = ""
|
69 |
+
for page in pdf_reader.pages:
|
70 |
+
text += page.extract_text()
|
71 |
+
st.write(text)
|