script_writing / script_writing.py
Abu1998's picture
Update script_writing.py
d3e37a4 verified
raw
history blame contribute delete
No virus
3.08 kB
import csv
import re
from datetime import datetime
from gradio_client import Client
import requests
import os
# Giphy API details
API_KEY = "KzPlVn6nz6czmjWpPEy6reL52r1H5gs7"
SEARCH_URL = "https://api.giphy.com/v1/gifs/search"
# Initialize the client with the correct Hugging Face Space
client = Client("Abu1998/Meme_finder")
print(client) # Add this line to verify the client object
def generate_script(user_input):
# Define the system message and input sentence
print("Generating script for:", user_input) # Add this line to verify the function call
# ... rest of the function ...
system_message = """Task: Act as a YouTube Shorts content writer.
Objective: Create engaging, catchy, and trendy scripts for YouTube Shorts videos that are brief, attention-grabbing, and optimized for viral potential.
Guidelines:
Each script should be 15-30 seconds long.
Use a hook in the first few seconds to capture viewers' attention.
Ensure the content is aligned with trending topics, challenges, or popular culture.
Incorporate humor, relatable scenarios, or strong emotions to resonate with the audience.
End with a clear call-to-action (CTA) like “Follow for more!” or a cliffhanger.
Example Flow:
User Input: “Write a script about the Monday blues.”
AI Output:
Script: "POV: It’s Monday morning, and you’re already done with the week. [Clip shows someone groggily hitting the snooze button, dragging themselves out of bed]. But wait… there’s coffee. And suddenly, everything’s okay! ☕✨ [Cut to a quick burst of energy with upbeat music]. If you’re just surviving till the weekend, hit that follow button for more relatable vibes!"
"""
# Make the API call with the specified parameters
result = client.predict(
message=user_input,
system_message=system_message,
max_tokens=512,
temperature=0.7,
top_p=0.95,
api_name="/chat"
)
# Extract the script from the result
script = result.strip()
# Function to split script into words
def split_into_words(script_text):
words = re.findall(r'\w+', script_text) # Find all words
return words
# Convert the script to a list of words
words = split_into_words(script)
# Create download directory if it doesn't exist
DOWNLOAD_DIR = '/content/memes2'
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
# Download GIFs for each word
for index, word in enumerate(words):
params = {
'api_key': API_KEY,
'q': word,
'limit': 1
}
response = requests.get(SEARCH_URL, params=params)
data = response.json()
if data['data']:
gif_url = data['data'][0]['images']['original']['url']
gif_response = requests.get(gif_url)
filename = f"{index}.gif"
filepath = os.path.join(DOWNLOAD_DIR, filename)
with open(filepath, 'wb') as f:
f.write(gif_response.content)
print(f"Downloaded GIF for '{word}' as '{filename}'")
return script