Spaces:
Runtime error
Runtime error
Simplify configuration: use Docker SDK with Streamlit interface and FastAPI backend
Browse files- Dockerfile +2 -4
- README.md +1 -1
- app.py +46 -7
Dockerfile
CHANGED
|
@@ -30,9 +30,7 @@ RUN pip install --no-cache-dir fastapi uvicorn[standard] streamlit
|
|
| 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
|
| 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,5 +47,5 @@ ENV MAX_NEW_TOKENS=256
|
|
| 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
|
| 53 |
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableCORS=false", "--server.enableXsrfProtection=false"]
|
|
|
|
| 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 . /app/
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Créer le dossier cache Hugging Face et donner les droits d'écriture
|
| 36 |
RUN mkdir -p /app/cache/huggingface && chmod -R 777 /app/cache/huggingface
|
|
|
|
| 47 |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
| 48 |
CMD curl -f http://localhost:7860/health || exit 1
|
| 49 |
|
| 50 |
+
# Commande de démarrage - Streamlit qui lance FastAPI en arrière-plan
|
| 51 |
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:
|
| 7 |
app_port: 7860
|
| 8 |
tags:
|
| 9 |
- fastapi
|
|
|
|
| 3 |
emoji: 🌱
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: blue
|
| 6 |
+
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
tags:
|
| 9 |
- fastapi
|
app.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
st.set_page_config(
|
| 6 |
page_title="AgriLens AI API",
|
|
@@ -8,6 +12,24 @@ st.set_page_config(
|
|
| 8 |
layout="wide"
|
| 9 |
)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
st.title("🌱 AgriLens AI FastAPI")
|
| 12 |
|
| 13 |
st.markdown("""
|
|
@@ -33,7 +55,7 @@ curl -X POST \\
|
|
| 33 |
-F "image=@plant_leaf.jpg" \\
|
| 34 |
-F "culture=tomato" \\
|
| 35 |
-F "notes=Feuilles jaunes" \\
|
| 36 |
-
|
| 37 |
```
|
| 38 |
|
| 39 |
### Documentation complète
|
|
@@ -44,12 +66,29 @@ Consultez la documentation FastAPI automatique à l'endpoint `/docs` pour plus d
|
|
| 44 |
# Test de l'endpoint health
|
| 45 |
if st.button("🔍 Tester l'endpoint /health"):
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
st.markdown("---")
|
| 55 |
st.markdown("**Développé avec FastAPI et Hugging Face Spaces**")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import subprocess
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
st.set_page_config(
|
| 10 |
page_title="AgriLens AI API",
|
|
|
|
| 12 |
layout="wide"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Fonction pour lancer FastAPI en arrière-plan
|
| 16 |
+
def start_fastapi():
|
| 17 |
+
try:
|
| 18 |
+
subprocess.Popen([
|
| 19 |
+
"uvicorn", "src.app:app",
|
| 20 |
+
"--host", "0.0.0.0",
|
| 21 |
+
"--port", "7860",
|
| 22 |
+
"--reload"
|
| 23 |
+
])
|
| 24 |
+
except Exception as e:
|
| 25 |
+
st.error(f"Erreur lors du lancement de FastAPI: {e}")
|
| 26 |
+
|
| 27 |
+
# Démarrer FastAPI en arrière-plan
|
| 28 |
+
if 'fastapi_started' not in st.session_state:
|
| 29 |
+
st.session_state.fastapi_started = True
|
| 30 |
+
threading.Thread(target=start_fastapi, daemon=True).start()
|
| 31 |
+
time.sleep(2) # Attendre que FastAPI démarre
|
| 32 |
+
|
| 33 |
st.title("🌱 AgriLens AI FastAPI")
|
| 34 |
|
| 35 |
st.markdown("""
|
|
|
|
| 55 |
-F "image=@plant_leaf.jpg" \\
|
| 56 |
-F "culture=tomato" \\
|
| 57 |
-F "notes=Feuilles jaunes" \\
|
| 58 |
+
http://localhost:7860/diagnose
|
| 59 |
```
|
| 60 |
|
| 61 |
### Documentation complète
|
|
|
|
| 66 |
# Test de l'endpoint health
|
| 67 |
if st.button("🔍 Tester l'endpoint /health"):
|
| 68 |
try:
|
| 69 |
+
response = requests.get("http://localhost:7860/health", timeout=5)
|
| 70 |
+
if response.status_code == 200:
|
| 71 |
+
data = response.json()
|
| 72 |
+
st.success("✅ L'API est opérationnelle !")
|
| 73 |
+
st.json(data)
|
| 74 |
+
else:
|
| 75 |
+
st.error(f"❌ Erreur HTTP: {response.status_code}")
|
| 76 |
+
except requests.exceptions.RequestException as e:
|
| 77 |
+
st.error(f"❌ Erreur de connexion: {e}")
|
| 78 |
+
st.info("L'API FastAPI est peut-être encore en cours de démarrage...")
|
| 79 |
+
|
| 80 |
+
# Test de l'endpoint racine
|
| 81 |
+
if st.button("🏠 Tester l'endpoint racine"):
|
| 82 |
+
try:
|
| 83 |
+
response = requests.get("http://localhost:7860/", timeout=5)
|
| 84 |
+
if response.status_code == 200:
|
| 85 |
+
data = response.json()
|
| 86 |
+
st.success("✅ Endpoint racine accessible !")
|
| 87 |
+
st.json(data)
|
| 88 |
+
else:
|
| 89 |
+
st.error(f"❌ Erreur HTTP: {response.status_code}")
|
| 90 |
+
except requests.exceptions.RequestException as e:
|
| 91 |
+
st.error(f"❌ Erreur de connexion: {e}")
|
| 92 |
|
| 93 |
st.markdown("---")
|
| 94 |
st.markdown("**Développé avec FastAPI et Hugging Face Spaces**")
|