Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GROQ_API_KEY = "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"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myenv/
Binary file added AIML.pdf
Binary file not shown.
Binary file added APP.pdf
Binary file not shown.
Binary file added BLOCKCHAIN.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Submission Details

Submit the following information via the provided Google Form for us to access your AI applications:

Team Name: Your team's unique name.
Team Name: TechDuo

App Name: The name of your AI application.
App Name: Study Sphere

App Description: A brief overview of what your app does and its core features.
App Description: STUDY-SPHERE is an AI-driven learning platform offering personalized domain-specific resources, interactive tools, and real-time support for students and professionals to enhance their knowledge and connect with like-minded communities.

GitHub PR Link: The link to your Pull Request (PR) in the project repository.

App Link: A link to the deployed version of your app.

Loom Video: A demo video link showing your app in action.
Loom Video: [A demo video link showing your app in action.](https://drive.google.com/drive/folders/1i3vVhHjqSb8PRuJB-pnu-UOewQNn6JN5?usp=sharing)


For detailed submission guidelines, please refer to the last doubt session recording: https://www.buildfastwithai.com/hackathon.
Expand Down
Binary file added WEB.pdf
Binary file not shown.
153 changes: 153 additions & 0 deletions a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import streamlit as st
from streamlit_chat import message
from langchain_groq import ChatGroq
from langchain.chains import ConversationalRetrievalChain
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.llms import CTransformers
# from langchain.llms import Replicate
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.memory import ConversationBufferMemory
from langchain.document_loaders import PyPDFLoader
from langchain.prompts import PromptTemplate

from langchain.document_loaders import TextLoader
from langchain.document_loaders import Docx2txtLoader
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
import os
from dotenv import load_dotenv
import tempfile


load_dotenv()

# Create a prompt template
template = """
You are an assistant specialized in analyzing documents. The user will provide questions based on a document.
Please answer the following question using the context provided:

Context: {context}

Question: {question}

Answer in a concise and informative manner.
Try to answer in bullet points
"""

prompt = PromptTemplate(input_variables=["context", "question"], template=template)

def initialize_session_state():
if 'history' not in st.session_state:
st.session_state['history'] = []

if 'generated' not in st.session_state:
st.session_state['generated'] = ["Hello! Ask me anything about 🤗"]

if 'past' not in st.session_state:
st.session_state['past'] = ["Hey! 👋"]

def conversation_chat(query, chain, history):
# Retrieve relevant context from the retriever first
context = chain.retriever.get_relevant_documents(query)

# Format the input with the context using the prompt template
formatted_prompt = prompt.format(context=context, question=query)

# Now pass the formatted prompt to the LLM within the chain
result = chain({"question": formatted_prompt, "chat_history": history})

# Append to the conversation history
history.append((query, result["answer"]))

return result["answer"]


def display_chat_history(chain):
reply_container = st.container()
container = st.container()

with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_input("Question:", placeholder="Ask about your Documents", key='input')
submit_button = st.form_submit_button(label='Send')

if submit_button and user_input:
with st.spinner('Generating response...'):
output = conversation_chat(user_input, chain, st.session_state['history'])

st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)

if st.session_state['generated']:
with reply_container:
for i in range(len(st.session_state['generated'])):
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="thumbs")
message(st.session_state["generated"][i], key=str(i), avatar_style="fun-emoji")

def create_conversational_chain(vector_store):
load_dotenv()
groq_api_key = os.getenv('GROQ_API_KEY')
model = 'mixtral-8x7b-32768' # Or any other model you prefer

# Initialize Groq Langchain chat object
llm = ChatGroq(groq_api_key=groq_api_key, model_name=model)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

# Create the chain without passing the prompt directly
chain = ConversationalRetrievalChain.from_llm(
llm=llm,
chain_type='stuff',
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
memory=memory
)
return chain


def main():
load_dotenv()
# Initialize session state
initialize_session_state()
st.title("Multi-Docs ChatBot using llama2 :books:")
# Initialize Streamlit
st.sidebar.title("Document Processing")
uploaded_files = st.sidebar.file_uploader("Upload files", accept_multiple_files=True)


if uploaded_files:
text = []
for file in uploaded_files:
file_extension = os.path.splitext(file.name)[1]
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(file.read())
temp_file_path = temp_file.name

loader = None
if file_extension == ".pdf":
loader = PyPDFLoader(temp_file_path)
elif file_extension == ".docx" or file_extension == ".doc":
loader = Docx2txtLoader(temp_file_path)
elif file_extension == ".txt":
loader = TextLoader(temp_file_path)

if loader:
text.extend(loader.load())
os.remove(temp_file_path)

text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=100, length_function=len)
text_chunks = text_splitter.split_documents(text)

# Create embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
model_kwargs={'device': 'cpu'})

# Create vector store
vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)

# Create the chain object
chain = create_conversational_chain(vector_store)


display_chat_history(chain)

if __name__ == "__main__":
main()
95 changes: 95 additions & 0 deletions appAIML.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from flask import Flask, request, jsonify
from flask_cors import CORS # Import CORS
from langchain.vectorstores import Qdrant
from langchain.embeddings import HuggingFaceBgeEmbeddings
from langchain_groq import ChatGroq
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationChain
from qdrant_client import QdrantClient
from dotenv import load_dotenv
import os

# Load environment variables
load_dotenv()

# Initialize Flask app
app = Flask(__name__)

# Enable CORS for all routes
CORS(app) # This will allow all origins. You can customize it if needed.

