File size: 2,312 Bytes
7c467fb
 
 
 
 
 
 
f07250b
7c467fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup
import json
import os

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate


EVENTBRITE_API_KEY = os.environ['EVENTBRITE_API_KEY']

def get_event_id(url):
    uid = url.split('?')[0]
    uid = uid.split('/')[-1]
    uid = uid.split('-')[-1]
    return uid

def get_title_subtitle_from_event(event_url):
    # Get the title and subtitle
    res = requests.get(url=event_url)
    soup = BeautifulSoup(res.content, 'html.parser')
    tag_classes = {'title': 'event-title',
                   'subtitle': 'summary',
                   'details': 'has-user-generated-content'}
    title = soup.find(class_ = tag_classes['title']).text
    subtitle = soup.find(class_ = tag_classes['subtitle']).text
    return title, subtitle

def get_event_details(event_url):
    # Now get details:

    event_id = get_event_id(event_url)
    headers = {'Authorization': 'Bearer {}'.format(EVENTBRITE_API_KEY)}
    params = {}

    base_url = "https://www.eventbriteapi.com/v3/events/{id}/structured_content/"

    r = requests.get(base_url.format(id=event_id), headers=headers, params=params)
    effective_encoding =  'utf-8-sig' #r.apparent_encoding  #
    r.encoding = effective_encoding

    res_tree = json.loads(r.text)

    content = res_tree['modules'][0]['data']['body']['text']
    content = content.replace('\ufeff', '')  # Remove byte order mark for utf-8-sig decoding.

    soup = BeautifulSoup(content, 'html.parser')

    details = soup.get_text(separator=' ')
    return details

def get_eventbrite_summary(event_url, top_level_prompt_stub):

    title, subtitle = get_title_subtitle_from_event(event_url)
    details = get_event_details(event_url)

    temperature = 0.3
    openai_modeltype = "text-davinci-003"

    top_level_prompt = top_level_prompt_stub + """
    Event Title:{title}
    Event Subtitle: {subtitle}
    Event Description:
    {description}"""
    
    prompt = PromptTemplate(
        input_variables=['title', 'subtitle', 'description'],
        template=top_level_prompt,
    )

    chat_prompt = prompt.format_prompt(title=title, subtitle=subtitle, description=details)

    llm = OpenAI(model_name=openai_modeltype, temperature=temperature)
    res = llm(chat_prompt.to_messages()[0].content)
    return res.strip()