Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 answer_question(transcript, question):
|
| 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"Using the following transcript as context, please answer the question:\n\nTranscript:\n{transcript}\n\nQuestion:\n{question}"}
|
| 36 |
+
],
|
| 37 |
+
max_tokens=150
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if response.choices and response.choices[0].message:
|
| 41 |
+
answer = response.choices[0].message.content.strip()
|
| 42 |
+
return answer
|
| 43 |
+
else:
|
| 44 |
+
return "No answer available."
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error in answering question: {str(e)}"
|
| 47 |
+
|
| 48 |
+
def handle_query(youtube_url, question):
|
| 49 |
+
transcript = get_transcript(youtube_url)
|
| 50 |
+
if "Error" in transcript:
|
| 51 |
+
return transcript
|
| 52 |
+
answer = answer_question(transcript, question)
|
| 53 |
+
return answer
|
| 54 |
+
|
| 55 |
+
st.title("YouTube Video Doubt Bot")
|
| 56 |
+
youtube_url = st.text_input("YouTube URL", placeholder="Enter YouTube URL here...")
|
| 57 |
+
|
| 58 |
+
if youtube_url:
|
| 59 |
+
transcript = get_transcript(youtube_url)
|
| 60 |
+
if "Error" in transcript:
|
| 61 |
+
st.write(transcript)
|
| 62 |
+
else:
|
| 63 |
+
st.write("Transcript successfully loaded.")
|
| 64 |
+
question = st.text_input("Ask a question about the video")
|
| 65 |
+
if st.button("Get Answer"):
|
| 66 |
+
answer = handle_query(youtube_url, question)
|
| 67 |
+
st.write("### Answer")
|
| 68 |
+
st.write(answer)
|