codewithharsha commited on
Commit
7c148a4
·
verified ·
1 Parent(s): 47773dc

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +72 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,74 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ from langchain_groq import ChatGroq
4
+ from langchain_openai import OpenAIEmbeddings
5
+ from langchain_community.embeddings import OllamaEmbeddings
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain.chains.combine_documents import create_stuff_documents_chain
8
+ from langchain_core.prompts import ChatPromptTemplate
9
+ from langchain.chains import create_retrieval_chain
10
+ from langchain_community.vectorstores import FAISS
11
+ from langchain_community.document_loaders import PyPDFDirectoryLoader
12
+ from langchain_huggingface import HuggingFaceEmbeddings
13
 
14
+ import openai
15
+
16
+ from dotenv import load_dotenv
17
+ load_dotenv()
18
+ ## load the GROQ API Key
19
+ # os.environ['OPENAI_API_KEY']=os.getenv("OPENAI_API_KEY")
20
+ # os.environ['GROQ_API']=os.getenv("GROQ_API")
21
+
22
+ groq_api_key="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"
23
+
24
+ llm=ChatGroq(groq_api_key=groq_api_key,model_name="Llama3-8b-8192")
25
+
26
+ prompt=ChatPromptTemplate.from_template(
27
+ """
28
+ You are Clara, the chatbot for Lakireddy Bali Reddy College of Engineering (LBRCE). Your role is to provide friendly, helpful, and clear responses to questions about the college. Always respond professionally, using phrases like "Currently, I understand," or "Based on recent information," to keep the interaction conversational.
29
+ Answer general queries concisely and clearly. Avoid technical terms and keep answers user-friendly. Include info on:
30
+ - Courses, faculty, facilities, and placements
31
+ - Admissions, eligibility, and campus life
32
+ <context>
33
+ {context}
34
+ <context>
35
+ Question:{input}
36
+
37
+ """
38
+
39
+ )
40
+
41
+ def create_vector_embedding():
42
+ if "vectors" not in st.session_state:
43
+ st.session_state.embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
44
+ st.session_state.loader=PyPDFDirectoryLoader("data") ## Data Ingestion step
45
+ st.session_state.docs=st.session_state.loader.load() ## Document Loading
46
+ st.session_state.text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200)
47
+ st.session_state.final_documents=st.session_state.text_splitter.split_documents(st.session_state.docs[:50])
48
+ st.session_state.vectors=FAISS.from_documents(st.session_state.final_documents,st.session_state.embeddings)
49
+ st.title("RAG College Chat Bot")
50
+
51
+ user_prompt=st.text_input("Enter your query regarding LBRCE")
52
+
53
+ if st.button("Document Embedding"):
54
+ create_vector_embedding()
55
+ st.write("Vector Database is ready")
56
+
57
+ import time
58
+
59
+ if user_prompt:
60
+ document_chain=create_stuff_documents_chain(llm,prompt)
61
+ retriever=st.session_state.vectors.as_retriever()
62
+ retrieval_chain=create_retrieval_chain(retriever,document_chain)
63
+
64
+ start=time.process_time()
65
+ response=retrieval_chain.invoke({'input':user_prompt})
66
+ print(f"Response time :{time.process_time()-start}")
67
+
68
+ st.write(response['answer'])
69
+
70
+ ## With a streamlit expander
71
+ with st.expander("Document similarity Search"):
72
+ for i,doc in enumerate(response['context']):
73
+ st.write(doc.page_content)
74
+ st.write('------------------------')