File size: 3,471 Bytes
7bbea06
d1a57bc
 
 
 
7bbea06
d1a57bc
 
 
 
 
 
 
fccb3d2
d1a57bc
 
 
 
 
 
 
fccb3d2
 
 
 
 
 
d1a57bc
6899cb0
fccb3d2
d1a57bc
 
 
 
 
 
 
 
 
 
fccb3d2
 
 
 
 
d1a57bc
 
 
fccb3d2
 
 
 
 
 
d1a57bc
 
 
 
 
 
 
 
 
fccb3d2
7bbea06
fccb3d2
7bbea06
d1a57bc
 
 
 
 
 
fccb3d2
d1a57bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fccb3d2
d1a57bc
fccb3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1a57bc
 
 
 
 
7bbea06
fccb3d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import streamlit as st
import pandas as pd
import numpy as np
import faiss
import os

from sentence_transformers import SentenceTransformer
from huggingface_hub import InferenceClient

# ==============================
# CONFIG
# ==============================
st.set_page_config(page_title="Company ChatGPT", layout="wide")
st.title("🏒 Company AI Assistant (RAG Powered)")

# ==============================
# LOAD MODELS
# ==============================
@st.cache_resource
def load_models():
    embed_model = SentenceTransformer("all-MiniLM-L6-v2")

    HF_TOKEN = os.environ.get("HF_TOKEN")
    if not HF_TOKEN:
        st.error("❌ Please add HF_TOKEN in Hugging Face Secrets")
        st.stop()

    llm = InferenceClient(
        model="meta-llama/Meta-Llama-3-8B-Instruct",
        token=HF_TOKEN
    )
    return embed_model, llm

embed_model, llm = load_models()

# ==============================
# LOAD DATA
# ==============================
@st.cache_data
def load_data():
    path = "src/company_sample.csv"
    if not os.path.exists(path):
        st.error(f"❌ File not found: {path}")
        st.stop()
    df = pd.read_csv(path)
    return df

df = load_data()

if "text" not in df.columns:
    st.error("❌ CSV must contain 'text' column")
    st.stop()

documents = df["text"].fillna("").tolist()

# ==============================
# CREATE VECTOR DB
# ==============================
@st.cache_resource
def create_faiss(docs):
    embeddings = embed_model.encode(docs)
    index = faiss.IndexFlatL2(embeddings.shape[1])
    index.add(np.array(embeddings))
    return index

index = create_faiss(documents)

# ==============================
# RETRIEVAL FUNCTION
# ==============================
def retrieve(query, top_k=3):
    q_emb = embed_model.encode([query])
    D, I = index.search(np.array(q_emb), top_k)
    return [documents[i] for i in I[0] if i < len(documents)]

# ==============================
# CHAT HISTORY
# ==============================
if "messages" not in st.session_state:
    st.session_state.messages = []

for msg in st.session_state.messages:
    st.chat_message(msg["role"]).write(msg["content"])

# ==============================
# USER INPUT
# ==============================
query = st.chat_input("Ask about company...")

if query:
    st.session_state.messages.append({"role": "user", "content": query})
    st.chat_message("user").write(query)

    # πŸ” Retrieve context
    context_docs = retrieve(query)
    context = "\n\n".join(context_docs)

    # ==============================
    # πŸ€– LLM CALL (FIXED)
    # ==============================
    try:
        response = llm.chat_completion(
            messages=[
                {
                    "role": "system",
                    "content": "You are a company assistant. Answer ONLY from given context. If not found, say 'Not available in company data.'"
                },
                {
                    "role": "user",
                    "content": f"""
Context:
{context}

Question:
{query}
"""
                }
            ],
            max_tokens=200,
            temperature=0.5
        )

        answer = response.choices[0].message.content

    except Exception as e:
        answer = f"❌ Error: {str(e)}"

    # ==============================
    # DISPLAY RESPONSE
    # ==============================
    st.session_state.messages.append({"role": "assistant", "content": answer})
    st.chat_message("assistant").write(answer)