Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +19 -0
- main.py +72 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
# Install dependencies
|
| 6 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
# Copy the app
|
| 10 |
+
COPY . /code
|
| 11 |
+
|
| 12 |
+
# Set permissions (Required by HF Spaces)
|
| 13 |
+
RUN useradd -m -u 1000 user
|
| 14 |
+
USER user
|
| 15 |
+
ENV HOME=/home/user \
|
| 16 |
+
PATH=/home/user/.local/bin:$PATH
|
| 17 |
+
|
| 18 |
+
# Run the API on port 7860
|
| 19 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from transformers import AutoTokenizer, pipeline
|
| 6 |
+
from rake_nltk import Rake
|
| 7 |
+
|
| 8 |
+
# --- Setup & Model Loading ---
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
# CRITICAL: Allow your Frontend to access this API
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"], # Allows all origins (Safe for public free APIs)
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
print("Loading Models...")
|
| 21 |
+
# Download NLTK data
|
| 22 |
+
try:
|
| 23 |
+
nltk.data.find('tokenizers/punkt_tab')
|
| 24 |
+
except LookupError:
|
| 25 |
+
nltk.download('stopwords')
|
| 26 |
+
nltk.download('punkt_tab')
|
| 27 |
+
|
| 28 |
+
# Load AI Models (Cached in memory)
|
| 29 |
+
t5_tokenizer = AutoTokenizer.from_pretrained("Michau/t5-base-en-generate-headline", use_fast=False)
|
| 30 |
+
title_pipe = pipeline("text2text-generation", model="Michau/t5-base-en-generate-headline", tokenizer=t5_tokenizer)
|
| 31 |
+
|
| 32 |
+
bart_tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn", use_fast=False)
|
| 33 |
+
desc_pipe = pipeline("summarization", model="facebook/bart-large-cnn", tokenizer=bart_tokenizer)
|
| 34 |
+
print("Models Ready!")
|
| 35 |
+
|
| 36 |
+
# --- Logic ---
|
| 37 |
+
class VideoInput(BaseModel):
|
| 38 |
+
text: str
|
| 39 |
+
|
| 40 |
+
def get_tags(text):
|
| 41 |
+
rake = Rake()
|
| 42 |
+
rake.extract_keywords_from_text(text)
|
| 43 |
+
phrases = rake.get_ranked_phrases()[:5]
|
| 44 |
+
hashtags = ["#" + p.replace(" ", "") for p in phrases]
|
| 45 |
+
tags = [p.replace(" ", "") for p in phrases]
|
| 46 |
+
return hashtags, tags
|
| 47 |
+
|
| 48 |
+
@app.get("/")
|
| 49 |
+
def home():
|
| 50 |
+
return {"status": "API is running. POST to /generate"}
|
| 51 |
+
|
| 52 |
+
@app.post("/generate")
|
| 53 |
+
async def generate(payload: VideoInput):
|
| 54 |
+
text = payload.text
|
| 55 |
+
if not text.strip():
|
| 56 |
+
raise HTTPException(status_code=400, detail="Empty text")
|
| 57 |
+
|
| 58 |
+
# 1. Generate Title
|
| 59 |
+
title_out = title_pipe("headline: " + text, max_new_tokens=70, do_sample=False)
|
| 60 |
+
|
| 61 |
+
# 2. Generate Description
|
| 62 |
+
desc_out = desc_pipe(text, max_new_tokens=150, do_sample=False)
|
| 63 |
+
|
| 64 |
+
# 3. Get Tags
|
| 65 |
+
hashtags, tags = get_tags(text)
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
"title": title_out[0]["generated_text"],
|
| 69 |
+
"description": desc_out[0]["summary_text"],
|
| 70 |
+
"hashtags": hashtags,
|
| 71 |
+
"tags": tags
|
| 72 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
sentencepiece
|
| 6 |
+
rake-nltk
|
| 7 |
+
nltk
|