import requests import pandas as pd import time from datetime import datetime from dotenv import load_dotenv import os import gradio as gr load_dotenv() XAI_API_KEY = os.getenv("XAI_API_KEY") # Global variable to store the most recent analysis results GLOBAL_ANALYSIS_STORAGE = { 'subreddit': None, 'data': None } def call_LLM(query): return call_groq(query) def call_groq(query): from groq import Groq client = Groq() chat_completion = client.chat.completions.create( messages=[ {"role": "system", "content": query} ], model="llama3-8b-8192", temperature=0.5, max_tokens=1024, top_p=1, stop=None, stream=False, ) return chat_completion.choices[0].message.content def process(row): """ Format this so that the model sees full post for now """ # title # comment_body prompt = f"The below is a reddit post. Take a look and tell me if there is a business problem to be solved here ||| title: {row['post_title']} ||| comment: {row['comment_body']}" return call_LLM(prompt) def fetch_top_comments(subreddit): df = pd.read_csv('comments.csv') filtered_df = df[df['subreddit'] == subreddit] return filtered_df def fetch_subreddits(): return pd.read_csv('subreddits.csv') def show_dataframe(subreddit): # Fetch top comments for these posts data_to_analyze = fetch_top_comments(subreddit) # Process and analyze each comment responses = [] for _, row in data_to_analyze.iterrows(): print(f"{_} done") responses.append(process(row)) # Add analysis to the dataframe data_to_analyze['analysis'] = responses # Store in global storage for quick access GLOBAL_ANALYSIS_STORAGE['subreddit'] = subreddit GLOBAL_ANALYSIS_STORAGE['data'] = data_to_analyze return data_to_analyze def launch_interface(): # Fetch list of subreddits for user to choose from sub_reddits = fetch_subreddits() subreddit_list = sub_reddits["display_name"].tolist() # Create Gradio Blocks for more flexible interface with gr.Blocks() as demo: # Title and author gr.Markdown("# Reddit Business Problem Analyzer") gr.Markdown("**Discover potential business opportunities from Reddit discussions**") gr.Markdown("### Created by [@matthewjgunton](https://x.com/matthewjgunton)", elem_id="twitter-handle") with gr.Accordion("Instructions", open=False): gr.Markdown(""" 1. **Select a Subreddit:** Use the dropdown to choose a subreddit to analyze. 2. **View Results:** The analysis table shows posts, comments, and AI-generated insights. 3. **Detailed View:** Enter a row index to view detailed analysis and links for a specific post. """, visible=True) # Subreddit selection subreddit_dropdown = gr.Dropdown( choices=subreddit_list, label="Select Subreddit", info="Choose a subreddit to analyze" ) # Outputs with gr.Row(): with gr.Column(): # Overall Analysis Section gr.Markdown("## Overall Analysis") # Results Table results_table = gr.Dataframe( label="Analysis Results", headers=["Index", "Post Title", "Comment", "Analysis"], interactive=False ) # Row Selection row_index = gr.Number( label="Select Row Index for Detailed View", precision=0 ) with gr.Column(): # Detailed Post Analysis gr.Markdown("## Detailed Post Analysis") detailed_analysis = gr.Markdown( label="Detailed Insights" ) # Function to update posts when subreddit is selected def update_posts(subreddit): # Fetch and analyze data data_to_analyze = show_dataframe(subreddit) # Prepare table data table_data = data_to_analyze[['post_title', 'comment_body', 'analysis']].reset_index() table_data.columns = ['Index', 'Post Title', 'Comment', 'Analysis'] return table_data, None # Function to show detailed analysis for a specific row def show_row_details(row_index): # Ensure we have data loaded if GLOBAL_ANALYSIS_STORAGE['data'] is None: return "Please select a subreddit first." try: # Convert to integer and subtract 1 (since index is 0-based) row_index = int(row_index) # Retrieve the specific row row_data = GLOBAL_ANALYSIS_STORAGE['data'].loc[row_index] # Format detailed view detailed_view = f""" ### Post Details **Title:** {row_data.get('post_title', 'N/A')} **Comment:** {row_data.get('comment_body', 'N/A')} **Comment Score:** {row_data.get('comment_score', 'N/A')} **Analysis:** {row_data.get('analysis', 'No analysis available')} **Post URL:** {row_data.get('post_url', 'N/A')} **Comment URL:** {row_data.get('comment_url', 'N/A')} """ return detailed_view except (KeyError, ValueError, TypeError) as e: return f"Error retrieving row details: {str(e)}" # Event Listeners subreddit_dropdown.change( fn=update_posts, inputs=subreddit_dropdown, outputs=[results_table, detailed_analysis] ) row_index.change( fn=show_row_details, inputs=row_index, outputs=detailed_analysis ) return demo # Launch the interface if __name__ == "__main__": interface = launch_interface() interface.launch(share=True)