Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- .env +1 -0
- .gitignore +84 -0
- app.py +115 -0
- main.ipynb +0 -0
- requirements.txt +15 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GROQ_API_KEY=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
|
.gitignore
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Python
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.py[cod]
|
| 5 |
+
*$py.class
|
| 6 |
+
*.so
|
| 7 |
+
.Python
|
| 8 |
+
build/
|
| 9 |
+
develop-eggs/
|
| 10 |
+
dist/
|
| 11 |
+
downloads/
|
| 12 |
+
eggs/
|
| 13 |
+
.eggs/
|
| 14 |
+
lib/
|
| 15 |
+
lib64/
|
| 16 |
+
parts/
|
| 17 |
+
sdist/
|
| 18 |
+
var/
|
| 19 |
+
wheels/
|
| 20 |
+
*.egg-info/
|
| 21 |
+
.installed.cfg
|
| 22 |
+
*.egg
|
| 23 |
+
|
| 24 |
+
# Virtual Environment
|
| 25 |
+
.env
|
| 26 |
+
.venv
|
| 27 |
+
env/
|
| 28 |
+
venv/
|
| 29 |
+
ENV/
|
| 30 |
+
|
| 31 |
+
# Jupyter Notebook
|
| 32 |
+
.ipynb_checkpoints
|
| 33 |
+
*/.ipynb_checkpoints/*
|
| 34 |
+
|
| 35 |
+
# VS Code
|
| 36 |
+
.vscode/
|
| 37 |
+
*.code-workspace
|
| 38 |
+
|
| 39 |
+
# Environment variables
|
| 40 |
+
.env
|
| 41 |
+
|
| 42 |
+
# Model files and vectors
|
| 43 |
+
vectorDB/
|
| 44 |
+
embeddings/
|
| 45 |
+
*.bin
|
| 46 |
+
*.pkl
|
| 47 |
+
*.h5
|
| 48 |
+
*.faiss
|
| 49 |
+
*.index
|
| 50 |
+
|
| 51 |
+
# Logs
|
| 52 |
+
*.log
|
| 53 |
+
logs/
|
| 54 |
+
log/
|
| 55 |
+
|
| 56 |
+
# Data
|
| 57 |
+
data/
|
| 58 |
+
*.csv
|
| 59 |
+
*.json
|
| 60 |
+
*.xlsx
|
| 61 |
+
|
| 62 |
+
# Operating System
|
| 63 |
+
.DS_Store
|
| 64 |
+
Thumbs.db
|
| 65 |
+
|
| 66 |
+
# IDE specific files
|
| 67 |
+
.idea/
|
| 68 |
+
*.swp
|
| 69 |
+
*.swo
|
| 70 |
+
.spyderproject
|
| 71 |
+
.spyproject
|
| 72 |
+
|
| 73 |
+
# Large media files
|
| 74 |
+
*.jpg
|
| 75 |
+
*.jpeg
|
| 76 |
+
*.png
|
| 77 |
+
*.gif
|
| 78 |
+
*.pdf
|
| 79 |
+
*.mp4
|
| 80 |
+
*.mov
|
| 81 |
+
|
| 82 |
+
# Local development settings
|
| 83 |
+
local_settings.py
|
| 84 |
+
settings_local.py
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from environs import Env
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain_groq import ChatGroq
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
# Load resources and initialize environment
|
| 11 |
+
def load_resources():
|
| 12 |
+
parser = StrOutputParser()
|
| 13 |
+
env = Env()
|
| 14 |
+
env.read_env(".env")
|
| 15 |
+
|
| 16 |
+
api_key = env("GROQ_API_KEY")
|
| 17 |
+
chat = ChatGroq(temperature=0.4, model_name="mixtral-8x7b-32768")
|
| 18 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
vector_store = FAISS.load_local("vectorDB", embeddings, allow_dangerous_deserialization=True)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
st.error(f"Error loading vector store: {e}")
|
| 24 |
+
return None, None
|
| 25 |
+
|
| 26 |
+
template = """
|
| 27 |
+
You are 'AskIIIT', a reliable and trustworthy AI assistant specifically designed
|
| 28 |
+
to answer questions about IIITDMJ, developed by Prince Deepak Siddharth,
|
| 29 |
+
who is an undergrad at IIITDMJ pursuing BTech in CSE of 2023 batch.
|
| 30 |
+
Your responses must be strictly based on the provided context.
|
| 31 |
+
|
| 32 |
+
- Do not provide information beyond the context.
|
| 33 |
+
- If the context does not cover the question, respond with:
|
| 34 |
+
"I don't have enough information about this. Please visit www.iiitdmj.ac.in for more details."
|
| 35 |
+
- Avoid assumptions, speculations, or hallucinations.
|
| 36 |
+
|
| 37 |
+
Ensure clarity, accuracy, and relevance in your responses.
|
| 38 |
+
|
| 39 |
+
Context: {context}
|
| 40 |
+
|
| 41 |
+
Question: {question}
|
| 42 |
+
"""
|
| 43 |
+
prompt = ChatPromptTemplate.from_template(template)
|
| 44 |
+
llm_chain = prompt | chat | parser
|
| 45 |
+
return llm_chain, vector_store
|
| 46 |
+
|
| 47 |
+
# Function to get assistant response
|
| 48 |
+
def get_assistant_response(user_query, vector_store, llm_chain):
|
| 49 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
|
| 50 |
+
retrieved_docs = retriever.invoke(user_query)
|
| 51 |
+
context = "\n\n".join([doc.page_content for doc in retrieved_docs])
|
| 52 |
+
input_data = {"context": context, "question": user_query}
|
| 53 |
+
assistant_response = llm_chain.invoke(input_data)
|
| 54 |
+
return assistant_response
|
| 55 |
+
|
| 56 |
+
# Function to display typing animation for assistant response
|
| 57 |
+
def display_typing_animation(response_text):
|
| 58 |
+
response_placeholder = st.empty()
|
| 59 |
+
typing_speed = 00.01 # Adjust typing speed as needed
|
| 60 |
+
displayed_text = ""
|
| 61 |
+
|
| 62 |
+
for char in response_text:
|
| 63 |
+
displayed_text += char
|
| 64 |
+
response_placeholder.write(displayed_text)
|
| 65 |
+
time.sleep(typing_speed)
|
| 66 |
+
|
| 67 |
+
return response_placeholder
|
| 68 |
+
|
| 69 |
+
# Load resources
|
| 70 |
+
llm_chain, vector_store = load_resources()
|
| 71 |
+
|
| 72 |
+
# Streamlit UI
|
| 73 |
+
st.set_page_config(
|
| 74 |
+
page_title="AskIIIT",
|
| 75 |
+
layout="centered"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Add the IIITDMJ logo and heading
|
| 79 |
+
logo_path = "photo\iiitdmjLOGO.jpeg" # Replace with the path to your logo file
|
| 80 |
+
col1, col2 = st.columns([1, 4]) # Adjust column widths as needed
|
| 81 |
+
with col1:
|
| 82 |
+
st.image(logo_path, width=50) # Adjust width as per your requirement
|
| 83 |
+
with col2:
|
| 84 |
+
st.title("AskIIIT")
|
| 85 |
+
st.caption("Your AI-Powered IIITDMJ Knowledge Companion")
|
| 86 |
+
|
| 87 |
+
if vector_store is None:
|
| 88 |
+
st.error("Failed to load vector store. Please check your setup.")
|
| 89 |
+
else:
|
| 90 |
+
# Initialize session state for chat history
|
| 91 |
+
if "chat_history" not in st.session_state:
|
| 92 |
+
st.session_state["chat_history"] = []
|
| 93 |
+
|
| 94 |
+
# Display previous chat history
|
| 95 |
+
for user_message, assistant_message in st.session_state["chat_history"]:
|
| 96 |
+
st.chat_message("user").write(user_message)
|
| 97 |
+
st.chat_message("assistant").write(assistant_message)
|
| 98 |
+
|
| 99 |
+
# Input field for user queries
|
| 100 |
+
user_query = st.chat_input("Ask me anything about IIITDMJ!")
|
| 101 |
+
if user_query:
|
| 102 |
+
# Immediately show the user's query in the chat
|
| 103 |
+
st.chat_message("user").write(user_query)
|
| 104 |
+
|
| 105 |
+
# Add a placeholder for assistant's response
|
| 106 |
+
assistant_placeholder = st.chat_message("assistant").empty()
|
| 107 |
+
|
| 108 |
+
# Get assistant response
|
| 109 |
+
assistant_response = get_assistant_response(user_query, vector_store, llm_chain)
|
| 110 |
+
|
| 111 |
+
# Show assistant's response with typing animation
|
| 112 |
+
display_typing_animation(assistant_response)
|
| 113 |
+
|
| 114 |
+
# Add the query and response to the chat history
|
| 115 |
+
st.session_state["chat_history"].append((user_query, assistant_response))
|
main.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain>=0.1.0
|
| 2 |
+
langchain-core>=0.1.0
|
| 3 |
+
langchain-community>=0.1.0
|
| 4 |
+
langchain-groq>=0.1.0
|
| 5 |
+
langchain-huggingface>=0.1.0
|
| 6 |
+
streamlit>=1.30.0
|
| 7 |
+
environs>=9.5.0
|
| 8 |
+
faiss-cpu>=1.7.4
|
| 9 |
+
sentence-transformers>=2.2.2
|
| 10 |
+
torch>=2.1.0
|
| 11 |
+
transformers>=4.36.0
|
| 12 |
+
gradio>=4.0.0
|
| 13 |
+
python-dotenv>=1.0.0
|
| 14 |
+
tqdm>=4.66.1
|
| 15 |
+
ipywidgets>=8.0.0
|