Spaces:
Runtime error
Runtime error
Download app.py from Sumayyea/TrendingAIBot: direct link, hf CLI and curl.
- Browser
- Download file 13.5 kB
-
https://huggingface.co/spaces/Sumayyea/TrendingAIBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sumayyea/TrendingAIBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sumayyea/TrendingAIBot/resolve/main/app.py
13.5 kB
| """ | |
| AI Trends Assistant - Fetches trending AI papers (arXiv) and YouTube videos | |
| """ | |
| import os | |
| import streamlit as st | |
| import arxiv | |
| import requests | |
| from dotenv import load_dotenv | |
| from typing import List, Dict | |
| load_dotenv() | |
| # --------------------- | |
| # API Keys | |
| # --------------------- | |
| GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") | |
| GEMINI_API_URL = os.environ.get("GEMINI_API_URL", "").strip() | |
| YOUTUBE_API_KEY = os.environ.get("YOUTUBE_API_KEY") | |
| # --------------------- | |
| # Super Prompt | |
| # --------------------- | |
| SYSTEM_PROMPT = """ | |
| Role: You are an expert AI research assistant who specializes in discovering and explaining trending topics, papers, and videos about Artificial Intelligence. | |
| Goal: Help users explore and understand what's currently trending in AI. | |
| """ | |
| # --------------------- | |
| # Gemini API wrapper | |
| # --------------------- | |
| def call_gemini(system_prompt: str, user_message: str, model: str = "gemini-2.0-flash") -> str: | |
| if not GEMINI_API_KEY: | |
| return "[Gemini unavailable] No GEMINI_API_KEY found." | |
| if not GEMINI_API_URL: | |
| return "[Gemini endpoint not configured] Set GEMINI_API_URL." | |
| try: | |
| headers = { | |
| "Authorization": f"Bearer {GEMINI_API_KEY}", | |
| "Content-Type": "application/json", | |
| } | |
| payload = {"system": system_prompt, "input": user_message, "model": model} | |
| resp = requests.post(GEMINI_API_URL, headers=headers, json=payload, timeout=30) | |
| resp.raise_for_status() | |
| data = resp.json() | |
| if isinstance(data, dict): | |
| if "output" in data: | |
| return data["output"] | |
| if "choices" in data and data["choices"]: | |
| ch0 = data["choices"][0] | |
| if "text" in ch0: | |
| return ch0["text"] | |
| if "message" in ch0 and "content" in ch0["message"]: | |
| return ch0["message"]["content"] | |
| return str(data) | |
| except Exception as e: | |
| return f"[Gemini request failed] {e}" | |
| # --------------------- | |
| # arXiv fetcher (IMPROVED) | |
| # --------------------- | |
| def fetch_arxiv(query: str, max_results: int = 5, sort_by: str = "Relevance") -> List[Dict]: | |
| """ | |
| Fetch AI papers from arXiv with improved search relevance. | |
| Improvements: | |
| - Searches in title (ti:) and abstract (abs:) for better matching | |
| - Supports sorting by Relevance or Recent | |
| - Uses quoted phrases for multi-word queries | |
| - Filters to CS/AI-related categories | |
| """ | |
| # Clean and prepare query | |
| query = query.strip() | |
| # Build search query - search in title and abstract | |
| # For multi-word queries, search as phrase AND individual terms | |
| words = query.split() | |
| if len(words) > 1: | |
| # Multi-word: search exact phrase in title OR abstract, plus individual terms | |
| phrase_query = f'"{query}"' | |
| title_search = f'ti:{phrase_query}' | |
| abstract_search = f'abs:{phrase_query}' | |
| # Also search individual important words (for broader matching) | |
| word_searches = " AND ".join([f'all:{w}' for w in words if len(w) > 2]) | |
| # Combine: (exact phrase in title OR abstract) OR (all words present) | |
| search_query = f'({title_search} OR {abstract_search}) OR ({word_searches})' | |
| else: | |
| # Single word: search in title and abstract | |
| search_query = f'ti:{query} OR abs:{query}' | |
| # Add AI-related category filter (but use OR to not be too restrictive) | |
| ai_categories = "(cat:cs.AI OR cat:cs.LG OR cat:cs.CL OR cat:cs.CV OR cat:cs.NE OR cat:cs.MA OR cat:stat.ML)" | |
| full_query = f'({search_query}) AND {ai_categories}' | |
| # Determine sort criteria | |
| if sort_by == "Relevance": | |
| sort_criterion = arxiv.SortCriterion.Relevance | |
| else: | |
| sort_criterion = arxiv.SortCriterion.SubmittedDate | |
| search = arxiv.Search( | |
| query=full_query, | |
| max_results=max_results * 2, # Fetch extra to filter duplicates | |
| sort_by=sort_criterion, | |
| sort_order=arxiv.SortOrder.Descending | |
| ) | |
| results = [] | |
| seen_titles = set() | |
| try: | |
| for r in search.results(): | |
| # Skip duplicates | |
| title_lower = r.title.lower().strip() | |
| if title_lower in seen_titles: | |
| continue | |
| seen_titles.add(title_lower) | |
| # Calculate a simple relevance indicator | |
| title_match = query.lower() in r.title.lower() | |
| abstract_match = query.lower() in r.summary.lower() | |
| results.append({ | |
| "title": r.title, | |
| "url": r.entry_id, | |
| "pdf_url": r.pdf_url, | |
| "summary": r.summary[:400] + "..." if len(r.summary) > 400 else r.summary, | |
| "published": r.published.strftime("%Y-%m-%d") if r.published else "N/A", | |
| "updated": r.updated.strftime("%Y-%m-%d") if r.updated else "N/A", | |
| "authors": ", ".join([a.name for a in r.authors[:4]]) + ("..." if len(r.authors) > 4 else ""), | |
| "categories": ", ".join(r.categories[:3]), | |
| "title_match": title_match, | |
| "abstract_match": abstract_match | |
| }) | |
| if len(results) >= max_results: | |
| break | |
| except Exception as e: | |
| st.error(f"arXiv API error: {e}") | |
| # Sort results: prioritize title matches, then abstract matches | |
| results.sort(key=lambda x: (x["title_match"], x["abstract_match"]), reverse=True) | |
| return results | |
| def fetch_arxiv_simple(query: str, max_results: int = 5) -> List[Dict]: | |
| """ | |
| Fallback simple search if advanced search returns no results. | |
| """ | |
| search = arxiv.Search( | |
| query=query, | |
| max_results=max_results, | |
| sort_by=arxiv.SortCriterion.Relevance, | |
| sort_order=arxiv.SortOrder.Descending | |
| ) | |
| results = [] | |
| try: | |
| for r in search.results(): | |
| results.append({ | |
| "title": r.title, | |
| "url": r.entry_id, | |
| "pdf_url": r.pdf_url, | |
| "summary": r.summary[:400] + "..." if len(r.summary) > 400 else r.summary, | |
| "published": r.published.strftime("%Y-%m-%d") if r.published else "N/A", | |
| "updated": r.updated.strftime("%Y-%m-%d") if r.updated else "N/A", | |
| "authors": ", ".join([a.name for a in r.authors[:4]]) + ("..." if len(r.authors) > 4 else ""), | |
| "categories": ", ".join(r.categories[:3]), | |
| "title_match": False, | |
| "abstract_match": False | |
| }) | |
| except Exception as e: | |
| st.error(f"arXiv API error: {e}") | |
| return results | |
| # --------------------- | |
| # YouTube fetcher (Official API) | |
| # --------------------- | |
| def fetch_youtube(query: str, max_results: int = 5) -> List[Dict]: | |
| """Fetch YouTube videos using official YouTube Data API v3.""" | |
| if not YOUTUBE_API_KEY: | |
| return [] | |
| try: | |
| url = "https://www.googleapis.com/youtube/v3/search" | |
| params = { | |
| "part": "snippet", | |
| "q": f"{query} AI tutorial", | |
| "type": "video", | |
| "maxResults": max_results, | |
| "order": "relevance", | |
| "key": YOUTUBE_API_KEY | |
| } | |
| response = requests.get(url, params=params, timeout=10) | |
| response.raise_for_status() | |
| data = response.json() | |
| videos = [] | |
| for item in data.get("items", []): | |
| video_id = item["id"]["videoId"] | |
| snippet = item["snippet"] | |
| videos.append({ | |
| "title": snippet.get("title", "Unknown"), | |
| "url": f"https://www.youtube.com/watch?v={video_id}", | |
| "channel": snippet.get("channelTitle", "Unknown"), | |
| "description": snippet.get("description", "")[:150] + "...", | |
| "thumbnail": snippet.get("thumbnails", {}).get("medium", {}).get("url", "") | |
| }) | |
| return videos | |
| except Exception as e: | |
| st.error(f"YouTube API error: {e}") | |
| return [] | |
| # --------------------- | |
| # Streamlit UI | |
| # --------------------- | |
| st.set_page_config(page_title="AI Trends Assistant", layout="centered") | |
| st.title("AI Trends Assistant") | |
| st.markdown("Fetch trending AI papers from arXiv and related YouTube videos.") | |
| with st.sidebar: | |
| st.header("βοΈ Settings") | |
| mode = st.radio("Mode", ["Fetch Links", "Chat (Gemini)"]) | |
| max_results = st.slider("Max results", min_value=1, max_value=20, value=5) | |
| st.markdown("---") | |
| st.subheader("arXiv Settings") | |
| arxiv_sort = st.radio("Sort papers by:", ["Relevance", "Recent"], index=0) | |
| st.markdown("---") | |
| st.markdown("**API Status:**") | |
| st.markdown(f"YouTube API: {'β Connected' if YOUTUBE_API_KEY else 'β Not configured'}") | |
| st.markdown(f"Gemini API: {'β Connected' if GEMINI_API_KEY else 'β Not configured'}") | |
| if mode == "Fetch Links": | |
| st.subheader("π Search Trending AI Topics") | |
| trending_topics = [ | |
| "Custom query...", | |
| "generative AI", | |
| "large language models", | |
| "multimodal learning", | |
| "diffusion models", | |
| "reinforcement learning from human feedback", | |
| "vision transformer", | |
| "retrieval augmented generation", | |
| "graph neural networks", | |
| "federated learning", | |
| "AI agents", | |
| "neural radiance fields", | |
| "text to image generation", | |
| "chain of thought reasoning", | |
| "mixture of experts" | |
| ] | |
| selected_topic = st.selectbox("Select a trending topic:", trending_topics) | |
| if selected_topic == "Custom query...": | |
| user_query = st.text_input("Enter your search query:", value="", placeholder="e.g., generative AI, transformer models, etc.") | |
| else: | |
| user_query = selected_topic | |
| col1, col2 = st.columns(2) | |
| fetch_arxiv_btn = col1.checkbox("Fetch arXiv Papers", value=True) | |
| fetch_youtube_btn = col2.checkbox("Fetch YouTube Videos", value=True) | |
| if st.button("π Fetch Results"): | |
| if not user_query.strip(): | |
| st.warning("Please enter or select a query.") | |
| else: | |
| # arXiv Results | |
| if fetch_arxiv_btn: | |
| st.write("### π arXiv Papers") | |
| st.caption(f"Searching for: **{user_query}** | Sorted by: **{arxiv_sort}**") | |
| with st.spinner("Fetching papers..."): | |
| papers = fetch_arxiv(user_query, max_results=max_results, sort_by=arxiv_sort) | |
| # Fallback to simple search if no results | |
| if not papers: | |
| st.info("Trying broader search...") | |
| papers = fetch_arxiv_simple(user_query, max_results=max_results) | |
| if not papers: | |
| st.warning("No arXiv results found. Try different keywords.") | |
| else: | |
| for i, p in enumerate(papers, 1): | |
| # Show relevance badge | |
| badge = "" | |
| if p.get("title_match"): | |
| badge = "π― " | |
| elif p.get("abstract_match"): | |
| badge = "β " | |
| with st.expander(f"{badge}{i}. {p['title']}", expanded=(i <= 3)): | |
| st.markdown(f"**Authors:** {p['authors']}") | |
| st.markdown(f"**Published:** {p['published']} | **Updated:** {p['updated']}") | |
| st.markdown(f"**Categories:** `{p['categories']}`") | |
| st.markdown(f"**Abstract:** {p['summary']}") | |
| col1, col2 = st.columns(2) | |
| col1.markdown(f"π [View Paper]({p['url']})") | |
| col2.markdown(f"π₯ [Download PDF]({p['pdf_url']})") | |
| # YouTube Results | |
| if fetch_youtube_btn: | |
| st.write("### π¬ YouTube Videos") | |
| if not YOUTUBE_API_KEY: | |
| st.warning("β οΈ YouTube API key not configured. Add `YOUTUBE_API_KEY` to your Hugging Face Secrets.") | |
| else: | |
| with st.spinner("Fetching videos..."): | |
| videos = fetch_youtube(user_query, max_results=max_results) | |
| if not videos: | |
| st.info("No YouTube results found.") | |
| else: | |
| for v in videos: | |
| col1, col2 = st.columns([1, 3]) | |
| with col1: | |
| if v.get("thumbnail"): | |
| st.image(v["thumbnail"], width=120) | |
| with col2: | |
| st.markdown(f"**[{v['title']}]({v['url']})**") | |
| st.caption(f"πΊ {v['channel']}") | |
| else: | |
| st.subheader("π¬ Chat with AI Trends Assistant") | |
| user_msg = st.text_area("Your message:", value="What's trending in multimodal AI research?") | |
| if st.button("Send"): | |
| with st.spinner("Calling Gemini..."): | |
| reply = call_gemini(SYSTEM_PROMPT, user_msg) | |
| st.markdown("**Assistant:**") | |
| st.write(reply) | |
| st.markdown("---") | |
| st.caption("Data sources: arXiv API, YouTube Data API v3") |