Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import yake | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| # --- Model (small, CPU-friendly) | |
| MODEL_NAME = "google/flan-t5-small" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME) | |
| # Keyword extractor | |
| def extract_keywords(text, lang="en", max_kw=8): | |
| kw_extractor = yake.KeywordExtractor(lan=lang, n=1, top=max_kw) | |
| kws = kw_extractor.extract_keywords(text) | |
| return [kw for kw, score in kws] | |
| # Simple SEO scoring | |
| def seo_score(title, description, keywords, tags): | |
| score = 0 | |
| # Title rules | |
| if title and title.strip(): | |
| score += 15 | |
| L = len(title) | |
| if 40 <= L <= 70: | |
| score += 20 | |
| elif 30 <= L < 40 or 71 <= L <= 90: | |
| score += 10 | |
| # Keywords in title/description | |
| kw_in_title = sum(1 for k in keywords if k.lower() in (title or "").lower()) | |
| kw_in_desc = sum(1 for k in keywords if k.lower() in (description or "").lower()) | |
| score += min(20, kw_in_title * 10) | |
| score += min(15, kw_in_desc * 5) | |
| # Description length | |
| dlen = len(description or "") | |
| if dlen >= 300: | |
| score += 20 | |
| elif dlen >= 150: | |
| score += 10 | |
| # Tags | |
| if tags: | |
| if 3 <= len(tags) <= 15: | |
| score += 10 | |
| elif len(tags) > 15: | |
| score += 5 | |
| return min(100, score) | |
| # Generate titles & description | |
| def gen_suggestions(main_text, keywords, max_titles=3): | |
| prompt = ( | |
| "You are an assistant that generates catchy YouTube video titles and an SEO-optimized description.\n" | |
| f"Content: {main_text}\n" | |
| f"Keywords: {', '.join(keywords)}\n" | |
| "Produce 3 short titles (<70 chars each) separated by || and one SEO-friendly description after ---" | |
| ) | |
| inputs = tokenizer(prompt, return_tensors="pt") | |
| out = model.generate(**inputs, max_new_tokens=300) | |
| text = tokenizer.decode(out[0], skip_special_tokens=True) | |
| # Split | |
| if "||" in text: | |
| parts = text.split("---") | |
| titles = parts[0].split("||") | |
| desc = parts[1].strip() if len(parts) > 1 else "" | |
| else: | |
| lines = [l.strip() for l in text.split("\n") if l.strip()] | |
| titles = lines[:max_titles] | |
| desc = "\n".join(lines[max_titles:]) | |
| titles = [t.strip() for t in titles if t.strip()] | |
| return titles[:max_titles], desc | |
| # Main function | |
| def analyze(title, description, lang_choice):_ | |