MojoHz commited on
Commit
5aa5b9a
·
verified ·
1 Parent(s): 35cfda2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -69,12 +69,12 @@ def download_youtube_video(video_url, output_dir, title=None):
69
  def fetch_and_download_youtube_video(query, output_dir="./downloads"):
70
  """Fetch and download the best YouTube video for a query."""
71
  ydl_opts = {
72
- 'quiet': True,
73
- 'noplaylist': True,
74
- 'default_search': 'ytsearch',
75
- 'max_downloads': 1,
76
- 'skip_download': True,
77
- }
78
  try:
79
  with YoutubeDL(ydl_opts) as ydl:
80
  search_results = ydl.extract_info(query, download=False)
@@ -87,28 +87,37 @@ def fetch_and_download_youtube_video(query, output_dir="./downloads"):
87
  print(f"Error fetching YouTube video for query '{query}': {e}")
88
  return []
89
 
 
 
90
  def fetch_from_arxiv(query="machine learning", max_results=2, output_dir="./papers"):
91
  """Fetch papers from arXiv and download their PDFs."""
92
- search = arxiv.Search(
 
 
93
  query=query,
94
  max_results=max_results,
95
- sort_by=arxiv.SortCriterion.Relevance
96
  )
97
  metadata = []
98
- for i, result in enumerate(search.results()):
99
- pdf_url = result.pdf_url
100
  filename = f"{query.replace(' ', '_')}_arxiv_{i}.pdf"
101
  local_path = os.path.join(output_dir, filename)
102
  try:
 
103
  response = requests.get(pdf_url)
104
  if response.status_code == 200:
105
  with open(local_path, 'wb') as f:
106
  f.write(response.content)
 
107
  metadata.append({"title": result.title, "url": pdf_url, "file_path": local_path, "type": "paper"})
 
 
108
  except Exception as e:
109
  print(f"Error downloading paper: {e}")
110
  return metadata
111
 
 
112
  def generate_llama_response(query, context=None):
113
  """Generate a response using LLaMA 2."""
114
  input_text = f"Query: {query}\n"
 
69
  def fetch_and_download_youtube_video(query, output_dir="./downloads"):
70
  """Fetch and download the best YouTube video for a query."""
71
  ydl_opts = {
72
+ 'quiet': True,
73
+ 'outtmpl': f"{output_dir}/{sanitized_title}.%(ext)s",
74
+ 'format': 'best',
75
+ 'cookiesfrombrowser': ('chrome',), # Adjust for your browser (e.g., 'firefox', 'edge')
76
+ }
77
+
78
  try:
79
  with YoutubeDL(ydl_opts) as ydl:
80
  search_results = ydl.extract_info(query, download=False)
 
87
  print(f"Error fetching YouTube video for query '{query}': {e}")
88
  return []
89
 
90
+ from arxiv import Client, Search, SortCriterion
91
+
92
  def fetch_from_arxiv(query="machine learning", max_results=2, output_dir="./papers"):
93
  """Fetch papers from arXiv and download their PDFs."""
94
+ print(f"Fetching papers for query: {query}")
95
+ client = Client()
96
+ search = Search(
97
  query=query,
98
  max_results=max_results,
99
+ sort_by=SortCriterion.Relevance
100
  )
101
  metadata = []
102
+ for i, result in enumerate(client.results(search)):
103
+ pdf_url = result.pdf_url # Direct link to PDF
104
  filename = f"{query.replace(' ', '_')}_arxiv_{i}.pdf"
105
  local_path = os.path.join(output_dir, filename)
106
  try:
107
+ # Download the PDF
108
  response = requests.get(pdf_url)
109
  if response.status_code == 200:
110
  with open(local_path, 'wb') as f:
111
  f.write(response.content)
112
+ print(f"Downloaded paper: {filename}")
113
  metadata.append({"title": result.title, "url": pdf_url, "file_path": local_path, "type": "paper"})
114
+ else:
115
+ print(f"Failed to download paper: {pdf_url}. Status code: {response.status_code}")
116
  except Exception as e:
117
  print(f"Error downloading paper: {e}")
118
  return metadata
119
 
120
+
121
  def generate_llama_response(query, context=None):
122
  """Generate a response using LLaMA 2."""
123
  input_text = f"Query: {query}\n"