Spaces:
Runtime error
Runtime error
ChandraP12330
commited on
Commit
•
772bf48
1
Parent(s):
767c1c3
Upload 2 files
Browse files- app.py +67 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
import os
|
5 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
6 |
+
import google.generativeai as genai
|
7 |
+
from langchain.vectorstores import FAISS
|
8 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
9 |
+
from langchain.chains.question_answering import load_qa_chain
|
10 |
+
from langchain.prompts import PromptTemplate
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
def get_conversational_chain():
|
15 |
+
|
16 |
+
prompt_template = """
|
17 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
18 |
+
provided context just say, "Sorry, I don't know about this. You can ask anther question.", don't provide the wrong answer\n\n
|
19 |
+
Context:\n {context}?\n
|
20 |
+
Question: \n{question}\n
|
21 |
+
|
22 |
+
Answer:
|
23 |
+
"""
|
24 |
+
|
25 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
26 |
+
temperature=0.3)
|
27 |
+
|
28 |
+
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
29 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
30 |
+
|
31 |
+
return chain
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
def user_input(user_question):
|
36 |
+
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
37 |
+
|
38 |
+
new_db = FAISS.load_local("faiss_index", embeddings)
|
39 |
+
docs = new_db.similarity_search(user_question)
|
40 |
+
|
41 |
+
chain = get_conversational_chain()
|
42 |
+
|
43 |
+
|
44 |
+
response = chain(
|
45 |
+
{"input_documents":docs, "question": user_question}
|
46 |
+
, return_only_outputs=True)
|
47 |
+
|
48 |
+
print(response)
|
49 |
+
st.write("Reply: ", response["output_text"])
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
st.set_page_config("DIAT Rakshak")
|
56 |
+
st.header("DIAT Assistant Chatbot�")
|
57 |
+
GOOGLE_API_KEY = st.text_input("Please enter your GOOGLE_API_KEY")
|
58 |
+
os.environ['GOOGLE_API_KEY'] = GOOGLE_API_KEY
|
59 |
+
|
60 |
+
|
61 |
+
user_question = st.text_input("Hello! I am Rakshak, your DIAT assistant. Please ask your query regarding DIAT.")
|
62 |
+
|
63 |
+
if user_question:
|
64 |
+
user_input(user_question)
|
65 |
+
|
66 |
+
|
67 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
langchain
|
5 |
+
PyPDF2
|
6 |
+
faiss-cpu
|
7 |
+
langchain_google_genai
|