update recommender and news_fetcher

This commit is contained in:
Ayomide
2025-07-24 16:35:04 +01:00
parent f28755e1fd
commit 8d2a277afe
4 changed files with 330 additions and 36 deletions
+24 -4
View File
@@ -1,7 +1,7 @@
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from .news_fetcher import fetch_all_news, save_raw_news, save_processed_news
from .recommender import recommend_similar, process_articles_for_vector_db
from .recommender import recommend_similar, process_articles_for_vector_db, news_recommender
from .recommender import analyze_article_with_groq
from .recommender import get_personalized_recommendations, vector_db
from .vector_store import VectorDB
@@ -41,7 +41,7 @@ async def root():
@app.get("/fetch-news")
async def fetch_news():
"""Fetch news from RSS feeds"""
"""Fetch news from RSS feeds with duplicate detection"""
try:
articles = fetch_all_news()
@@ -74,7 +74,7 @@ async def fetch_news():
@app.get("/recommend-news")
async def recommend_news(article_id: str):
"""Retrieve similar news based on the selected article"""
"""Retrieve similar news based on the selected article (backward compatibility)"""
try:
recommendations = recommend_similar(article_id)
@@ -91,6 +91,25 @@ async def recommend_news(article_id: str):
raise HTTPException(status_code=500, detail=f"Error getting recommendations: {str(e)}")
@app.get("/recommend-by-text")
async def recommend_by_text(text_description: str, top_n: int = 3):
"""Recommend articles based on text description"""
try:
recommendations = news_recommender.recommend_by_text(text_description, top_n)
if not recommendations:
raise HTTPException(status_code=404, detail="No recommendations found")
return {
"text_description": text_description,
"recommendations": recommendations,
"count": len(recommendations)
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error getting recommendations: {str(e)}")
@app.get("/analyze-article")
async def analyze_article(article_id: str):
"""Analyze article using Groq LLM"""
@@ -134,6 +153,7 @@ async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "database_articles": len(vector_db.articles)}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
uvicorn.run(app, host="0.0.0.0", port=8000)