Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,217 +1,249 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from google import genai
|
3 |
-
|
4 |
-
|
5 |
-
import re
|
6 |
-
import
|
7 |
-
import
|
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 |
-
def
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
return
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from google import genai
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import re
|
6 |
+
import os
|
7 |
+
from urllib.parse import urlparse, parse_qs
|
8 |
+
|
9 |
+
# Video ID çıkarma fonksiyonu
|
10 |
+
def extract_video_id(url):
|
11 |
+
# YouTube URL formatları için regex
|
12 |
+
youtube_regex = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
|
13 |
+
|
14 |
+
match = re.match(youtube_regex, url)
|
15 |
+
if match:
|
16 |
+
return match.group(6)
|
17 |
+
|
18 |
+
# URL parametrelerinden ID çıkarma
|
19 |
+
parsed_url = urlparse(url)
|
20 |
+
if parsed_url.netloc in ['youtube.com', 'www.youtube.com']:
|
21 |
+
query_params = parse_qs(parsed_url.query)
|
22 |
+
if 'v' in query_params:
|
23 |
+
return query_params['v'][0]
|
24 |
+
|
25 |
+
# youtu.be URL'leri için
|
26 |
+
if parsed_url.netloc == 'youtu.be':
|
27 |
+
return parsed_url.path.strip('/')
|
28 |
+
|
29 |
+
return None
|
30 |
+
|
31 |
+
# RapidAPI üzerinden transkript alma fonksiyonu
|
32 |
+
def get_transcript_via_rapidapi(video_url):
|
33 |
+
try:
|
34 |
+
# Video ID'yi çıkar
|
35 |
+
video_id = extract_video_id(video_url)
|
36 |
+
if not video_id:
|
37 |
+
return "Geçersiz YouTube URL'si. Lütfen geçerli bir YouTube video URL'si girin."
|
38 |
+
|
39 |
+
# Hugging Face'de ayarladığınız secret değeri al
|
40 |
+
RAPID_API_KEY = os.environ.get("RAPID_API_KEY", "")
|
41 |
+
|
42 |
+
if not RAPID_API_KEY:
|
43 |
+
return "RapidAPI anahtarı bulunamadı. Lütfen Hugging Face Space ayarlarında RAPID_API_KEY adlı bir secret oluşturun."
|
44 |
+
|
45 |
+
# ----- RapidAPI Endpoint Seçeneği 1: YouTube Transcriptor -----
|
46 |
+
# API endpoint ve parametreleri
|
47 |
+
url = "https://youtube-transcriptor.p.rapidapi.com/transcript"
|
48 |
+
querystring = {"video_id": video_id, "lang": "tr"}
|
49 |
+
headers = {
|
50 |
+
"X-RapidAPI-Key": RAPID_API_KEY,
|
51 |
+
"X-RapidAPI-Host": "youtube-transcriptor.p.rapidapi.com"
|
52 |
+
}
|
53 |
+
|
54 |
+
# API isteği gönder
|
55 |
+
response = requests.get(url, headers=headers, params=querystring)
|
56 |
+
|
57 |
+
# ----- VEYA -----
|
58 |
+
|
59 |
+
# ----- RapidAPI Endpoint Seçeneği 2: Ultimate YouTube API -----
|
60 |
+
# url = "https://ultimate-youtube-api.p.rapidapi.com/video/transcript"
|
61 |
+
# querystring = {"id": video_id}
|
62 |
+
# headers = {
|
63 |
+
# "X-RapidAPI-Key": RAPID_API_KEY,
|
64 |
+
# "X-RapidAPI-Host": "ultimate-youtube-api.p.rapidapi.com"
|
65 |
+
# }
|
66 |
+
# response = requests.get(url, headers=headers, params=querystring)
|
67 |
+
|
68 |
+
# API yanıtını kontrol et
|
69 |
+
if response.status_code == 200:
|
70 |
+
try:
|
71 |
+
data = response.json()
|
72 |
+
|
73 |
+
# API'nin yanıt formatına göre ayarla - YouTube Transcriptor API için:
|
74 |
+
if data and 'transcript' in data:
|
75 |
+
transcript_parts = []
|
76 |
+
for item in data['transcript']:
|
77 |
+
if 'text' in item:
|
78 |
+
transcript_parts.append(item['text'])
|
79 |
+
return " ".join(transcript_parts)
|
80 |
+
|
81 |
+
# Ultimate YouTube API formatı için:
|
82 |
+
# if data and isinstance(data, list):
|
83 |
+
# transcript_parts = []
|
84 |
+
# for item in data:
|
85 |
+
# if 'text' in item:
|
86 |
+
# transcript_parts.append(item['text'])
|
87 |
+
# return " ".join(transcript_parts)
|
88 |
+
|
89 |
+
return "Transkript verileri beklenen formatta değil. Manuel olarak girmeyi deneyin."
|
90 |
+
except Exception as e:
|
91 |
+
return f"JSON verileri işlenirken hata: {str(e)}. API yanıtı: {response.text[:100]}..."
|
92 |
+
else:
|
93 |
+
return f"API yanıt vermedi (Hata kodu: {response.status_code}). Yanıt: {response.text[:100]}... Lütfen transkripti manuel olarak girin."
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
return f"Transkript alınırken hata oluştu: {str(e)}. Lütfen transkripti manuel olarak girin."
|
97 |
+
|
98 |
+
# Gemini API ile metni özetleme
|
99 |
+
def fn_sum_text(transkript_text, word_count, model_sel, lang_sel, action_sel):
|
100 |
+
try:
|
101 |
+
# Hugging Face'de ayarladığınız secret değeri al
|
102 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
103 |
+
|
104 |
+
if not GEMINI_API_KEY:
|
105 |
+
return "Gemini API anahtarı bulunamadı. Lütfen Hugging Face Space ayarlarında GEMINI_API_KEY adlı bir secret oluşturun."
|
106 |
+
|
107 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
108 |
+
prompt = f"{transkript_text} metni {word_count} sayıda kelimeyle {lang_sel} dilinde {action_sel}"
|
109 |
+
|
110 |
+
response = client.models.generate_content(
|
111 |
+
model=model_sel,
|
112 |
+
contents=[prompt]
|
113 |
+
)
|
114 |
+
return response.text
|
115 |
+
except Exception as e:
|
116 |
+
return f"Özet oluşturulurken hata: {str(e)}"
|
117 |
+
|
118 |
+
# Her ikisini birleştiren fonksiyon
|
119 |
+
def process_video(video_url, word_count, model_sel, lang_sel, action_sel):
|
120 |
+
# Önce transkripti al
|
121 |
+
transcript = get_transcript_via_rapidapi(video_url)
|
122 |
+
|
123 |
+
# Hata kontrolü
|
124 |
+
if transcript.startswith("Hata") or transcript.startswith("Geçersiz") or transcript.startswith("API"):
|
125 |
+
return transcript, ""
|
126 |
+
|
127 |
+
# Transkript başarıyla alındıysa, özetle
|
128 |
+
summary = fn_sum_text(transcript, word_count, model_sel, lang_sel, action_sel)
|
129 |
+
return transcript, summary
|
130 |
+
|
131 |
+
# Gradio arayüzü
|
132 |
+
with gr.Blocks(title="YouTube Transcript Özetleyici") as demo:
|
133 |
+
gr.Markdown("""
|
134 |
+
# YouTube Transcript Özetleyici
|
135 |
+
|
136 |
+
## Nasıl Kullanılır:
|
137 |
+
1. YouTube video URL'sini girin
|
138 |
+
2. Özet ayarlarını yapın
|
139 |
+
3. "Transkript Al ve Özetle" butonuna tıklayın
|
140 |
+
|
141 |
+
Not: Bu uygulama RapidAPI ve Gemini API'yi kullanır. API anahtarları Hugging Face Space'in secret bölümünde saklanmalıdır.
|
142 |
+
""")
|
143 |
+
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column():
|
146 |
+
video_url = gr.Textbox(
|
147 |
+
placeholder="https://www.youtube.com/watch?v=...",
|
148 |
+
label="YouTube Video URL'si"
|
149 |
+
)
|
150 |
+
|
151 |
+
with gr.Accordion("Özet Ayarları", open=True):
|
152 |
+
word_count = gr.Slider(
|
153 |
+
minimum=50,
|
154 |
+
maximum=1000,
|
155 |
+
value=200,
|
156 |
+
step=10,
|
157 |
+
label="Özet Kelime Sayısı"
|
158 |
+
)
|
159 |
+
|
160 |
+
model_sel = gr.Dropdown(
|
161 |
+
choices=[
|
162 |
+
'gemini-2.0-flash',
|
163 |
+
'gemini-2.0-flash-lite',
|
164 |
+
'gemini-1.5-pro'
|
165 |
+
],
|
166 |
+
value='gemini-2.0-flash',
|
167 |
+
label="Model Seçimi"
|
168 |
+
)
|
169 |
+
|
170 |
+
lang_sel = gr.Dropdown(
|
171 |
+
choices=[
|
172 |
+
'Türkçe',
|
173 |
+
'İngilizce',
|
174 |
+
'Almanca',
|
175 |
+
'Fransızca',
|
176 |
+
'İspanyolca'
|
177 |
+
],
|
178 |
+
value='Türkçe',
|
179 |
+
label="Dil Seçimi"
|
180 |
+
)
|
181 |
+
|
182 |
+
action_sel = gr.Dropdown(
|
183 |
+
choices=[
|
184 |
+
'özetle',
|
185 |
+
'tam çeviri yap',
|
186 |
+
'madde madde özetle',
|
187 |
+
'ana başlıkları çıkar'
|
188 |
+
],
|
189 |
+
value='özetle',
|
190 |
+
label="İşlem Türü"
|
191 |
+
)
|
192 |
+
|
193 |
+
auto_process_btn = gr.Button("Transkript Al ve Özetle", variant="primary")
|
194 |
+
|
195 |
+
with gr.Accordion("Manuel İşlemler", open=False):
|
196 |
+
get_transcript_btn = gr.Button("Sadece Transkripti Al")
|
197 |
+
summarize_btn = gr.Button("Sadece Özetle")
|
198 |
+
|
199 |
+
with gr.Column():
|
200 |
+
transcript_text = gr.Textbox(
|
201 |
+
label="Video Transkripti",
|
202 |
+
placeholder="Transkript burada görünecek veya manuel olarak girebilirsiniz...",
|
203 |
+
lines=10
|
204 |
+
)
|
205 |
+
|
206 |
+
summary_text = gr.Textbox(
|
207 |
+
label="Özet Sonucu",
|
208 |
+
lines=10
|
209 |
+
)
|
210 |
+
|
211 |
+
# Buton işlevleri
|
212 |
+
auto_process_btn.click(
|
213 |
+
fn=process_video,
|
214 |
+
inputs=[video_url, word_count, model_sel, lang_sel, action_sel],
|
215 |
+
outputs=[transcript_text, summary_text]
|
216 |
+
)
|
217 |
+
|
218 |
+
get_transcript_btn.click(
|
219 |
+
fn=get_transcript_via_rapidapi,
|
220 |
+
inputs=[video_url],
|
221 |
+
outputs=[transcript_text]
|
222 |
+
)
|
223 |
+
|
224 |
+
summarize_btn.click(
|
225 |
+
fn=fn_sum_text,
|
226 |
+
inputs=[transcript_text, word_count, model_sel, lang_sel, action_sel],
|
227 |
+
outputs=[summary_text]
|
228 |
+
)
|
229 |
+
|
230 |
+
with gr.Accordion("Sorun Giderme", open=False):
|
231 |
+
gr.Markdown("""
|
232 |
+
### Sık Karşılaşılan Sorunlar
|
233 |
+
|
234 |
+
**API Anahtarı Hatası Alıyorum**
|
235 |
+
- Hugging Face Space'inizin ayarlarında "Repository Secrets" bölümünde "RAPID_API_KEY" ve "GEMINI_API_KEY" adlı iki secret oluşturduğunuzdan emin olun.
|
236 |
+
|
237 |
+
**"Transkript Bulunamadı" Hatası**
|
238 |
+
- Videonun altyazıları bulunmuyor olabilir
|
239 |
+
- Seçilen RapidAPI servisi bu video için altyazı sunmuyor olabilir
|
240 |
+
- Farklı bir RapidAPI endpoint'i deneyebilirsiniz (kod içinde yorum satırlarındaki alternatif endpoint'i kullanın)
|
241 |
+
|
242 |
+
**Diğer Sorunlar**
|
243 |
+
- Hugging Face Space'in loglarını kontrol edin
|
244 |
+
- RapidAPI kullanım limitinizi aşmış olabilirsiniz
|
245 |
+
""")
|
246 |
+
|
247 |
+
# Uygulamayı başlat
|
248 |
+
if __name__ == '__main__':
|
249 |
+
demo.launch()
|