yunuseduran commited on
Commit
d055852
·
verified ·
1 Parent(s): c2c01f4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +217 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from google import genai
3
+ from youtube_transcript_api import YouTubeTranscriptApi
4
+ from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
5
+ import re
6
+ import time
7
+ import requests
8
+ from urllib.parse import urlparse, parse_qs
9
+ import socket
10
+
11
+ def find_free_port(start_port=7860, max_port=7960):
12
+ """Boş bir port bul"""
13
+ for port in range(start_port, max_port):
14
+ try:
15
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
16
+ s.bind(('', port))
17
+ return port
18
+ except OSError:
19
+ continue
20
+ return None
21
+
22
+ #https://www.youtube.com/watch?v=bhR274lZNLo
23
+ def is_valid_youtube_url(url):
24
+ try:
25
+ parsed_url = urlparse(url)
26
+ if 'youtube.com' in parsed_url.netloc or 'youtu.be' in parsed_url.netloc:
27
+ return True
28
+ return False
29
+ except:
30
+ return False
31
+
32
+ def extract_video_id(url):
33
+ if not url:
34
+ return None
35
+
36
+ try:
37
+ if 'youtu.be' in url:
38
+ return url.split('youtu.be/')[-1].split('?')[0]
39
+
40
+ parsed_url = urlparse(url)
41
+ if 'youtube.com' in parsed_url.netloc:
42
+ query = parse_qs(parsed_url.query)
43
+ return query.get('v', [None])[0]
44
+ except Exception:
45
+ return None
46
+
47
+ return None
48
+
49
+ def get_transcript(video_url):
50
+ if not video_url:
51
+ return "Lütfen bir YouTube URL'si girin."
52
+
53
+ if not is_valid_youtube_url(video_url):
54
+ return "Geçersiz YouTube URL'si. Lütfen doğru bir URL girin."
55
+
56
+ video_id = extract_video_id(video_url)
57
+ if not video_id:
58
+ return "Video ID çıkarılamadı. Lütfen geçerli bir YouTube URL'si girin."
59
+
60
+ max_retries = 3
61
+ retry_delay = 2
62
+
63
+ for attempt in range(max_retries):
64
+ try:
65
+ # Önce video erişilebilirliğini kontrol et
66
+ check_url = f"https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v={video_id}"
67
+ response = requests.get(check_url)
68
+ if response.status_code != 200:
69
+ return "Video bulunamadı veya erişilemez durumda."
70
+
71
+ # Transkript almayı dene
72
+ transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en', 'tr'])
73
+ if not transcript:
74
+ return "Transkript bulunamadı."
75
+
76
+ raw_text = " ".join([entry['text'] for entry in transcript])
77
+ if not raw_text.strip():
78
+ return "Transkript boş görünüyor."
79
+
80
+ return raw_text
81
+
82
+ except requests.exceptions.RequestException:
83
+ if attempt < max_retries - 1:
84
+ time.sleep(retry_delay)
85
+ continue
86
+ return "Bağlantı hatası oluştu. Lütfen internet bağlantınızı kontrol edin."
87
+
88
+ except TranscriptsDisabled:
89
+ return "Bu video için altyazılar devre dışı bırakılmış."
90
+
91
+ except NoTranscriptFound:
92
+ return "Bu video için transkript bulunamadı. Video başka bir dilde olabilir."
93
+
94
+ except VideoUnavailable:
95
+ return "Video bulunamadı veya erişilemez durumda."
96
+
97
+ except Exception as e:
98
+ if attempt < max_retries - 1:
99
+ time.sleep(retry_delay)
100
+ continue
101
+ return f"Bir hata oluştu: {str(e)}"
102
+
103
+ return "Maksimum deneme sayısına ulaşıldı. Lütfen daha sonra tekrar deneyin."
104
+
105
+ def fn_sum_text(transkript_text, word_count, model_sel, lang_sel, action_sel, GEMINI_API_KEY):
106
+ if not GEMINI_API_KEY:
107
+ return "Lütfen Gemini API anahtarını girin."
108
+
109
+ if not transkript_text:
110
+ return "Önce transkript alınmalıdır."
111
+
112
+ try:
113
+ client = genai.Client(api_key=GEMINI_API_KEY)
114
+ prompt = f"{transkript_text} metni {word_count} sayıda kelimeyle {lang_sel} dilinde {action_sel}"
115
+
116
+ max_retries = 3
117
+ retry_delay = 2
118
+
119
+ for attempt in range(max_retries):
120
+ try:
121
+ response = client.models.generate_content(
122
+ model=model_sel,
123
+ contents=[prompt]
124
+ )
125
+ return response.text
126
+ except Exception as e:
127
+ if attempt < max_retries - 1:
128
+ time.sleep(retry_delay)
129
+ continue
130
+ return f"API hatası: {str(e)}"
131
+
132
+ except Exception as e:
133
+ return f"Özet oluşturulurken hata: {str(e)}"
134
+
135
+ with gr.Blocks() as demo:
136
+ gr.Markdown("## YouTube Video Transkript ve Özet Aracı")
137
+
138
+ with gr.Row():
139
+ with gr.Column():
140
+ video_url = gr.Textbox(
141
+ label="YouTube Video URL",
142
+ placeholder="https://www.youtube.com/watch?v=...",
143
+ show_label=True
144
+ )
145
+ trs_btn = gr.Button('Transkripti Al', variant="primary")
146
+
147
+ with gr.Group():
148
+ GEMINI_API_KEY = gr.Textbox(
149
+ label="GEMINI API KEY",
150
+ placeholder="Gemini API anahtarınızı girin",
151
+ type="password",
152
+ show_label=True
153
+ )
154
+ word_count = gr.Slider(
155
+ minimum=50,
156
+ maximum=1000,
157
+ value=200,
158
+ step=10,
159
+ label="Özet Kelime Sayısı"
160
+ )
161
+ model_sel = gr.Dropdown(
162
+ choices=['gemini-2.0-flash', 'gemini-2.0-flash-lite', 'gemini-1.5-pro'],
163
+ value='gemini-2.0-flash',
164
+ label="Model Seçimi"
165
+ )
166
+ lang_sel = gr.Dropdown(
167
+ choices=['Türkçe', 'İngilizce', 'Almanca'],
168
+ value='Türkçe',
169
+ label="Dil Seçimi"
170
+ )
171
+ action_sel = gr.Dropdown(
172
+ choices=['Özetle', 'tam çeviri yap.'],
173
+ value='Özetle',
174
+ label="İşlem"
175
+ )
176
+ sum_btn = gr.Button('Özetle', variant="primary")
177
+
178
+ with gr.Column():
179
+ transkript_text = gr.Textbox(
180
+ label='Transkript',
181
+ lines=10,
182
+ show_label=True
183
+ )
184
+ sum_text = gr.Textbox(
185
+ label='Özet',
186
+ lines=10,
187
+ show_label=True
188
+ )
189
+
190
+ trs_btn.click(
191
+ fn=get_transcript,
192
+ inputs=video_url,
193
+ outputs=transkript_text,
194
+ api_name="get_transcript"
195
+ )
196
+
197
+ sum_btn.click(
198
+ fn=fn_sum_text,
199
+ inputs=[transkript_text, word_count, model_sel, lang_sel, action_sel, GEMINI_API_KEY],
200
+ outputs=sum_text,
201
+ api_name="summarize"
202
+ )
203
+
204
+ if __name__ == '__main__':
205
+ # Boş port bul
206
+ port = find_free_port()
207
+ if port is None:
208
+ print("Kullanılabilir port bulunamadı. Lütfen farklı bir port aralığı deneyin.")
209
+ exit(1)
210
+
211
+ print(f"Uygulama {port} portunda başlatılıyor...")
212
+ demo.launch(
213
+ server_name="127.0.0.1", # Localhost'a değiştirdik
214
+ server_port=port,
215
+ show_error=True,
216
+ share=False
217
+ )
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==4.44.1
2
+ google-generativeai==0.3.2
3
+ youtube-transcript-api==0.6.2
4
+ requests==2.31.0
5
+ websockets>=13.0,<15.0