File size: 6,437 Bytes
e6580d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
# Main Application Module (Gradio Interface)

This module provides the web interface and core functionality for the VidInsight AI application,
integrating video fetching, transcription, summarization, and content idea generation.

## Summary
- Creates a Gradio web interface
- Processes user topic input
- Coordinates video fetching and transcription
- Generates summaries and content ideas
- Displays results in a formatted JSON output

## Dependencies

### System Requirements
- Python 3.8+
- Internet connection for API calls
- FFmpeg for audio processing

### Package Dependencies
1. **gradio==3.50.2**
   - Install: `pip install gradio`
   - Purpose: Web interface creation

2. **Other Project Packages**
   - fetch_youtube_videos
   - transcribe_videos
   - summary
   - YouTubeAgent

### Project Dependencies
1. **Local Modules**
   - fetch_youtube_videos.py: For YouTube video retrieval
   - transcribe_videos.py: For video transcription
   - summary.py: For generating summaries
   - YouTubeAgent.py: For content idea generation

2. **Output Directory**
   - 'output/' folder for saving transcriptions

## Functions

1. format_results(results)
   - Formats view counts with commas
   - Cleans transcript preview text
   
2. analyze(topic)
   - Main processing function
   - Coordinates all operations:
     - Video fetching
     - Transcription
     - Summary generation
     - Content idea creation

## Returns
JSON output containing:
1. Video Information
   - Title
   - Channel
   - Views
   - Transcript preview
   - File paths
2. Analysis
   - Topic title
   - Summary
   - Key points
   - Content ideas

## Error Handling
- Empty topic validation
- Video fetching errors
- Transcription failures
- Analysis generation issues

"""


import gradio as gr
from fetch_youtube_videos import fetch_videos
from transcribe_videos import transcribe_and_save
from summary import generate_combined_summary_and_key_points
from YouTubeAgent import generateidea
from embeddings import mainApp

def format_results(results):
    """Format results for better display"""
    if isinstance(results, list):
        for result in results:
            if 'Views' in result:
                result['Views'] = f"{result['Views']:,}"  # Format numbers with commas
            if 'Transcript Preview' in result:
                result['Transcript Preview'] = result['Transcript Preview'].replace('\n', ' ')
    return results

def analyze(topic):
    """
    Fetch videos, transcribe them, and generate analysis including summaries and content ideas.
    """
    if not topic.strip():
        return {"error": "⚠️ Please enter a topic to analyze"}
    
    try:
        # Fetch videos based on topic
        videos = fetch_videos(topic)
        
        if isinstance(videos, str):
            return {"error": f"⚠️ {videos}"}
        
        if not videos:
            return {"error": "⚠️ No relevant videos found for this topic."}
        
        results = []
        transcriptions = []  # Store transcriptions for summary generation
        
        # Process each video
        for video in videos:
            transcription_result = transcribe_and_save(video['url'])
            
            if "error" in transcription_result:
                results.append({
                    'Video': video['title'],
                    'Channel': video['channel'],
                    'Views': video['views'],
                    'Transcript Preview': transcription_result["error"]
                })
            else:
                results.append({
                    'Video': video['title'],
                    'Channel': video['channel'],
                    'Views': video['views'],
                    'Transcript Preview': transcription_result["transcription"][:500] + "...",
                    'Transcript File': transcription_result["file_path"]
                })
                # Add transcription for summary generation
                transcriptions.append(transcription_result["transcription"])
        
        # Generate summary and content ideas if transcriptions exist
        if transcriptions:
            
            mainApp(topic)
            
            topic_title, summary, key_points = generate_combined_summary_and_key_points(transcriptions)
            
            # Generate content idea
            input_for_idea = {
                "summary": summary,
                "keypoints": key_points
            }
            content_idea = generateidea(input_for_idea)
            
            # Add analysis to results
            results.append({
                "Analysis": {
                    "Topic Title": topic_title,
                    "Summary": summary,
                    "Key Points": key_points,
                    "Content Idea": content_idea
                }
            })
        
        return format_results(results)

    except Exception as e:
        return {"error": f"⚠️ An unexpected error occurred: {str(e)}"}

# Create Gradio interface with improved styling
with gr.Blocks(theme=gr.themes.Soft()) as app:
    gr.Markdown(
        """
        # πŸŽ₯ VidInsight AI
        ### AI-Powered YouTube Content Analyzer
        
        This tool helps you:
        - πŸ“ Get transcriptions of educational videos
        - πŸ“Š Generate summaries and key points
        - πŸ’‘ Create content ideas
        """
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            topic_input = gr.Textbox(
                label="Enter Topic",
                placeholder="e.g., Machine Learning, Data Science, Python Programming",
                lines=2
            )
            
        with gr.Column(scale=1):
            submit_btn = gr.Button("πŸ” Analyze", variant="primary")
            clear_btn = gr.Button("πŸ—‘οΈ Clear")
    
    with gr.Row():
        output = gr.JSON(
            label="Analysis Results",
            show_label=True
        )
    
    # Add footer
    gr.Markdown(
        """
        ---
        πŸ“Œ **Note**: This tool analyzes educational YouTube videos and generates AI-powered insights.
        
        Made by VidInsight Team πŸ€–
        """
    )
    
    # Set up button actions
    submit_btn.click(
        fn=analyze,
        inputs=topic_input,
        outputs=output,
        api_name="analyze"
    )
    clear_btn.click(lambda: None, None, topic_input, queue=False)

if __name__ == "__main__":
    app.launch()