Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
| 4 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings # FIXED: new package
|
| 6 |
+
from langchain_chroma import Chroma
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 10 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 11 |
+
|
| 12 |
+
st.title("📚 MCA Module AI Assistant")
|
| 13 |
+
st.markdown("Ask anything about your Computer Vision modules!")
|
| 14 |
+
|
| 15 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 16 |
+
llm = ChatGroq(model="llama-3.2-3b-preview", groq_api_key=groq_api_key)
|
| 17 |
+
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def prepare_vectorstore():
|
| 20 |
+
loader = PyPDFDirectoryLoader("mca modules/")
|
| 21 |
+
docs = loader.load()
|
| 22 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 23 |
+
splits = text_splitter.split_documents(docs)
|
| 24 |
+
|
| 25 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 26 |
+
|
| 27 |
+
return Chroma.from_documents(
|
| 28 |
+
documents=splits,
|
| 29 |
+
embedding=embeddings,
|
| 30 |
+
persist_directory="/tmp/chroma_db" # FIXED: writable path on HF Spaces
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
vectorstore = prepare_vectorstore()
|
| 34 |
+
|
| 35 |
+
template = """You are an expert AI Assistant. Use the provided context to answer the question.
|
| 36 |
+
Rules:
|
| 37 |
+
1. If the user asks for a comparison or specific categories, use a Markdown TABLE.
|
| 38 |
+
2. Use bolding for key terms.
|
| 39 |
+
3. If the answer isn't in the context, say "I cannot find this in your modules."
|
| 40 |
+
|
| 41 |
+
Context:
|
| 42 |
+
{context}
|
| 43 |
+
|
| 44 |
+
Question: {question}
|
| 45 |
+
"""
|
| 46 |
+
prompt = ChatPromptTemplate.from_template(template)
|
| 47 |
+
rag_chain = (
|
| 48 |
+
{"context": vectorstore.as_retriever(), "question": RunnablePassthrough()}
|
| 49 |
+
| prompt | llm | StrOutputParser()
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if "messages" not in st.session_state:
|
| 53 |
+
st.session_state.messages = []
|
| 54 |
+
|
| 55 |
+
for message in st.session_state.messages:
|
| 56 |
+
with st.chat_message(message["role"]):
|
| 57 |
+
st.markdown(message["content"])
|
| 58 |
+
|
| 59 |
+
if prompt_input := st.chat_input("What is covered in Module 1?"):
|
| 60 |
+
st.session_state.messages.append({"role": "user", "content": prompt_input})
|
| 61 |
+
with st.chat_message("user"):
|
| 62 |
+
st.markdown(prompt_input)
|
| 63 |
+
|
| 64 |
+
with st.chat_message("assistant"):
|
| 65 |
+
response = rag_chain.invoke(prompt_input)
|
| 66 |
+
st.markdown(response)
|
| 67 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|