Spaces:
Runtime error
Runtime error
Create app.py
#1
by
Ugurrrrr - opened
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yake
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
# --- Model (small, CPU-friendly)
|
| 6 |
+
MODEL_NAME = "google/flan-t5-small"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 9 |
+
|
| 10 |
+
# Keyword extractor
|
| 11 |
+
def extract_keywords(text, lang="en", max_kw=8):
|
| 12 |
+
kw_extractor = yake.KeywordExtractor(lan=lang, n=1, top=max_kw)
|
| 13 |
+
kws = kw_extractor.extract_keywords(text)
|
| 14 |
+
return [kw for kw, score in kws]
|
| 15 |
+
|
| 16 |
+
# Simple SEO scoring
|
| 17 |
+
def seo_score(title, description, keywords, tags):
|
| 18 |
+
score = 0
|
| 19 |
+
# Title rules
|
| 20 |
+
if title and title.strip():
|
| 21 |
+
score += 15
|
| 22 |
+
L = len(title)
|
| 23 |
+
if 40 <= L <= 70:
|
| 24 |
+
score += 20
|
| 25 |
+
elif 30 <= L < 40 or 71 <= L <= 90:
|
| 26 |
+
score += 10
|
| 27 |
+
# Keywords in title/description
|
| 28 |
+
kw_in_title = sum(1 for k in keywords if k.lower() in (title or "").lower())
|
| 29 |
+
kw_in_desc = sum(1 for k in keywords if k.lower() in (description or "").lower())
|
| 30 |
+
score += min(20, kw_in_title * 10)
|
| 31 |
+
score += min(15, kw_in_desc * 5)
|
| 32 |
+
# Description length
|
| 33 |
+
dlen = len(description or "")
|
| 34 |
+
if dlen >= 300:
|
| 35 |
+
score += 20
|
| 36 |
+
elif dlen >= 150:
|
| 37 |
+
score += 10
|
| 38 |
+
# Tags
|
| 39 |
+
if tags:
|
| 40 |
+
if 3 <= len(tags) <= 15:
|
| 41 |
+
score += 10
|
| 42 |
+
elif len(tags) > 15:
|
| 43 |
+
score += 5
|
| 44 |
+
return min(100, score)
|
| 45 |
+
|
| 46 |
+
# Generate titles & description
|
| 47 |
+
def gen_suggestions(main_text, keywords, max_titles=3):
|
| 48 |
+
prompt = (
|
| 49 |
+
"You are an assistant that generates catchy YouTube video titles and an SEO-optimized description.\n"
|
| 50 |
+
f"Content: {main_text}\n"
|
| 51 |
+
f"Keywords: {', '.join(keywords)}\n"
|
| 52 |
+
"Produce 3 short titles (<70 chars each) separated by || and one SEO-friendly description after ---"
|
| 53 |
+
)
|
| 54 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 55 |
+
out = model.generate(**inputs, max_new_tokens=300)
|
| 56 |
+
text = tokenizer.decode(out[0], skip_special_tokens=True)
|
| 57 |
+
# Split
|
| 58 |
+
if "||" in text:
|
| 59 |
+
parts = text.split("---")
|
| 60 |
+
titles = parts[0].split("||")
|
| 61 |
+
desc = parts[1].strip() if len(parts) > 1 else ""
|
| 62 |
+
else:
|
| 63 |
+
lines = [l.strip() for l in text.split("\n") if l.strip()]
|
| 64 |
+
titles = lines[:max_titles]
|
| 65 |
+
desc = "\n".join(lines[max_titles:])
|
| 66 |
+
titles = [t.strip() for t in titles if t.strip()]
|
| 67 |
+
return titles[:max_titles], desc
|
| 68 |
+
|
| 69 |
+
# Main function
|
| 70 |
+
def analyze(title, description, lang_choice):_
|