{"id":175,"date":"2025-01-05T00:45:41","date_gmt":"2025-01-05T00:45:41","guid":{"rendered":"https:\/\/steveprudhomme.org\/?p=175"},"modified":"2025-01-05T00:54:12","modified_gmt":"2025-01-05T00:54:12","slug":"tutoriel-dynamique-generation-de-definitions-astronomiques-de-lensemble-des-types-objets-du-cosmos-avec-llama","status":"publish","type":"post","link":"https:\/\/steveprudhomme.org\/index.php\/2025\/01\/05\/tutoriel-dynamique-generation-de-definitions-astronomiques-de-lensemble-des-types-objets-du-cosmos-avec-llama\/","title":{"rendered":"G\u00e9n\u00e9ration de d\u00e9finitions astronomiques sur l&rsquo;ensemble des types objets du cosmos avec LLaMA"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Salut les passionn\u00e9s d&rsquo;astronomie et les codeurs curieux ! Aujourd&rsquo;hui, je vous propose un voyage interstellaire \u00e0 travers un script Python qui utilise l&rsquo;API locale d&rsquo;Ollama pour g\u00e9n\u00e9rer des d\u00e9finitions et des notes explicatives sur des objets astronomiques. \ud83c\udf0c\u2728<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pourquoi ce script est-il g\u00e9nial ?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Imaginez que vous avez un fichier Excel rempli de donn\u00e9es sur des objets c\u00e9lestes, mais vous avez besoin de descriptions d\u00e9taill\u00e9es et de notes explicatives pour chaque type et sous-type d&rsquo;objet. C&rsquo;est l\u00e0 que notre script entre en jeu ! Il parcourt chaque ligne de votre fichier Excel, envoie des requ\u00eates \u00e0 l&rsquo;API d&rsquo;Ollama pour obtenir des d\u00e9finitions en fran\u00e7ais, et sauvegarde les r\u00e9sultats dans un nouveau fichier Excel. Et le meilleur dans tout \u00e7a ? Si un type ou un sous-type d&rsquo;objet a d\u00e9j\u00e0 \u00e9t\u00e9 trait\u00e9, le script r\u00e9utilise la d\u00e9finition pr\u00e9c\u00e9demment g\u00e9n\u00e9r\u00e9e pour \u00e9viter des appels redondants \u00e0 l&rsquo;API. \ud83d\ude80<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Origine du Fichier Excel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Le fichier Excel utilis\u00e9 dans ce script provient du&nbsp;<strong>Catalogue Exotica<\/strong>&nbsp;de Breakthrough Listen, un projet de recherche de l&rsquo;Universit\u00e9 de Californie \u00e0 Berkeley. Le Catalogue Exotica est une collection de plus de 700 objets c\u00e9lestes distincts, visant \u00e0 inclure \u00ab\u00a0un de chaque\u00a0\u00bb type d&rsquo;objet astronomique connu. Il comprend des exemples de chaque type dans l&rsquo;\u00e9chantillon Prototype, des objets extr\u00eames avec des propri\u00e9t\u00e9s record dans l&rsquo;\u00e9chantillon Superlative, et des cibles \u00e9nigmatiques dans l&rsquo;\u00e9chantillon Anomaly. \ud83c\udf20<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Le fichier Excel a \u00e9t\u00e9 extrait du code source de l&rsquo;article scientifique \u00ab\u00a0One of Everything: The Breakthrough Listen Exotica Catalog\u00a0\u00bb disponible sur arXiv. La conversion du tableau LaTeX en fichier Excel a \u00e9t\u00e9 r\u00e9alis\u00e9e \u00e0 l&rsquo;aide du convertisseur en ligne disponible sur TableConvert.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pr\u00e9requis<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Avant de plonger dans le code, assurez-vous d&rsquo;avoir les \u00e9l\u00e9ments suivants :<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python 3.x<\/li>\n\n\n\n<li>Biblioth\u00e8que\u00a0<code>pandas<\/code><\/li>\n\n\n\n<li>Biblioth\u00e8que\u00a0<code>requests<\/code><\/li>\n\n\n\n<li>API locale d&rsquo;Ollama accessible \u00e0 l&rsquo;adresse\u00a0<code>http:\/\/localhost:11434\/api\/generate<\/code><\/li>\n\n\n\n<li>Fichier Excel\u00a0<code>updated_table.xlsx<\/code>\u00a0avec les colonnes\u00a0<code>Type<\/code>,\u00a0<code>Sous-Type<\/code>, et\u00a0<code>Exemple<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installation des Pr\u00e9requis<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Installer Python 3.x<\/strong>\u00a0: Vous pouvez t\u00e9l\u00e9charger et installer Python \u00e0 partir du site officiel python.org.<\/li>\n\n\n\n<li><strong>Installer les biblioth\u00e8ques n\u00e9cessaires<\/strong>\u00a0:<code>pip install pandas requests openpyxl<\/code><\/li>\n\n\n\n<li><strong>Configurer l&rsquo;API locale d&rsquo;Ollama<\/strong>\u00a0: Assurez-vous que l&rsquo;API locale d&rsquo;Ollama est accessible \u00e0 l&rsquo;adresse\u00a0<code>http:\/\/localhost:11434\/api\/generate<\/code>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Le Script Magique \ud83e\ude84<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Voici le script complet avec des commentaires d\u00e9taill\u00e9s en fran\u00e7ais et en anglais :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n# This program is free software: you can redistribute it and\/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program. If not, see &lt;https:\/\/www.gnu.org\/licenses\/>.\n\n# Description:\n# This script uses the local Ollama API to generate definitions and explanatory notes\n# on astronomical objects from an Excel file. The script iterates over each row of the\n# Excel file, sends requests to the API to obtain definitions in French, and saves the\n# results in a new Excel file. If a type or subtype of object has already been processed,\n# the script reuses the previously generated definition to avoid redundant API calls.\n#\n# Description :\n# Ce script utilise l'API locale d'Ollama pour g\u00e9n\u00e9rer des d\u00e9finitions et des notes explicatives\n# sur des objets astronomiques \u00e0 partir d'un fichier Excel. Le script parcourt chaque ligne du\n# fichier Excel, envoie des requ\u00eates \u00e0 l'API pour obtenir des d\u00e9finitions en fran\u00e7ais, et sauvegarde\n# les r\u00e9sultats dans un nouveau fichier Excel. Si un type ou un sous-type d'objet a d\u00e9j\u00e0 \u00e9t\u00e9 trait\u00e9,\n# le script r\u00e9utilise la d\u00e9finition pr\u00e9c\u00e9demment g\u00e9n\u00e9r\u00e9e pour \u00e9viter des appels redondants \u00e0 l'API.\n\n# Origin of the Excel file:\n# The Excel file used in this script comes from the Breakthrough Listen Exotica Catalog,\n# a research project at the University of California, Berkeley. The Exotica Catalog is a\n# collection of over 700 distinct celestial objects, aiming to include \"one of everything\"\n# type of astronomical object known. It includes examples of each type in the Prototype sample,\n# extreme objects with record properties in the Superlative sample, and enigmatic targets in the Anomaly sample.\n#\n# The Excel file was extracted from the source code of the scientific article \"One of Everything:\n# The Breakthrough Listen Exotica Catalog\" available on arXiv. The conversion of the LaTeX table\n# to an Excel file was done using the online converter available on TableConvert.\n#\n# Origine du fichier Excel :\n# Le fichier Excel utilis\u00e9 dans ce script provient du Catalogue Exotica de Breakthrough Listen,\n# un projet de recherche de l'Universit\u00e9 de Californie \u00e0 Berkeley. Le Catalogue Exotica est une\n# collection de plus de 700 objets c\u00e9lestes distincts, visant \u00e0 inclure \"un de chaque\" type d'objet\n# astronomique connu. Il comprend des exemples de chaque type dans l'\u00e9chantillon Prototype, des objets\n# extr\u00eames avec des propri\u00e9t\u00e9s record dans l'\u00e9chantillon Superlative, et des cibles \u00e9nigmatiques dans\n# l'\u00e9chantillon Anomaly.\n#\n# Le fichier Excel a \u00e9t\u00e9 extrait du code source de l'article scientifique \"One of Everything:\n# The Breakthrough Listen Exotica Catalog\" disponible sur arXiv. La conversion du tableau LaTeX\n# en fichier Excel a \u00e9t\u00e9 r\u00e9alis\u00e9e \u00e0 l'aide du convertisseur en ligne disponible sur TableConvert.\n\n# Import necessary libraries\n# Importer les biblioth\u00e8ques n\u00e9cessaires\nimport pandas as pd\nimport requests\nimport json\n\n# Load the Excel file\n# Charger le fichier Excel\nprint(\"Loading the Excel file...\")\nprint(\"Chargement du fichier Excel...\")\ndf = pd.read_excel('updated_table.xlsx', engine='openpyxl')\nprint(\"Excel file loaded successfully.\")\nprint(\"Fichier Excel charg\u00e9 avec succ\u00e8s.\")\n\n# Dictionaries to store already generated definitions\n# Dictionnaires pour stocker les d\u00e9finitions d\u00e9j\u00e0 g\u00e9n\u00e9r\u00e9es\ndefinitions_type = {}\ndefinitions_subtype = {}\ndefinitions_example = {}\n\n# Function to generate text using the local Ollama API\n# Fonction pour g\u00e9n\u00e9rer du texte avec l'API locale d'Ollama\ndef generate_text(prompt):\n    print(f\"Sending request to the API for the prompt: {prompt}\")\n    print(f\"Envoi de la requ\u00eate \u00e0 l'API pour le prompt : {prompt}\")\n    response = requests.post(\n        \"http:\/\/localhost:11434\/api\/generate\",  # Ensure the local API is accessible at this address\n        # Assurez-vous que l'API locale est accessible \u00e0 cette adresse\n        json={\"model\": \"llama3.3:70b-instruct-q2_K\", \"prompt\": prompt}\n    )\n    \n    # Debugging: Print the raw API response\n    # D\u00e9bogage : Afficher la r\u00e9ponse brute de l'API\n    print(\"Raw API response:\", response.text)\n    print(\"R\u00e9ponse brute de l'API:\", response.text)\n    \n    # Assemble fragmented responses\n    # Assembler les r\u00e9ponses fragment\u00e9es\n    full_response = \"\"\n    for line in response.text.splitlines():\n        try:\n            json_line = json.loads(line)\n            full_response += json_line&#91;\"response\"]\n            if json_line.get(\"done\", False):\n                break\n        except json.JSONDecodeError as e:\n            print(\"JSON decoding error:\", e)\n            print(\"Erreur de d\u00e9codage JSON:\", e)\n            return \"Text generation error\"\n            return \"Erreur de g\u00e9n\u00e9ration de texte\"\n    \n    print(f\"Complete API response: {full_response}\")\n    print(f\"R\u00e9ponse compl\u00e8te de l'API : {full_response}\")\n    return full_response\n\n# Iterate over the DataFrame rows and fill the columns\n# Parcourir les lignes du DataFrame et remplir les colonnes\nprint(\"Starting to process DataFrame rows...\")\nprint(\"D\u00e9but du traitement des lignes du DataFrame...\")\nfor index, row in df.iterrows():\n    print(f\"Processing row {index + 1}\/{len(df)}\")\n    print(f\"Traitement de la ligne {index + 1}\/{len(df)}\")\n    type_query = row&#91;'Type']\n    subtype_query = row&#91;'Sous-Type']\n    example_query = row&#91;'Exemple']\n    \n    # Check if the type definition has already been generated\n    # V\u00e9rifier si la d\u00e9finition du type a d\u00e9j\u00e0 \u00e9t\u00e9 g\u00e9n\u00e9r\u00e9e\n    if type_query in definitions_type:\n        df.at&#91;index, 'D\u00e9finition du type'] = definitions_type&#91;type_query]\n    else:\n        definition_type = generate_text(f\"D\u00e9finition du type d'objet astronomique {type_query} en fran\u00e7ais:\")\n        definitions_type&#91;type_query] = definition_type\n        df.at&#91;index, 'D\u00e9finition du type'] = definition_type\n\n# Save the updated Excel file after each definition\n    # Sauvegarder le fichier Excel mis \u00e0 jour apr\u00e8s chaque d\u00e9finition\n    df.to_excel(f'updated_table_with_definitions_{index + 1}_type.xlsx', index=False)\n    \n    # Check if the subtype definition has already been generated\n    # V\u00e9rifier si la d\u00e9finition du sous-type a d\u00e9j\u00e0 \u00e9t\u00e9 g\u00e9n\u00e9r\u00e9e\n    subtype_key = (type_query, subtype_query)\n    if subtype_key in definitions_subtype:\n        df.at&#91;index, 'D\u00e9finition du sous-type'] = definitions_subtype&#91;subtype_key]\n    else:\n        definition_subtype = generate_text(f\"D\u00e9finition du sous-type d'objet astronomique {subtype_query} de type {type_query} en fran\u00e7ais:\")\n        definitions_subtype&#91;subtype_key] = definition_subtype\n        df.at&#91;index, 'D\u00e9finition du sous-type'] = definition_subtype\n    \n    # Save the updated Excel file after each definition\n    # Sauvegarder le fichier Excel mis \u00e0 jour apr\u00e8s chaque d\u00e9finition\n    df.to_excel(f'updated_table_with_definitions_{index + 1}_subtype.xlsx', index=False)\n    \n    # Check if the explanatory note on the example has already been generated\n    # V\u00e9rifier si la note explicative sur l'exemple a d\u00e9j\u00e0 \u00e9t\u00e9 g\u00e9n\u00e9r\u00e9e\n    example_key = (type_query, subtype_query, example_query)\n    if example_key in definitions_example:\n        df.at&#91;index, 'Note explicative sur l\\'exemple'] = definitions_example&#91;example_key]\n    else:\n        definition_example = generate_text(f\"Note explicative sur l'exemple d'objet astronomique {type_query}, {subtype_query}, {example_query} en fran\u00e7ais:\")\n        definitions_example&#91;example_key] = definition_example\n        df.at&#91;index, 'Note explicative sur l\\'exemple'] = definition_example\n    \n    # Save the updated Excel file after each definition\n    # Sauvegarder le fichier Excel mis \u00e0 jour apr\u00e8s chaque d\u00e9finition\n    df.to_excel(f'updated_table_with_definitions_{index + 1}_example.xlsx', index=False)\n\nprint(\"Finished processing rows. Saving the final Excel file...\")\nprint(\"Traitement des lignes termin\u00e9. Sauvegarde du fichier Excel final...\")\n\n# Save the final updated Excel file\n# Sauvegarder le fichier Excel final mis \u00e0 jour\ndf.to_excel('updated_table_with_definitions_final.xlsx', index=False)\n\nprint(\"The Excel file has been updated with definitions generated by LLaMA in French.\")\nprint(\"Le fichier Excel a \u00e9t\u00e9 mis \u00e0 jour avec des d\u00e9finitions g\u00e9n\u00e9r\u00e9es par LLaMA en fran\u00e7ais.\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">M\u00e9diagraphie<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Breakthrough Listen. (n.d.).\u00a0<em>Exotic Target Catalog<\/em>. R\u00e9cup\u00e9r\u00e9 de\u00a0http:\/\/seti.berkeley.edu\/exotica\/<\/li>\n\n\n\n<li>Lacki, B. C., Lebofsky, M., Isaacson, H., Siemion, A., Sheikh, S., Croft, S., &#8230; &amp; Werthimer, D. (2020).\u00a0<em>One of Everything: The Breakthrough Listen Exotica Catalog<\/em>. arXiv. R\u00e9cup\u00e9r\u00e9 de\u00a0https:\/\/arxiv.org\/pdf\/2006.11304.pdf<\/li>\n\n\n\n<li>TableConvert. (n.d.).\u00a0<em>Convert LaTeX Table to Excel Online<\/em>. R\u00e9cup\u00e9r\u00e9 de\u00a0https:\/\/tableconvert.com\/latex-to-excel<\/li>\n\n\n\n<li>Python Software Foundation. (n.d.).\u00a0<em>pandas documentation<\/em>. R\u00e9cup\u00e9r\u00e9 de\u00a0https:\/\/pandas.pydata.org\/pandas-docs\/stable\/<\/li>\n\n\n\n<li>Reitz, K., &amp; Chovanec, T. (n.d.).\u00a0<em>Requests: HTTP for Humans<\/em>. R\u00e9cup\u00e9r\u00e9 de\u00a0https:\/\/docs.python-requests.org\/en\/latest\/<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Et voil\u00e0 ! Vous avez maintenant un script Python puissant et flexible pour g\u00e9n\u00e9rer des d\u00e9finitions et des notes explicatives sur des objets astronomiques \u00e0 partir d&rsquo;un fichier Excel. Ce script utilise l&rsquo;API locale d&rsquo;Ollama pour obtenir des d\u00e9finitions en fran\u00e7ais et sauvegarde les r\u00e9sultats dans un nouveau fichier Excel. N&rsquo;h\u00e9sitez pas \u00e0 personnaliser ce script selon vos besoins et \u00e0 explorer les merveilles de l&rsquo;astronomie avec des descriptions d\u00e9taill\u00e9es et pr\u00e9cises. \ud83d\ude80\ud83c\udf0c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Si vous avez des questions ou des probl\u00e8mes, n&rsquo;h\u00e9sitez pas \u00e0 demander de l&rsquo;aide ! \ud83d\ude0a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bon codage et bon voyage interstellaire ! \ud83c\udf20\u2728<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Plus de d\u00e9tails sur mon site Github : <a href=\"https:\/\/github.com\/steveprudhomme\/astronomical-object-type-metadata\">https:\/\/github.com\/steveprudhomme\/astronomical-object-type-metadata<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Salut les passionn\u00e9s d&rsquo;astronomie et les codeurs curieux ! Aujourd&rsquo;hui, je vous propose un voyage interstellaire \u00e0 travers un script Python qui utilise l&rsquo;API locale d&rsquo;Ollama pour g\u00e9n\u00e9rer des d\u00e9finitions et des notes explicatives sur des objets astronomiques. \ud83c\udf0c\u2728 Pourquoi ce script est-il g\u00e9nial ? Imaginez que vous avez un fichier Excel rempli de donn\u00e9es [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[415,410,414,412,411,413],"tags":[452,419,430,66,169,393,450,425,426,432,435,437,440,121,420,443,421,416,418,453,166,423,389,427,444,417,422,442,424,449,397,436,401,438,445,392,429,428,446,431,404,441,433,434,451,439,447,58,448],"class_list":["post-175","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-informatique-information-et-ouvrages-generaux","category-informatique","category-intelligence-artificielle","category-language-de-programmation-specifiques","category-languages-de-programmation","category-methodes-informatiques-speciales","tag-analyse-de-donnees","tag-api-locale","tag-arxiv","tag-astronomie","tag-automatisation","tag-automatisation-des-taches","tag-bibliotheques-python","tag-breakthrough-listen","tag-catalogue-exotica","tag-classification-dewey","tag-connaissance","tag-convertisseur-latex-en-excel","tag-developpement-logiciel","tag-exploration-spatiale","tag-fichier-excel","tag-generation-automatique-de-contenu","tag-generation-de-texte","tag-information-et-ouvrages-generaux","tag-informatique","tag-innovations-technologiques","tag-intelligence-artificielle","tag-langage-de-programmation-specifiques","tag-langages-de-programmation","tag-mathematiques","tag-methodes-informatiques-avancees","tag-methodes-informatiques-speciales","tag-objets-astronomiques","tag-objets-celestes","tag-ollama","tag-outils-de-developpement","tag-pandas","tag-programmation","tag-programmation-informatique","tag-programmes-et-donnees","tag-projets-de-recherche","tag-python","tag-recherche-scientifique","tag-requests","tag-science-des-donnees","tag-sciences-naturelles","tag-script-python","tag-systemes","tag-systemes-dinformation","tag-tableconvert","tag-technologies-emergentes","tag-traitement-des-donnees","tag-traitement-des-donnees-et-informatique","tag-tutoriel","tag-universite-de-californie-a-berkeley"],"_links":{"self":[{"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/posts\/175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":2,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/posts\/175\/revisions\/178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/media\/176"}],"wp:attachment":[{"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steveprudhomme.org\/index.php\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}