Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
3 |
+
import re
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
from groq import Groq
|
7 |
+
|
8 |
+
# Load the API key from .env file
|
9 |
+
load_dotenv()
|
10 |
+
api_key = os.getenv('GROQ_API_KEY')
|
11 |
+
client = Groq(api_key=api_key)
|
12 |
+
|
13 |
+
if not api_key:
|
14 |
+
raise ValueError("API key is not set. Please check your .env file and ensure GROQ_API_KEY is set.")
|
15 |
+
|
16 |
+
def get_transcript(url):
|
17 |
+
try:
|
18 |
+
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url)
|
19 |
+
if match:
|
20 |
+
video_id = match.group(1)
|
21 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
22 |
+
transcript_text = ' '.join([entry['text'] for entry in transcript])
|
23 |
+
return transcript_text
|
24 |
+
else:
|
25 |
+
return "No video ID found in URL."
|
26 |
+
except Exception as e:
|
27 |
+
return f"Error: {str(e)}"
|
28 |
+
|
29 |
+
def summarize_text(text):
|
30 |
+
try:
|
31 |
+
response = client.chat.completions.create(
|
32 |
+
model="mixtral-8x7b-32768",
|
33 |
+
messages=[
|
34 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
35 |
+
{"role": "user", "content": f"Please provide a concise summary of the following text:\n\n{text}"}
|
36 |
+
],
|
37 |
+
max_tokens=150
|
38 |
+
)
|
39 |
+
|
40 |
+
if response.choices and response.choices[0].message:
|
41 |
+
summary = response.choices[0].message.content.strip()
|
42 |
+
return summary
|
43 |
+
else:
|
44 |
+
return "No summary available."
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error in summarizing text: {str(e)}"
|
47 |
+
|
48 |
+
def handle_summary(youtube_url):
|
49 |
+
transcript = get_transcript(youtube_url)
|
50 |
+
if "Error" in transcript:
|
51 |
+
return transcript
|
52 |
+
summary = summarize_text(transcript)
|
53 |
+
return summary
|
54 |
+
|
55 |
+
st.title("YouTube Summary Generator")
|
56 |
+
youtube_url = st.text_input("YouTube URL", placeholder="Enter YouTube URL here...")
|
57 |
+
|
58 |
+
if st.button("Generate Summary"):
|
59 |
+
summary = handle_summary(youtube_url)
|
60 |
+
st.text_area("Summary", summary, height=200)
|
61 |
+
if __name__ == "__main__":
|
62 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|