Spaces:
Running
Running
adding agent
Browse files- .python-version +1 -0
- functions.py +63 -0
- main.py +32 -0
- prompts/system_instructions.md +0 -0
- prompts/write_blog_post.md +0 -0
- prompts/write_social_post.md +0 -0
- prompts/write_video_chapters.md +0 -0
- pyproject.toml +13 -0
- uv.lock +0 -0
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.13
|
functions.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 2 |
+
import re
|
| 3 |
+
from agents import function_tool
|
| 4 |
+
|
| 5 |
+
@function_tool
|
| 6 |
+
def fetch_youtube_transcript(url: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Extract transcript with timestamps from a YouTube video URL and format it for LLM consumption
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
url (str): YouTube video URL
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
str: Formatted transcript with timestamps, where each entry is on a new line
|
| 15 |
+
in the format: "[MM:SS] Text"
|
| 16 |
+
"""
|
| 17 |
+
# Extract video ID from URL
|
| 18 |
+
video_id_pattern = r'(?:v=|\/)([0-9A-Za-z_-]{11}).*'
|
| 19 |
+
video_id_match = re.search(video_id_pattern, url)
|
| 20 |
+
|
| 21 |
+
if not video_id_match:
|
| 22 |
+
raise ValueError("Invalid YouTube URL")
|
| 23 |
+
|
| 24 |
+
video_id = video_id_match.group(1)
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
ytt_api = YouTubeTranscriptApi()
|
| 28 |
+
transcript = ytt_api.fetch(video_id)
|
| 29 |
+
|
| 30 |
+
# Format each entry with timestamp and text
|
| 31 |
+
formatted_entries = []
|
| 32 |
+
for entry in transcript:
|
| 33 |
+
# Convert seconds to MM:SS format
|
| 34 |
+
minutes = int(entry.start // 60)
|
| 35 |
+
seconds = int(entry.start % 60)
|
| 36 |
+
timestamp = f"[{minutes:02d}:{seconds:02d}]"
|
| 37 |
+
|
| 38 |
+
formatted_entry = f"{timestamp} {entry.text}"
|
| 39 |
+
formatted_entries.append(formatted_entry)
|
| 40 |
+
|
| 41 |
+
# Join all entries with newlines
|
| 42 |
+
return "\n".join(formatted_entries)
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise Exception(f"Error fetching transcript: {str(e)}")
|
| 46 |
+
|
| 47 |
+
@function_tool
|
| 48 |
+
def fetch_intstructions(prompt_name: str) -> str:
|
| 49 |
+
"""
|
| 50 |
+
Fetch instructions for a given prompt name from the prompts/ directory
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
prompt_name (str): Name of the prompt to fetch instructions for
|
| 54 |
+
Available prompts:
|
| 55 |
+
- write_blog_post
|
| 56 |
+
- write_social_post
|
| 57 |
+
- write_video_chapters
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
str: Instructions for the given prompt
|
| 61 |
+
"""
|
| 62 |
+
with open(f"prompts/{prompt_name}.md", "r") as f:
|
| 63 |
+
return f.read()
|
main.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from agents import Agent, function_tool, Runner
|
| 2 |
+
from openai.types.responses import ResponseTextDeltaEvent
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import asyncio
|
| 5 |
+
import os
|
| 6 |
+
from functions import fetch_video_transcript, fetch_intstructions
|
| 7 |
+
|
| 8 |
+
# try to import OPENAI_API_KEY from .env file, if not found, take user input
|
| 9 |
+
if not os.getenv("OPENAI_API_KEY"):
|
| 10 |
+
OPENAI_API_KEY = input("Enter your OpenAI API key: ")
|
| 11 |
+
with open(".env", "w") as f:
|
| 12 |
+
f.write(f"OPENAI_API_KEY={OPENAI_API_KEY}")
|
| 13 |
+
else:
|
| 14 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 15 |
+
|
| 16 |
+
# load system instructions
|
| 17 |
+
with open("prompts/system_instructions.md", "r") as f:
|
| 18 |
+
system_instructions = f.read()
|
| 19 |
+
|
| 20 |
+
# create agent
|
| 21 |
+
agent = Agent(
|
| 22 |
+
name="YouTube Agent",
|
| 23 |
+
model="gpt-5",
|
| 24 |
+
system_instructions=system_instructions,
|
| 25 |
+
tools=[fetch_video_transcript, fetch_intstructions],
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
async def main():
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
asyncio.run(main())
|
prompts/system_instructions.md
ADDED
|
File without changes
|
prompts/write_blog_post.md
ADDED
|
File without changes
|
prompts/write_social_post.md
ADDED
|
File without changes
|
prompts/write_video_chapters.md
ADDED
|
File without changes
|
pyproject.toml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "yt-agent-streamlit"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.13"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"openai>=1.107.2",
|
| 9 |
+
"openai-agents>=0.3.0",
|
| 10 |
+
"python-dotenv>=1.1.1",
|
| 11 |
+
"streamlit>=1.49.1",
|
| 12 |
+
"youtube-transcript-api>=1.2.2",
|
| 13 |
+
]
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|