-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathilit.py
61 lines (49 loc) · 2.19 KB
/
ilit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import CohereEmbeddings
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage, SystemMessage
# Set environment variables for API keys
os.environ["COHERE_API_KEY"] = "############################"
if "GOOGLE_API_KEY" not in os.environ:
os.environ["GOOGLE_API_KEY"] = "########################"
# Load PDF document
def load_document(file_path):
loader = PyPDFLoader(file_path)
pages = loader.load()
return pages
# Create FAISS index
def create_faiss_index(pages):
faiss_index = FAISS.from_documents(pages, CohereEmbeddings())
return faiss_index
# Streamlit app
def main():
st.title("Dream Interpretation with Langchain")
st.write("Enter your dream in Arabic and let Langchain interpret it!")
# Load PDF document
faiss_index = FAISS.load_local("faiss_index", CohereEmbeddings())
# Get user input
user_ask = st.text_input("ما هو حلمك؟")
if st.button("Interpret Dream"):
text_contents = []
# Similarity search
docs = faiss_index.similarity_search(user_ask, k=25)
# Extract text content from each document
for doc in docs:
text_content = doc.page_content[:]
text_contents.append(text_content)
# Concatenate text contents
concatenated_texts = [text_content + "رد بمساعده المعلومات المعطاه في تفسير الحلم ودائما انهى الرد بكلمة والله اعلم وفسرالاحداث كلها وأربط بينهم" for text_content in text_contents]
# Initialize Google GenAI model
model = ChatGoogleGenerativeAI(model="gemini-pro", convert_system_message_to_human=True)
# Invoke the model
res = model.invoke([
SystemMessage(content=concatenated_texts),
HumanMessage(content=user_ask)
])
# Display result
st.write(eval(f'f"""{res}"""'))
if __name__ == "__main__":
main()