Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,8 @@ import PyPDF2
|
|
5 |
from docx import Document
|
6 |
import pandas as pd
|
7 |
from dotenv import load_dotenv
|
8 |
-
from
|
|
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
@@ -48,7 +49,7 @@ st.title("🚀 Ataliba o Agent Nerdx 🚀")
|
|
48 |
# Sidebar
|
49 |
with st.sidebar:
|
50 |
st.header("⚡️ Hugging Face Model Loaded")
|
51 |
-
st.markdown("Model: amiguel/unsloth_finetune_test")
|
52 |
uploaded_file = st.file_uploader("Upload technical documents", type=["pdf", "docx", "xlsx", "xlsm"])
|
53 |
|
54 |
# Session state
|
@@ -81,17 +82,23 @@ if uploaded_file and not st.session_state.file_context:
|
|
81 |
|
82 |
# Load model
|
83 |
@st.cache_resource
|
84 |
-
def
|
85 |
-
|
86 |
-
tokenizer =
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# Generate response
|
91 |
def generate_response(prompt):
|
92 |
bio_triggers = ['who are you', 'ataliba', 'yourself', 'skilled at',
|
93 |
'background', 'experience', 'valonylabs', 'totalenergies']
|
94 |
-
|
95 |
if any(trigger in prompt.lower() for trigger in bio_triggers):
|
96 |
for line in ATALIBA_BIO.split('\n'):
|
97 |
yield line + '\n'
|
@@ -99,16 +106,17 @@ def generate_response(prompt):
|
|
99 |
return
|
100 |
|
101 |
try:
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
110 |
yield line + '\n'
|
111 |
-
time.sleep(0.
|
112 |
|
113 |
except Exception as e:
|
114 |
yield f"⚠️ Model Error: {str(e)}"
|
|
|
5 |
from docx import Document
|
6 |
import pandas as pd
|
7 |
from dotenv import load_dotenv
|
8 |
+
from unsloth import FastLanguageModel
|
9 |
+
from transformers import AutoTokenizer
|
10 |
|
11 |
# Load environment variables
|
12 |
load_dotenv()
|
|
|
49 |
# Sidebar
|
50 |
with st.sidebar:
|
51 |
st.header("⚡️ Hugging Face Model Loaded")
|
52 |
+
st.markdown("Model: `amiguel/unsloth_finetune_test` with LoRA")
|
53 |
uploaded_file = st.file_uploader("Upload technical documents", type=["pdf", "docx", "xlsx", "xlsm"])
|
54 |
|
55 |
# Session state
|
|
|
82 |
|
83 |
# Load model
|
84 |
@st.cache_resource
|
85 |
+
def load_unsloth_model():
|
86 |
+
base_model = "unsloth/llama-3-8b-Instruct-bnb-4bit"
|
87 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
88 |
+
model_name=base_model,
|
89 |
+
max_seq_length=2048,
|
90 |
+
dtype=None,
|
91 |
+
load_in_4bit=True,
|
92 |
+
adapter_path="amiguel/unsloth_finetune_test"
|
93 |
+
)
|
94 |
+
FastLanguageModel.for_inference(model)
|
95 |
+
return model, tokenizer
|
96 |
|
97 |
# Generate response
|
98 |
def generate_response(prompt):
|
99 |
bio_triggers = ['who are you', 'ataliba', 'yourself', 'skilled at',
|
100 |
'background', 'experience', 'valonylabs', 'totalenergies']
|
101 |
+
|
102 |
if any(trigger in prompt.lower() for trigger in bio_triggers):
|
103 |
for line in ATALIBA_BIO.split('\n'):
|
104 |
yield line + '\n'
|
|
|
106 |
return
|
107 |
|
108 |
try:
|
109 |
+
model, tokenizer = load_unsloth_model()
|
110 |
+
context = st.session_state.file_context or ""
|
111 |
+
full_prompt = f"You are an expert in Angolan labor law. Use the context to answer precisely.\nContext: {context}\n\nQuestion: {prompt}"
|
112 |
+
|
113 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
114 |
+
outputs = model.generate(**inputs, max_new_tokens=256, do_sample=False)
|
115 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
116 |
+
|
117 |
+
for line in response.split('\n'):
|
118 |
yield line + '\n'
|
119 |
+
time.sleep(0.05)
|
120 |
|
121 |
except Exception as e:
|
122 |
yield f"⚠️ Model Error: {str(e)}"
|