Spaces:
Sleeping
Sleeping
File size: 1,690 Bytes
fd3ec12 9732dd2 fd3ec12 9732dd2 ff61b19 9732dd2 fd3ec12 |
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 |
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() |