import os import gradio as gr import requests Rapid_API = os.getenv("rapidAPIkey") def get_hashtags(keyword): url = "https://hashtagy-generate-hashtags.p.rapidapi.com/v1/comprehensive/tags" headers = { "X-RapidAPI-Key": Rapid_API, "X-RapidAPI-Host": "hashtagy-generate-hashtags.p.rapidapi.com" } response = requests.get(url, headers=headers, params={"keyword": keyword}) response_json = response.json() data = response_json['data'] hashtags = data['best_30_hashtags']['hashtags'] return ', '.join(hashtags) def get_post_count(tag): url = "https://hashtag5.p.rapidapi.com/api/v2.1/tag/count" headers = { "X-RapidAPI-Key": Rapid_API, "X-RapidAPI-Host": "hashtag5.p.rapidapi.com" } response = requests.get(url, headers=headers, params={"tag": tag}) response_json = response.json() print(response.json()) count = response_json['count'] return count def search_hashtags(keyword, get_count): hashtags = get_hashtags(keyword) if get_count: # Assume the first hashtag is the most relevant one for count first_hashtag = hashtags.split(',')[0].strip() count = get_post_count(first_hashtag) return hashtags, f"The post count for {first_hashtag} is {count}" else: return hashtags, None iface = gr.Interface(fn=search_hashtags, inputs=["text", gr.inputs.Checkbox(label="Get post count for first hashtag")], outputs=["text", "text"], title="Hashtag Search", description="Search for hashtags and optionally get the post count for the first hashtag") iface.launch()