mgokg commited on
Commit
2b6e5be
·
verified ·
1 Parent(s): 3d5d663

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -34
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(user_input, api_key):
13
  """
14
- Generate video title using Gemini API
15
 
16
  Args:
17
- user_input (str): The input text describing the video
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 user_input.strip():
28
- return "Error: Please provide input text describing your video"
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=user_input),
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;'>Generate compelling video titles using AI</p>")
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
- user_input = gr.Textbox(
99
- label="Video Description",
100
- placeholder="Describe your video content here...",
101
- lines=5,
102
- info="Provide details about your video content to generate an appropriate title"
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=[user_input, api_key_input],
121
  outputs=output
122
  )
123
 
124
  clear_btn.click(
125
  fn=lambda: ("", ""),
126
- outputs=[user_input, output]
127
  )
128
 
129
  # Example inputs
130
- gr.HTML("<h3 style='margin-top: 20px;'>💡 Example Inputs:</h3>")
131
  examples = gr.Examples(
132
  examples=[
133
- ["A cooking tutorial showing how to make homemade pasta from scratch with step-by-step instructions"],
134
- ["A travel vlog exploring the hidden gems of Tokyo, including local food markets and traditional temples"],
135
- ["A tech review of the latest smartphone, covering camera quality, battery life, and performance"],
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=user_input,
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. **Describe your video**: Provide details about your video content
150
- 3. **Click "Generate Title"**: The AI will create a compelling title in German
151
- 4. **Copy the result**: Use the generated title for your video
152
 
153
- ### Tips for better results:
154
- - Be specific about your video content
155
- - Include the video type (tutorial, review, vlog, etc.)
156
- - Mention key topics or themes
157
- - Describe your target audience if relevant
 
 
 
 
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