Vidya_AI_RAG / app.py
Manas281's picture
Upload app.py
870c370 verified
import streamlit as st
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_chroma import Chroma
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_groq import ChatGroq
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
import os
from dotenv import load_dotenv
from langchain.vectorstores import FAISS
import streamlit as st
import google.generativeai as genai
import requests
load_dotenv()
os.environ['HF_TOKEN'] = HF_TOKEN = os.getenv('HF_TOKEN')
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
api_key = os.getenv('API_KEY')
YOUTUBE_API_KEY = os.getenv('YOUTUBE_API_KEY')
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
model = genai.GenerativeModel('gemini-pro')
st.title("๐Ÿ‘ฉโ€๐ŸŽ“ Vidya AI ๐Ÿ‘จโ€๐ŸŽ“")
st.sidebar.title("๐Ÿ‘ฉโ€๐Ÿ”ง Select your Preferences๐Ÿง‘โ€โš•๏ธ")
subjects_data = {
"CBSE": {
"11": ["Mathematics", "Physics", "Chemistry", "Biology"],
"12": ["Mathematics", "Physics", "Chemistry", "Biology"],
},
"ICSE": {
"11": ["Mathematics", "Physics", "Chemistry", "Biology"],
"12": ["Mathematics", "Physics", "Chemistry", "Biology"],
},
"Maharashtra": {
"11": ["Mathematics", "Physics", "Chemistry", "Biology"],
"12": ["Mathematics", "Physics", "Chemistry", "Biology"],
}
}
# Content data for each subject
content_data = {
"CBSE": {
"11": {
"Mathematics": [
"1. Sets",
"2. Relations and Functions",
"3. Trigonometric Functions",
"4. Principle of Mathematical Induction",
"5. Complex Numbers and Quadratic Equations",
"6. Linear Inequalities",
"7. Permutations and Combinations",
"8. Binomial Theorem",
"9. Sequences and Series",
"10. Straight Lines",
"11. Conic Sections",
"12. Introduction to Three-dimensional Geometry",
"13. Limits and Derivatives",
"14. Mathematical Reasoning",
"15. Statistics",
"16. Probability"
],
"Physics": [
"1. Physical World",
"2. Units and Measurements",
"3. Motion in a Straight Line",
"4. Motion in a Plane",
"5. Laws of Motion",
"6. Work, Energy and Power",
"7. System of Particles and Rotational Motion",
"8. Gravitation",
"9. Mechanical Properties of Solids",
"10. Mechanical Properties of Fluids",
"11. Thermal Properties of Matter",
"12. Thermodynamics",
"13. Kinetic Theory",
"14. Oscillations",
"15. Waves"
],
"Chemistry": [
"1. Some Basic Concepts of Chemistry",
"2. Structure of Atom",
"3. Classification of Elements and Periodicity in Properties",
"4. Chemical Bonding and Molecular Structure",
"5. States of Matter: Gases and Liquids",
"6. Thermodynamics",
"7. Equilibrium",
"8. Redox Reactions",
"9. Hydrogen",
"10. The s-Block Element",
"11. The p-Block Element",
"12. Organic Chemistry - Some Basic Principles and Techniques",
"13. Hydrocarbons",
"14. Environmental Chemistry"
],
"Biology": [
"1. The Living World",
"2. Biological Classification",
"3. Plant Kingdom",
"4. Animal Kingdom",
"5. Morphology of Flowering Plants",
"6. Anatomy of Flowering Plants",
"7. Structural Organisation in Animals",
"8. Cell: The Unit of Life",
"9. Biomolecules",
"10. Cell Cycle and Cell Division",
"11. Transport in Plants",
"12. Mineral Nutrition",
"13. Photosynthesis in Higher Plants",
"14. Respiration in Plants",
"15. Plant - Growth and Development",
"16. Digestion and Absorption",
"17. Breathing and Exchange of Gases",
"18. Body Fluids and Circulation",
"19. Excretory Products and their Elimination",
"20. Locomotion and Movement",
"21. Neural Control and Coordination",
"22. Chemical Coordination and Integration"
]
},
"12": {
"Mathematics": [
"1. Relations and Functions",
"2. Inverse Trigonometric Functions",
"3. Matrices",
"4. Determinants",
"5. Continuity and Differentiability",
"6. Applications of Derivatives",
"7. Integrals",
"8. Applications of Integrals",
"9. Differential Equations",
"10. Vector Algebra",
"11. Three-dimensional Geometry",
"12. Linear Programming",
"13. Probability"
],
"Physics": [
"1. Electric Charges and Fields",
"2. Electrostatic Potential and Capacitance",
"3. Current Electricity",
"4. Moving Charges and Magnetism",
"5. Magnetism and Matter",
"6. Electromagnetic Induction",
"7. Alternating Currents",
"8. Electromagnetic Waves",
"9. Optics",
"10. Wave Optics",
"11. Dual Nature of Radiation and Matter",
"12. Atoms",
"13. Nuclei",
"14. Semiconductor Electronics",
"15. Communication Systems"
],
"Chemistry": [
"1. The Solid State",
"2. Solutions",
"3. Electrochemistry",
"4. Chemical Kinetics",
"5. Surface Chemistry",
"6. General Principles and Processes of Isolation of Elements",
"7. p-Block Elements",
"8. d and f Block Elements",
"9. Coordination Compounds",
"10. Haloalkanes and Haloarenes",
"11. Alcohols, Phenols and Ethers",
"12. Aldehydes, Ketones and Carboxylic Acids",
"13. Organic Compounds Containing Nitrogen",
"14. Biomolecules",
"15. Polymers",
"16. Chemistry in Everyday Life"
],
"Biology": [
"1. Reproduction in Organisms",
"2. Sexual Reproduction in Flowering Plants",
"3. Human Reproduction",
"4. Reproductive Health",
"5. Principles of Inheritance and Variation",
"6. Molecular Basis of Inheritance",
"7. Evolution",
"8. Human Health and Disease",
"9. Strategies for Enhancement in Food Production",
"10. Microbes in Human Welfare",
"11. Biotechnology - Principles and Processes",
"12. Biotechnology and its Applications",
"13. Organisms and Populations",
"14. Ecosystem",
"15. Biodiversity and Conservation",
"16. Environmental Issues"
]
}
},
"ICSE": {
"11": {
"Mathematics": [
"1.Sets and Functions",
"2.Complex Numbers",
"3.Quadratic Equations",
"4.Permutations and Combinations",
"5.Binomial Theorem",
"6.Sequences and Series",
"7.Straight Lines",
"8.Conic Sections",
"9.Introduction to Three-Dimensional Geometry",
"10.Limits and Derivatives",
"11.Mathematical Reasoning",
"12.Statistics",
"13.Probability"
],
"Physics": [
"1.Measurement",
"2.Kinematics",
"3.Dynamics",
"4.Work, Energy, and Power",
"5.Motion of System of Particles and Rigid Body",
"6.Gravitation",
"7.Properties of Matter",
"8.Heat",
"9.Wave Motion",
"10.Sound",
"11.Light",
"12.Electricity",
"13.Magnetism",
"14.Modern Physics",
],
"Chemistry": [
"1. Basic Concepts of Chemistry",
"2. Structure of Atom",
"3. Periodic Table and Periodicity",
"4. Chemical Bonding and Molecular Structure",
"5. States of Matter",
"6. Thermodynamics",
"7. Equilibrium",
"8. Redox Reactions",
"9. Hydrogen",
"10. s-Block and p-Block Elements",
"11. Organic Chemistry - Basic Principles",
"12. Hydrocarbons",
"13. Environmental Chemistry"
],
"Biology": [
"1.Cell: Structure and Function",
"2.Biomolecules",
"3.Cell Division",
"4.Plant Kingdom",
"5.Animal Kingdom",
"6.Structural Organisation in Animals and Plants",
"7.Human Physiology",
"8.Ecology and Environment"
]
},
"12": {
"Mathematics": [
"1.Relations and Functions",
"2.Algebra",
"3.Calculus",
"4.Vectors",
"5.Three-Dimensional Geometry",
"6.Statistics",
"7.Probability",
"8.Linear Programming",
"9.Mathematical Reasoning",
],
"Physics": [
"1. Electrostatics",
"2. Current Electricity",
"3. Magnetism",
"4. Electromagnetic Induction",
"5. Alternating Currents",
"6. Electromagnetic Waves",
"7. Optics",
"8. Dual Nature of Radiation",
"9. Atoms and Nuclei",
"10. Electronic Devices",
],
"Chemistry": [
"1. Solutions",
"2. Electrochemistry",
"3. Chemical Kinetics",
"4. Surface Chemistry",
"5. General Principles of Isolation of Elements",
"6. p-Block Elements",
"7. d-Block and f-Block Elements",
"8. Coordination Compounds",
"9. Haloalkanes and Haloarenes",
"10. Alcohols, Phenols, and Ethers",
"11. Carbonyl Compounds",
"12.Carboxylic Acids",
"13. Organic Compounds Containing Nitrogen",
"14. Biomolecules",
"15. Polymers",
"16. Chemistry in Everyday Life"
],
"Biology": [
"1. Reproduction",
"2. Genetics and Evolution",
"3. Biotechnology and its Applications",
"4. Ecology and Environment",
]
}
},
"Maharashtra": {
"11": {
"Mathematics": [
"1.Angle and Its Measurement",
"2.Trigonometry - 1",
"3.Trigonometry - 2",
"4.Determinants and Matrices",
"5.Straight Line",
"6.Circle",
"7.Conic Sections",
"8.Measures of Dispersion",
"9.Probability",
"10.Complex Numbers",
"11.Sequences and Series",
"12.Permutations and Combination",
"13.Methods of Induction and Binomial Theorem",
"14.Sets and Relations",
"15.Functions",
"16.Limits",
"17.Continuity",
"18.Differentiation"
],
"Physics": [
"1.Units and Measurements",
"2.Mathematical Methods",
"3.Motion in a Plane",
"4.Laws of Motion",
"5.Gravitation",
"6.Mechanical Properties of Solids",
"7.Thermal Properties of Matter",
"8.Sound",
"9.Optics",
"10.Electrostatics",
"11.Electric Current Through Conductors",
"12.Magnetism",
"13.Electromagnetic Waves and Communication System",
"14.Semiconductors"
],
"Chemistry": [
"1.Some Basic Concepts of Chemistry",
"2.Structure of Atom",
"3.Classification of Elements and Periodicity in Properties",
"4.Chemical Bonding and Molecular Structure",
"5.States of Matter: Gases and Liquids",
"6.Thermodynamics",
"7.Equilibrium",
"8.Redox Reactions",
"9.Hydrogen",
"10.s-Block Element (Alkali and Alkaline earth metals)",
"11.Some p-Block Elements",
"12.Organic Chemistry - Some Basic Principles and Techniques",
"13.Hydrocarbons",
"14.Environmental Chemistry"
],
"Biology": [
"1.Living World",
"2.Systematics of Living Organisms",
"3.Kingdom Plantae",
"4.Kingdom Animalia",
"5.Cell Structure and Organization",
"6.Biomolecules",
"7.Cell Division",
"8.Plant Tissues and Anatomy",
"9.Morphology of Flowering Plants",
"10.Animal Tissue",
"11.Study of Animal Type: Cockroach",
"12.Photosynthesis",
"13.Respiration and Energy Transfer",
"14.Human Nutrition",
"15.Excretion and Osmoregulation",
"16.Skeleton and Movement"
]
},
"12": {
"Mathematics": [
"1.Relations and Functions",
"2.Inverse Trigonometric Functions",
"3.Matrices",
"4.Determinants",
"5.Continuity and Differentiability",
"6.Applications of Derivatives",
"7.Integrals",
"8.Applications of Integrals",
"9.Differential Equations",
"10.Vector Algebra",
"11.Three-dimensional Geometry",
"12.Linear Programming",
"13.Probability"
],
"Physics": [
"1.Electric Charges and Fields",
"2.Electrostatic Potential and Capacitance",
"3.Current Electricity",
"4.Moving Charges and Magnetism",
"5.Magnetism and Matter",
"6.Electromagnetic Induction",
"7.Alternating Currents",
"8.Electromagnetic Waves",
"9.Ray Optics and Optical Instruments",
"10.Wave Optics",
"11.Dual Nature of Radiation and Matter",
"12.Atoms and Nuclei",
"13.Electronic Devices"],
"Chemistry": [
"1.Solid State",
"2.Solutions",
"3.Electrochemistry",
"4.Chemical Kinetics",
"5.Surface Chemistry",
"6.General Principles and Processes of Isolation of Elements",
"7.p-Block Elements",
"8.d and f Block Elements",
"9.Coordination Compounds",
"10.Haloalkanes and Haloarenes",
"11.Alcohols, Phenols, and Ethers",
"12.Carbonyl Compounds",
"13.Carboxylic Acids and Their Derivatives",
"14.Biomolecules",
"15.Polymers",
"16.Chemistry in Everyday Life",
],
"Biology": [
"1.Reproduction in Organisms",
"2.Sexual Reproduction in Flowering Plants",
"3.Human Reproduction",
"4.Principles of Inheritance and Variation",
"5.Molecular Basis of Inheritance",
"6.Evolution",
"7.Biotechnology Principles",
"8.Biotechnology and its Applications in Medicine",
"9.Ecosystem",
"10.Biodiversity and Conservation",
"11.Environmental Issues",
]
}
}
}
board = st.sidebar.selectbox("Select Board", ["Maharashtra", "ICSE", "CBSE"])
# If "State" board is selected, show an additional dropdown for selecting the state
standard = st.sidebar.selectbox("Select Standard", ["11", "12"])
subject = st.sidebar.selectbox("Select Subject", subjects_data[board][standard])
chapter=st.sidebar.selectbox(("Select Chapter"),content_data[board][standard][subject])
# Form for creating short notes
with st.form("Short Notes"):
s_notes = st.form_submit_button("๐Ÿ“‹ Create Short Notes ๐Ÿ“‹")
if s_notes:
def generate_short_notes(board, standard, subject, chapter):
prompt = (f"Create concise ,brief ,including a section which student tend to neglect and informative short notes for {subject}, "
f"Chapter: {chapter} as per {board} {standard} curriculum.")
try:
response = model.generate_content(
prompt
)
return response.text
except Exception as e:
return "Error generating short notes: " + str(e)
# Generate and display the notes
notes = generate_short_notes(board, standard, subject, chapter)
st.write("### Short Notes")
st.write(notes)
with st.form("Formulas"):
formula = st.form_submit_button("โš›๏ธ Get Formulas โš›๏ธ")
if formula:
def formulas(board, standard, subject, chapter):
prompt = (f"Create a formula list for {subject}, "
f"Chapter: {chapter} as per {board} {standard} curriculum.")
try:
response = model.generate_content(
prompt
)
return response.text
except Exception as e:
return "Error generating short notes: " + str(e)
# Generate and display the notes
notes = formulas(board, standard, subject, chapter)
st.write("### Formula list")
st.write(notes)
with st.form("questions"):
questions= st.form_submit_button("๐Ÿ“‘๐Ÿ“ŠGet questions๐Ÿ“Š๐Ÿ“‘")
if questions:
def get_questions(board, standard, subject, chapter):
prompt = (f"Create atleast 10 most important questions (thoerotical as well as numericals) which are frequently asked for {subject}, "
f"Chapter: {chapter} as per {board} {standard} curriculum.")
try:
response = model.generate_content(
prompt
)
return response.text
except Exception as e:
return "Error generating short notes: " + str(e)
# Generate and display the notes
notes = get_questions(board, standard, subject, chapter)
st.write("### questions")
st.write(notes)
with st.form("PYQS"):
pyqs= st.form_submit_button("๐Ÿ“‡ Get PYQS ๐Ÿ“‡")
if pyqs:
def get_pyqs(board, standard, subject, chapter):
prompt = (f"Give the most trending, hot and important mcq pyqs from jee mains,neet and advance atleast 15 for {subject}, "
f"Chapter: {chapter} as per {board} {standard} curriculum.")
try:
response = model.generate_content(
prompt
)
return response.text
except Exception as e:
return "Error generating short notes: " + str(e)
# Generate and display the notes
notes = get_pyqs(board, standard, subject, chapter)
st.write("### pyqs")
st.write(notes)
# Streamlit form for user input
with st.form("YouTube Videos"):
videos = st.form_submit_button("โ–ถ๏ธ๐ŸŽฆGet Youtube videos links ๐ŸŽฆโ–ถ๏ธ")
if videos:
def get_youtube_videos(subject, chapter, board):
search_url = f"https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&q={subject}{chapter}{board}+tutorial&type=video&key={YOUTUBE_API_KEY}"
try:
response = requests.get(search_url)
data = response.json()
video_list = []
for item in data.get('items', []):
video_title = item['snippet']['title']
video_id = item['id'].get('videoId')
if video_id: # Check if videoId exists
video_url = f"https://www.youtube.com/watch?v={video_id}"
video_list.append((video_title, video_url))
return video_list
except Exception as e:
return [f"Error fetching YouTube videos: {str(e)}"]
# Generate and display the video links
links = get_youtube_videos(subject, chapter, board)
st.write("*YouTube Videos:*")
if links:
for title, url in links:
st.write(f"- [{title}]({url})")
else:
st.write("No videos found.")
with st.form("Books Recommendations"):
books= st.form_submit_button("๐Ÿ“š Get Books Recommendations ๐Ÿ“š")
if books:
def get_books(board, standard, subject, chapter):
prompt = (f"Give the book recommendation for {subject}, "
f"Chapter: {chapter} as per {board} {standard} curriculum.")
try:
response = model.generate_content(
prompt
)
return response.text
except Exception as e:
return "Error generating short notes: " + str(e)
# Generate and display the notes
book = get_books(board, standard, subject, chapter)
st.write("### Books")
st.write(book)
def ask_query(chat):
prompt = f"Solve the query as if you are his teacher: {chat}"
try:
# Generate response using the model
response = model.generate_content(prompt)
return response.text
except Exception as e:
return "Error generating response: " + str(e)
chat = st.text_input("โ“โ“Ask your queryโ“โ“")
if chat:
solution = ask_query(chat)
st.write(solution)
if not api_key:
st.warning("Please enter the Groq API Key in the code to proceed.")
else:
llm = ChatGroq(groq_api_key=api_key, model_name="Gemma2-9b-It")
uploaded_files = st.file_uploader("Choose a PDF file", type="pdf", accept_multiple_files=True)
if uploaded_files:
documents = []
for uploaded_file in uploaded_files:
temppdf = f"./temp.pdf"
with open(temppdf, "wb") as file:
file.write(uploaded_file.getvalue())
file_name = uploaded_file.name
loader = PyPDFLoader(temppdf)
docs = loader.load()
documents.extend(docs)
text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
splits = text_splitter.split_documents(documents)
vectorstore = FAISS.from_documents(splits, embeddings)
retriever = vectorstore.as_retriever()
system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Explain your reasoning and make sure you answer briefly enough so that user csn understand even if he is a begginer"
"answer concise and well explainatory."
"\n\n"
"{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
user_input = st.text_input("Ask your question:")
if user_input:
response = rag_chain.invoke(
{"input": user_input}
)
st.write(response['answer'])