File size: 12,930 Bytes
dfc4131
 
8bc7dc5
 
dfc4131
 
 
 
8bc7dc5
 
dfc4131
 
 
 
 
 
 
8bc7dc5
dfc4131
8bc7dc5
dfc4131
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bc7dc5
 
 
dfc4131
 
 
 
 
 
 
 
 
 
 
 
 
8bc7dc5
dfc4131
 
 
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
8bc7dc5
 
 
 
dfc4131
8bc7dc5
 
 
 
dfc4131
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
8bc7dc5
dfc4131
8bc7dc5
dfc4131
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bc7dc5
dfc4131
 
 
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
8bc7dc5
 
 
 
dfc4131
 
 
 
 
 
 
 
8bc7dc5
dfc4131
 
 
 
 
8bc7dc5
dfc4131
8bc7dc5
dfc4131
8bc7dc5
dfc4131
8bc7dc5
 
dfc4131
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
 
 
8bc7dc5
dfc4131
 
8bc7dc5
dfc4131
 
8bc7dc5
dfc4131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from transformers import pipeline

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

from load_model import call_palm
from calling_apis import google_api_key, openai_api_key

class meta_content_generation:
    
    """
    Class meta_content_generation:
    Provides methods to generate content for social media platforms using Language Models (LLMs) and prompt templates.
    """
        


    def facebook_ads_gen(self, product_name:str, product_description:str, tone_of_voice:str, targeted_audience="", plans_promotions="", language='En', model_name='Google Palm 2', creativity='Original')->str:
        
        """
        Generates a Facebook ad based on the provided product details and parameters.

        Parameters:
            - product_name (str): Name of the product for the ad.
            - product_description (str): Description of the product for the ad.
            - tone_of_voice (str): Tone of the ad.
            - targeted_audience (str): Target audience for the ad (optional).
            - plans_promotions (str): Plans and promotions for the ad (optional).

        Returns:
            - Generated Facebook ad text.
        """

        temp = 0

        if creativity == 'Original':
            temp = 0
        elif creativity == 'Balanced':
            temp = 0.25
        elif creativity == 'Creative':
            temp = 0.5
        elif creativity == 'Spirited':
            temp = 0.75
        elif creativity == 'Visionary':
            temp = 1


        if model_name == 'Google Palm 2':
            llm = call_palm(google_api_key, temperature=temp)

        elif model_name == 'GPT-3.5':
            llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)

        elif model_name == 'GPT-4':
            llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)


        
        if language == 'En':
            
            if targeted_audience != "" and plans_promotions != "":
                facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Our Target Audience is {targeted_audience}. Our plans and promotions is {plans_promotions}. Tone of the ad should be {tone_of_voice}"

            elif targeted_audience == "" and plans_promotions != "":
                facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Our plans and promotions is {plans_promotions}. Tone of the ad should be {tone_of_voice}"

            elif targeted_audience != "" and plans_promotions == "":
                facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Our Target Audience is {targeted_audience}. Tone of the ad should be{tone_of_voice}."

            else:
                facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Tone of the ad Should be {tone_of_voice}."
        
        elif language == 'Ar':
            
            if targeted_audience != "" and plans_promotions != "":
                facebook_ads_prompt = f"ุงู†ุดุฆ ุฅุนู„ุงู†ู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู…ู†ุชุฌ {product_name}. {product_name} ู‡ูˆ {product_description}. ุฌู…ู‡ูˆุฑู†ุง ุงู„ู…ุณุชู‡ุฏู ู‡ูˆ {targeted_audience}. ุฎุทุทู†ุง ูˆุนุฑูˆุถู†ุง ู‡ูŠ {plans_promotions}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุชูˆุฌู‡ ุงู„ุฅุนู„ุงู† {tone_of_voice}."

            elif targeted_audience == "" and plans_promotions != "":
                facebook_ads_prompt = f"ู‚ู… ุจุฅู†ุดุงุก ุฅุนู„ุงู† ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู€ {product_name} ุงู„ู…ู†ุชุฌ. {product_name} ู‡ูˆ {product_description}. ุฎุทุทู†ุง ูˆุนุฑูˆุถู†ุง ู‡ูŠ {plans_promotions}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุชูˆู† ุงู„ุฅุนู„ุงู† {tone_of_voice}"

            elif targeted_audience != "" and plans_promotions == "":
                facebook_ads_prompt = f"ุฃู†ุดุฆ ุฅุนู„ุงู†ู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู…ู†ุชุฌ {product_name}. {product_name} ู‡ูˆ {product_description}. ุฌู…ู‡ูˆุฑู†ุง ุงู„ู…ุณุชู‡ุฏู ู‡ูˆ {targeted_audience}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุทุงุจุน ุงู„ุฅุนู„ุงู† {tone_of_voice}"

            else:
                facebook_ads_prompt = f"ุงู†ุดุฆ ุฅุนู„ุงู†ู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู€ {product_name} ุงู„ู…ู†ุชุฌ. {product_name} ู‡ูˆ {product_description}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ู†ุจุฑุฉ ุงู„ุฅุนู„ุงู† {tone_of_voice}."

                
        facebook_ads_promptTemp = PromptTemplate(
        input_variables=["text_input"],
        template="You are a Professional Facebook Ad Copywriter:\n{text_input}\nFacebook Ad:")
    
        facebook_ad_extraction_chain = LLMChain(llm=llm, prompt=facebook_ads_promptTemp)
        facebook_ad = facebook_ad_extraction_chain.run(facebook_ads_prompt)
        
        return facebook_ad
    
    def facbook_camp_gen(self, product_name:str, product_desc:str, days:int, goal:str, language='En', model_name='Google Palm 2', creativity='Original') -> str:
        
         """
        Generates a Facebook Campign based on the provided product details and goals.

        Parameters:
            - product_name (str): Name of the product for the ad.
            - product_desc (str): Description of the product for the ad.
            - days (int): Number of campaign days.
            - goal (str): Goal of the campaign.
            - language (str): the language of the generated content (optional, default value is 'EN').

        Returns:
            - Facebook_ad (str): Generated Facebook Campaign .
        """
         temp = 0

         if creativity == 'Original':
            temp = 0
         elif creativity == 'Balanced':
            temp = 0.25
         elif creativity == 'Creative':
            temp = 0.5
         elif creativity == 'Spirited':
            temp = 0.75
         elif creativity == 'Visionary':
            temp = 1


         if model_name == 'Google Palm 2':
            llm = call_palm(google_api_key, temperature=temp)

         elif model_name == 'GPT-3.5':
            llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)

         elif model_name == 'GPT-4':
            llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)


         if language == 'En':
            
            facebook_ads_prompt = f"Generate a {days} days Persuative Facebook campaign (don't mention any budget) calendar for our {product_name}. {product_name} is {product_desc}. with the goal to {goal}."
            facebook_ads_promptTemp = PromptTemplate(
            input_variables=["text_input"],
            template="""You are a Professional Facebook Digital Marketer:\n{text_input}\nFacebook Campaign:""")
        
         elif language == 'Ar':
            
            facebook_ads_prompt = f"ู‚ู… ุจุฅู†ุดุงุก ุญู…ู„ุฉ ูุงูŠุณุจูˆูƒ ู‚ุงุฆู…ุฉ ู„ู…ุฏุฉ {days} ูŠูˆู…ู‹ุง (ุฏูˆู† ุงู„ุฅุดุงุฑุฉ ุฅู„ู‰ ุฃูŠ ู…ูŠุฒุงู†ูŠุฉ) ู„ู€ {product_name} ุงู„ุฎุงุต ุจู†ุง. {product_name} ู‡ูˆ {product_desc}. ุจู‡ุฏู {goal}."
            facebook_ads_promptTemp = PromptTemplate(
            input_variables=["text_input"],
            template="ุฃู†ุช ู…ุณูˆู‘ู‚ ุฑู‚ู…ูŠ ู…ุญุชุฑู ุนู„ู‰ ููŠุณุจูˆูƒ:\n{text_input}\nุญู…ู„ุฉ ููŠุณุจูˆูƒ:")
        
         facebook_ad_extraction_chain = LLMChain(llm=llm, prompt=facebook_ads_promptTemp)
         facebook_ad = facebook_ad_extraction_chain.run(facebook_ads_prompt)
        
         return facebook_ad

    def facebook_post_gen(self, tone_of_voice:str, topic:str, language='En', model_name='Google Palm 2', creativity='Original') -> str:
        
        """
        Generates a Facebook Post based on the provided topic.

        Parameters:
            - tone_of_voice (str): The tone of the facebook post.
            - topic (str): The facebook post topic
            - language (str): the language of the generated content (optional, default value is 'EN').

        Returns:
            - Facebook_post (str): Generated Facebook Post .
        """
        
        temp = 0

        if creativity == 'Original':
            temp = 0
        elif creativity == 'Balanced':
            temp = 0.25
        elif creativity == 'Creative':
            temp = 0.5
        elif creativity == 'Spirited':
            temp = 0.75
        elif creativity == 'Visionary':
            temp = 1


        if model_name == 'Google Palm 2':
            llm = call_palm(google_api_key, temperature=temp)

        elif model_name == 'GPT-3.5':
            llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)

        elif model_name == 'GPT-4':
            llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)


        if language == 'En':
            
            productDesc_prompt = f"Generate an attractive and persuasive facebook post on {topic}. Tone should be {tone_of_voice}. Post dosen't include any photos or videos."
            productDesc_promptTemp = PromptTemplate(
            input_variables=["text_input"],
            template="You are a professional facebook content creator:\n{text_input}\n\nFacebook Post:")
    
        
        elif language == 'Ar':
            
            productDesc_prompt = f"ุงู†ุดุฑ ู…ู†ุดูˆุฑู‹ุง ุฌุฐุงุจู‹ุง ูˆู…ู‚ู†ุนู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ุญูˆู„ {topic}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุงู„ู†ุจุฑุฉ {tone_of_voice}. ู„ุง ูŠุชุถู…ู† ุงู„ู…ู†ุดูˆุฑ ุฃูŠ ุตูˆุฑ ุฃูˆ ู…ู‚ุงุทุน ููŠุฏูŠูˆ."
            productDesc_promptTemp = PromptTemplate(
            input_variables=["text_input"],
            template="ุฃู†ุช ู…ุจุฏุน ู…ุญุชุฑู ููŠ ุฅู†ุดุงุก ู…ุญุชูˆู‰ ุนู„ู‰ ููŠุณุจูˆูƒ:\n{text_input}\n\nู…ู†ุดูˆุฑ ููŠุณุจูˆูƒ:")

        productDesc_extraction_chain = LLMChain(llm=llm, prompt=productDesc_promptTemp)
        product_desc = productDesc_extraction_chain.run(productDesc_prompt)
        
        return product_desc

    def img2text(self, url:str) -> str:
        
         """
        Function to extract text from an image using a pre-trained image captioning model.

        Parameters:
            self: The instance of a class (if this function is part of a class).
            url (str): The URL pointing to the image for which text extraction is required.

        Returns:
            out (str): The extracted text from the provided image.
        """
            
         image_to_text = pipeline("image-to-text", model='Salesforce/blip-image-captioning-base')

         text = image_to_text(url)

         out = text[0]['generated_text']
        
         return out


    def generate_InstaCap(self, scenario:str, tone_of_voice:str, form:str, language='En', model_name='Google Palm 2', creativity='Original') -> str:
        
        """
        Generates a Instagram Caption based on the image description.

        Parameters:
            - tone_of_voice (str): The tone of the facebook post.
            - scenario (str): The description of the image
            - form (str): the form of the caption (short - long) forms

        Returns:
            - instaCap (str): Generated Image Caption.
        """
        
        temp = 0

        if creativity == 'Original':
            temp = 0
        elif creativity == 'Balanced':
            temp = 0.25
        elif creativity == 'Creative':
            temp = 0.5
        elif creativity == 'Spirited':
            temp = 0.75
        elif creativity == 'Visionary':
            temp = 1


        if model_name == 'Google Palm 2':
            llm = call_palm(google_api_key, temperature=temp)

        elif model_name == 'GPT-3.5':
            llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)

        elif model_name == 'GPT-4':
            llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)


        if language == 'En':
            
            instaCap_prompt = f"Craft a {form} Caption on my Instagram Image Here is the description of my Instagram Image: {scenario}.\nThe tone should be {tone_of_voice}"
            instaCap_promptTemp = PromptTemplate(
            input_variables=["text_input"],
            template="You are infulencer:\n{text_input}\nInstagram Caption:")
        
        elif language == 'Ar':
            
            instaCap_prompt = f"ุตูŠุงุบุฉ ุชูˆุถูŠุญ {form} ุนู„ู‰ ุตูˆุฑุฉ ุฅู†ุณุชุบุฑุงู… ุงู„ุฎุงุตุฉ ุจูŠ. ุฅู„ูŠูƒ ูˆุตู ุตูˆุฑุฉ ุฅู†ุณุชุบุฑุงู…ูŠ: {scenario}.ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุงู„ุชูˆู† {tone_of_voice}."
            instaCap_promptTemp = PromptTemplate(
            input_variables=["text_input"],
            template="ุฃู†ุช ู…ุคุซุฑ: \n{text_input}\n ูˆุตู ุจูˆุณุช ุงู„ุงู†ุณุชุบุฑุงู…:")
            
        instaCap_extraction_chain = LLMChain(llm=llm, prompt=instaCap_promptTemp)
        instaCap = instaCap_extraction_chain.run(instaCap_prompt)
        
        return instaCap