-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
70 lines (55 loc) · 2.24 KB
/
app.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
62
63
64
65
66
67
68
69
70
# File: LangChainchatOpenAI.py
# Author: Denys L
# Date: October 8, 2023
# Description:
import os
import sys
import hashlib
from typing import Any
import streamlit as st
from dotenv import load_dotenv
from langchain.callbacks.base import BaseCallbackHandler
from fundamentals.langchain_utils import StuffSummarizerByChapter
class StreamingStdOutCallbackHandlerPersonal(BaseCallbackHandler):
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
st.session_state.full_response = st.session_state.full_response + token
st.session_state.placeholder.markdown(
st.session_state.full_response + "▌")
sys.stdout.write(token)
sys.stdout.flush()
def process_book(uploaded_file):
temp_file_path = f'.trash/{uploaded_file.name}'
with open(temp_file_path, 'wb') as file:
file.write(uploaded_file.read())
st.session_state.full_response = ""
st.session_state.handler_ia_message = st.chat_message(
"assistant", avatar="🤖")
st.session_state.placeholder = st.session_state.handler_ia_message.empty()
# magic
st.session_state.llm.summarize(temp_file_path)
# print output
st.session_state.placeholder.markdown(st.session_state.full_response)
st.session_state.messages.append(
{"role": "assistant", "content": st.session_state.full_response, "avatar": "🤖"})
st.session_state.full_response = ""
# remove temp file
os.remove(temp_file_path)
def main():
load_dotenv()
st.title("Storyteller")
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.handler = StreamingStdOutCallbackHandlerPersonal()
st.session_state.llm = StuffSummarizerByChapter(
st.session_state.handler)
for message in st.session_state.messages:
with st.chat_message(message["role"], avatar=message["avatar"]):
st.markdown(message["content"])
st.sidebar.subheader("Your books")
uploaded_file = st.sidebar.file_uploader(
"Upload your Books here and click on 'Process' to start the story", accept_multiple_files=False)
if st.sidebar.button("Process"):
with st.spinner("Processing"):
process_book(uploaded_file)
if __name__ == '__main__':
main()