# Initialize Qdrant and embeddings
model_name = "BAAI/bge-large-en"
embeddings = HuggingFaceBgeEmbeddings(
model_name=model_name,
model_kwargs={'device': 'cpu'},
encode_kwargs={'normalize_embeddings': False}
)
url = "http://localhost:6333"
client = QdrantClient(url=url, prefer_grpc=False)

memory = ConversationBufferWindowMemory(k=5)
collection_name = "AIML"
db = Qdrant(client=client, embeddings=embeddings, collection_name=collection_name)

# Initialize Groq LLM
groq_llm = ChatGroq(
groq_api_key=os.getenv('GROQ_API_KEY'),
model='mixtral-8x7b-32768'
)

# Define the prompt template
prompt_template = PromptTemplate(
input_variables=["query", "retrieved_documents"],
template=(
"You are an expert in Artificial Intelligence and Machine Learning (AIML). "
"Your task is to assist users with their queries related to AIML concepts, applications, and recent advancements. "
"You have access to relevant documents that provide detailed information about various AIML topics, algorithms, and case studies.\n\n"
"User Query: {query}\n\n"
"{retrieved_documents}\n\n"
"Your Response:\n"
"- Provide a concise and informative answer based on the user's query and the retrieved documents.\n"
"- Include key definitions, explanations of relevant concepts, and any important points from the retrieved documents.\n"
"- Ensure that the response is accessible to users with varying levels of knowledge about AIML."
)
)

# API endpoint to handle query
@app.route('/aiml_query', methods=['POST'])
def handle_query():
# Get the query from the POST request
data = request.json
query = data.get('query', '')

if not query:
return jsonify({"error": "No query provided"}), 400

# Perform similarity search with context
docs = db.similarity_search_with_score(query=query, k=5)

# Prepare retrieved documents content
retrieved_content = ""
for i, (doc, score) in enumerate(docs):
retrieved_content += f"**Result {i + 1}:**\n"
retrieved_content += f"**Score:** {score}\n"
retrieved_content += f"**Content:** {doc.page_content}\n"
retrieved_content += f"**Metadata:** {doc.metadata}\n"
retrieved_content += "-" * 40 + "\n"

# Format the input using the prompt template
formatted_input = prompt_template.format(query=query, retrieved_documents=retrieved_content)

# Initialize Groq Langchain chat object and conversation
groq_chat = ChatGroq(groq_api_key=os.getenv('GROQ_API_KEY'), model_name='mixtral-8x7b-32768')
conversation = ConversationChain(llm=groq_chat, memory=memory)

# Generate response using the formatted input
response = conversation.run(formatted_input)

# Return the response as JSON
return jsonify({"response": response})

# Run the Flask app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
83 changes: 83 additions & 0 deletions appAPP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from langchain.vectorstores import Qdrant
from langchain.embeddings import HuggingFaceBgeEmbeddings
from langchain_groq import ChatGroq
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationChain
from qdrant_client import QdrantClient
from dotenv import load_dotenv
import os
load_dotenv()

# Initialize Qdrant and embeddings
model_name = "BAAI/bge-large-en"
embeddings = HuggingFaceBgeEmbeddings(
model_name=model_name,
model_kwargs={'device': 'cpu'},
encode_kwargs={'normalize_embeddings': False}
)
url = "http://localhost:6333"

# Load the AIML vector database
client = QdrantClient(
url=url, prefer_grpc=False
)
memory = ConversationBufferWindowMemory(k=5)
collection_name = "APP"
db = Qdrant(client=client , embeddings=embeddings,collection_name=collection_name)

# Initialize Groq LLM
groq_llm = ChatGroq(
groq_api_key = os.environ['GROQ_API_KEY'],
model = 'mixtral-8x7b-32768'
)

# User input for query
query = input("Enter your query: ")

if query:
# Perform similarity search with context
docs = db.similarity_search_with_score(query=query, k=5)

# Display retrieved documents
print("Retrieved Documents:")
retrieved_content = ""
for i, (doc, score) in enumerate(docs):
retrieved_content += f"**Result {i + 1}:**\n"
retrieved_content += f"**Score:** {score}\n"
retrieved_content += f"**Content:** {doc.page_content}\n"
retrieved_content += f"**Metadata:** {doc.metadata}\n"
retrieved_content += "-" * 40 + "\n"

# Define the prompt template
prompt_template = PromptTemplate(
input_variables=["query", "retrieved_documents"],
template=(
"You are an expert in APP DEVELOPMENT. "
"Your task is to assist users with their queries related to APP DEVELOPMENT concepts, applications, and recent advancements. "
"You have access to relevant documents that provide detailed information about various APP DEVELOPMENT topics, algorithms, and case studies.\n\n"
"User Query: {query}\n\n"
"{retrieved_documents}\n\n"
"Your Response:\n"
"- Provide a concise and informative answer based on the user's query and the retrieved documents.\n"
"- Include key definitions, explanations of relevant concepts, and any important points from the retrieved documents.\n"
"- Ensure that the response is accessible to users with varying levels of knowledge about APP DEVELOPMENT."
)
)

# Format the input using the prompt template
formatted_input = prompt_template.format(query=query, retrieved_documents=retrieved_content)

# Set up generation model
groq_api_key = os.getenv('GROQ_API_KEY')
model = 'mixtral-8x7b-32768' # Or any other model you prefer

# Initialize Groq Langchain chat object and conversation
groq_chat = ChatGroq(groq_api_key=groq_api_key, model_name=model)
conversation = ConversationChain(llm=groq_chat, memory=memory)

# Generate response using the formatted input
response = conversation.run(formatted_input)
print(response)
Loading