-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
55 lines (44 loc) · 1.73 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
import streamlit as st
import json
from langchain_community.llms import OpenAI
from langchain.chains.summarize import load_summarize_chain
from langchain.schema import Document
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set page config and title
st.set_page_config(page_title="Conversation Summary Bot", page_icon="🤖")
st.title("Conversation Summary Bot 🤖")
st.text("This application accepts Json data containing messages and role(Prospect/Ambassador) and Genrate Conversation Summary.")
# File uploader
uploaded_file = st.file_uploader("Choose a JSON file", type="json")
@st.cache_resource
def load_summarizer():
"""Load the summarizer model and chain."""
llm = OpenAI(temperature=0)
chain = load_summarize_chain(llm, chain_type="map_reduce")
return chain
# Only process the file and generate summary when the user has uploaded a file
if uploaded_file is not None:
# Load the summarizer
summarizer = load_summarizer()
# Load and parse JSON file
conversation_data = json.load(uploaded_file)
# Extract conversation from JSON
conversation = ""
for message in conversation_data:
conversation += f"{message['role']}: {message['content']}\n\n"
# Create a Document object
document = Document(page_content=conversation)
# Generate summary
with st.spinner('Generating summary...'):
summary = summarizer.run([document]) # Pass a list containing the document
# Display summary
st.subheader("Conversation Summary")
st.write(summary)
else:
st.info("Please upload a JSON file to generate a summary.")
# Add a footer
st.markdown("---")
st.markdown("Created with By Varun Soni ❤️ using Streamlit and LangChain")