venirdev commited on
Commit
f912b02
·
1 Parent(s): 425b579

Initial Commit : AI Hashtag Generator

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +43 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from fastapi import FastAPI, Form
4
+ from fastapi.responses import JSONResponse
5
+
6
+ app = FastAPI()
7
+
8
+ # Secret key from Hugging Face Space -> Settings -> Variables and secrets
9
+ SECRET_API_KEY = os.getenv("SECRET_API_KEY", "changeme")
10
+
11
+ @app.post("/autocomplete")
12
+ async def autocomplete(text: str = Form(...), api_key: str = Form(...)):
13
+ # 🔑 Validate API key
14
+ if api_key != SECRET_API_KEY:
15
+ return JSONResponse(
16
+ status_code=403,
17
+ content={"status": False, "body": "Invalid API key."}
18
+ )
19
+
20
+ # 📝 Validate input
21
+ if not text.strip():
22
+ return JSONResponse(
23
+ status_code=400,
24
+ content={"status": False, "body": "Missing 'text' parameter."}
25
+ )
26
+
27
+ try:
28
+ url = f"https://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q={requests.utils.quote(text)}"
29
+ resp = requests.get(url, timeout=5)
30
+
31
+ if resp.status_code != 200:
32
+ return {"status": False, "body": f"YouTube request failed: {resp.status_code}"}
33
+
34
+ data = resp.json()
35
+
36
+ if not isinstance(data, list) or len(data) < 2:
37
+ return {"status": False, "body": "Invalid response format from YouTube"}
38
+
39
+ suggestions = data[1] # second element is suggestion array
40
+ return {"status": True, "body": suggestions}
41
+
42
+ except Exception as e:
43
+ return {"status": False, "body": str(e)}
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ requests