import gradio as gr import pandas as pd import numpy as np from google_play_scraper import reviews_all, Sort import plotly.express as px from transformers import pipeline import re # Load the sentiment analysis pipeline sentiment_analysis = pipeline("sentiment-analysis", model="siebert/sentiment-roberta-large-english") #test # Define function to process the app link, fetch reviews, analyze sentiment, and return results def analyze_sentiment(applink): # Extract the app ID from the link match = re.search(r'id=([^&]+)', applink) if not match: return "App code could not be extracted.", None extracted_code = match.group(1) # Fetch all reviews for the app project = reviews_all(extracted_code, sleep_milliseconds=0, lang='en', country='IN', sort=Sort.NEWEST) # Normalize the reviews data into a DataFrame df = pd.json_normalize(project) # Convert content to string type df['content'] = df['content'].astype('str') # Perform sentiment analysis on the reviews df['result'] = df['content'].apply(lambda x: sentiment_analysis(x)) # Extract sentiment label and score df['sentiment'] = df['result'].apply(lambda x: x[0]['label']) df['score'] = df['result'].apply(lambda x: x[0]['score']) # Display sentiment analysis counts sentiment_counts = df['sentiment'].value_counts(normalize=True) sentiment_text = sentiment_counts.to_frame().to_html() # Generate the sentiment analysis histogram fig = px.histogram(df, x='sentiment', color='sentiment', title='Sentiment Analysis', text_auto=True) return sentiment_text, fig # Set up Gradio interface app_link_input = gr.Textbox(label="Enter the Google Play app link:") html_output = gr.HTML(label="Sentiment Analysis Summary") plot_output = gr.Plot(label="Sentiment Analysis Histogram") # Create Gradio interface interface = gr.Interface( fn=analyze_sentiment, inputs=app_link_input, outputs=[html_output, plot_output], title="Google Play App Sentiment Analysis", description="Enter a Google Play app link to fetch and analyze user reviews for sentiment." ) # Launch the app interface.launch()