umarabbas890's picture
Update app.py
b2d21ea verified
import gradio as gr
import random
import requests
import os
# ==== CONFIG ====
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Set this in Hugging Face secrets
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
GROQ_MODEL = "llama3-8b-8192" # Or use llama3-70b-8192
# === Dummy topic generator ===
def generate_trending_topics(niche):
sample_topics = {
"finance": ["How to Save $10,000 in 6 Months", "Why Rich People Avoid Taxes", "The Psychology of Spending", "Is Real Estate Still Worth It?", "Side Hustles That Made Millionaires"],
"fitness": ["5-Minute Fat Burning Workout", "What Happens If You Never Skip Leg Day", "Foods That Kill Your Gains", "Morning Routine of Athletes", "How to Build Abs Fast"],
"horror": ["Real Haunted Places in America", "The Unsolved Mystery of Room 1046", "Creepiest Urban Legends by State", "What Happens in Sleep Paralysis", "Demonic Possessions or Mental Illness?"],
}
return sample_topics.get(niche.lower(), [f"Viral Topic #{i+1} for {niche}" for i in range(5)])
# === Script generator using Groq ===
def generate_script(title):
import json
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
prompt = f"""
You are a viral YouTube script writer. Generate a high-engagement short-form YouTube script (around 150–170 words) based on this video idea: "{title}".
Use simple, modern English. Include a strong hook in the first line to grab attention. Avoid long intros or boring facts. Write in a dramatic, emotional, or suspenseful tone depending on the topic. No outro needed.
"""
payload = {
"model": GROQ_MODEL,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt.strip()}
],
"temperature": 0.7
}
try:
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()['choices'][0]['message']['content']
except Exception as e:
return f"Error generating script: {e}"
# === Full pipeline ===
def generate_output(niche):
topics = generate_trending_topics(niche)
results = []
for topic in topics:
script = generate_script(topic)
results.append(f"πŸ“Œ **{topic}**\n\n{script}")
return results
# === Gradio Interface ===
with gr.Blocks() as demo:
gr.Markdown("# 🎬 YouTube Topic & Script Generator\nEnter a niche to generate trending topics and scripts.")
niche_input = gr.Textbox(label="Enter Your Niche (e.g. Finance, Fitness, Horror)")
generate_btn = gr.Button("Generate")
output_boxes = [gr.Markdown() for _ in range(5)] # Predefine 5 outputs
def on_click(niche):
scripts = generate_output(niche)
return scripts
generate_btn.click(on_click, inputs=[niche_input], outputs=output_boxes)
# Run the app
demo.launch()