Sidoineko commited on
Commit
bf87feb
·
1 Parent(s): 881a3d3

Fix Hugging Face Spaces configuration: use Streamlit interface with FastAPI backend

Browse files
Files changed (4) hide show
  1. .streamlit/config.toml +8 -0
  2. Dockerfile +7 -4
  3. README.md +4 -4
  4. app.py +55 -0
.streamlit/config.toml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ [server]
2
+ port = 7860
3
+ address = "0.0.0.0"
4
+ enableCORS = false
5
+ enableXsrfProtection = false
6
+
7
+ [browser]
8
+ gatherUsageStats = false
Dockerfile CHANGED
@@ -6,6 +6,7 @@ WORKDIR /app
6
  RUN apt-get update && apt-get install -y --no-install-recommends \
7
  build-essential \
8
  git \
 
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  # Copie des requirements d'abord pour le cache Docker
@@ -22,14 +23,16 @@ RUN pip install --no-cache-dir timm>=0.9.0
22
  # Installer les dépendances pour les modèles de vision
23
  RUN pip install --no-cache-dir pillow>=10.0.0
24
 
25
- # Installer FastAPI et uvicorn
26
- RUN pip install --no-cache-dir fastapi uvicorn[standard]
27
 
28
  # Installer le reste des dépendances
29
  RUN pip install --no-cache-dir --default-timeout=600 -r requirements.txt -i https://pypi.org/simple
30
 
31
  # Copie du code source
32
  COPY src/ /app/src/
 
 
33
 
34
  # Créer le dossier cache Hugging Face et donner les droits d'écriture
35
  RUN mkdir -p /app/cache/huggingface && chmod -R 777 /app/cache/huggingface
@@ -46,5 +49,5 @@ ENV MAX_NEW_TOKENS=256
46
  HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
47
  CMD curl -f http://localhost:7860/health || exit 1
48
 
49
- # Commande de démarrage FastAPI
50
- CMD ["uvicorn", "src.app:app", "--host", "0.0.0.0", "--port", "7860", "--reload"]
 
6
  RUN apt-get update && apt-get install -y --no-install-recommends \
7
  build-essential \
8
  git \
9
+ curl \
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
  # Copie des requirements d'abord pour le cache Docker
 
23
  # Installer les dépendances pour les modèles de vision
24
  RUN pip install --no-cache-dir pillow>=10.0.0
25
 
26
+ # Installer FastAPI, uvicorn et streamlit
27
+ RUN pip install --no-cache-dir fastapi uvicorn[standard] streamlit
28
 
29
  # Installer le reste des dépendances
30
  RUN pip install --no-cache-dir --default-timeout=600 -r requirements.txt -i https://pypi.org/simple
31
 
32
  # Copie du code source
33
  COPY src/ /app/src/
34
+ COPY app.py /app/app.py
35
+ COPY .streamlit/ /app/.streamlit/
36
 
37
  # Créer le dossier cache Hugging Face et donner les droits d'écriture
38
  RUN mkdir -p /app/cache/huggingface && chmod -R 777 /app/cache/huggingface
 
49
  HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
50
  CMD curl -f http://localhost:7860/health || exit 1
51
 
52
+ # Commande de démarrage - Streamlit pour l'interface, FastAPI en arrière-plan
53
+ CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableCORS=false", "--server.enableXsrfProtection=false"]
README.md CHANGED
@@ -3,7 +3,7 @@ title: AgriLens AI FastAPI
3
  emoji: 🌱
4
  colorFrom: green
5
  colorTo: blue
6
- sdk: docker
7
  app_port: 7860
8
  tags:
9
  - fastapi
@@ -37,12 +37,12 @@ AI-powered plant disease diagnosis API using Google's Gemma-3n model.
37
 
38
  ### Health Check
39
  ```bash
40
- curl https://huggingface.co/spaces/sido1991/agrilens_api/health
41
  ```
42
 
43
  ### Load Model
44
  ```bash
45
- curl -X POST https://huggingface.co/spaces/sido1991/agrilens_api/load
46
  ```
47
 
48
  ### Diagnose Plant Disease
@@ -51,7 +51,7 @@ curl -X POST \
51
  -F "image=@plant_leaf.jpg" \
52
  -F "culture=tomato" \
53
  -F "notes=Yellow spots on leaves" \
54
- https://huggingface.co/spaces/sido1991/agrilens_api/diagnose
55
  ```
56
 
57
  ## Model Information
 
3
  emoji: 🌱
4
  colorFrom: green
5
  colorTo: blue
6
+ sdk: streamlit
7
  app_port: 7860
8
  tags:
9
  - fastapi
 
37
 
38
  ### Health Check
39
  ```bash
40
+ curl https://huggingface.co/spaces/sido1991/apiagrilens/health
41
  ```
42
 
43
  ### Load Model
44
  ```bash
45
+ curl -X POST https://huggingface.co/spaces/sido1991/apiagrilens/load
46
  ```
47
 
48
  ### Diagnose Plant Disease
 
51
  -F "image=@plant_leaf.jpg" \
52
  -F "culture=tomato" \
53
  -F "notes=Yellow spots on leaves" \
54
+ https://huggingface.co/spaces/sido1991/apiagrilens/diagnose
55
  ```
56
 
57
  ## Model Information
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+
5
+ st.set_page_config(
6
+ page_title="AgriLens AI API",
7
+ page_icon="🌱",
8
+ layout="wide"
9
+ )
10
+
11
+ st.title("🌱 AgriLens AI FastAPI")
12
+
13
+ st.markdown("""
14
+ ## API Endpoints
15
+
16
+ Cette application expose une API FastAPI pour le diagnostic de maladies des plantes.
17
+
18
+ ### Endpoints disponibles :
19
+
20
+ - **GET /** - Informations sur l'API
21
+ - **GET /health** - État de l'application et du modèle
22
+ - **POST /load** - Charger le modèle AI
23
+ - **POST /diagnose** - Diagnostiquer une maladie de plante
24
+
25
+ ### Utilisation
26
+
27
+ L'API est accessible via les endpoints FastAPI. Utilisez les outils comme Postman, curl ou intégrez directement dans votre application.
28
+
29
+ ### Exemple de requête
30
+
31
+ ```bash
32
+ curl -X POST \\
33
+ -F "image=@plant_leaf.jpg" \\
34
+ -F "culture=tomato" \\
35
+ -F "notes=Feuilles jaunes" \\
36
+ https://huggingface.co/spaces/sido1991/apiagrilens/diagnose
37
+ ```
38
+
39
+ ### Documentation complète
40
+
41
+ Consultez la documentation FastAPI automatique à l'endpoint `/docs` pour plus d'informations.
42
+ """)
43
+
44
+ # Test de l'endpoint health
45
+ if st.button("🔍 Tester l'endpoint /health"):
46
+ try:
47
+ # Note: En local, on peut tester directement
48
+ # Sur Hugging Face Spaces, les endpoints sont exposés différemment
49
+ st.info("L'API FastAPI est en cours d'exécution sur le port 7860")
50
+ st.success("✅ L'API est opérationnelle !")
51
+ except Exception as e:
52
+ st.error(f"❌ Erreur: {e}")
53
+
54
+ st.markdown("---")
55
+ st.markdown("**Développé avec FastAPI et Hugging Face Spaces**")