File size: 3,290 Bytes
0e6ad50
 
 
 
 
 
1874289
 
 
 
 
 
 
 
 
 
 
0e6ad50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1874289
0e6ad50
 
 
 
 
 
 
 
 
 
 
1874289
0e6ad50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os

from pytrends.request import TrendReq
from openai import OpenAI

pytrends = TrendReq(
    hl="en-US",
    tz=360,
    timeout=(10, 25),
    proxies=[
        "https://34.203.233.13:80",
    ],
    retries=2,
    backoff_factor=0.1,
    requests_args={"verify": False},
)

kw_list = [""]
client = OpenAI(
    # This is the default and can be omitted
    api_key=os.getenv("openaikey"),
)


def fetch_clothing_themes_and_generate_banner_2(geo, start_date, end_date):
    # Initialize pytrends and OpenAI client
    pytrends = TrendReq(hl="en-US", tz=360)
    # openai.api_key = "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"

    # Define the keywords list for clothing related searches
    kw_list = [""]

    # Build payload for given geo and date range
    timeframe = f"{start_date} {end_date}"
    pytrends.build_payload(kw_list, timeframe=timeframe, geo=geo)

    # Fetch related queries
    all_top_queries = pytrends.related_queries()

    # Extract top and rising queries
    top_queries = all_top_queries[""]["top"]
    rising_queries = all_top_queries[""]["rising"]

    # Format the queries for the ChatGPT prompt
    formatted_queries = ", ".join(
        top_queries["query"].tolist() + rising_queries["query"].tolist()
    )

    # Create a prompt for ChatGPT
    # prompt = f"From the following top and rising keywords in {geo} from {start_date} to {end_date}: {formatted_queries}, suggest the most fun and entertaining theme related to clothing. Select a topic based on one of the keywords. Just specify the theme with one sentence description of its fashion style. Make the description suitable for a metaverse avatar"
    prompt = f"Out of all the follwing keywords, which one is the most fun for a clothing themed topic? {formatted_queries}. Ignore commonly used words or apps like 'weather', 'tiktok' or 'instagram'. Focus on events that could be popular. Reply with a small phrase"
    print(prompt)

    # Pass the prompt to ChatGPT API
    chat_completion = client.chat.completions.create(
        model="gpt-4-1106-preview",
        messages=[
            # {"role": "system", "content": "You are a fashion expert."},
            {"role": "user", "content": prompt},
        ],
    )

    # Extract the theme suggestion
    theme_suggestion = chat_completion.choices[0].message.content

    return theme_suggestion, all_top_queries


def greet(city, start_date_yyyy_mm_dd, end_date_yyyy_mm_dd):
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"ISO 3166-2 code for {city}. Only reply with one word. Reply GLOBAL if invalid",
            }
        ],
        model="gpt-3.5-turbo-1106",
    )
    geo = chat_completion.choices[0].message.content.strip()

    timeframe = f"{start_date_yyyy_mm_dd} {end_date_yyyy_mm_dd}"
    pytrends.build_payload(kw_list, timeframe=timeframe, geo=geo)
    all_top_queries = pytrends.related_queries()
    top_queries = all_top_queries[""]["top"]
    rising_queries = all_top_queries[""]["rising"]

    return top_queries, rising_queries


demo = gr.Interface(
    fn=greet,
    inputs=["text", "text", "text"],
    outputs=["dataframe", "dataframe"],
)
if __name__ == "__main__":
    demo.launch()