Loren commited on
Commit
e8ff22d
·
verified ·
1 Parent(s): 483f33e

Upload 2 files

Browse files
Files changed (2) hide show
  1. script/create_sqlite_db.py +20 -3
  2. script/test_api.py +35 -0
script/create_sqlite_db.py CHANGED
@@ -1,21 +1,27 @@
1
  import sqlite3
2
  import pandas as pd
 
3
  import itertools
4
  import ast
5
  import uuid
 
6
  from pathlib import Path
7
 
8
  # Initialisations
9
  print("Initialisations ...")
 
10
  DATA_DIR = Path("../data") # dossier parent du script
11
- PARQUET_FILE = DATA_DIR / "medium_articles.parquet"
12
- SQLITE_FILE = DATA_DIR / "articles.db"
 
 
 
13
  # Créer le dossier data s'il n'existe pas
14
  DATA_DIR.mkdir(exist_ok=True)
15
 
16
  # Chargement des données
17
  print("Chargement des données ...")
18
- df = pd.read_parquet(PARQUET_FILE)
19
 
20
  # Initialisations de la base SQLite
21
  print("Initialisations de la base SQLite ...")
@@ -96,4 +102,15 @@ for _, row in df.iterrows():
96
 
97
  conn.commit()
98
  conn.close()
 
 
 
 
 
 
 
 
 
 
 
99
  print("Traitement terminé.")
 
1
  import sqlite3
2
  import pandas as pd
3
+ import os
4
  import itertools
5
  import ast
6
  import uuid
7
+ from huggingface_hub import hf_hub_download, upload_file
8
  from pathlib import Path
9
 
10
  # Initialisations
11
  print("Initialisations ...")
12
+ hf_token = os.environ["API_HF_TOKEN"]
13
  DATA_DIR = Path("../data") # dossier parent du script
14
+ parquet_path = hf_hub_download(repo_id="Loren/articles_data",
15
+ filename="mon_fichier.parquet")
16
+ REPO_ID = "Loren/articles_db" # dataset HF
17
+ DB_NAME = 'articles.db'
18
+ SQLITE_FILE = DATA_DIR / DB_NAME
19
  # Créer le dossier data s'il n'existe pas
20
  DATA_DIR.mkdir(exist_ok=True)
21
 
22
  # Chargement des données
23
  print("Chargement des données ...")
24
+ df = pd.read_parquet(parquet_path)
25
 
26
  # Initialisations de la base SQLite
27
  print("Initialisations de la base SQLite ...")
 
102
 
103
  conn.commit()
104
  conn.close()
105
+
106
+ # Upload dans le dataset hugging face
107
+ print("Upload dans le dataset hugging face ...")
108
+ upload_file(
109
+ path_or_fileobj=SQLITE_FILE,
110
+ path_in_repo=DB_NAME,
111
+ repo_id=REPO_ID,
112
+ repo_type="dataset",
113
+ token=hf_token
114
+ )
115
+
116
  print("Traitement terminé.")
script/test_api.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ BASE_URL = "http://localhost:8000" # ⚠️ changer en URL HF Space si déployé
4
+
5
+ def test_get_tags():
6
+ print("🔍 Test: /get_tags")
7
+ resp = requests.get(f"{BASE_URL}/get_tags")
8
+ if resp.status_code != 200:
9
+ print("❌ Erreur", resp.status_code, resp.text)
10
+ return
11
+ tags = resp.json().get("tags", [])
12
+ print(f"✅ {len(tags)} tags récupérés : {tags[:10]}...") # affiche 10 premiers
13
+
14
+ return tags
15
+
16
+ def test_get_articles_with_tags(tags):
17
+ if not tags:
18
+ print("⚠️ Aucun tag trouvé, test sauté")
19
+ return
20
+
21
+ print("\n🔍 Test: /get_articles_with_tags")
22
+ params = {"tags": tags}
23
+ resp = requests.get(f"{BASE_URL}/get_articles_with_tags", params=params)
24
+ if resp.status_code != 200:
25
+ print("❌ Erreur", resp.status_code, resp.text)
26
+ return
27
+ data = resp.json()
28
+ print(f"✅ Articles trouvés: {len(data['articles'])}")
29
+
30
+ def main():
31
+ tags = test_get_tags()
32
+ test_get_articles_with_tags(tags)
33
+
34
+ if __name__ == "__main__":
35
+ main()