Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,18 @@
|
|
| 1 |
# To run this code you need to install the following dependencies:
|
| 2 |
# pip install google-genai gradio
|
| 3 |
|
| 4 |
-
import base64
|
| 5 |
import os
|
| 6 |
import gradio as gr
|
| 7 |
import json
|
| 8 |
from google import genai
|
| 9 |
from google.genai import types
|
| 10 |
-
api_key=os.environ.get("GEMINI_API_KEY")
|
| 11 |
|
| 12 |
-
def generate_video_title(
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
|
| 16 |
Args:
|
| 17 |
-
|
| 18 |
api_key (str): The Gemini API key
|
| 19 |
|
| 20 |
Returns:
|
|
@@ -24,8 +22,8 @@ def generate_video_title(user_input, api_key):
|
|
| 24 |
if not api_key:
|
| 25 |
return "Error: Please provide a valid Gemini API key"
|
| 26 |
|
| 27 |
-
if not
|
| 28 |
-
return "Error: Please provide
|
| 29 |
|
| 30 |
# Initialize the client with the provided API key
|
| 31 |
client = genai.Client(api_key=api_key)
|
|
@@ -35,7 +33,7 @@ def generate_video_title(user_input, api_key):
|
|
| 35 |
types.Content(
|
| 36 |
role="user",
|
| 37 |
parts=[
|
| 38 |
-
types.Part.from_text(text=
|
| 39 |
],
|
| 40 |
),
|
| 41 |
]
|
|
@@ -82,9 +80,9 @@ def generate_video_title(user_input, api_key):
|
|
| 82 |
def create_gradio_interface():
|
| 83 |
"""Create and return the Gradio interface"""
|
| 84 |
|
| 85 |
-
with gr.Blocks(title="Video Title Generator", theme=gr.themes.Soft()) as app:
|
| 86 |
-
gr.HTML("<h1 style='text-align: center; color: #2563eb;'>🎬 Video Title Generator</h1>")
|
| 87 |
-
gr.HTML("<p style='text-align: center; color: #6b7280;'>
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=2):
|
|
@@ -95,15 +93,15 @@ def create_gradio_interface():
|
|
| 95 |
info="Your API key is not stored and only used for this session"
|
| 96 |
)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
label="Video
|
| 100 |
-
placeholder="
|
| 101 |
-
lines=
|
| 102 |
-
info="
|
| 103 |
)
|
| 104 |
|
| 105 |
with gr.Row():
|
| 106 |
-
generate_btn = gr.Button("Generate Title", variant="primary", size="lg")
|
| 107 |
clear_btn = gr.Button("Clear", variant="secondary")
|
| 108 |
|
| 109 |
with gr.Column(scale=1):
|
|
@@ -117,26 +115,24 @@ def create_gradio_interface():
|
|
| 117 |
# Event handlers
|
| 118 |
generate_btn.click(
|
| 119 |
fn=generate_video_title,
|
| 120 |
-
inputs=[
|
| 121 |
outputs=output
|
| 122 |
)
|
| 123 |
|
| 124 |
clear_btn.click(
|
| 125 |
fn=lambda: ("", ""),
|
| 126 |
-
outputs=[
|
| 127 |
)
|
| 128 |
|
| 129 |
# Example inputs
|
| 130 |
-
gr.HTML("<h3 style='margin-top: 20px;'>💡 Example
|
| 131 |
examples = gr.Examples(
|
| 132 |
examples=[
|
| 133 |
-
["
|
| 134 |
-
["
|
| 135 |
-
["
|
| 136 |
-
["A fitness workout routine for beginners focusing on bodyweight exercises at home"],
|
| 137 |
-
["A DIY home improvement project showing how to build a wooden bookshelf"]
|
| 138 |
],
|
| 139 |
-
inputs=
|
| 140 |
label="Click on any example to try it out:"
|
| 141 |
)
|
| 142 |
|
|
@@ -146,15 +142,19 @@ def create_gradio_interface():
|
|
| 146 |
### How to use this app:
|
| 147 |
|
| 148 |
1. **Enter your Gemini API Key**: Get one from [Google AI Studio](https://aistudio.google.com/app/apikey)
|
| 149 |
-
2. **
|
| 150 |
-
3. **Click "Generate Title"**:
|
| 151 |
-
4. **Copy the result**: Use the generated title
|
| 152 |
|
| 153 |
-
###
|
| 154 |
-
-
|
| 155 |
-
-
|
| 156 |
-
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
""")
|
| 159 |
|
| 160 |
return app
|
|
|
|
| 1 |
# To run this code you need to install the following dependencies:
|
| 2 |
# pip install google-genai gradio
|
| 3 |
|
|
|
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
| 6 |
import json
|
| 7 |
from google import genai
|
| 8 |
from google.genai import types
|
|
|
|
| 9 |
|
| 10 |
+
def generate_video_title(youtube_url, api_key):
|
| 11 |
"""
|
| 12 |
+
Analyze YouTube video and generate title using Gemini API
|
| 13 |
|
| 14 |
Args:
|
| 15 |
+
youtube_url (str): YouTube video URL
|
| 16 |
api_key (str): The Gemini API key
|
| 17 |
|
| 18 |
Returns:
|
|
|
|
| 22 |
if not api_key:
|
| 23 |
return "Error: Please provide a valid Gemini API key"
|
| 24 |
|
| 25 |
+
if not youtube_url.strip():
|
| 26 |
+
return "Error: Please provide a YouTube video URL"
|
| 27 |
|
| 28 |
# Initialize the client with the provided API key
|
| 29 |
client = genai.Client(api_key=api_key)
|
|
|
|
| 33 |
types.Content(
|
| 34 |
role="user",
|
| 35 |
parts=[
|
| 36 |
+
types.Part.from_text(text=youtube_url),
|
| 37 |
],
|
| 38 |
),
|
| 39 |
]
|
|
|
|
| 80 |
def create_gradio_interface():
|
| 81 |
"""Create and return the Gradio interface"""
|
| 82 |
|
| 83 |
+
with gr.Blocks(title="YouTube Video Title Generator", theme=gr.themes.Soft()) as app:
|
| 84 |
+
gr.HTML("<h1 style='text-align: center; color: #2563eb;'>🎬 YouTube Video Title Generator</h1>")
|
| 85 |
+
gr.HTML("<p style='text-align: center; color: #6b7280;'>Analyze YouTube videos and generate titles with AI</p>")
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=2):
|
|
|
|
| 93 |
info="Your API key is not stored and only used for this session"
|
| 94 |
)
|
| 95 |
|
| 96 |
+
youtube_url_input = gr.Textbox(
|
| 97 |
+
label="YouTube Video URL",
|
| 98 |
+
placeholder="https://www.youtube.com/watch?v=...",
|
| 99 |
+
lines=2,
|
| 100 |
+
info="Paste the YouTube video URL here"
|
| 101 |
)
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
+
generate_btn = gr.Button("Analyze Video & Generate Title", variant="primary", size="lg")
|
| 105 |
clear_btn = gr.Button("Clear", variant="secondary")
|
| 106 |
|
| 107 |
with gr.Column(scale=1):
|
|
|
|
| 115 |
# Event handlers
|
| 116 |
generate_btn.click(
|
| 117 |
fn=generate_video_title,
|
| 118 |
+
inputs=[youtube_url_input, api_key_input],
|
| 119 |
outputs=output
|
| 120 |
)
|
| 121 |
|
| 122 |
clear_btn.click(
|
| 123 |
fn=lambda: ("", ""),
|
| 124 |
+
outputs=[youtube_url_input, output]
|
| 125 |
)
|
| 126 |
|
| 127 |
# Example inputs
|
| 128 |
+
gr.HTML("<h3 style='margin-top: 20px;'>💡 Example YouTube URLs:</h3>")
|
| 129 |
examples = gr.Examples(
|
| 130 |
examples=[
|
| 131 |
+
["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
|
| 132 |
+
["https://youtu.be/dQw4w9WgXcQ"],
|
| 133 |
+
["https://www.youtube.com/watch?v=example123"]
|
|
|
|
|
|
|
| 134 |
],
|
| 135 |
+
inputs=youtube_url_input,
|
| 136 |
label="Click on any example to try it out:"
|
| 137 |
)
|
| 138 |
|
|
|
|
| 142 |
### How to use this app:
|
| 143 |
|
| 144 |
1. **Enter your Gemini API Key**: Get one from [Google AI Studio](https://aistudio.google.com/app/apikey)
|
| 145 |
+
2. **Paste YouTube URL**: Copy and paste the YouTube video URL
|
| 146 |
+
3. **Click "Analyze Video & Generate Title"**: Gemini will analyze the video and generate a title
|
| 147 |
+
4. **Copy the result**: Use the generated title
|
| 148 |
|
| 149 |
+
### Supported URL formats:
|
| 150 |
+
- `https://www.youtube.com/watch?v=VIDEO_ID`
|
| 151 |
+
- `https://youtu.be/VIDEO_ID`
|
| 152 |
+
- YouTube playlist URLs (will analyze the first video)
|
| 153 |
+
|
| 154 |
+
### Note:
|
| 155 |
+
- The AI analyzes the YouTube video content to generate relevant titles
|
| 156 |
+
- Generated titles are in German as per the system instruction
|
| 157 |
+
- Make sure the YouTube video is publicly accessible
|
| 158 |
""")
|
| 159 |
|
| 160 |
return app
|