dipenvekaria commited on
Commit
86257ee
·
verified ·
1 Parent(s): 44fb8bf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +247 -0
app.py ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from youtube_transcript_api import YouTubeTranscriptApi
3
+ from openai import OpenAI
4
+ import os
5
+ import yt_dlp
6
+ import logging
7
+ from langgraph.graph import StateGraph, END
8
+ from typing import Dict, List, TypedDict, Optional
9
+ from langgraph.checkpoint.memory import MemorySaver
10
+
11
+ # Set up logging to debug issues
12
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
13
+ logger = logging.getLogger(__name__)
14
+
15
+ # Initialize OpenAI client
16
+ api_key = os.getenv("OPENAI_API_KEY")
17
+ if not api_key:
18
+ raise ValueError("OPENAI_API_KEY environment variable is not set. Please set it in Hugging Face Secrets or your environment.")
19
+ client = OpenAI(api_key=api_key)
20
+
21
+ # Define the state for LangGraph
22
+ class SummarizerState(TypedDict):
23
+ urls: List[str]
24
+ titles: List[str]
25
+ transcripts: List[str]
26
+ summaries: List[str]
27
+ master_summary: str
28
+ errors: List[str]
29
+ master_output: str
30
+ individual_output: str
31
+ error_output: str
32
+
33
+ # Instructions for the front-end
34
+ INSTRUCTIONS = """
35
+ **YouTube Video Summarizer**
36
+
37
+ - **Left Panel**: Enter a YouTube URL and click "Add URL" to include it (up to 3 videos). View added URLs and their titles below. Click "Clear" to start over.
38
+ - **Right Panel**: Click "Summarize" to generate a master summary (combining insights from all videos) and individual summaries for each video in bullet points.
39
+ """
40
+
41
+ # Function to get video title using yt-dlp
42
+ def get_video_title(video_url: str) -> str:
43
+ try:
44
+ ydl_opts = {
45
+ 'quiet': True,
46
+ 'no_warnings': True,
47
+ }
48
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
49
+ info = ydl.extract_info(video_url, download=False)
50
+ return info['title']
51
+ except Exception as e:
52
+ logger.error(f"Error fetching title for {video_url}: {str(e)}")
53
+ return f"Unknown Title (Error: {str(e)})"
54
+
55
+ # Function to fetch transcript using youtube_transcript_api
56
+ def get_transcript(video_url: str) -> tuple[Optional[str], Optional[str]]:
57
+ try:
58
+ video_id = video_url.split("v=")[-1].split("&")[0]
59
+ logger.info(f"Fetching transcript for video_id: {video_id}")
60
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
61
+ text = " ".join([entry["text"] for entry in transcript])
62
+ logger.info(f"Transcript fetched for {video_url}: {text[:100]}...")
63
+ return text, None
64
+ except Exception as e:
65
+ logger.error(f"Error fetching transcript for {video_url}: {str(e)}")
66
+ return None, f"Error fetching transcript for {video_url}: {str(e)}"
67
+
68
+ # Function to summarize text using ChatGPT 4o-mini in bullet points
69
+ def summarize_text(text: str, prompt_prefix: str = "Summarize the following transcript in 2-4 bullet points:") -> str:
70
+ try:
71
+ response = client.chat.completions.create(
72
+ model="gpt-4o-mini",
73
+ messages=[
74
+ {"role": "system", "content": "You are a helpful assistant that summarizes text concisely in bullet point format."},
75
+ {"role": "user", "content": f"{prompt_prefix}\n\n{text}"}
76
+ ],
77
+ max_tokens=300,
78
+ temperature=0.7
79
+ )
80
+ summary = response.choices[0].message.content.strip()
81
+ logger.info(f"Generated summary: {summary}")
82
+ return summary
83
+ except Exception as e:
84
+ logger.error(f"Error summarizing text: {str(e)}")
85
+ return f"Error summarizing text: {str(e)}"
86
+
87
+ # LangGraph Nodes
88
+ def fetch_transcripts(state: SummarizerState) -> SummarizerState:
89
+ state["transcripts"] = []
90
+ state["errors"] = []
91
+ for url in state["urls"]:
92
+ transcript, error = get_transcript(url)
93
+ if error:
94
+ state["errors"].append(error)
95
+ state["transcripts"].append("")
96
+ else:
97
+ state["transcripts"].append(transcript)
98
+ logger.info(f"Fetched transcripts: {state['transcripts']}")
99
+ return state
100
+
101
+ def generate_individual_summaries(state: SummarizerState) -> SummarizerState:
102
+ state["summaries"] = []
103
+ for url, title, transcript in zip(state["urls"], state["titles"], state["transcripts"]):
104
+ if not transcript:
105
+ state["summaries"].append(f"**Summary for '{title}' ({url})**:\nNo transcript available.")
106
+ else:
107
+ summary = summarize_text(transcript, "Summarize the following transcript in 2-4 bullet points:")
108
+ state["summaries"].append(f"**Summary for '{title}' ({url})**:\n{summary}")
109
+ logger.info(f"Individual summaries: {state['summaries']}")
110
+ return state
111
+
112
+ def generate_master_summary(state: SummarizerState) -> SummarizerState:
113
+ valid_transcripts = [t for t in state["transcripts"] if t]
114
+ if valid_transcripts:
115
+ combined_text = " ".join(valid_transcripts)
116
+ master_prompt = "Provide a concise summary in 2-4 bullet points that combines key insights from the following transcripts, highlighting common themes or differences:"
117
+ state["master_summary"] = summarize_text(combined_text, master_prompt)
118
+ else:
119
+ state["master_summary"] = "No valid transcripts available for master summary."
120
+ logger.info(f"Master summary: {state['master_summary']}")
121
+ return state
122
+
123
+ def format_output(state: SummarizerState) -> SummarizerState:
124
+ state["master_output"] = f"**Master Summary**:\n{state['master_summary']}"
125
+ state["individual_output"] = "\n\n".join(state["summaries"])
126
+ state["error_output"] = "\n\n".join(state["errors"]) if state["errors"] else "All summaries generated successfully."
127
+ logger.info(f"Formatted output - Master: {state['master_output']}, Individual: {state['individual_output']}, Errors: {state['error_output']}")
128
+ return state
129
+
130
+ # Build the LangGraph workflow
131
+ workflow = StateGraph(SummarizerState)
132
+
133
+ # Add nodes
134
+ workflow.add_node("fetch_transcripts", fetch_transcripts)
135
+ workflow.add_node("generate_individual_summaries", generate_individual_summaries)
136
+ workflow.add_node("generate_master_summary", generate_master_summary)
137
+ workflow.add_node("format_output", format_output)
138
+
139
+ # Define edges
140
+ workflow.add_edge("fetch_transcripts", "generate_individual_summaries")
141
+ workflow.add_edge("generate_individual_summaries", "generate_master_summary")
142
+ workflow.add_edge("generate_master_summary", "format_output")
143
+ workflow.add_edge("format_output", END)
144
+
145
+ # Set the entry point
146
+ workflow.set_entry_point("fetch_transcripts")
147
+
148
+ # Compile the graph with memory
149
+ memory = MemorySaver()
150
+ app = workflow.compile(checkpointer=memory)
151
+
152
+ # Function to add a URL to the list
153
+ def add_url(url: str, state: Dict) -> tuple[Dict, str, str, str]:
154
+ if not url:
155
+ return state, "Please enter a valid YouTube URL.", "", ""
156
+ if len(state["urls"]) >= 3:
157
+ return state, "Maximum limit of 3 URLs reached.", "", ""
158
+ if url in state["urls"]:
159
+ return state, "This URL has already been added.", "", ""
160
+ title = get_video_title(url)
161
+ state["urls"].append(url)
162
+ state["titles"].append(title)
163
+ url_list = "\n".join([f"{title} ({url})" for title, url in zip(state["titles"], state["urls"])])
164
+ return state, f"Added URL: {url}", url_list, ""
165
+
166
+ # Function to summarize all videos using LangGraph
167
+ def summarize_videos(state: Dict) -> tuple[Dict, str, str, str]:
168
+ if not state["urls"]:
169
+ return state, "No URLs added yet.", "", ""
170
+
171
+ # Initialize state for LangGraph
172
+ initial_state = SummarizerState(
173
+ urls=state["urls"],
174
+ titles=state["titles"],
175
+ transcripts=[],
176
+ summaries=[],
177
+ master_summary="",
178
+ errors=[],
179
+ master_output="",
180
+ individual_output="",
181
+ error_output=""
182
+ )
183
+
184
+ # Run the LangGraph workflow
185
+ thread_id = "summarizer_thread"
186
+ final_state = app.invoke(initial_state, config={"configurable": {"thread_id": thread_id}})
187
+
188
+ # Update the Gradio state with the results
189
+ state["summaries"] = final_state["summaries"]
190
+ state["master_summary"] = final_state["master_summary"]
191
+
192
+ return (
193
+ state,
194
+ final_state["error_output"],
195
+ final_state["master_output"],
196
+ final_state["individual_output"]
197
+ )
198
+
199
+ # Function to clear the state
200
+ def clear_state() -> tuple[Dict, str, str, str]:
201
+ return {"urls": [], "titles": [], "summaries": [], "master_summary": ""}, "Cleared all URLs and summaries.", "", ""
202
+
203
+ # Gradio interface
204
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
205
+ gr.Markdown(INSTRUCTIONS)
206
+
207
+ # Split the interface into two columns
208
+ with gr.Row():
209
+ # Left side: URL input and management
210
+ with gr.Column(scale=1):
211
+ gr.Markdown("### Add YouTube URLs")
212
+ url_input = gr.Textbox(label="Enter YouTube URL", placeholder="https://www.youtube.com/watch?v=...")
213
+ add_button = gr.Button("Add URL")
214
+ url_list = gr.Textbox(label="Added URLs", interactive=False, lines=5)
215
+ with gr.Row():
216
+ summarize_button = gr.Button("Summarize")
217
+ clear_button = gr.Button("Clear")
218
+ status_output = gr.Textbox(label="Status")
219
+
220
+ # Right side: Summaries
221
+ with gr.Column(scale=2):
222
+ gr.Markdown("### Summaries")
223
+ master_summary = gr.Markdown(label="Master Summary", min_height=200)
224
+ individual_summaries = gr.Markdown(label="Individual Summaries", min_height=300)
225
+
226
+ # State to track URLs, titles, and summaries
227
+ state = gr.State(value={"urls": [], "titles": [], "summaries": [], "master_summary": ""})
228
+
229
+ # Event handlers
230
+ add_button.click(
231
+ fn=add_url,
232
+ inputs=[url_input, state],
233
+ outputs=[state, status_output, url_list, url_input]
234
+ )
235
+ summarize_button.click(
236
+ fn=summarize_videos,
237
+ inputs=[state],
238
+ outputs=[state, status_output, master_summary, individual_summaries]
239
+ )
240
+ clear_button.click(
241
+ fn=clear_state,
242
+ inputs=[],
243
+ outputs=[state, status_output, master_summary, individual_summaries]
244
+ )
245
+
246
+ # Launch the app
247
+ demo.launch(share=False)