File size: 2,030 Bytes
32f82e2
 
55f8962
 
 
32f82e2
 
55f8962
dccfd4a
c643395
dccfd4a
 
 
 
 
 
 
 
 
 
 
 
 
 
c643395
dccfd4a
 
32f82e2
dccfd4a
 
 
 
 
 
 
 
 
 
 
 
c643395
dccfd4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c643395
32f82e2
dccfd4a
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
56
57
58
59
60
61
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"<div style='color: orange; font-size: 48px; font-weight: bold; text-align: center;'>LM6 Rating: {lm6_score:.2f}</div>"
    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()