File size: 3,353 Bytes
8537019
aa4f694
8537019
 
3e68ccf
 
 
 
 
aa4f694
 
 
 
 
e55d16a
3e68ccf
 
 
 
 
 
 
 
 
 
e55d16a
3e68ccf
 
 
 
 
 
 
 
e55d16a
3e68ccf
 
 
 
 
 
 
aa4f694
3e68ccf
e55d16a
3e68ccf
 
 
 
 
 
 
e55d16a
3e68ccf
 
e55d16a
 
3e68ccf
e55d16a
 
 
3e68ccf
e55d16a
 
3e68ccf
e55d16a
3e68ccf
 
 
 
8537019
 
 
c6643c7
8537019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4f694
 
8537019
 
 
 
 
 
 
 
 
aa4f694
 
8537019
 
 
aa4f694
8537019
 
 
aa4f694
8537019
 
 
 
 
 
 
 
 
3e68ccf
8537019
 
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
import json
import logging
import time
import requests
from langchain.llms import Clarifai

from global_config import GlobalConfig


logging.basicConfig(
    level=GlobalConfig.LOG_LEVEL,
    format='%(asctime)s - %(message)s',
)

llm = None


def get_llm(use_gpt: bool) -> Clarifai:
    """
    Get a large language model.

    :param use_gpt: True if GPT-3.5 is required; False is Llama 2 is required
    """

    if use_gpt:
        _ = Clarifai(
            pat=GlobalConfig.CLARIFAI_PAT,
            user_id=GlobalConfig.CLARIFAI_USER_ID_GPT,
            app_id=GlobalConfig.CLARIFAI_APP_ID_GPT,
            model_id=GlobalConfig.CLARIFAI_MODEL_ID_GPT,
            verbose=True,
            # temperature=0.1,
        )
    else:
        _ = Clarifai(
            pat=GlobalConfig.CLARIFAI_PAT,
            user_id=GlobalConfig.CLARIFAI_USER_ID,
            app_id=GlobalConfig.CLARIFAI_APP_ID,
            model_id=GlobalConfig.CLARIFAI_MODEL_ID,
            verbose=True,
            # temperature=0.1,
        )
    # print(llm)

    return _


def generate_slides_content(topic: str) -> str:
    """
    Generate the outline/contents of slides for a presentation on a given topic.

    :param topic: Topic/subject matter/idea on which slides are to be generated
    :return: The content in JSON format
    """

    # global prompt
    global llm

    with open(GlobalConfig.SLIDES_TEMPLATE_FILE, 'r') as in_file:
        template_txt = in_file.read().strip()
        template_txt = template_txt.replace('<REPLACE_PLACEHOLDER>', topic)

    if llm is None:
        llm = get_llm(use_gpt=True)

    slides_content = llm(template_txt, verbose=True)

    return slides_content


def get_ai_image(text: str) -> str:
    """
    Get a Stable Diffusion-generated image based on a given text.

    :param text: The input text
    :return: The Base 64-encoded image
    """

    url = f'''https://api.clarifai.com/v2/users/{GlobalConfig.CLARIFAI_USER_ID_SD}/apps/{GlobalConfig.CLARIFAI_APP_ID_SD}/models/{GlobalConfig.CLARIFAI_MODEL_ID_SD}/versions/{GlobalConfig.CLARIFAI_MODEL_VERSION_ID_SD}/outputs'''
    headers = {
        "Content-Type": "application/json",
        "Authorization": f'Key {GlobalConfig.CLARIFAI_PAT}'
    }
    data = {
        "inputs": [
            {
                "data": {
                    "text": {
                        "raw": text
                    }
                }
            }
        ]
    }

    # print('*** AI image generator...')
    # print(url)

    start = time.time()
    response = requests.post(
        url=url,
        headers=headers,
        data=json.dumps(data)
    )
    stop = time.time()

    # print('Response:', response, response.status_code)
    logging.debug('Image generation took', stop - start, 'seconds')
    img_data = ''

    if response.ok:
        # print('*** Clarifai SDXL request: Response OK')
        json_data = json.loads(response.text)
        img_data = json_data['outputs'][0]['data']['image']['base64']
    else:
        logging.error('*** Image generation failed:', response.text)

    return img_data


if __name__ == '__main__':
    # results = get_related_websites('5G AI WiFi 6')
    #
    # for a_result in results.results:
    #     print(a_result.title, a_result.url, a_result.extract)

    # get_ai_image('A talk on AI, covering pros and cons')
    pass