Download api.py from AhmedShahan/portfolio_knowledge_base: direct link, hf CLI and curl.
- Browser
- Download file 7.55 kB
-
https://huggingface.co/spaces/AhmedShahan/portfolio_knowledge_base/resolve/main/api.py
- Command line
-
hf download hf://spaces/AhmedShahan/portfolio_knowledge_base/api.py
-
curl -L -o api.py https://huggingface.co/spaces/AhmedShahan/portfolio_knowledge_base/resolve/main/api.py
7.55 kB
| import os | |
| from datetime import datetime, timezone | |
| from dotenv import load_dotenv | |
| from fastapi import FastAPI,HTTPException | |
| from pydantic import BaseModel | |
| from sentence_transformers import SentenceTransformer | |
| from qdrant_client import QdrantClient | |
| from groq import Groq | |
| from groq import RateLimitError | |
| from tavily import TavilyClient | |
| import sheet_writer | |
| load_dotenv() | |
| # ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| COLLECTION_NAME = "portfolio" | |
| TOP_K = 5 | |
| MODEL_NAME = "nomic-ai/nomic-embed-text-v1.5" | |
| GROQ_MODEL = "llama-3.3-70b-versatile" | |
| AI_NEWS_COUNT = 10 | |
| # ββ Init ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app = FastAPI() | |
| print("π§ Loading embedding model...") | |
| embedder = SentenceTransformer(MODEL_NAME, trust_remote_code=True) | |
| qdrant = QdrantClient( | |
| url=os.getenv("QDRANT_URL"), | |
| api_key=os.getenv("QDRANT_API_KEY"), | |
| ) | |
| groq_client = Groq(api_key=os.getenv("GROQ_API_KEY")) | |
| tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API_KEY")) | |
| # ββ Schema ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class QueryRequest(BaseModel): | |
| question: str | |
| # ββ Routes βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def root(): | |
| return {"status": "ok", "message": "Portfolio RAG API is running"} | |
| def query(request: QueryRequest): | |
| # Step 1: Embed query | |
| vector = embedder.encode(request.question, normalize_embeddings=True).tolist() | |
| # Step 2: Search Qdrant | |
| results = qdrant.query_points( | |
| collection_name=COLLECTION_NAME, | |
| query=vector, | |
| limit=TOP_K, | |
| ).points | |
| # Step 3: Build context | |
| context = "\n\n---\n\n".join([r.payload.get("text", "") for r in results]) | |
| # Step 4: Ask Groq | |
| prompt = f"""You are an AI assistant for Ahmed Shahan's portfolio. | |
| Answer the user's question using ONLY the context below. | |
| If the answer is not in the context, say "I don't have that information." | |
| Context: | |
| {context} | |
| Question: {request.question} | |
| """ | |
| try: | |
| response = groq_client.chat.completions.create( | |
| model=GROQ_MODEL, | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=0.2, | |
| ) | |
| return { | |
| "question": request.question, | |
| "answer": response.choices[0].message.content, | |
| } | |
| except RateLimitError as e: | |
| # Extract retry time from error message | |
| import re | |
| match = re.search(r"try again in (.+?)\.", str(e)) | |
| retry_after = match.group(1) if match else "some time" | |
| raise HTTPException( | |
| status_code=429, | |
| detail=f"Daily chat limit hit. Please try again after {retry_after}." | |
| ) | |
| # ββ AI News Endpoint ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def get_ai_news(): | |
| """Fetch today's top AI news, ranked by popularity, with simple explanations.""" | |
| try: | |
| # Step 1: Fetch AI news from Tavily | |
| search_result = tavily_client.search( | |
| query="artificial intelligence AI news today", | |
| topic="news", | |
| time_range="day", | |
| max_results=AI_NEWS_COUNT, | |
| ) | |
| articles = search_result.get("results", []) | |
| if not articles: | |
| return {"articles": [], "message": "No AI news found today."} | |
| # Step 2: Sort by popularity (score descending) | |
| articles.sort(key=lambda a: a.get("score", 0), reverse=True) | |
| # Step 3: Build a single prompt for Groq to simplify all articles | |
| news_block = "\n\n".join( | |
| f"--- Article {i+1} ---\n" | |
| f"Title: {a['title']}\n" | |
| f"Content: {a.get('content', 'No summary available.')}" | |
| for i, a in enumerate(articles) | |
| ) | |
| simplify_prompt = f"""You are a helpful assistant that explains AI news in simple, easy-to-understand language for a general audience. | |
| Below are {len(articles)} AI news articles from today. For EACH article, provide: | |
| 1. The original title | |
| 2. A point-by-point explanation using VERY SIMPLE words that anyone can understand (like you're explaining to your grandmother) | |
| Format your response EXACTLY like this (separate each article with a blank line): | |
| ===== Article 1 ===== | |
| Title: <original title> | |
| Key points in simple words: | |
| β’ <point 1> | |
| β’ <point 2> | |
| β’ <point 3> | |
| ===== Article 2 ===== | |
| Title: <original title> | |
| Key points in simple words: | |
| β’ <point 1> | |
| β’ <point 2> | |
| ...and so on for all articles. | |
| Rules: | |
| - Use ONLY simple, everyday words. No jargon. | |
| - Each article should have 2-4 bullet points. | |
| - Keep each bullet point short (1-2 sentences max). | |
| - Focus on WHAT happened and WHY it matters. | |
| Here are the articles: | |
| {news_block} | |
| """ | |
| response = groq_client.chat.completions.create( | |
| model=GROQ_MODEL, | |
| messages=[{"role": "user", "content": simplify_prompt}], | |
| temperature=0.3, | |
| ) | |
| simplified_text = response.choices[0].message.content | |
| # Step 4: Parse the response back into structured data | |
| # Each article block starts with ===== Article N ===== | |
| import re | |
| blocks = re.split(r"=====\s*Article\s+\d+\s*=====", simplified_text) | |
| blocks = [b.strip() for b in blocks if b.strip()] | |
| retrieved_at = datetime.now(timezone.utc).isoformat() | |
| result_articles = [] | |
| for i, article in enumerate(articles): | |
| explanation = blocks[i] if i < len(blocks) else "Explanation not available." | |
| # Clean up the explanation - remove the "Title: ..." line since we have it separately | |
| explanation_lines = explanation.split("\n") | |
| # Filter out the "Title:" line if present | |
| clean_lines = [ | |
| line for line in explanation_lines | |
| if not line.strip().lower().startswith("title:") | |
| ] | |
| clean_explanation = "\n".join(clean_lines).strip() | |
| result_articles.append({ | |
| "title": article["title"], | |
| "url": article["url"], | |
| "published_date": article.get("published_date", ""), | |
| "popularity_score": round(article.get("score", 0), 4), | |
| "explanation_simple": clean_explanation, | |
| }) | |
| # Write to Google Sheet (non-blocking β failures are logged, never crash the API) | |
| sheet_writer.append_ai_news_articles(retrieved_at, result_articles) | |
| return { | |
| "data_retrieved_at": retrieved_at, | |
| "timezone": "UTC", | |
| "articles": result_articles, | |
| "total": len(result_articles), | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Failed to fetch AI news: {str(e)}") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True) |