Shruti9756 commited on
Commit
a0665c0
Β·
verified Β·
1 Parent(s): d12117a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from transformers import pipeline, AutoTokenizer
4
+ import base64
5
+
6
+ # Load the EasyTerms/legalSummerizerET model from Hugging Face
7
+ summarizer = pipeline("summarization", model="EasyTerms/legalSummerizerET")
8
+
9
+ # Increase the maximum token limit
10
+ tokenizer = AutoTokenizer.from_pretrained("EasyTerms/legalSummerizerET")
11
+ summarizer.model.config.max_position_embeddings = tokenizer.model_max_length
12
+
13
+ # Function to generate summary using the EasyTerms/legalSummerizerET model
14
+ def generate_summary(contract_text):
15
+ summary = summarizer(contract_text, max_length=512, min_length=50, length_penalty=2.0, num_beams=4, early_stopping=True)
16
+ return summary[0]['summary_text']
17
+
18
+ # Function to handle feedback and store it in a CSV file
19
+ def handle_feedback(feedback_data, feedback_file):
20
+ feedback_df = pd.DataFrame(feedback_data, columns=['Contract', 'Summary', 'πŸ‘', 'πŸ‘Ž'])
21
+
22
+ # Save the dataframe to the feedback CSV file
23
+ feedback_df.to_csv(feedback_file, mode='a', index=False, header=not st.session_state.feedback_csv_exists)
24
+
25
+ # Display a feedback collected message only if thumbs up or thumbs down is clicked
26
+ if 'πŸ‘' in feedback_df['πŸ‘'].values or 'πŸ‘Ž' in feedback_df['πŸ‘Ž'].values:
27
+ st.success("Feedback collected successfully!")
28
+
29
+ # Display a download button for the user
30
+ st.markdown(get_binary_file_downloader_html(feedback_file, 'Feedback Data'), unsafe_allow_html=True)
31
+
32
+ # Function to create a download link for a binary file
33
+ def get_binary_file_downloader_html(file_path, file_label):
34
+ with open(file_path, 'rb') as file:
35
+ file_content = file.read()
36
+ b64 = base64.b64encode(file_content).decode()
37
+ return f'<a href="data:file/csv;base64,{b64}" download="{file_label}.csv">Click here to download {file_label}</a>'
38
+
39
+ # Main Streamlit app
40
+ def main():
41
+ st.title("Legal Contract Summarizer with Feedback")
42
+
43
+ # Input area for legal contract
44
+ contract_text = st.text_area("Enter the legal contract:", height=200) # Increase the height to handle larger contracts
45
+
46
+ # Button to generate summary
47
+ if st.button("Generate Summary"):
48
+ summary = generate_summary(contract_text)
49
+ st.subheader("Generated Summary:")
50
+ st.write(summary)
51
+
52
+ # Feedback section
53
+ st.subheader("Feedback:")
54
+ thumbs_up = st.button("πŸ‘")
55
+ thumbs_down = st.button("πŸ‘Ž")
56
+
57
+ chosen = "πŸ‘" if thumbs_up else None
58
+ rejected = "πŸ‘Ž" if thumbs_down else None
59
+
60
+ feedback_data.append((contract_text, summary, chosen, rejected))
61
+
62
+ # Handle feedback data
63
+ if feedback_data:
64
+ feedback_file = 'feedback.csv'
65
+ st.session_state.feedback_csv_exists = True
66
+ handle_feedback(feedback_data, feedback_file)
67
+
68
+ # Initialize feedback data
69
+ feedback_data = []
70
+
71
+ # Run the app
72
+ if __name__ == "__main__":
73
+ main()