Upload 2 files
Browse files- app.py +94 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import docx2txt
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
from langchain.schema import (
|
| 8 |
+
SystemMessage,
|
| 9 |
+
HumanMessage,
|
| 10 |
+
AIMessage
|
| 11 |
+
)
|
| 12 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 13 |
+
|
| 14 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 15 |
+
from langchain.vectorstores import Chroma
|
| 16 |
+
|
| 17 |
+
import streamlit as st
|
| 18 |
+
from streamlit_chat import message
|
| 19 |
+
|
| 20 |
+
load_dotenv()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 24 |
+
llm = ChatOpenAI(temperature=0.3, model="gpt-3.5-turbo")
|
| 25 |
+
embeddings = OpenAIEmbeddings()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@st.cache_data
|
| 29 |
+
def load_into_chroma(docs):
|
| 30 |
+
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 31 |
+
docs = text_splitter.split_documents(text_splitter)
|
| 32 |
+
global db_chroma
|
| 33 |
+
db_chroma = Chroma.from_documents(docs, embeddings)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def generate_content(query, call_transcript):
|
| 37 |
+
# relevant_docs = db_chroma.similarity_search(query)
|
| 38 |
+
system_prompt = f"""You are a professional writer of motivational letters.\
|
| 39 |
+
You will be given a content from a knowledge base below, delimited by triple \
|
| 40 |
+
backticks. Your job is to use knowledge from this data and write a \
|
| 41 |
+
motivational letter for graduate school application. Only write content \
|
| 42 |
+
using data from the knowledgebase, do not claim facts from outside of it. \
|
| 43 |
+
Make the letter very personal with regards to the knowledge base.
|
| 44 |
+
|
| 45 |
+
Knowledge Base: ```{call_transcript}```
|
| 46 |
+
"""
|
| 47 |
+
system_message = SystemMessage(content=system_prompt)
|
| 48 |
+
human_message = HumanMessage(content=query)
|
| 49 |
+
message = [system_message, human_message]
|
| 50 |
+
response = llm(message)
|
| 51 |
+
return response.content
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
system_session_prompt = """As a professional writer of motivational letters, \
|
| 55 |
+
your task is to write a sales proposal provided to you according to \
|
| 56 |
+
the required changes. You will make the recommended changes to the \
|
| 57 |
+
sales proposal and return the entire proposal with thse changes. \
|
| 58 |
+
Your job depends on the answers you provide so play close attention to \
|
| 59 |
+
the queries you recieve.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
st.title("ChatGPT 🤖 Powered Chatbot")
|
| 65 |
+
st.header("Sales Proposal Generator")
|
| 66 |
+
|
| 67 |
+
uploaded_file = st.file_uploader("Upload a word file", type="docx")
|
| 68 |
+
if "messages" not in st.session_state:
|
| 69 |
+
st.session_state.messages = [AIMessage(content="How can I help you?")]
|
| 70 |
+
if uploaded_file is not None:
|
| 71 |
+
# extract text from word file
|
| 72 |
+
call_transcript = docx2txt.process(uploaded_file)
|
| 73 |
+
# load_into_chroma(call_transcript)
|
| 74 |
+
|
| 75 |
+
with st.sidebar:
|
| 76 |
+
user_input = st.text_area("Enter your query: ", key="user_input")
|
| 77 |
+
st.session_state.messages.append(HumanMessage(content=user_input))
|
| 78 |
+
|
| 79 |
+
if st.button("Generate content"):
|
| 80 |
+
with st.spinner("GPT is thinking..."):
|
| 81 |
+
response = generate_content(user_input, call_transcript)
|
| 82 |
+
st.session_state.messages.append(AIMessage(content=response))
|
| 83 |
+
|
| 84 |
+
# display message history
|
| 85 |
+
messages = st.session_state.get('messages', [])
|
| 86 |
+
for i in range(len(messages)):
|
| 87 |
+
if i % 2 == 0:
|
| 88 |
+
message(messages[i].content, is_user=False, key=str(i) + '_user')
|
| 89 |
+
else:
|
| 90 |
+
message(messages[i].content, is_user=True, key=str(i) + '_ai')
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if __name__ == '__main__':
|
| 94 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
docx2txt
|
| 2 |
+
openai
|
| 3 |
+
langchain
|
| 4 |
+
pinecone-client
|
| 5 |
+
streamlit
|
| 6 |
+
streamlit_chat
|
| 7 |
+
python-dotenv
|
| 8 |
+
tiktoken
|