File size: 5,081 Bytes
dc9fe79
 
 
dba7b2d
 
a331557
 
0fa3d57
abf0bb7
a331557
 
0fa3d57
a331557
 
 
 
 
 
 
 
 
 
 
52103dc
a331557
0fa3d57
a331557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc9fe79
a331557
 
 
 
dba7b2d
a331557
0fa3d57
dba7b2d
 
 
 
dc9fe79
dba7b2d
 
 
 
 
 
 
 
 
 
 
dc9fe79
dba7b2d
dc9fe79
0fa3d57
 
dba7b2d
 
 
2b6e5be
 
 
dba7b2d
 
4629274
dba7b2d
2b6e5be
 
 
 
 
dba7b2d
 
 
2b6e5be
dba7b2d
 
 
8dc24ba
dba7b2d
 
 
 
 
dc9fe79
dba7b2d
 
 
8c23a50
dba7b2d
dc9fe79
dba7b2d
 
 
2b6e5be
dba7b2d
 
 
2b6e5be
dba7b2d
 
2b6e5be
 
 
dba7b2d
2b6e5be
dba7b2d
 
 
 
 
 
2b6e5be
 
 
 
dba7b2d
 
 
dc9fe79
 
dba7b2d
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
import json
from google import genai
from google.genai import types

api_key=os.environ.get("GEMINI_API_KEY")

def generate_video_title(youtube_url,api_key):
    try:
        client = genai.Client(api_key=api_key)

        model = "gemini-2.5-pro"
        contents = [
            types.Content(
                role="user",
                parts=[
                    types.Part(
                        file_data=types.FileData(
                            file_uri=youtube_url,
                            mime_type="video/*",
                        ),
                        video_metadata=types.VideoMetadata(
                            end_offset="5s",
                        ),
                    ),
                    types.Part.from_text(text=""" """),
                ],
            ),
        ]
        generate_content_config = types.GenerateContentConfig(
            thinking_config = types.ThinkingConfig(
                thinking_budget=-1,
            ),
            response_mime_type="application/json",
            response_schema=genai.types.Schema(
                type = genai.types.Type.OBJECT,
                required = ["titel"],
                properties = {
                    "titel": genai.types.Schema(
                        type = genai.types.Type.STRING,
                    ),
                },
            ),
            system_instruction=[
                types.Part.from_text(text="""always answer this question: wie lautet der titel des videos"""),
            ],
        )

        # Collect the streamed response
        response_text = ""
        for chunk in client.models.generate_content_stream(
            model=model,
            contents=contents,
            config=generate_content_config,
        ):
            response_text += chunk.text
        
        # Parse the JSON response to extract the title
        try:
            response_json = json.loads(response_text)
            return response_json.get("titel", "No title generated")
        except json.JSONDecodeError:
            return f"Error parsing response: {response_text}"
            
    except Exception as e:
        return f"Error: {str(e)}"



def create_gradio_interface():
    """Create and return the Gradio interface"""
    
    with gr.Blocks(title="YouTube Video Title Generator", theme=gr.themes.Soft()) as app:
        gr.HTML("<h1 style='text-align: center; color: #2563eb;'>🎬 YouTube Video Title Generator</h1>")
        gr.HTML("<p style='text-align: center; color: #6b7280;'>Analyze YouTube videos and generate titles with AI</p>")
        
        with gr.Row():
            with gr.Column(scale=1):
                
                youtube_url_input = gr.Textbox(
                    label="YouTube Video URL",
                    placeholder="https://www.youtube.com/watch?v=...",
                    lines=2,
                    info="Paste the YouTube video URL here"
                )
                
                with gr.Row():
                    generate_btn = gr.Button("Analyze Video & Generate Title", variant="primary", size="lg")
                    clear_btn = gr.Button("Clear", variant="secondary")
            
            with gr.Column(scale=1):
                output = gr.Textbox(
                    label="Generated Title",
                    placeholder="Your generated video title will appear here...",
                    lines=3,
                    interactive=False
                )
        
        # Event handlers
        generate_btn.click(
            fn=generate_video_title,
            inputs=youtube_url_input,
            outputs=output
        )
        
        clear_btn.click(
            fn=lambda: ("", ""),
            outputs=[youtube_url_input, output]
        )
        
        # Example inputs
        gr.HTML("<h3 style='margin-top: 20px;'>πŸ’‘ Example YouTube URLs:</h3>")
        examples = gr.Examples(
            examples=[
                ["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
                ["https://youtu.be/dQw4w9WgXcQ"],
                ["https://www.youtube.com/watch?v=example123"]
            ],
            inputs=youtube_url_input,
            label="Click on any example to try it out:"
        )
        
        # Instructions
        with gr.Accordion("πŸ“‹ Instructions", open=False):
            gr.Markdown("""
            ### Supported URL formats:
            - `https://www.youtube.com/watch?v=VIDEO_ID`
            - `https://youtu.be/VIDEO_ID`
            - YouTube playlist URLs (will analyze the first video)
            """)
    
    return app

if __name__ == "__main__":
    # Create and launch the Gradio app
    app = create_gradio_interface()
    
    # Launch with sharing enabled - set to False for local use only
    app.launch(
        share=True,  # Set to False if you don't want to create a public link
        server_name="0.0.0.0",  # Allow access from other devices on your network
        server_port=7860,  # Default Gradio port
        show_api=True  # Show API documentation
    )