File size: 5,605 Bytes
479c5b5
d1c6587
ebf808d
8f66103
3360e0e
 
 
0f61c06
b516947
3360e0e
8f66103
b516947
 
 
 
479c5b5
3360e0e
b516947
3360e0e
 
8f66103
3360e0e
 
 
 
 
 
 
 
 
 
 
 
 
d1c6587
 
b516947
3360e0e
d1c6587
 
 
 
 
ebf808d
 
b516947
ebf808d
 
3360e0e
ebf808d
 
37ae3c7
 
 
 
b516947
3360e0e
 
 
 
 
 
 
 
d1c6587
3360e0e
d1c6587
3360e0e
b516947
 
 
 
3360e0e
b516947
 
 
 
 
 
 
 
3360e0e
 
b516947
 
 
 
 
 
 
 
 
 
 
 
 
3360e0e
 
 
8f66103
3360e0e
 
 
 
 
cdb3883
3360e0e
 
 
 
cdb3883
3360e0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb3883
3360e0e
 
 
cdb3883
3360e0e
 
8f66103
cdb3883
 
d1c6587
3360e0e
c0529d2
3360e0e
 
 
 
cdb3883
c0529d2
3360e0e
 
 
 
d1c6587
479c5b5
3360e0e
479c5b5
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
import gradio as gr
import subprocess
import json
import requests
import re
import pandas as pd
import openai
from bs4 import BeautifulSoup

# Simple function to strip html

def strip_html_tags(html_text):
    # Use BeautifulSoup to parse and clean HTML content
    soup = BeautifulSoup(html_text, 'html.parser')
    return soup.get_text()

def html_posts_to_table(html_posts):

    subject_pattern = r"Subject: (.*?)\n"
    body_text_pattern = r"Body Text: (.*?)\n"

    subjects = re.findall(subject_pattern, html_posts)
    body_texts = re.findall(body_text_pattern, html_posts)

    data = {
        'Subject': subjects,
        'Body Text': body_texts
    }

    df = pd.DataFrame(data)

    return(df)

def api_call(board_id, api_key):
    curl_command = [
        'curl', '-s', '--request', 'GET',
        '--url', f"https://api.padlet.dev/v1/boards/{board_id}?include=posts%2Csections",
        '--header', f"X-Api-Key: {api_key}",
        '--header', 'accept: application/vnd.api+json'
    ]
    
    try:
        response = subprocess.check_output(curl_command, universal_newlines=True)
        response_data = json.loads(response)
        
        # Extract the contents of all posts, stripping HTML tags from bodyHtml
        posts_data = response_data.get("included", [])
        post_contents = []

        for post in posts_data:
            if post.get("type") == "post":
                attributes = post.get("attributes", {}).get("content", {})
                subject = attributes.get("subject", "")
                body_html = attributes.get("bodyHtml", "")
                
                if subject:
                    cleaned_body = strip_html_tags(body_html)
                    post_contents.append({"subject": subject, "content": cleaned_body})

        if post_contents:
            df = pd.DataFrame(post_contents)
            return df
        else:
            return pd.DataFrame({"subject": ["No post contents found."], "content": [""]})
    except subprocess.CalledProcessError:
        return pd.DataFrame({"subject": ["Error: Unable to fetch data using cURL."], "content": [""]})

def create_post(subject, post_content, board_id, api_key):

    curl_command = [
        'curl', '-s', '--request', 'POST',
        '--url', f"https://api.padlet.dev/v1/boards/{board_id}/posts",
        '--header', f"X-Api-Key: {api_key}",
        '--header', 'accept: application/vnd.api+json',
        '--header', 'content-type: application/vnd.api+json',
        '--data',
        json.dumps({
            "data": {
                "type": "post",
                "attributes": {
                    "content": {
                        "subject": subject,
                        "body": post_content
                    }
                }
            }
        })
    ]
    
    try:
        response = subprocess.check_output(curl_command, universal_newlines=True)
        response_data = json.loads(response)
        return "Post created successfully."
    except subprocess.CalledProcessError as e:
        return f"Error: Unable to create post - {str(e)}"

def posts_to_prompt(padlet_posts):
    post_prompt = padlet_posts.apply(lambda row: f"{row['subject']} {row['content']}", axis=1).str.cat(sep=', ')
    return post_prompt

def remove_html_tags(text):
    # Use a regular expression to remove HTML tags
    clean = re.compile('<.*?>')
    return re.sub(clean, '', text)

def summarize_padlet_posts(padlet_posts, openai_api_key, system_prompt):
    # Concatenate padlet post df
    post_prompt = posts_to_prompt(padlet_posts)

    # Set the system prompt with more specific instructions
    system_prompt = system_prompt

    # Set the prompt for the GPT-3.5 model
    prompt = system_prompt + "\n" + post_prompt  # Added a newline after system_prompt

    try:
        # Make the API call to GPT-3.5
        response = openai.Completion.create(
            engine="text-davinci-003",  # GPT-3.5 engine
            prompt=prompt,
            max_tokens=1000,  # Limit response length for concise summaries
            api_key=openai_api_key,
            temperature=0.5  # Adjust temperature as needed
        )

        # Extract and return the summary, removing leading newlines and HTML tags
        summary = response.choices[0].text.lstrip('\n')
        summary = remove_html_tags(summary)
        return summary
    except Exception as e:
        return f"Error: {str(e)}"

def summarize_padlets(input_board_id, output_board_id, padlet_api, openai_api, system_prompt):

    posts_to_summarize = api_call(input_board_id, padlet_api)

    summary = summarize_padlet_posts(posts_to_summarize, openai_api, system_prompt)

    create_post("Summary",summary, output_board_id, padlet_api)

    return(summary)

iface = gr.Interface(
    fn=summarize_padlets,
    inputs=[
        gr.inputs.Textbox(label="Input Board ID"),
        gr.inputs.Textbox(label="Output Board ID"),
        gr.inputs.Textbox(label="Padlet API Key", type="password"),
        gr.inputs.Textbox(label="OpenAI API Key", type="password", placeholder="sk.."),
        gr.inputs.Textbox(label="System Prompt", default = "You are an AI assistant tasked with summarizing the main points of the following Padlet posts. Please provide a concise summary of the posts based on their content.")
    ],
    outputs=gr.outputs.Textbox(label="Summary"),
    live=False,  # Set to True to show the result without clicking a button
    title="Padlet Summarization",
    description="Summarize Padlet posts and create a summary post on another board using OpenAI GPT3.5.",
)

# Run the Gradio interface
iface.launch()