File size: 6,351 Bytes
613e758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bcd2144
 
 
 
613e758
bcd2144
 
613e758
 
 
bcd2144
613e758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6626587
613e758
6626587
 
 
 
 
 
 
 
 
 
613e758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6626587
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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)