|
import streamlit as st |
|
import os |
|
import time |
|
import PyPDF2 |
|
from docx import Document |
|
import pandas as pd |
|
from dotenv import load_dotenv |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
USER_AVATAR = "https://raw.githubusercontent.com/achilela/vila_fofoka_analysis/9904d9a0d445ab0488cf7395cb863cce7621d897/USER_AVATAR.png" |
|
BOT_AVATAR = "https://raw.githubusercontent.com/achilela/vila_fofoka_analysis/991f4c6e4e1dc7a8e24876ca5aae5228bcdb4dba/Ataliba_Avatar.jpg" |
|
|
|
ATALIBA_BIO = """ |
|
**I am Ataliba Miguel's Digital Twin** π€ |
|
|
|
**Background:** |
|
- π Mechanical Engineering (BSc) |
|
- β½ Oil & Gas Engineering (MSc Specialization) |
|
- π§ 17+ years in Oil & Gas Industry |
|
- π Current: Topside Inspection Methods Engineer @ TotalEnergies |
|
- π€ AI Practitioner Specialist |
|
- π Founder of ValonyLabs (AI solutions for industrial corrosion, retail analytics, and KPI monitoring) |
|
|
|
**Capabilities:** |
|
- Technical document analysis |
|
- Engineering insights |
|
- AI-powered problem solving |
|
- Cross-domain knowledge integration |
|
|
|
Ask me about engineering challenges, AI applications, or industry best practices! |
|
""" |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
@import url('https://fonts.cdnfonts.com/css/tw-cen-mt'); |
|
* { font-family: 'Tw Cen MT', sans-serif; } |
|
.st-emotion-cache-1y4p8pa { padding: 2rem 1rem; } |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
st.title("π Ataliba o Agent Nerdx π") |
|
|
|
|
|
with st.sidebar: |
|
st.header("β‘οΈ Hugging Face Model Loaded") |
|
st.markdown("Model: amiguel/unsloth_finetune_test") |
|
uploaded_file = st.file_uploader("Upload technical documents", type=["pdf", "docx", "xlsx", "xlsm"]) |
|
|
|
|
|
if "file_context" not in st.session_state: |
|
st.session_state.file_context = None |
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = [] |
|
|
|
|
|
def parse_file(file): |
|
try: |
|
if file.type == "application/pdf": |
|
reader = PyPDF2.PdfReader(file) |
|
return "\n".join([page.extract_text() for page in reader.pages]) |
|
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": |
|
doc = Document(file) |
|
return "\n".join([para.text for para in doc.paragraphs]) |
|
elif file.type in ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-excel"]: |
|
df = pd.read_excel(file) |
|
return df.to_string() |
|
except Exception as e: |
|
st.error(f"Error processing file: {str(e)}") |
|
return None |
|
|
|
|
|
if uploaded_file and not st.session_state.file_context: |
|
st.session_state.file_context = parse_file(uploaded_file) |
|
if st.session_state.file_context: |
|
st.sidebar.success("β
Document loaded successfully") |
|
|
|
|
|
@st.cache_resource |
|
def load_custom_model(): |
|
model_name = "amiguel/unsloth_finetune_test" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
return pipeline("text-classification", model=model, tokenizer=tokenizer) |
|
|
|
|
|
def generate_response(prompt): |
|
bio_triggers = ['who are you', 'ataliba', 'yourself', 'skilled at', |
|
'background', 'experience', 'valonylabs', 'totalenergies'] |
|
|
|
if any(trigger in prompt.lower() for trigger in bio_triggers): |
|
for line in ATALIBA_BIO.split('\n'): |
|
yield line + '\n' |
|
time.sleep(0.1) |
|
return |
|
|
|
try: |
|
classifier = load_custom_model() |
|
result = classifier(prompt)[0] |
|
label = result['label'] |
|
score = result['score'] |
|
context = st.session_state.file_context or "No document loaded." |
|
|
|
response_text = f"\nπ **Prediction**: `{label}`\nπ **Confidence**: `{score:.2%}`\nποΈ **Context**: `{context[:300]}...`" |
|
for line in response_text.split('\n'): |
|
yield line + '\n' |
|
time.sleep(0.1) |
|
|
|
except Exception as e: |
|
yield f"β οΈ Model Error: {str(e)}" |
|
|
|
|
|
for msg in st.session_state.chat_history: |
|
with st.chat_message(msg["role"], avatar=USER_AVATAR if msg["role"] == "user" else BOT_AVATAR): |
|
st.markdown(msg["content"]) |
|
|
|
if prompt := st.chat_input("Ask about documents or technical matters..."): |
|
st.session_state.chat_history.append({"role": "user", "content": prompt}) |
|
with st.chat_message("user", avatar=USER_AVATAR): |
|
st.markdown(prompt) |
|
|
|
with st.chat_message("assistant", avatar=BOT_AVATAR): |
|
response_placeholder = st.empty() |
|
full_response = "" |
|
|
|
for chunk in generate_response(prompt): |
|
full_response += chunk |
|
response_placeholder.markdown(full_response + "β") |
|
|
|
response_placeholder.markdown(full_response) |
|
st.session_state.chat_history.append({"role": "assistant", "content": full_response}) |
|
|