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("

🎬 YouTube Video Title Generator

") gr.HTML("

Analyze YouTube videos and generate titles with AI

") 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("

💡 Example YouTube URLs:

") 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 )