pradeep4321 commited on
Commit
d1a57bc
ยท
verified ยท
1 Parent(s): ddf3e90

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +99 -34
src/streamlit_app.py CHANGED
@@ -1,40 +1,105 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  """
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import faiss
5
+ import os
6
 
7
+ from sentence_transformers import SentenceTransformer
8
+ from huggingface_hub import InferenceClient
9
+
10
+ # ==============================
11
+ # CONFIG
12
+ # ==============================
13
+ st.set_page_config(page_title="Company ChatGPT", layout="wide")
14
+ st.title("๐Ÿข Company AI Assistant")
15
+
16
+ # ==============================
17
+ # LOAD MODELS
18
+ # ==============================
19
+ @st.cache_resource
20
+ def load_models():
21
+ embed_model = SentenceTransformer("all-MiniLM-L6-v2")
22
+ llm = InferenceClient(
23
+ model="meta-llama/Llama-3-8b-instruct",
24
+ token=os.environ.get("HF_TOKEN")
25
+ )
26
+ return embed_model, llm
27
+
28
+ embed_model, llm = load_models()
29
+
30
+ # ==============================
31
+ # LOAD DATA
32
+ # ==============================
33
+ @st.cache_data
34
+ def load_data():
35
+ df = pd.read_csv("data/company_docs.csv")
36
+ return df
37
+
38
+ df = load_data()
39
+ documents = df["text"].tolist()
40
+
41
+ # ==============================
42
+ # CREATE VECTOR DB
43
+ # ==============================
44
+ @st.cache_resource
45
+ def create_faiss(docs):
46
+ embeddings = embed_model.encode(docs)
47
+ index = faiss.IndexFlatL2(embeddings.shape[1])
48
+ index.add(np.array(embeddings))
49
+ return index, embeddings
50
 
51
+ index, doc_embeddings = create_faiss(documents)
 
 
52
 
53
+ # ==============================
54
+ # RETRIEVAL FUNCTION
55
+ # ==============================
56
+ def retrieve(query, top_k=3):
57
+ q_emb = embed_model.encode([query])
58
+ D, I = index.search(np.array(q_emb), top_k)
59
+ return [documents[i] for i in I[0]]
60
+
61
+ # ==============================
62
+ # CHAT HISTORY
63
+ # ==============================
64
+ if "messages" not in st.session_state:
65
+ st.session_state.messages = []
66
+
67
+ # Display history
68
+ for msg in st.session_state.messages:
69
+ st.chat_message(msg["role"]).write(msg["content"])
70
+
71
+ # ==============================
72
+ # USER INPUT
73
+ # ==============================
74
+ query = st.chat_input("Ask about company...")
75
+
76
+ if query:
77
+ st.session_state.messages.append({"role": "user", "content": query})
78
+ st.chat_message("user").write(query)
79
+
80
+ # ๐Ÿ” Retrieve relevant docs
81
+ context_docs = retrieve(query)
82
+ context = "\n".join(context_docs)
83
+
84
+ # ๐Ÿง  Build prompt
85
+ prompt = f"""
86
+ You are a company assistant. Answer ONLY based on the context below.
87
+
88
+ Context:
89
+ {context}
90
+
91
+ Question:
92
+ {query}
93
+
94
+ Answer:
95
  """
96
 
97
+ # ๐Ÿค– LLM Call
98
+ response = llm.text_generation(
99
+ prompt,
100
+ max_new_tokens=200,
101
+ temperature=0.5
102
+ )
103
+
104
+ st.session_state.messages.append({"role": "assistant", "content": response})
105
+ st.chat_message("assistant").write(response)