import gradio as gr
from datasets import load_dataset
import pandas as pd
from jinja2 import Template
import datetime
import random
import weasyprint
import requests
from bs4 import BeautifulSoup
css = '''
.gradio-container{max-width: 1300px !important}
h1{text-align:center}
.submit-btn {
background-color: #a753be !important;
color: white !important;
}
.submit-btn:hover {
background-color: #d94eff !important;
}
'''
dataset = load_dataset("maxiw/hf-posts", split="train")
df = pd.DataFrame(dataset)
df["Name"] = df["author"].apply(lambda x: x["fullname"])
df["username"] = df["author"].apply(lambda x: x["name"])
df["totalReactions"] = df["reactions"].apply(lambda x: sum([_["count"] for _ in x]))
df["Num of posts"] = 1
metrics = ["totalUniqueImpressions", "totalReactions", "numComments", "Num of posts"]
data = (
df.groupby(["username", "Name"])[metrics]
.sum()
.reset_index()
)
data["Rank"] = data["totalUniqueImpressions"].rank(method="min", ascending=False).astype(int)
receipt_template = """
HuggingFace Receipt
#HF_POSTS🤗
{{ current_date }}
ORDER #{{ order_id }}
{{ user_name }}
User Name:
{{ username }}
Details |
Value |
NO. OF POSTS |
{{ num_posts }} |
REACTIONS EARNED |
{{ total_reactions }} |
TOTAL COMMENTS |
{{ num_comments }} |
IMPRESSIONS |
{{ total_impressions }} |
RANKING |
{{ rank }} |
Thank you for using HuggingFace!
"""
def fetch_profile_photo(username):
url = f"https://huggingface.co/{username}"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
img_tag = soup.find('img', {'class': 'h-32 w-32 overflow-hidden rounded-full shadow-inner lg:h-48 lg:w-48'})
if img_tag:
return img_tag['src']
return None
def generate_receipt(username):
user_data = data[data['username'] == username]
if user_data.empty:
return "User not found"
else:
current_date = datetime.datetime.now().strftime("%A, %B %d, %Y")
current_time = datetime.datetime.now().strftime("%I:%M:%S %p")
order_id = random.randint(1000, 9999)
user_name = user_data['Name'].values[0]
profile_photo = fetch_profile_photo(username)
num_posts = user_data['Num of posts'].values[0]
total_reactions = user_data['totalReactions'].values[0]
num_comments = user_data['numComments'].values[0]
total_impressions = user_data['totalUniqueImpressions'].values[0]
rank = user_data['Rank'].values[0]
template = Template(receipt_template)
html_content = template.render(
current_date=current_date,
order_id=order_id,
user_name=user_name,
username=username,
profile_photo=profile_photo,
num_posts=num_posts,
total_reactions=total_reactions,
num_comments=num_comments,
total_impressions=total_impressions,
rank=rank
)
html_path = "receipt.html"
with open(html_path, "w") as f:
f.write(html_content)
pdf_path = "receipt.pdf"
weasyprint.HTML(html_path).write_pdf(pdf_path)
return html_path, pdf_path
iface = gr.Interface(
fn=generate_receipt,
inputs="text",
outputs=["file", "file"],
title="HF_POSTS_RECEIPTS 🤗",
description="Enter a username to generate a receipt with post details.",
examples=[
["prithivMLmods"],
["clem"],
["maxiw"],
["merve"],
["reach-vb"],
["fdaudens"],
["akhaliq"],
["thomwolf"]
],
css=css,
theme="bethecloud/storj_theme",
)
iface.launch(share=True)