MujtabaAhmed commited on
Commit
d6f23d4
·
verified ·
1 Parent(s): 7ac1856

Update lessonsummarize.py

Browse files
Files changed (1) hide show
  1. lessonsummarize.py +75 -75
lessonsummarize.py CHANGED
@@ -1,75 +1,75 @@
1
- import streamlit as st
2
- import time
3
- import langchain
4
- import openai
5
- from langchain.llms import OpenAI
6
- from langchain.vectorstores import Chroma
7
- from langchain.embeddings.openai import OpenAIEmbeddings
8
- from langchain.chains import RetrievalQA
9
- from langchain.chat_models import ChatOpenAI
10
- from langchain.prompts import PromptTemplate
11
- from langchain.embeddings.openai import OpenAIEmbeddings
12
-
13
-
14
- from langchain.document_loaders import PyPDFLoader
15
- from langchain.text_splitter import RecursiveCharacterTextSplitter
16
-
17
-
18
-
19
- openai_key = "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"
20
-
21
-
22
- def summarize():
23
- # st.markdown('<h1 style="font-family:Lora;color:darkred;text-align:center;">Summarize Your Lesson</h1>',unsafe_allow_html=True)
24
- # st.markdown('<i><h3 style="font-family:Arial;color:darkred;text-align:center;font-size:20px;padding-left:50px">Your AI Assistant To Summarize Lessons To Help You Cover Bullet Points!</h3><i>',unsafe_allow_html=True)
25
-
26
- uploaded_file = st.file_uploader("Upload PDF File Of Your Lesson")
27
-
28
- if uploaded_file:
29
- with st.spinner("Summarizing lesson into bullet points..."):
30
- with open(uploaded_file.name, mode='wb') as w:
31
- w.write(uploaded_file.getvalue())
32
- loader = PyPDFLoader(uploaded_file.name)
33
- pages = loader.load()
34
-
35
- # Split
36
- from langchain.text_splitter import RecursiveCharacterTextSplitter
37
- text_splitter = RecursiveCharacterTextSplitter(
38
- chunk_size = 1500,
39
- chunk_overlap = 150
40
- )
41
-
42
- splits = text_splitter.split_documents(pages)
43
-
44
- embedding = OpenAIEmbeddings(api_key="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")
45
-
46
- persist_directory = 'docs/chroma/'
47
-
48
- vectordb = Chroma.from_documents(
49
- documents=splits,
50
- embedding=embedding,
51
- persist_directory=persist_directory
52
- )
53
-
54
- llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, api_key = openai_key)
55
-
56
- template = """Use the following pieces of context and summarize the whole lesson for the teacher in bullet point to help teachers understand lesson. If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer as concise as possible.
57
- {context}
58
- Question: {question}
59
- Helpful Answer:"""
60
-
61
- QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"],template=template)
62
-
63
- # Run chain
64
- qa_chain = RetrievalQA.from_chain_type(
65
- llm,
66
- retriever=vectordb.as_retriever(),
67
- chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
68
- )
69
-
70
- result = qa_chain({"query": "Summarize this lesson for me. I am a teacher, I need to better understand this lesson. put it in bullet points"})
71
-
72
- st.success(result['result'])
73
-
74
- vectordb.delete_collection()
75
-
 
1
+ import streamlit as st
2
+ import time
3
+ import langchain
4
+ import openai
5
+ from langchain.llms import OpenAI
6
+ from langchain.vectorstores import Chroma
7
+ from langchain.embeddings.openai import OpenAIEmbeddings
8
+ from langchain.chains import RetrievalQA
9
+ from langchain.chat_models import ChatOpenAI
10
+ from langchain.prompts import PromptTemplate
11
+ from langchain.embeddings.openai import OpenAIEmbeddings
12
+
13
+
14
+ from langchain.document_loaders import PyPDFLoader
15
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
16
+
17
+ import os
18
+
19
+ openai_key = os.environ["key"]
20
+
21
+
22
+ def summarize():
23
+ # st.markdown('<h1 style="font-family:Lora;color:darkred;text-align:center;">Summarize Your Lesson</h1>',unsafe_allow_html=True)
24
+ # st.markdown('<i><h3 style="font-family:Arial;color:darkred;text-align:center;font-size:20px;padding-left:50px">Your AI Assistant To Summarize Lessons To Help You Cover Bullet Points!</h3><i>',unsafe_allow_html=True)
25
+
26
+ uploaded_file = st.file_uploader("Upload PDF File Of Your Lesson")
27
+
28
+ if uploaded_file:
29
+ with st.spinner("Summarizing lesson into bullet points..."):
30
+ with open(uploaded_file.name, mode='wb') as w:
31
+ w.write(uploaded_file.getvalue())
32
+ loader = PyPDFLoader(uploaded_file.name)
33
+ pages = loader.load()
34
+
35
+ # Split
36
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
37
+ text_splitter = RecursiveCharacterTextSplitter(
38
+ chunk_size = 1500,
39
+ chunk_overlap = 150
40
+ )
41
+
42
+ splits = text_splitter.split_documents(pages)
43
+
44
+ embedding = OpenAIEmbeddings(api_key="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")
45
+
46
+ persist_directory = 'docs/chroma/'
47
+
48
+ vectordb = Chroma.from_documents(
49
+ documents=splits,
50
+ embedding=embedding,
51
+ persist_directory=persist_directory
52
+ )
53
+
54
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, api_key = openai_key)
55
+
56
+ template = """Use the following pieces of context and summarize the whole lesson for the teacher in bullet point to help teachers understand lesson. If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer as concise as possible.
57
+ {context}
58
+ Question: {question}
59
+ Helpful Answer:"""
60
+
61
+ QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"],template=template)
62
+
63
+ # Run chain
64
+ qa_chain = RetrievalQA.from_chain_type(
65
+ llm,
66
+ retriever=vectordb.as_retriever(),
67
+ chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
68
+ )
69
+
70
+ result = qa_chain({"query": "Summarize this lesson for me. I am a teacher, I need to better understand this lesson. put it in bullet points"})
71
+
72
+ st.success(result['result'])
73
+
74
+ vectordb.delete_collection()
75
+