Spaces:
Build error
Build error
Update app.py
Browse files11 unmodified lines12131415161745 unmodified lines6364656667686911 unmodified lines818283848586878889909192939495969798991001011025 unmodified lines10810911011111211311411511611711811912012112212312412512612712812913013150 unmodified lines18218318418518618718818911 unmodified linesimport arxivimport numpy as npimport torch # Add torch to explicitly set the device# Access the Hugging Face token from the environment variableHF_TOKEN = os.getenv("HF_Token")45 unmodified linestry:with YoutubeDL(ydl_opts) as ydl:info = ydl.extract_info(video_url, download=True)# Extract the final downloaded file pathdownloaded_file = ydl.prepare_filename(info)return downloaded_fileexcept Exception as e:11 unmodified linestry:with YoutubeDL(ydl_opts) as ydl:# Perform a search for the query on YouTubesearch_results = ydl.extract_info(f"ytsearch:{query}", download=False)if 'entries' not in search_results or len(search_results['entries']) == 0:print(f"No YouTube results found for query: '{query}'")return []video_info = search_results['entries'][0] # Take the first resultvideo_title = video_info.get("title", "unknown_title")video_url = video_info.get("webpage_url", None)if not video_url:print("No URL found for the video.")return []# Download the videolocal_path = download_youtube_video(video_url, output_dir, title=video_title)if not local_path:return []5 unmodified linesprint(f"Error fetching YouTube video for query '{query}': {e}")return []from arxiv import Client, Search, SortCriteriondef fetch_from_arxiv(query="machine learning", max_results=2, output_dir="./papers"):"""Fetch papers from arXiv and download their PDFs."""print(f"Fetching papers for query: {query}")client = Client()search = Search(query=query,max_results=max_results,sort_by=SortCriterion.Relevance)metadata = []for i, result in enumerate(client.results(search)):pdf_url = result.pdf_url # Direct link to PDFfilename = f"{query.replace(' ', '_')}_arxiv_{i}.pdf"local_path = os.path.join(output_dir, filename)try:# Download the PDFresponse = requests.get(pdf_url)if response.status_code == 200:with open(local_path, 'wb') as f:50 unmodified linesfinal_response = generate_llama_response(query, context)return final_results, final_response# Example queryquery = "short easy machine learning"results, final_response = hybrid_rag_system_with_llama(query)print("\nFinal Response Generated by Llama 3:")print(final_response)11 unmodified lines1213141516171845 unmodified lines64656667686911 unmodified lines8182838485868788899091929394959697985 unmodified lines10410510610710810911011111211311411511611711811912012112212312450 unmodified lines17517617717817918018118218318418518618718818919019119219319419511 unmodified linesimport arxivimport numpy as npimport torch # Add torch to explicitly set the deviceimport gradio as gr# Access the Hugging Face token from the environment variableHF_TOKEN = os.getenv("HF_Token")45 unmodified linestry:with YoutubeDL(ydl_opts) as ydl:info = ydl.extract_info(video_url, download=True)downloaded_file = ydl.prepare_filename(info)return downloaded_fileexcept Exception as e:11 unmodified linestry:with YoutubeDL(ydl_opts) as ydl:search_results = ydl.extract_info(f"ytsearch:{query}", download=False)if 'entries' not in search_results or len(search_results['entries']) == 0:print(f"No YouTube results found for query: '{query}'")return []video_info = search_results['entries'][0]video_title = video_info.get("title", "unknown_title")video_url = video_info.get("webpage_url", None)if not video_url:print("No URL found for the video.")return []local_path = download_youtube_video(video_url, output_dir, title=video_title)if not local_path:return []5 unmodified linesprint(f"Error fetching YouTube video for query '{query}': {e}")return []def fetch_from_arxiv(query="machine learning", max_results=2, output_dir="./papers"):"""Fetch papers from arXiv and download their PDFs."""print(f"Fetching papers for query: {query}")client = arxiv.Client()search = arxiv.Search(query=query,max_results=max_results,sort_by=arxiv.SortCriterion.Relevance)metadata = []for i, result in enumerate(client.results(search)):pdf_url = result.pdf_urlfilename = f"{query.replace(' ', '_')}_arxiv_{i}.pdf"local_path = os.path.join(output_dir, filename)try:response = requests.get(pdf_url)if response.status_code == 200:with open(local_path, 'wb') as f:50 unmodified linesfinal_response = generate_llama_response(query, context)return final_results, final_response# Define Gradio interfacedef gradio_interface(query):"""Gradio wrapper for hybrid RAG system."""_, final_response = hybrid_rag_system_with_llama(query)return final_response# Create Gradio appinterface = gr.Interface(fn=gradio_interface,inputs=gr.Textbox(label="Enter your query", placeholder="e.g., short easy machine learning"),outputs=gr.Textbox(label="Generated Response"),title="Hybrid RAG System with LLaMA",description="Enter a query to retrieve relevant resources and generate a response using LLaMA.")# Launch Gradio appif __name__ == "__main__":interface.launch()
app.py+24-18