Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py (runs on Hugging Face Spaces w/ Firebase Firestore)
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
|
7 |
+
import hashlib
|
8 |
+
|
9 |
+
import requests
|
10 |
+
|
11 |
+
import firebase_admin
|
12 |
+
|
13 |
+
from firebase_admin import credentials, firestore
|
14 |
+
|
15 |
+
import os
|
16 |
+
|
17 |
+
from io import BytesIO
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
# 🔐 Firebase Setup (replace with your Firebase project credentials)
|
22 |
+
|
23 |
+
cred_json = {
|
24 |
+
|
25 |
+
"type": "service_account",
|
26 |
+
|
27 |
+
"project_id": "YOUR_PROJECT_ID",
|
28 |
+
|
29 |
+
"private_key_id": "YOUR_KEY_ID",
|
30 |
+
|
31 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\\nYOUR_PRIVATE_KEY\\n-----END PRIVATE KEY-----\\n",
|
32 |
+
|
33 |
+
"client_email": "[email protected]",
|
34 |
+
|
35 |
+
"client_id": "your_client_id",
|
36 |
+
|
37 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
38 |
+
|
39 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
40 |
+
|
41 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
42 |
+
|
43 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-xxx%40your-project.iam.gserviceaccount.com"
|
44 |
+
|
45 |
+
}
|
46 |
+
|
47 |
+
if not firebase_admin._apps:
|
48 |
+
|
49 |
+
cred = credentials.Certificate(cred_json)
|
50 |
+
|
51 |
+
firebase_admin.initialize_app(cred)
|
52 |
+
|
53 |
+
db = firestore.client()
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
# 🤖 Hugging Face Emoji Gen
|
58 |
+
|
59 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
60 |
+
|
61 |
+
HF_MODEL = "valhalla/emoji-diffusion"
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
def hf_generate_emoji(prompt):
|
66 |
+
|
67 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
68 |
+
|
69 |
+
payload = {"inputs": prompt}
|
70 |
+
|
71 |
+
response = requests.post(
|
72 |
+
|
73 |
+
f"https://api-inference.huggingface.co/models/{HF_MODEL}",
|
74 |
+
|
75 |
+
headers=headers,
|
76 |
+
|
77 |
+
json=payload,
|
78 |
+
|
79 |
+
)
|
80 |
+
|
81 |
+
if response.status_code != 200:
|
82 |
+
|
83 |
+
raise ValueError("Hugging Face API failed")
|
84 |
+
|
85 |
+
return Image.open(BytesIO(response.content))
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
# 🎯 Fingerprint = hash of emoji image
|
90 |
+
|
91 |
+
def compute_fingerprint(img: Image.Image) -> str:
|
92 |
+
|
93 |
+
gray = img.convert("L").resize((32, 32))
|
94 |
+
|
95 |
+
return hashlib.sha256(gray.tobytes()).hexdigest()
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
# 🏷 Overlay micro-ad / sponsor
|
100 |
+
|
101 |
+
def embed_ad(img: Image.Image, tag: str) -> Image.Image:
|
102 |
+
|
103 |
+
draw = ImageDraw.Draw(img)
|
104 |
+
|
105 |
+
font = ImageFont.load_default()
|
106 |
+
|
107 |
+
draw.text((img.width - 30, img.height - 12), tag[:3].upper(), fill="yellow", font=font)
|
108 |
+
|
109 |
+
return img
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
# 🧠 Full pipeline: gen → fingerprint → save to Firestore
|
114 |
+
|
115 |
+
def process(prompt, sponsor=""):
|
116 |
+
|
117 |
+
img = hf_generate_emoji(prompt).resize((128,128))
|
118 |
+
|
119 |
+
if sponsor:
|
120 |
+
|
121 |
+
img = embed_ad(img, sponsor)
|
122 |
+
|
123 |
+
fingerprint = compute_fingerprint(img)
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
doc_ref = db.collection("emojis").document(fingerprint)
|
128 |
+
|
129 |
+
doc = doc_ref.get()
|
130 |
+
|
131 |
+
if not doc.exists:
|
132 |
+
|
133 |
+
doc_ref.set({
|
134 |
+
|
135 |
+
"prompt": prompt,
|
136 |
+
|
137 |
+
"sponsor": sponsor,
|
138 |
+
|
139 |
+
"usage": 0,
|
140 |
+
|
141 |
+
})
|
142 |
+
|
143 |
+
|
144 |
+
|
145 |
+
return img, fingerprint
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
# 🔁 Increment emoji usage count
|
150 |
+
|
151 |
+
def use_emoji(fp):
|
152 |
+
|
153 |
+
doc_ref = db.collection("emojis").document(fp)
|
154 |
+
|
155 |
+
doc = doc_ref.get()
|
156 |
+
|
157 |
+
if doc.exists:
|
158 |
+
|
159 |
+
new_usage = doc.to_dict()["usage"] + 1
|
160 |
+
|
161 |
+
doc_ref.update({"usage": new_usage})
|
162 |
+
|
163 |
+
return f"Usage count updated: {new_usage}"
|
164 |
+
|
165 |
+
return "Emoji not found"
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
# 🎛 Gradio App
|
170 |
+
|
171 |
+
with gr.Blocks() as demo:
|
172 |
+
|
173 |
+
gr.Markdown("# 🧬 Emoji Factory: Generate, Track & Embed Micro-Ads")
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
with gr.Row():
|
178 |
+
|
179 |
+
prompt = gr.Textbox(label="Enter your idea prompt (e.g. pig vault rocket)")
|
180 |
+
|
181 |
+
sponsor = gr.Textbox(label="Sponsor / Ad Tag (optional)")
|
182 |
+
|
183 |
+
gen_btn = gr.Button("🚀 Generate Emoji")
|
184 |
+
|
185 |
+
emoji_out = gr.Image(label="Generated Emoji")
|
186 |
+
|
187 |
+
fp_out = gr.Textbox(label="Emoji Fingerprint")
|
188 |
+
|
189 |
+
use_btn = gr.Button("🔁 Use Emoji")
|
190 |
+
|
191 |
+
result = gr.Textbox(label="Usage Count / Message")
|
192 |
+
|
193 |
+
|
194 |
+
|
195 |
+
gen_btn.click(fn=process, inputs=[prompt, sponsor], outputs=[emoji_out, fp_out])
|
196 |
+
|
197 |
+
use_btn.click(fn=use_emoji, inputs=[fp_out], outputs=[result])
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
demo.launch()
|