Spaces:
Runtime error
Runtime error
initial commit
Browse files- app.py +87 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
|
4 |
+
from pytrends.request import TrendReq
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
pytrends = TrendReq(hl="en-US")
|
8 |
+
|
9 |
+
kw_list = [""]
|
10 |
+
client = OpenAI(
|
11 |
+
# This is the default and can be omitted
|
12 |
+
api_key=os.getenv("openaikey"),
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
def fetch_clothing_themes_and_generate_banner_2(geo, start_date, end_date):
|
17 |
+
# Initialize pytrends and OpenAI client
|
18 |
+
pytrends = TrendReq(hl="en-US", tz=360)
|
19 |
+
# openai.api_key = "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"
|
20 |
+
|
21 |
+
# Define the keywords list for clothing related searches
|
22 |
+
kw_list = [""]
|
23 |
+
|
24 |
+
# Build payload for given geo and date range
|
25 |
+
timeframe = f"{start_date} {end_date}"
|
26 |
+
pytrends.build_payload(kw_list, timeframe=timeframe, geo=geo)
|
27 |
+
|
28 |
+
# Fetch related queries
|
29 |
+
all_top_queries = pytrends.related_queries()
|
30 |
+
|
31 |
+
# Extract top and rising queries
|
32 |
+
top_queries = all_top_queries[""]["top"]
|
33 |
+
rising_queries = all_top_queries[""]["rising"]
|
34 |
+
|
35 |
+
# Format the queries for the ChatGPT prompt
|
36 |
+
formatted_queries = ", ".join(
|
37 |
+
top_queries["query"].tolist() + rising_queries["query"].tolist()
|
38 |
+
)
|
39 |
+
|
40 |
+
# Create a prompt for ChatGPT
|
41 |
+
# 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"
|
42 |
+
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"
|
43 |
+
print(prompt)
|
44 |
+
|
45 |
+
# Pass the prompt to ChatGPT API
|
46 |
+
chat_completion = client.chat.completions.create(
|
47 |
+
model="gpt-4-1106-preview",
|
48 |
+
messages=[
|
49 |
+
# {"role": "system", "content": "You are a fashion expert."},
|
50 |
+
{"role": "user", "content": prompt},
|
51 |
+
],
|
52 |
+
)
|
53 |
+
|
54 |
+
# Extract the theme suggestion
|
55 |
+
theme_suggestion = chat_completion.choices[0].message.content
|
56 |
+
|
57 |
+
return theme_suggestion, all_top_queries
|
58 |
+
|
59 |
+
|
60 |
+
def greet(city, start_date_dd_mm_yyyy, end_date_dd_mm_yyyy):
|
61 |
+
chat_completion = client.chat.completions.create(
|
62 |
+
messages=[
|
63 |
+
{
|
64 |
+
"role": "user",
|
65 |
+
"content": f"ISO 3166-2 code for {city}. Only reply with one word. Reply GLOBAL if invalid",
|
66 |
+
}
|
67 |
+
],
|
68 |
+
model="gpt-3.5-turbo-1106",
|
69 |
+
)
|
70 |
+
geo = chat_completion.choices[0].message.content.strip()
|
71 |
+
|
72 |
+
timeframe = f"{start_date_dd_mm_yyyy} {end_date_dd_mm_yyyy}"
|
73 |
+
pytrends.build_payload(kw_list, timeframe=timeframe, geo=geo)
|
74 |
+
all_top_queries = pytrends.related_queries()
|
75 |
+
top_queries = all_top_queries[""]["top"]
|
76 |
+
rising_queries = all_top_queries[""]["rising"]
|
77 |
+
|
78 |
+
return top_queries, rising_queries
|
79 |
+
|
80 |
+
|
81 |
+
demo = gr.Interface(
|
82 |
+
fn=greet,
|
83 |
+
inputs=["text", "text", "text"],
|
84 |
+
outputs=["dataframe", "dataframe"],
|
85 |
+
)
|
86 |
+
if __name__ == "__main__":
|
87 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
pytrends
|
3 |
+
pycountry
|
4 |
+
holidays
|