Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gc
|
3 |
+
import tempfile
|
4 |
+
import uuid
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
from cerebras.cloud.sdk import Cerebras
|
8 |
+
from llama_index.core import Settings
|
9 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
10 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
11 |
+
from llama_index.readers.docling import DoclingReader
|
12 |
+
from llama_index.core.node_parser import MarkdownNodeParser
|
13 |
+
|
14 |
+
import streamlit as st
|
15 |
+
|
16 |
+
if "id" not in st.session_state:
|
17 |
+
st.session_state.id = uuid.uuid4()
|
18 |
+
st.session_state.file_cache = {}
|
19 |
+
|
20 |
+
session_id = st.session_state.id
|
21 |
+
|
22 |
+
@st.cache_resource
|
23 |
+
def load_llm():
|
24 |
+
# Initialize Cerebras client
|
25 |
+
client = Cerebras(api_key=os.environ.get("CEREBRAS_API_KEY"))
|
26 |
+
return client
|
27 |
+
|
28 |
+
def query_cerebras(client, prompt, max_tokens=1024, temperature=0.2, top_p=1):
|
29 |
+
# Query Cerebras model
|
30 |
+
stream = client.chat.completions.create(
|
31 |
+
messages=[{"role": "user", "content": prompt}],
|
32 |
+
model="llama-3.3-70b",
|
33 |
+
stream=True,
|
34 |
+
max_completion_tokens=max_tokens,
|
35 |
+
temperature=temperature,
|
36 |
+
top_p=top_p,
|
37 |
+
)
|
38 |
+
return stream
|
39 |
+
|
40 |
+
def reset_chat():
|
41 |
+
st.session_state.messages = []
|
42 |
+
gc.collect()
|
43 |
+
|
44 |
+
def display_excel(file):
|
45 |
+
st.markdown("### Excel Preview")
|
46 |
+
# Read the Excel file
|
47 |
+
df = pd.read_excel(file)
|
48 |
+
# Display the dataframe
|
49 |
+
st.dataframe(df)
|
50 |
+
|
51 |
+
with st.sidebar:
|
52 |
+
st.header(f"Add your documents!")
|
53 |
+
|
54 |
+
uploaded_file = st.file_uploader("Choose your `.xlsx` file", type=["xlsx", "xls"])
|
55 |
+
|
56 |
+
if uploaded_file:
|
57 |
+
try:
|
58 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
59 |
+
file_path = os.path.join(temp_dir, uploaded_file.name)
|
60 |
+
|
61 |
+
with open(file_path, "wb") as f:
|
62 |
+
f.write(uploaded_file.getvalue())
|
63 |
+
|
64 |
+
file_key = f"{session_id}-{uploaded_file.name}"
|
65 |
+
st.write("Indexing your document...")
|
66 |
+
|
67 |
+
if file_key not in st.session_state.get('file_cache', {}):
|
68 |
+
|
69 |
+
if os.path.exists(temp_dir):
|
70 |
+
reader = DoclingReader()
|
71 |
+
loader = SimpleDirectoryReader(
|
72 |
+
input_dir=temp_dir,
|
73 |
+
file_extractor={".xlsx": reader},
|
74 |
+
)
|
75 |
+
else:
|
76 |
+
st.error('Could not find the file you uploaded, please check again...')
|
77 |
+
st.stop()
|
78 |
+
|
79 |
+
docs = loader.load_data()
|
80 |
+
|
81 |
+
# Setup embedding model
|
82 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-large-en-v1.5", trust_remote_code=True)
|
83 |
+
Settings.embed_model = embed_model
|
84 |
+
|
85 |
+
# Creating an index over loaded data
|
86 |
+
node_parser = MarkdownNodeParser()
|
87 |
+
index = VectorStoreIndex.from_documents(documents=docs, transformations=[node_parser], show_progress=True)
|
88 |
+
|
89 |
+
# Store index as query engine
|
90 |
+
query_engine = index.as_query_engine(streaming=True)
|
91 |
+
|
92 |
+
st.session_state.file_cache[file_key] = query_engine
|
93 |
+
else:
|
94 |
+
query_engine = st.session_state.file_cache[file_key]
|
95 |
+
|
96 |
+
# Inform the user that the file is processed and display the Excel uploaded
|
97 |
+
st.success("Ready to Chat!")
|
98 |
+
display_excel(uploaded_file)
|
99 |
+
except Exception as e:
|
100 |
+
st.error(f"An error occurred: {e}")
|
101 |
+
st.stop()
|
102 |
+
|
103 |
+
col1, col2 = st.columns([6, 1])
|
104 |
+
|
105 |
+
with col1:
|
106 |
+
st.header(f"RAG over Excel using Dockling 🐥 & Llama-3.3-70B")
|
107 |
+
|
108 |
+
with col2:
|
109 |
+
st.button("Clear ↺", on_click=reset_chat)
|
110 |
+
|
111 |
+
# Initialize chat history
|
112 |
+
if "messages" not in st.session_state:
|
113 |
+
reset_chat()
|
114 |
+
|
115 |
+
# Initialize LLM client
|
116 |
+
client = load_llm()
|
117 |
+
|
118 |
+
# Display chat messages from history on app rerun
|
119 |
+
for message in st.session_state.messages:
|
120 |
+
with st.chat_message(message["role"]):
|
121 |
+
st.markdown(message["content"])
|
122 |
+
|
123 |
+
# Accept user input
|
124 |
+
if prompt := st.chat_input("What's up?"):
|
125 |
+
# Add user message to chat history
|
126 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
127 |
+
# Display user message in chat message container
|
128 |
+
with st.chat_message("user"):
|
129 |
+
st.markdown(prompt)
|
130 |
+
|
131 |
+
# Display assistant response in chat message container
|
132 |
+
with st.chat_message("assistant"):
|
133 |
+
message_placeholder = st.empty()
|
134 |
+
full_response = ""
|
135 |
+
|
136 |
+
# Query Cerebras model
|
137 |
+
stream = query_cerebras(client, prompt)
|
138 |
+
|
139 |
+
# Handle streaming response
|
140 |
+
for chunk in stream:
|
141 |
+
content = chunk.choices[0].delta.get("content", "")
|
142 |
+
full_response += content
|
143 |
+
message_placeholder.markdown(full_response + "▌")
|
144 |
+
|
145 |
+
message_placeholder.markdown(full_response)
|
146 |
+
|
147 |
+
# Add assistant response to chat history
|
148 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|