Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from src.file_loader import load_file
|
3 |
+
from src.rag_pipeline import build_rag_pipeline, get_relevant_docs
|
4 |
+
from src.model_utils import load_hf_model, generate_answer
|
5 |
+
from src.utils import get_font_css
|
6 |
+
|
7 |
+
st.set_page_config(page_title="AI Chatbot", page_icon=":robot_face:", layout="wide")
|
8 |
+
st.markdown(get_font_css(), unsafe_allow_html=True)
|
9 |
+
|
10 |
+
st.sidebar.image("assets/logo.png", width=180)
|
11 |
+
st.sidebar.title("AI Chatbot")
|
12 |
+
st.sidebar.markdown("Upload a file to get started:")
|
13 |
+
|
14 |
+
uploaded_file = st.sidebar.file_uploader(
|
15 |
+
"Upload PDF, CSV, or XLSX", type=["pdf", "csv", "xlsx"]
|
16 |
+
)
|
17 |
+
|
18 |
+
model_name = st.sidebar.text_input(
|
19 |
+
"HuggingFace Model (text-generation)", value="amiguel/GM_Qwen1.8B_Finetune"
|
20 |
+
)
|
21 |
+
embedding_model = st.sidebar.text_input(
|
22 |
+
"Embedding Model", value="sentence-transformers/all-MiniLM-L6-v2"
|
23 |
+
)
|
24 |
+
|
25 |
+
st.sidebar.markdown("---")
|
26 |
+
st.sidebar.markdown("Powered by [Your Company]")
|
27 |
+
|
28 |
+
st.markdown(
|
29 |
+
"""
|
30 |
+
<div style="display: flex; align-items: center; margin-bottom: 1rem;">
|
31 |
+
<img src="app/assets/logo.png" width="60" style="margin-right: 1rem;">
|
32 |
+
<h1 style="font-family: 'Tw Cen MT', sans-serif; margin: 0;">AI Chatbot</h1>
|
33 |
+
</div>
|
34 |
+
""",
|
35 |
+
unsafe_allow_html=True,
|
36 |
+
)
|
37 |
+
|
38 |
+
if uploaded_file:
|
39 |
+
with st.spinner("Processing file..."):
|
40 |
+
text = load_file(uploaded_file)
|
41 |
+
docs = [{"page_content": chunk, "metadata": {}} for chunk in text]
|
42 |
+
retriever = build_rag_pipeline(docs, embedding_model)
|
43 |
+
st.success("File processed and indexed!")
|
44 |
+
|
45 |
+
with st.spinner("Loading model..."):
|
46 |
+
text_gen = load_hf_model(model_name)
|
47 |
+
st.success("Model loaded!")
|
48 |
+
|
49 |
+
if "chat_history" not in st.session_state:
|
50 |
+
st.session_state.chat_history = []
|
51 |
+
|
52 |
+
user_input = st.text_input("Ask a question about your document:", key="user_input")
|
53 |
+
if st.button("Send", use_container_width=True) and user_input:
|
54 |
+
with st.spinner("Generating answer..."):
|
55 |
+
context_docs = get_relevant_docs(retriever, user_input)
|
56 |
+
context = " ".join([doc["page_content"] for doc in context_docs])
|
57 |
+
answer = generate_answer(text_gen, user_input, context)
|
58 |
+
st.session_state.chat_history.append(("user", user_input))
|
59 |
+
st.session_state.chat_history.append(("bot", answer))
|
60 |
+
|
61 |
+
for sender, msg in st.session_state.chat_history:
|
62 |
+
if sender == "user":
|
63 |
+
st.markdown(
|
64 |
+
f"""
|
65 |
+
<div style="background: #e6f0fa; border-radius: 10px; padding: 10px; margin-bottom: 5px; text-align: right; font-family: 'Tw Cen MT', sans-serif;">
|
66 |
+
<b>You:</b> {msg}
|
67 |
+
</div>
|
68 |
+
""",
|
69 |
+
unsafe_allow_html=True,
|
70 |
+
)
|
71 |
+
else:
|
72 |
+
st.markdown(
|
73 |
+
f"""
|
74 |
+
<div style="background: #f4f4f4; border-radius: 10px; padding: 10px; margin-bottom: 10px; text-align: left; font-family: 'Tw Cen MT', sans-serif;">
|
75 |
+
<b>AI:</b> {msg}
|
76 |
+
</div>
|
77 |
+
""",
|
78 |
+
unsafe_allow_html=True,
|
79 |
+
)
|
80 |
+
else:
|
81 |
+
st.info("Please upload a PDF, CSV, or XLSX file to begin.")
|