wisalkhanmv commited on
Commit
dccfd4a
·
verified ·
1 Parent(s): afe62d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -54
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 clean_reviews(text_reviews):
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 file is not None and review_column:
18
- df = pd.read_csv(file.name)
19
- if review_column in df.columns:
20
- df[review_column] = df[review_column].dropna()
21
- reviews.extend(df[review_column].astype(str).tolist())
22
- else:
23
- return "Invalid column name", "| Aspect | Rating |\n| --- | --- |\n| No data | No data |"
24
-
25
- if text_reviews:
26
- reviews.extend(clean_reviews(text_reviews))
27
-
 
 
 
28
  if not reviews:
29
- return "No reviews to rate", "| Aspect | Rating |\n| --- | --- |\n| No data | No data |"
30
-
31
  entity = Entity(name="Movie")
32
- try:
33
- rating = entity.rate(reviews=reviews)
34
- except Exception as e:
35
- return f"Error in rating: {e}", "| Aspect | Rating |\n| --- | --- |\n| No data | No data |"
36
-
37
- lm6_rating = rating.get("LM6")
38
- lm6_rating_scaled = scale_rating(lm6_rating) if lm6_rating is not None else "No LM6 rating available"
39
- aspects_output = "| Aspect | Rating |\n| --- | --- |\n"
40
- for aspect, value in rating.items():
41
- if aspect != "LM6":
42
- scaled_value = scale_rating(value)
43
- aspects_output += f"| {aspect} | {scaled_value:.2f}% |\n"
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
- with gr.Column(scale=1):
50
- file_input = gr.File(label="Upload CSV")
51
- column_input = gr.Textbox(label="Enter Review Column Name")
52
- with gr.Column(scale=1):
53
- text_reviews = gr.Textbox(lines=10, placeholder="Enter comma-separated reviews here...", label="Text Reviews")
54
-
55
- output_lm6 = gr.Textbox(label="LM6 Rating")
56
- output_aspects = gr.Markdown(label="Aspect Ratings")
57
-
58
- gr.Button("Rate Reviews").click(
59
- fn=rate_reviews,
60
- inputs=[file_input, column_input, text_reviews],
61
- outputs=[output_lm6, output_aspects]
 
 
 
62
  )
63
 
64
- app.launch(share=True)
 
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()