import gradio as gr import pandas as pd import nltk nltk.download('punkt_tab') from lowerated.rate.entity import Entity def rate_movies(reviews_text, review_file, column_name): reviews = [] if reviews_text: reviews = reviews_text.split("\n") elif review_file is not None: try: for file in review_file: if file.name.endswith('.csv'): df = pd.read_csv(file) elif file.name.endswith('.xlsx'): df = pd.read_excel(file) if column_name in df.columns: reviews.extend(df[column_name].tolist()) except Exception as e: return f"Error processing file: {str(e)}" if not reviews: return "No reviews provided." entity = Entity(name="Movie") ratings = entity.rate(reviews=reviews) ratings_df = pd.DataFrame([ratings]) # Extract LM6 score and format it for display lm6_score = ratings.get('LM6', 0) formatted_lm6 = f"
LM6 Rating: {lm6_score:.2f}
" return ratings_df, formatted_lm6 # Interface components with gr.Blocks(css=".gradio app { font-family: Arial; }") as demo: gr.Markdown("### Movie Ratings Calculator") with gr.Row(): text_input = gr.Textbox(label="Enter Reviews (line-separated)", placeholder="Enter one review per line", lines=10) file_input = gr.File( label="Upload a CSV or Excel file with reviews", file_types=["csv", "xlsx"], file_count="multiple" ) column_input = gr.Textbox(label="Column Name", placeholder="Enter the column name that contains the reviews") output_table = gr.Dataframe() output_lm6 = gr.HTML() button = gr.Button("Calculate Ratings") button.click( fn=rate_movies, inputs=[text_input, file_input, column_input], outputs=[output_table, output_lm6] ) demo.launch()