amiguel commited on
Commit
b5b8672
·
verified ·
1 Parent(s): 284de86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -58
app.py CHANGED
@@ -1,59 +1,71 @@
1
  import streamlit as st
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
- import torch
4
-
5
- model_name = "amiguel/fintune_naming_model" # Replace with your model repo
6
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
- model.to(device)
10
-
11
- def classify_review(text, model, tokenizer, device, max_length=512):
12
- model.eval()
13
- inputs = tokenizer.encode_plus(
14
- text,
15
- truncation=True,
16
- padding='max_length',
17
- max_length=max_length,
18
- return_tensors="pt"
19
- )
20
- input_ids = inputs['input_ids'].to(device)
21
- attention_mask = inputs['attention_mask'].to(device)
22
-
23
- with torch.no_grad():
24
- outputs = model(input_ids, attention_mask=attention_mask)
25
- logits = outputs.logits
26
- predicted_label = torch.argmax(logits, dim=-1).item()
27
- return "Proper Naming otfcn" if predicted_label == 1 else "Wrong Naming notfcn"
28
-
29
- def main():
30
- st.title("Notifications Naming Classifier")
31
-
32
- input_option = st.radio("Select input option", ("Single Text Query", "Upload Table"))
33
-
34
- if input_option == "Single Text Query":
35
- text_query = st.text_input("Enter text query")
36
- if st.button("Classify"):
37
- if text_query:
38
- predicted_label = classify_review(text_query, model, tokenizer, device)
39
- st.write("Predicted Label:")
40
- st.write(predicted_label)
41
- else:
42
- st.warning("Please enter a text query.")
43
-
44
- elif input_option == "Upload Table":
45
- uploaded_file = st.file_uploader("Choose a file", type=["csv", "xlsx"])
46
- if uploaded_file is not None:
47
- import pandas as pd
48
- if uploaded_file.name.endswith(".csv"):
49
- df = pd.read_csv(uploaded_file)
50
- else:
51
- df = pd.read_excel(uploaded_file)
52
-
53
- text_column = st.selectbox("Select the text column", df.columns)
54
- predicted_labels = [classify_review(text, model, tokenizer, device) for text in df[text_column]]
55
- df["Predicted Label"] = predicted_labels
56
- st.write(df)
57
-
58
- if __name__ == "__main__":
59
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
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)