Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from youtube_transcript_api import YouTubeTranscriptApi # Fixed case sensitivity
|
| 4 |
+
from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
|
| 5 |
+
|
| 6 |
+
# Function to extract transcript from Youtube video
|
| 7 |
+
def get_transcript(video_url):
|
| 8 |
+
video_id = video_url.split("v=")[1].split("&")[0]
|
| 9 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=["en", "tr"]) # Fixed case sensitivity
|
| 10 |
+
raw_text = " ".join([entry["text"] for entry in transcript])
|
| 11 |
+
return raw_text
|
| 12 |
+
|
| 13 |
+
# Function to summarize or translate the transcript
|
| 14 |
+
def fn_sum_text(transkript_text, word_count, model_sel, lang_sel, action_sel, GEMINI_API_KEY):
|
| 15 |
+
genai.configure(api_key=GEMINI_API_KEY) # Initializing Gemini AI API with the provided key
|
| 16 |
+
|
| 17 |
+
model = genai.GenerativeModel(model_sel) # Creating a model instance based on selected model
|
| 18 |
+
|
| 19 |
+
# Creating the prompt for AI processing
|
| 20 |
+
prompt = f"{transkript_text} metni {word_count} sayıda kelimeyle {lang_sel} dilinde {action_sel}"
|
| 21 |
+
|
| 22 |
+
response = model.generate_content(
|
| 23 |
+
contents=[prompt] # Sending the prompt to the AI model
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
return response.text # Returning the generated response
|
| 27 |
+
|
| 28 |
+
# Creating the Gradio UI
|
| 29 |
+
demo = gr.Blocks(theme=gr.themes.Citrus()) # Initializing Gradio Blocks with Citrus theme
|
| 30 |
+
with demo:
|
| 31 |
+
gr.Markdown("## Youtube Video Url Çeviri-Özet Gemini API") # Title for UI
|
| 32 |
+
|
| 33 |
+
with gr.Row(): # Creating a row layout
|
| 34 |
+
with gr.Column(): # Left column for input fields
|
| 35 |
+
video_url = gr.Textbox(placeholder="Youtube Video URL") # Input field for video URL
|
| 36 |
+
trs_btn = gr.Button("Transkripti Al") # Button to fetch transcript
|
| 37 |
+
GEMINI_API_KEY = gr.Textbox(placeholder="GEMINI_API_KEY", type="password", show_label=False) # API key input
|
| 38 |
+
|
| 39 |
+
word_count = gr.Slider(minimum=50, maximum=1000, value=200, step=10, label="Kelime Sayısı Seçimi") # Word count Selection
|
| 40 |
+
|
| 41 |
+
model_sel = gr.Dropdown(
|
| 42 |
+
choices=["gemini-2.0-flash", "gemini-2.0-flash-lite", "gemini-1.5-pro"],
|
| 43 |
+
value="gemini-2.0-flash",
|
| 44 |
+
label="Model Seçimi"
|
| 45 |
+
) # Dropdown for selecting AI model
|
| 46 |
+
|
| 47 |
+
lang_sel = gr.Dropdown(
|
| 48 |
+
choices=['Türkçe', 'İngilizce', 'Almanca', 'Rusça', 'İspanyolca'],
|
| 49 |
+
value="Türkçe",
|
| 50 |
+
label="Dil Seçimi"
|
| 51 |
+
) # Dropdown for language selection
|
| 52 |
+
|
| 53 |
+
action_sel = gr.Dropdown(
|
| 54 |
+
choices=["Özetle", "Tam çeviri yap."],
|
| 55 |
+
value="Özetle",
|
| 56 |
+
label="İşlem"
|
| 57 |
+
) # Dropdown to choose between summary and full translation
|
| 58 |
+
|
| 59 |
+
sum_btn = gr.Button("Özetle") # Button to generate summary
|
| 60 |
+
|
| 61 |
+
with gr.Column(): # Right column for output fields
|
| 62 |
+
transkript_text = gr.Textbox(label="Transkripti", lines=5) # Textbox to display transcript
|
| 63 |
+
sum_text = gr.Textbox(label="Özet", lines=5) # Textbox to display summary or translation
|
| 64 |
+
|
| 65 |
+
trs_btn.click(
|
| 66 |
+
fn=get_transcript, # Function to get transcript
|
| 67 |
+
inputs=video_url, # Input : Youtube URL
|
| 68 |
+
outputs=transkript_text # Output : Transcript Text
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
sum_btn.click(
|
| 72 |
+
fn=fn_sum_text, # Function to summarize/translate transcript
|
| 73 |
+
inputs=[transkript_text, word_count, model_sel, lang_sel, action_sel, GEMINI_API_KEY], # Inputs
|
| 74 |
+
outputs=sum_text # Output: Summary/translated text
|
| 75 |
+
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
demo.launch() # Launching Gradio app
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|