Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from transformers import pipeline, AutoTokenizer | |
import base64 | |
# Load the EasyTerms/legalSummerizerET model from Hugging Face | |
summarizer = pipeline("summarization", model="EasyTerms/legalSummerizerET") | |
# Increase the maximum token limit | |
tokenizer = AutoTokenizer.from_pretrained("EasyTerms/legalSummerizerET") | |
summarizer.model.config.max_position_embeddings = tokenizer.model_max_length | |
# Function to generate summary using the EasyTerms/legalSummerizerET model | |
def generate_summary(contract_text): | |
summary = summarizer(contract_text, max_length=512, min_length=50, length_penalty=2.0, num_beams=4, early_stopping=True) | |
return summary[0]['summary_text'] | |
# Function to handle feedback and store it in a CSV file | |
def handle_feedback(feedback_data, feedback_file): | |
feedback_df = pd.DataFrame(feedback_data, columns=['Contract', 'Summary', 'π', 'π']) | |
# Save the dataframe to the feedback CSV file | |
feedback_df.to_csv(feedback_file, mode='a', index=False, header=not st.session_state.feedback_csv_exists) | |
# Display a feedback collected message only if thumbs up or thumbs down is clicked | |
if 'π' in feedback_df['π'].values or 'π' in feedback_df['π'].values: | |
st.success("Feedback collected successfully!") | |
# Display a download button for the user | |
st.markdown(get_binary_file_downloader_html(feedback_file, 'Feedback Data'), unsafe_allow_html=True) | |
# Function to create a download link for a binary file | |
def get_binary_file_downloader_html(file_path, file_label): | |
with open(file_path, 'rb') as file: | |
file_content = file.read() | |
b64 = base64.b64encode(file_content).decode() | |
return f'<a href="data:file/csv;base64,{b64}" download="{file_label}.csv">Click here to download {file_label}</a>' | |
# Main Streamlit app | |
def main(): | |
st.title("Legal Contract Summarizer with Feedback") | |
# Input area for legal contract | |
contract_text = st.text_area("Enter the legal contract:", height=200) # Increase the height to handle larger contracts | |
# Button to generate summary | |
if st.button("Generate Summary"): | |
summary = generate_summary(contract_text) | |
st.subheader("Generated Summary:") | |
st.write(summary) | |
# Feedback section | |
st.subheader("Feedback:") | |
thumbs_up = st.button("π") | |
thumbs_down = st.button("π") | |
chosen = "π" if thumbs_up else None | |
rejected = "π" if thumbs_down else None | |
feedback_data.append((contract_text, summary, chosen, rejected)) | |
# Handle feedback data | |
if feedback_data: | |
feedback_file = 'feedback.csv' | |
st.session_state.feedback_csv_exists = True | |
handle_feedback(feedback_data, feedback_file) | |
# Initialize feedback data | |
feedback_data = [] | |
# Run the app | |
if __name__ == "__main__": | |
main() | |