Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,63 +2,55 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from lowerated.rate.entity import Entity
|
4 |
|
5 |
-
def
|
6 |
-
# Clean and split the reviews
|
7 |
-
reviews = text_reviews.strip().replace("\n", ", ").split(",")
|
8 |
-
reviews = [review.strip() for review in reviews if review.strip()]
|
9 |
-
return reviews
|
10 |
-
|
11 |
-
def scale_rating(value, min_rating=-1, max_rating=10):
|
12 |
-
# Scale the rating to the range -1 to 10
|
13 |
-
return ((value - min_rating) / (max_rating - min_rating)) * 200 - 100
|
14 |
-
|
15 |
-
def rate_reviews(file, review_column, text_reviews):
|
16 |
reviews = []
|
17 |
-
if
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
if not reviews:
|
29 |
-
return "No reviews
|
30 |
-
|
31 |
entity = Entity(name="Movie")
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
return f"{lm6_rating_scaled:.2f}%", aspects_output
|
46 |
-
|
47 |
-
with gr.Blocks(css=".input, .output {padding: 10px;} .label {font-weight: bold;}") as app:
|
48 |
with gr.Row():
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
gr.
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
62 |
)
|
63 |
|
64 |
-
|
|
|
2 |
import pandas as pd
|
3 |
from lowerated.rate.entity import Entity
|
4 |
|
5 |
+
def rate_movies(reviews_text, review_file, column_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
reviews = []
|
7 |
+
if reviews_text:
|
8 |
+
reviews = reviews_text.split("\n")
|
9 |
+
elif review_file is not None:
|
10 |
+
try:
|
11 |
+
for file in review_file:
|
12 |
+
if file.name.endswith('.csv'):
|
13 |
+
df = pd.read_csv(file)
|
14 |
+
elif file.name.endswith('.xlsx'):
|
15 |
+
df = pd.read_excel(file)
|
16 |
+
if column_name in df.columns:
|
17 |
+
reviews.extend(df[column_name].tolist())
|
18 |
+
except Exception as e:
|
19 |
+
return f"Error processing file: {str(e)}"
|
20 |
+
|
21 |
if not reviews:
|
22 |
+
return "No reviews provided."
|
23 |
+
|
24 |
entity = Entity(name="Movie")
|
25 |
+
ratings = entity.rate(reviews=reviews)
|
26 |
+
|
27 |
+
ratings_df = pd.DataFrame([ratings])
|
28 |
+
|
29 |
+
# Extract LM6 score and format it for display
|
30 |
+
lm6_score = ratings.get('LM6', 0)
|
31 |
+
formatted_lm6 = f"<div style='color: orange; font-size: 48px; font-weight: bold; text-align: center;'>LM6 Rating: {lm6_score:.2f}</div>"
|
32 |
+
return ratings_df, formatted_lm6
|
33 |
+
|
34 |
+
# Interface components
|
35 |
+
with gr.Blocks(css=".gradio app { font-family: Arial; }") as demo:
|
36 |
+
gr.Markdown("### Movie Ratings Calculator")
|
|
|
|
|
|
|
|
|
37 |
with gr.Row():
|
38 |
+
text_input = gr.Textbox(label="Enter Reviews (line-separated)", placeholder="Enter one review per line", lines=10)
|
39 |
+
file_input = gr.File(
|
40 |
+
label="Upload a CSV or Excel file with reviews",
|
41 |
+
file_types=["csv", "xlsx"],
|
42 |
+
file_count="multiple"
|
43 |
+
)
|
44 |
+
column_input = gr.Textbox(label="Column Name", placeholder="Enter the column name that contains the reviews")
|
45 |
+
|
46 |
+
output_table = gr.Dataframe()
|
47 |
+
output_lm6 = gr.HTML()
|
48 |
+
button = gr.Button("Calculate Ratings")
|
49 |
+
|
50 |
+
button.click(
|
51 |
+
fn=rate_movies,
|
52 |
+
inputs=[text_input, file_input, column_input],
|
53 |
+
outputs=[output_table, output_lm6]
|
54 |
)
|
55 |
|
56 |
+
demo.launch()
|