import feedparser from transformers import MarianMTModel, MarianTokenizer, pipeline  # Première étape, récupération des articles
def fetch_rss_articles(rss_url):     feed = feedparser.parse(rss_url)     articles = [{         'title_ja': entry.title,         'link': entry.link     } for entry in feed.entries[:8]] # Prendre les 8 premiers articles     return articles  # Deuxième étape, traduction jp vers en, puis en vers fr
def translate_ja_to_en_to_fr(texts):     # jp vers en     tokenizer_ja_en = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ja-en")     model_ja_en = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ja-en")     translated_to_en = model_ja_en.generate(**tokenizer_ja_en(texts, return_tensors="pt", padding=True, truncation=True, max_length=512))     texts_en = [tokenizer_ja_en.decode(t, skip_special_tokens=True) for t in translated_to_en]          # en vers fr
    tokenizer_en_fr = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")     model_en_fr = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-en-fr")     translated_to_fr = model_en_fr.generate(**tokenizer_en_fr(texts_en, return_tensors="pt", padding=True, truncation=True, max_length=512))     return [tokenizer_en_fr.decode(t, skip_special_tokens=True) for t in translated_to_fr]  # Troisième étape, classification
def classify_articles(translated_titles):     classifier = pipeline('zero-shot-classification', model='facebook/bart-large-mnli')     categories = ['politics', 'sports', 'culture', 'economy', 'international', 'others']     results = []     for title in translated_titles:         result = classifier(title, categories, hypothesis_template="This text is about {}.")         results.append((title, result['labels'][0], result['scores'][0]))     return results  # URL du flux RSS rss_url = "https://www.hokkaido-np.co.jp/output/7/free/index.ad.xml" articles = fetch_rss_articles(rss_url) titles_ja = [article['title_ja'] for article in articles] links = [article['link'] for article in articles] translated_titles = translate_ja_to_en_to_fr(titles_ja) categorized_articles = classify_articles(translated_titles)
 
for article, (title_fr, category, score), link in zip(articles, categorized_articles, links):
    print(f"Titre (jp) : {article['title_ja']}\nTitre (fr) : {title_fr}\nCatégorie prédite : {category}, Score : {score:.4f}\nLien: {link}\n")