Krish30 commited on
Commit
74905c1
·
verified ·
1 Parent(s): 127b7e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -85
app.py CHANGED
@@ -1,85 +1,52 @@
1
-
2
- import streamlit as st
3
- import http.client
4
- import json
5
- import google.generativeai as genai
6
-
7
- # Configure the Generative AI API
8
- genai.configure(api_key="AIzaSyDiGhxeTJVQoK8jyRge6aaZGg7CrLq0zGk")
9
-
10
- import urllib.parse
11
-
12
-
13
- # Function to fetch subtitles using RapidAPI
14
- def fetch_youtube_subtitles(video_url):
15
- try:
16
- # Ensure the URL is properly encoded
17
- encoded_url = urllib.parse.quote(video_url, safe="")
18
-
19
- # Establish connection to RapidAPI service
20
- conn = http.client.HTTPSConnection("youtube-subtitles-captions-downloader.p.rapidapi.com")
21
- headers = {
22
- 'x-rapidapi-key': "ef4899abb4msh72ad9a5b860e697p191aa4jsn14fbc3ce6661",
23
- 'x-rapidapi-host': "youtube-subtitles-captions-downloader.p.rapidapi.com"
24
- }
25
-
26
- # Request subtitles
27
- conn.request("GET", f"/subtitles/srt/?url={encoded_url}&lang=en", headers=headers)
28
- res = conn.getresponse()
29
-
30
- # Handle response
31
- if res.status != 200:
32
- raise Exception(f"Failed to fetch subtitles. Status code: {res.status}")
33
-
34
- data = res.read()
35
- transcript = data.decode("utf-8")
36
-
37
- # Check if subtitle data is empty
38
- if not transcript:
39
- raise Exception("No subtitles available for this video.")
40
-
41
- return transcript
42
-
43
- except Exception as e:
44
- st.error(f"Error fetching subtitles: {str(e)}")
45
- return None
46
-
47
- # Function to generate summary using Gemini
48
- def get_gemini_response(prompt):
49
- model = genai.GenerativeModel('gemini-pro')
50
- response = model.generate_content(prompt)
51
- if hasattr(response.candidates[0], "content") and hasattr(response.candidates[0].content, "parts"):
52
- return response.candidates[0].content.parts[0].text
53
- else:
54
- return "Failed to generate summary."
55
-
56
- # Streamlit UI
57
- st.title("YouTube Video Summarizer")
58
-
59
- video_url = st.text_input("Enter YouTube Video URL:")
60
-
61
- if st.button("Summarize"):
62
- if video_url:
63
- try:
64
- # Fetch subtitles
65
- st.info("Fetching subtitles...")
66
- transcript = fetch_youtube_subtitles(video_url)
67
-
68
- if transcript:
69
- # Display fetched subtitles
70
- st.subheader("Fetched Transcript:")
71
- st.text_area("Transcript", transcript, height=200)
72
-
73
- # Summarize the transcript using Gemini
74
- st.info("Summarizing transcript...")
75
- summary = get_gemini_response(f"Summarize the following transcript: {transcript}")
76
-
77
- # Display the summary
78
- st.subheader("Summary:")
79
- st.write(summary)
80
- else:
81
- st.warning("No subtitles found for the video.")
82
- except Exception as e:
83
- st.error(f"An error occurred: {e}")
84
- else:
85
- st.error("Please enter a valid YouTube URL.")
 
1
+
2
+ import streamlit as st
3
+ import http.client
4
+ import json
5
+ import google.generativeai as genai
6
+
7
+ # Configure the Generative AI API
8
+ genai.configure(api_key="AIzaSyDiGhxeTJVQoK8jyRge6aaZGg7CrLq0zGk")
9
+
10
+
11
+
12
+ # Function to generate summary using Generative AI
13
+ def get_gemini_response(prompt):
14
+ model = genai.GenerativeModel('gemini-pro')
15
+ response = model.generate_content(prompt)
16
+ if hasattr(response.candidates[0], "content"):
17
+ return response.candidates[0].content
18
+ else:
19
+ return "Failed to generate summary."
20
+
21
+ # Streamlit App
22
+ st.title("YouTube Video Summarizer")
23
+
24
+ # Input for YouTube video URL
25
+ video_url = st.text_input("Enter YouTube Video URL:")
26
+
27
+ if st.button("Generate Summary"):
28
+ if "youtube.com" in video_url or "youtu.be" in video_url:
29
+ # Extract video ID
30
+ video_id = video_url.split("v=")[-1].split("&")[0] if "v=" in video_url else video_url.split("/")[-1]
31
+
32
+ # Fetch transcript
33
+ with st.spinner("Fetching transcript..."):
34
+ api_url = "https://resumegenie-c0pc.onrender.com/fetch_captions"
35
+ try:
36
+ response = requests.post(api_url, json={"video_id": video_id})
37
+ if response.status_code == 200:
38
+ transcript_data = response.json().get("transcript")
39
+ transcript = " ".join([item["text"] for item in transcript_data])
40
+
41
+ # Generate summary using Gemini
42
+ with st.spinner("Generating summary..."):
43
+ summary = get_gemini_response(f"Summarize the following text: {transcript}")
44
+ st.subheader("Video Summary")
45
+ st.write(summary)
46
+ else:
47
+ st.error(f"Error: {response.json().get('detail', 'Unknown error')}")
48
+ except Exception as e:
49
+ st.error(f"Error: {str(e)}")
50
+ else:
51
+ st.error("Invalid YouTube URL.")
52
+