pn23 commited on
Commit
0919e86
·
1 Parent(s): 78e82ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py CHANGED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.callbacks import StreamlitCallbackHandler
3
+ import streamlit as st
4
+ from langchain.llms import OpenAI
5
+ from langchain.agents import AgentType, initialize_agent, load_tools
6
+ from langchain.callbacks import StreamlitCallbackHandler
7
+ import streamlit as st
8
+
9
+ from scraping import prompty
10
+
11
+ import os
12
+
13
+
14
+ st.set_page_config(page_title="CalHacks2023", page_icon=":robot:")
15
+ st.header("CalHacks2023")
16
+
17
+ from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
18
+ from langchain.text_splitter import CharacterTextSplitter
19
+ from langchain.vectorstores import Chroma
20
+ from langchain.document_loaders import TextLoader
21
+
22
+ import os
23
+ os.environ["OPENAI_API_KEY"] = "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"
24
+
25
+ from langchain.chat_models import ChatOpenAI
26
+ from langchain.chains.question_answering import load_qa_chain
27
+
28
+ from langchain.chains import RetrievalQA
29
+
30
+ if "generated" not in st.session_state:
31
+ st.session_state["generated"] = []
32
+
33
+ if "past" not in st.session_state:
34
+ st.session_state["past"] = []
35
+
36
+ if "messages" not in st.session_state:
37
+ st.session_state["messages"] = []
38
+
39
+ # load the document and split it into chunks
40
+ loader = TextLoader("/content/shermaissian_how_to.txt")
41
+ documents = loader.load()
42
+
43
+ # split it into chunks
44
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
45
+ docs = text_splitter.split_documents(documents)
46
+
47
+ # create the open-source embedding function
48
+ embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
49
+
50
+ # load it into Chroma
51
+ db = Chroma.from_documents(docs, embedding_function)
52
+
53
+
54
+
55
+ st_callback = StreamlitCallbackHandler(st.container())
56
+
57
+
58
+
59
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k":10})
60
+
61
+ qa = RetrievalQA.from_chain_type(
62
+ llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=False)
63
+
64
+ bool called = False
65
+
66
+ if prompt := st.chat_input():
67
+ st.chat_message("user").write(prompt)
68
+ with st.chat_message("assistant"):
69
+ #st_callback = StreamlitCallbackHandler(st.container())
70
+ if not called:
71
+ result = qa({"query": "Image yourself in the perspective of a high school student trying to find extracurricular activities to do for college applications. The following shows a specific student answering questions about their passions and experiences:" +
72
+ prompt +
73
+ "Use the information provided to analyze the underlying passions this student has. Use their past experiences and interests to come up with a list of a few potential extracurricular activities that they can engage in to develop their college application. This has to fit both their interests and allow them to gain skills that will enable them to build a successful college application portfolio. Provide reasoning as to why you are recommending these activities and as part of the description as open-ended questions that challenge them to think more about these ideas. Discover the root cause of what drives them to change the world for a better place. Based on that, craft extracurriculars around a personalized, nuanced, and unique story that can make these applicants truly showcase how they’re creating an impact in novel, meaningful​​, and creative ways."})
74
+ retriever.get_relevant_documents(query)
75
+ st.write(result['result'])
76
+ called = True
77
+ else:
78
+ result = qa({"query" : prompt})
79
+ retriever.get_relevant_documents(query)
80
+ st.write(result['result'])