ilsa15 commited on
Commit
440cc29
·
verified ·
1 Parent(s): a314bc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -14
app.py CHANGED
@@ -1,5 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import nest_asyncio
2
- import openai
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  import requests
5
  import streamlit as st
@@ -8,8 +73,8 @@ import os
8
  nest_asyncio.apply()
9
 
10
  # --- CONFIGURATION ---
11
- YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set this in HuggingFace Secrets
12
- channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
13
 
14
  # --- FUNCTION: Fetch recent video IDs from YouTube channel ---
15
  def get_latest_video_ids(channel_id, max_results=5):
@@ -30,15 +95,27 @@ def get_video_transcripts(video_ids):
30
  continue
31
  return all_transcripts
32
 
33
- # --- FUNCTION: Ask OpenAI ---
34
- def ask_openai(context, question):
35
- openai.api_key = os.environ.get("OPENAI_API_KEY") # Set this in HuggingFace Secrets
36
- prompt = f"Context: {context}\n\nQuestion: {question}\nAnswer:"
37
- response = openai.ChatCompletion.create(
38
- model="gpt-3.5-turbo",
39
- messages=[{"role": "user", "content": prompt}]
40
- )
41
- return response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # --- STREAMLIT APP ---
44
  def main():
@@ -53,11 +130,12 @@ def main():
53
  transcripts = get_video_transcripts(video_ids)
54
  full_context = "\n\n".join(transcripts)
55
  with st.spinner("🧠 Thinking..."):
56
- answer = ask_openai(full_context, question)
57
  st.success(answer)
58
 
59
  st.markdown("---")
60
- st.caption("Powered by YouTube + OpenAI | Built for @icodeguru0")
61
 
62
  if __name__ == "__main__":
63
  main()
 
 
1
+ # import nest_asyncio
2
+ # import openai
3
+ # from youtube_transcript_api import YouTubeTranscriptApi
4
+ # import requests
5
+ # import streamlit as st
6
+ # import os
7
+
8
+ # nest_asyncio.apply()
9
+
10
+ # # --- CONFIGURATION ---
11
+ # YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set this in HuggingFace Secrets
12
+ # channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
13
+
14
+ # # --- FUNCTION: Fetch recent video IDs from YouTube channel ---
15
+ # def get_latest_video_ids(channel_id, max_results=5):
16
+ # url = f"https://www.googleapis.com/youtube/v3/search?key={YOUTUBE_API_KEY}&channelId={channel_id}&part=snippet,id&order=date&maxResults={max_results}"
17
+ # response = requests.get(url)
18
+ # videos = response.json().get('items', [])
19
+ # return [v['id']['videoId'] for v in videos if v['id']['kind'] == 'youtube#video']
20
+
21
+ # # --- FUNCTION: Get video transcripts ---
22
+ # def get_video_transcripts(video_ids):
23
+ # all_transcripts = []
24
+ # for vid in video_ids:
25
+ # try:
26
+ # transcript = YouTubeTranscriptApi.get_transcript(vid)
27
+ # text = " ".join([t['text'] for t in transcript])
28
+ # all_transcripts.append(text)
29
+ # except:
30
+ # continue
31
+ # return all_transcripts
32
+
33
+ # # --- FUNCTION: Ask OpenAI ---
34
+ # def ask_openai(context, question):
35
+ # openai.api_key = os.environ.get("OPENAI_API_KEY") # Set this in HuggingFace Secrets
36
+ # prompt = f"Context: {context}\n\nQuestion: {question}\nAnswer:"
37
+ # response = openai.ChatCompletion.create(
38
+ # model="gpt-3.5-turbo",
39
+ # messages=[{"role": "user", "content": prompt}]
40
+ # )
41
+ # return response.choices[0].message.content.strip()
42
+
43
+ # # --- STREAMLIT APP ---
44
+ # def main():
45
+ # st.set_page_config(page_title="EduBot - YouTube Channel QA", layout="wide")
46
+ # st.title("🎓 EduBot for @icodeguru0")
47
+ # st.markdown("Ask anything based on the channel’s recent videos.")
48
+
49
+ # question = st.text_input("💬 Ask your question here:")
50
+ # if question:
51
+ # with st.spinner("🔍 Fetching videos and transcripts..."):
52
+ # video_ids = get_latest_video_ids(channel_id)
53
+ # transcripts = get_video_transcripts(video_ids)
54
+ # full_context = "\n\n".join(transcripts)
55
+ # with st.spinner("🧠 Thinking..."):
56
+ # answer = ask_openai(full_context, question)
57
+ # st.success(answer)
58
+
59
+ # st.markdown("---")
60
+ # st.caption("Powered by YouTube + OpenAI | Built for @icodeguru0")
61
+
62
+ # if __name__ == "__main__":
63
+ # main()
64
+
65
+
66
+
67
  import nest_asyncio
 
68
  from youtube_transcript_api import YouTubeTranscriptApi
69
  import requests
70
  import streamlit as st
 
73
  nest_asyncio.apply()
74
 
75
  # --- CONFIGURATION ---
76
+ YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") # Set in your HuggingFace Secrets
77
+ channel_id = "UCsv3kmQ5k1eIRG2R9mWN" # @icodeguru0
78
 
79
  # --- FUNCTION: Fetch recent video IDs from YouTube channel ---
80
  def get_latest_video_ids(channel_id, max_results=5):
 
95
  continue
96
  return all_transcripts
97
 
98
+ # --- FUNCTION: Ask Groq API directly ---
99
+ def ask_groq(context, question):
100
+ api_key = os.environ.get("GROQ_API_KEY") # Set this in your Secrets
101
+ url = "https://api.groq.com/v1/chat/completions" # Update if Groq uses a different URL
102
+
103
+ headers = {
104
+ "Authorization": f"Bearer {api_key}",
105
+ "Content-Type": "application/json"
106
+ }
107
+
108
+ data = {
109
+ "model": "gemma-7b", # Or your Groq model name
110
+ "messages": [
111
+ {"role": "system", "content": "You are a helpful assistant."},
112
+ {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}\nAnswer:"}
113
+ ]
114
+ }
115
+
116
+ response = requests.post(url, headers=headers, json=data)
117
+ response.raise_for_status()
118
+ return response.json()["choices"][0]["message"]["content"].strip()
119
 
120
  # --- STREAMLIT APP ---
121
  def main():
 
130
  transcripts = get_video_transcripts(video_ids)
131
  full_context = "\n\n".join(transcripts)
132
  with st.spinner("🧠 Thinking..."):
133
+ answer = ask_groq(full_context, question)
134
  st.success(answer)
135
 
136
  st.markdown("---")
137
+ st.caption("Powered by YouTube + Groq | Built for @icodeguru0")
138
 
139
  if __name__ == "__main__":
140
  main()
141
+