Shruti9756 commited on
Commit
41f09d2
Β·
verified Β·
1 Parent(s): c828789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -27
app.py CHANGED
@@ -1,39 +1,53 @@
1
  import streamlit as st
 
2
  from transformers import pipeline, AutoTokenizer
3
  import base64
4
 
5
- # Load the GPT-2 text generation model from Hugging Face
6
- text_generator = pipeline("text-generation", model="Shruti9756/G24_Contract_Summarization_step3")
7
 
8
  # Increase the maximum token limit
9
  tokenizer = AutoTokenizer.from_pretrained("Shruti9756/G24_Contract_Summarization_step3")
10
- text_generator.model.config.max_position_embeddings = tokenizer.model_max_length
11
 
12
- # Function to generate text using the GPT-2 model
13
- def generate_text(prompt):
14
- generated_text = text_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7)[0]['generated_text']
15
- return generated_text
16
 
17
- # # Function to handle feedback and store it in a CSV file
18
- # def handle_feedback(feedback_data, feedback_file):
19
- # # Your existing feedback handling logic remains the same
20
 
21
- # # Function to create a download link for a binary file
22
- # def get_binary_file_downloader_html(file_path, file_label):
23
- # # Your existing file downloader logic remains the same
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Main Streamlit app
26
  def main():
27
- st.title("Text Generation with Feedback")
28
 
29
- # Input area for prompt
30
- prompt = st.text_area("Enter a prompt:", height=100)
31
 
32
- # Button to generate text
33
- if st.button("Generate Text"):
34
- generated_text = generate_text(prompt)
35
- st.subheader("Generated Text:")
36
- st.write(generated_text)
37
 
38
  # Feedback section
39
  st.subheader("Feedback:")
@@ -43,16 +57,16 @@ def main():
43
  chosen = "πŸ‘" if thumbs_up else None
44
  rejected = "πŸ‘Ž" if thumbs_down else None
45
 
46
- # feedback_data.append((prompt, generated_text, chosen, rejected))
47
 
48
  # Handle feedback data
49
- # if feedback_data:
50
- # feedback_file = 'feedback.csv'
51
- # st.session_state.feedback_csv_exists = True
52
- # handle_feedback(feedback_data, feedback_file)
53
 
54
  # Initialize feedback data
55
- # feedback_data = []
56
 
57
  # Run the app
58
  if __name__ == "__main__":
 
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="Shruti9756/G24_Contract_Summarization_step3")
8
 
9
  # Increase the maximum token limit
10
  tokenizer = AutoTokenizer.from_pretrained("Shruti9756/G24_Contract_Summarization_step3")
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:")
 
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__":