Download app.py from Ajeet001/sample: direct link, hf CLI and curl.
- Browser
- Download file 3.92 kB
-
https://huggingface.co/spaces/Ajeet001/sample/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ajeet001/sample/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ajeet001/sample/resolve/main/app.py
3.92 kB
| import os | |
| import streamlit as st | |
| import yaml | |
| from dotenv import load_dotenv | |
| from src.document_loader import load_document | |
| from src.chunking_embedding import setup_chunking_and_embedding | |
| from src.vector_store import create_vectorstore | |
| from langchain_groq import ChatGroq | |
| from langchain.chains import ConversationalRetrievalChain | |
| from langchain.memory import ConversationBufferWindowMemory | |
| from src.utils import log_interaction | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Load configuration | |
| config_path = os.path.join(os.getcwd(), "config", "config.yaml") # Ensure correct path | |
| with open(config_path, "r") as f: | |
| config = yaml.safe_load(f) | |
| # Get the GROQ API key from environment variables | |
| groq_api_key = os.getenv('groq_api_key') | |
| if not groq_api_key: | |
| raise ValueError("GROQ_API_KEY not found. Please set it in the .env file.") | |
| # Streamlit UI | |
| st.set_page_config(page_title="Your Document AI Assistant", page_icon="📄", layout="centered") | |
| st.title("🪅 Document AI Assistant!") | |
| # Sidebar for document upload | |
| uploaded_file = st.sidebar.file_uploader(label="Upload your document (PDF/DOC/DOCX)", type=['pdf', 'doc', 'docx']) | |
| loader_type = st.sidebar.selectbox('Choose a loader type', config['loaders']) | |
| embedding_model = st.sidebar.selectbox('Choose an embedding model', config['embedding_models']) | |
| chunking_strategy = st.sidebar.selectbox('Choose a chunking strategy', config['chunking_strategies']) | |
| chunk_size = st.sidebar.number_input('Chunk Size', min_value=100, value=500, step=100) | |
| chunk_overlap = st.sidebar.number_input('Chunk Overlap', min_value=0, value=100, step=100) | |
| temperature = st.sidebar.slider('Temperature', min_value=0.0, max_value=1.0, value=0.0, step=0.1) | |
| top_p = st.sidebar.slider('Top-p', min_value=0.0, max_value=1.0, value=0.9, step=0.1) | |
| # Initialize session state for conversation | |
| if 'chat_history' not in st.session_state: | |
| st.session_state.chat_history = [] | |
| if 'conversation_chain' not in st.session_state: | |
| st.session_state.conversation_chain = None | |
| # Process the uploaded file | |
| if uploaded_file is not None: | |
| file_path = os.path.join("temp", uploaded_file.name) | |
| os.makedirs("temp", exist_ok=True) | |
| with open(file_path, "wb") as f: | |
| f.write(uploaded_file.getbuffer()) | |
| documents = load_document(file_path, loader_type) | |
| doc_chunks, embeddings = setup_chunking_and_embedding(documents, chunking_strategy, chunk_size, chunk_overlap, embedding_model) | |
| vectorstore = create_vectorstore(doc_chunks, embeddings) | |
| # Create the conversational retrieval chain | |
| llm = ChatGroq(groq_api_key=groq_api_key, model_name='llama-3.3-70b-versatile', temperature=temperature) | |
| retriever = vectorstore.as_retriever() | |
| memory = ConversationBufferWindowMemory(k=5, memory_key="chat_history", return_messages=True) | |
| st.session_state.conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory) | |
| # User input for asking a question | |
| user_input = st.text_input("Ask a question:") | |
| if st.button("Ask Question"): | |
| if user_input: | |
| with st.chat_message("user"): | |
| st.markdown(user_input) | |
| with st.chat_message("assistant"): | |
| response = st.session_state.conversation_chain({"question": user_input}) | |
| assistant_response = response['answer'] | |
| st.markdown(assistant_response) | |
| st.session_state.chat_history.append({"role": "assistant", "content": assistant_response}) | |
| # Log interaction | |
| params = { | |
| "loader": loader_type, | |
| "chunking_strategy": chunking_strategy, | |
| "chunk_size": chunk_size, | |
| "chunk_overlap": chunk_overlap, | |
| "embedding_model": embedding_model, | |
| "temperature": temperature, | |
| "top_p": top_p | |
| } | |
| log_interaction(user_input, assistant_response, params) | |