Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import youtube_dl
|
| 4 |
+
import openai
|
| 5 |
+
import requests
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import moviepy.editor
|
| 9 |
+
import openai
|
| 10 |
+
from IPython.display import Audio
|
| 11 |
+
import youtube_dl
|
| 12 |
+
import json
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 16 |
+
RAPIDAPI_KEY = os.environ.get("OPENAI_API_KEY")
|
| 17 |
+
|
| 18 |
+
def process_video(videoid, prompt):
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
url = "https://tiktok-video-no-watermark2.p.rapidapi.com/"
|
| 22 |
+
|
| 23 |
+
querystring = {"url":videoid,"hd":"0"}
|
| 24 |
+
|
| 25 |
+
headers = {
|
| 26 |
+
"X-RapidAPI-Key": RAPIDAPI_KEY,
|
| 27 |
+
"X-RapidAPI-Host": "tiktok-video-no-watermark2.p.rapidapi.com"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
response = requests.request("GET", url, headers=headers, params=querystring)
|
| 31 |
+
# Download video
|
| 32 |
+
wmplay_link = response.json().get('data').get('wmplay')
|
| 33 |
+
ydl_opts = {
|
| 34 |
+
'format': 'worstaudio/worst',
|
| 35 |
+
'outtmpl': 'temp_audio.%(ext)s',
|
| 36 |
+
'postprocessors': [{
|
| 37 |
+
'key': 'FFmpegExtractAudio',
|
| 38 |
+
'preferredcodec': 'mp3',
|
| 39 |
+
'preferredquality': '64',
|
| 40 |
+
}]
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
| 44 |
+
try:
|
| 45 |
+
ydl.download([wmplay_link])
|
| 46 |
+
except youtube_dl.utils.DownloadError as e:
|
| 47 |
+
return f"Error: {e}", ""
|
| 48 |
+
|
| 49 |
+
# Get the transcript
|
| 50 |
+
with open("temp_audio.mp3", "rb") as audio_file:
|
| 51 |
+
try:
|
| 52 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 53 |
+
except openai.error.InvalidRequestError as e:
|
| 54 |
+
return f"Error: {e.http_body}", ""
|
| 55 |
+
|
| 56 |
+
# Get keywords and summary
|
| 57 |
+
messages = [{"role": "system", "content": prompt}]
|
| 58 |
+
messages.append({"role": "user", "content": transcript["text"]})
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
response = openai.ChatCompletion.create(
|
| 62 |
+
model="gpt-3.5-turbo", messages=messages)
|
| 63 |
+
summary = response.choices[0].message.content
|
| 64 |
+
return transcript["text"], summary
|
| 65 |
+
except openai.error.InvalidRequestError as e:
|
| 66 |
+
return f"Error: {e.http_body}", ""
|
| 67 |
+
|
| 68 |
+
# Gradio interface
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=process_video,
|
| 71 |
+
inputs=[
|
| 72 |
+
gr.inputs.Textbox(lines=5, label="Video ID", default = "7083232374267596078"),
|
| 73 |
+
gr.inputs.Textbox(lines=5, label="Prompt", default="You are a creative content writer. Based on this video transcript, I want you to extract 3-5 relevant keywords from the transcript. Then, I want you to summarise the video in 1-2 sentences. Make sure to delimit the parts with a new line. Finally, turn this into an amazing LinkedIn post that is 550-600 characters long. Include all of the LinkedIn post best practices. Delimit each section with a new line delimiter. Return the output in English.")
|
| 74 |
+
],
|
| 75 |
+
outputs=[
|
| 76 |
+
gr.outputs.Textbox(label="🖨️ Transcript"),
|
| 77 |
+
gr.outputs.Textbox(label="🤫 Keywords and summary")
|
| 78 |
+
],
|
| 79 |
+
title="Video Transcript Processing",
|
| 80 |
+
description="Extract keywords and summarize a video's transcript."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
iface.launch(debug = False, auth=(os.environ.get(USERNAME), os.environ.get(PASSWORD)))
|