Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,72 +1,68 @@
|
|
| 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 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
nltk.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
print("Models Ready!")
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
rake
|
| 42 |
-
rake.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
"title": title_out[0]["generated_text"],
|
| 69 |
-
"description": desc_out[0]["summary_text"],
|
| 70 |
-
"hashtags": hashtags,
|
| 71 |
-
"tags": tags
|
| 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 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_credentials=True,
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
print("Loading Models...")
|
| 19 |
+
try:
|
| 20 |
+
nltk.data.find('tokenizers/punkt_tab')
|
| 21 |
+
except LookupError:
|
| 22 |
+
nltk.download('stopwords')
|
| 23 |
+
nltk.download('punkt_tab')
|
| 24 |
+
|
| 25 |
+
# 1. TITLE MODEL (Keep this, or swap to 't5-small' if still desperate)
|
| 26 |
+
t5_tokenizer = AutoTokenizer.from_pretrained("Michau/t5-base-en-generate-headline", use_fast=False)
|
| 27 |
+
title_pipe = pipeline("text2text-generation", model="Michau/t5-base-en-generate-headline", tokenizer=t5_tokenizer)
|
| 28 |
+
|
| 29 |
+
# 2. DESCRIPTION MODEL (>>> CHANGED TO DISTILBART <<<)
|
| 30 |
+
# This model is 3x faster and smaller than bart-large-cnn
|
| 31 |
+
bart_tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6", use_fast=False)
|
| 32 |
+
desc_pipe = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", tokenizer=bart_tokenizer)
|
| 33 |
+
|
| 34 |
+
print("Models Ready!")
|
| 35 |
+
|
| 36 |
+
class VideoInput(BaseModel):
|
| 37 |
+
text: str
|
| 38 |
+
|
| 39 |
+
def get_tags(text):
|
| 40 |
+
rake = Rake()
|
| 41 |
+
rake.extract_keywords_from_text(text)
|
| 42 |
+
phrases = rake.get_ranked_phrases()[:5]
|
| 43 |
+
hashtags = ["#" + p.replace(" ", "") for p in phrases]
|
| 44 |
+
tags = [p.replace(" ", "") for p in phrases]
|
| 45 |
+
return hashtags, tags
|
| 46 |
+
|
| 47 |
+
@app.get("/")
|
| 48 |
+
def home():
|
| 49 |
+
return {"status": "API is running."}
|
| 50 |
+
|
| 51 |
+
@app.post("/generate")
|
| 52 |
+
async def generate(payload: VideoInput):
|
| 53 |
+
text = payload.text
|
| 54 |
+
if not text.strip():
|
| 55 |
+
raise HTTPException(status_code=400, detail="Empty text")
|
| 56 |
+
|
| 57 |
+
# Lower max_new_tokens slightly to speed up generation
|
| 58 |
+
title_out = title_pipe("headline: " + text, max_new_tokens=50, do_sample=False)
|
| 59 |
+
desc_out = desc_pipe(text, max_new_tokens=100, do_sample=False)
|
| 60 |
+
|
| 61 |
+
hashtags, tags = get_tags(text)
|
| 62 |
+
|
| 63 |
+
return {
|
| 64 |
+
"title": title_out[0]["generated_text"],
|
| 65 |
+
"description": desc_out[0]["summary_text"],
|
| 66 |
+
"hashtags": hashtags,
|
| 67 |
+
"tags": tags
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|