File size: 3,562 Bytes
eac952a
357d82f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eac952a
357d82f
 
 
 
 
 
 
 
 
 
 
 
 
 
eac952a
357d82f
eac952a
 
357d82f
eac952a
357d82f
 
 
eac952a
357d82f
 
eac952a
357d82f
 
eac952a
357d82f
 
eac952a
357d82f
 
eac952a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357d82f
eac952a
357d82f
eac952a
 
 
 
 
 
 
 
357d82f
eac952a
8d085fe
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import gradio as gr
import pandas as pd
import google.generativeai as genai
import os
import json
import re

# Set your API key here directly
os.environ["API_KEY"] = "AIzaSyAFHyRhWWEVGTzNXH3xHq8vBx229DzVkPM"
genai.configure(api_key=os.environ["API_KEY"])
model = genai.GenerativeModel("gemini-1.5-flash")

# Load schema for Gemini model (if needed for your specific summarization task)
with open("./scheme.json", "r") as f:
    gemini_flash_schema = json.load(f)

# Preprocess text function
def preprocess_text(text):
    stopwords = {
        "the", "is", "in", "at", "on", "a", "an", "and", "or", "for", "to", "of", "with", "that", "by", "it",
    }
    text = re.sub(r"\d+|[^\w\s]|\s+", " ", text.lower()).strip()
    return " ".join([word for word in text.split() if word not in stopwords])

# Generate sentiment and grade using Gemini
def generate_review_grade_with_sentiment(review_text):
    try:
        prompt = f"""
        Analyze the following review: {review_text}.
        Determine its sentiment (positive, neutral, or negative) based on your analysis...
        """
        response = model.generate_content(prompt)

        # Extract only sentiment and grade
        sentiment_match = re.search(r"(positive|negative|neutral)", response.text, re.IGNORECASE)
        grade_match = re.search(r"\d(\.\d+)?", response.text)

        if sentiment_match and grade_match:
            sentiment_label = sentiment_match.group().upper()
            grade = float(grade_match.group())
            return sentiment_label, grade
        else:
            return "Unknown", None
    except Exception as e:
        return f"Error: {e}"

# Define function to analyze product reviews
def analyze_product_reviews(product_name):
    default_encoding = "latin1"
    result = {}
    try:
        df = pd.read_csv("/content/English_Reviews_WithNewDateISO&IDColumn-WhichIdon'tAgreeOn.csv", encoding=default_encoding)
    except UnicodeDecodeError as e:
        return f"Error reading file: {e}"

    filtered_reviews = df[df["product_name"].str.contains(product_name, case=False)]
    
    if not filtered_reviews.empty:
        combined_reviews_text = " ".join(filtered_reviews["product_review_name"].tolist())
        
        # Summarize reviews
        summary = generate_summary(combined_reviews_text)
        
        # Generate pros and cons
        pros, cons = generate_pros_and_cons(combined_reviews_text)
        
        # Process reviews for grading
        grades = []
        for _, row in filtered_reviews.iterrows():
            review_text = preprocess_text(row["product_review_name"])
            sentiment_label, grade = generate_review_grade_with_sentiment(review_text)
            grades.append({
                "review": row['product_review_name'],
                "sentiment": sentiment_label,
                "grade": grade
            })
        
        result = {
            "summary": summary,
            "pros": pros,
            "cons": cons,
            "grades": grades,
        }
    else:
        result = {"error": "No reviews found for product."}

    return result

# Gradio Interface
interface = gr.Interface(
    fn=analyze_product_reviews,
    inputs=gr.Textbox(label="Enter Product Name"),
    outputs=gr.JSON(label="Analysis Result"),
    title="Product Review Analyzer and Grader",
    description="Analyze product reviews to generate summary, pros, cons, and grading."
)

if __name__ == "__main__":
    # Launch the interface with external access
    interface.launch(server_name="0.0.0.0", server_port=7860, share=True)