import os import random import gdown import numpy as np import moviepy from moviepy.editor import * from moviepy.editor import concatenate_videoclips # from melo.api import TTS import torch import cv2 from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip, concatenate_videoclips from PIL import Image, ImageFilter import numpy as np from flask import Flask, flash, request, redirect, url_for, jsonify, Response import subprocess import requests from bs4 import BeautifulSoup from urllib.parse import urljoin, urlparse, unquote from scraper import Scraper import openai app = Flask(__name__) @app.route('/scrape', methods=['POST']) def scrape(): scraper = Scraper() url = request.json['url'] if not url: return jsonify(status="error", message="No URL provided."), 400 # Decode the URL if it's percent-encoded url = unquote(url) # Scrape the homepage homepage_data = scraper.scrape_page(url) if "error" in homepage_data: return jsonify(status="error", message=homepage_data["error"]), 500 # Scrape 1-level-down links all_text_content = homepage_data["text"] all_images = homepage_data["images"] for link in homepage_data["links"]: link_data = scraper.scrape_page(link) if "text" in link_data and "images" in link_data: all_text_content += " " + link_data["text"] all_images.extend(link_data["images"]) # Remove duplicates from all_images while preserving order all_images = list(dict.fromkeys(all_images)) return jsonify( {"text": all_text_content, "images": all_images} ) # Initialize OpenAI API key from environment variables openai.api_key = "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" @app.route('/generate_summary', methods=['POST']) def generate_summary(): if request.method != 'POST': return jsonify(error=f"Method {request.method} Not Allowed"), 405 # Extract 'prompt' from the request body data = request.get_json() prompt = data.get('prompt') if not prompt: return jsonify(error="Prompt is required."), 400 try: # Call the OpenAI API to generate a chat completion response = openai.ChatCompletion.create( model='gpt-4o-mini', # Use the appropriate model available to you messages=[ {"role": "system", "content": "You are a creative assistant specializing in advertisement scripts. Follow the prompt and tone exactly. Only generate the content that will be spoken out by the narrator."}, {"role": "user", "content": prompt}, ], max_tokens=1500, # Limit the response size temperature=0.7, # Adjust for creativity ) # Extract the content of the response summary = response.choices[0]['message']['content'].strip() if not summary: raise ValueError("No content generated.") return jsonify(summary=summary), 200 except Exception as e: # Log the error and return a 500 Internal Server Error print(f"Error generating summary: {e}") return jsonify(error="Failed to generate summary."), 500 @app.route('/generate-video', methods=['POST']) def generate_video(): return if __name__ == '__main__': app.run(host = "0.0.0.0",port=3333, debug = True, threaded = True)