Download app.py from Slash-eg/Product-Summarizer: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/Slash-eg/Product-Summarizer/resolve/main/app.py
- Command line
-
hf download hf://Slash-eg/Product-Summarizer/app.py
-
curl -L -o app.py https://huggingface.co/Slash-eg/Product-Summarizer/resolve/main/app.py
3.56 kB
| 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) | |