Spaces:
Runtime error
Runtime error
| 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() | |