Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- Dockerfile +37 -0
- app/__init__.py +0 -0
- app/main.py +53 -0
- app/utils.py +5 -0
- requirements.txt +0 -0
- static/index.html +37 -0
- static/script.js +35 -0
- static/styles.css +30 -0
Dockerfile
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.9.11
|
| 2 |
+
FROM python:3.9.11
|
| 3 |
+
|
| 4 |
+
# Set the working directory
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Add a non-root user
|
| 14 |
+
RUN useradd -m user
|
| 15 |
+
|
| 16 |
+
# Set environment variables
|
| 17 |
+
ENV HOME=/home/user \
|
| 18 |
+
PATH=/home/user/.local/bin:$PATH \
|
| 19 |
+
TRANSFORMERS_CACHE=/home/user/.cache/huggingface
|
| 20 |
+
|
| 21 |
+
# Create the cache directory and set permissions
|
| 22 |
+
RUN mkdir -p /home/user/.cache/huggingface && chown -R user:user /home/user/.cache
|
| 23 |
+
|
| 24 |
+
# Switch to the non-root user
|
| 25 |
+
USER user
|
| 26 |
+
|
| 27 |
+
# Set the working directory for the user
|
| 28 |
+
WORKDIR $HOME/app
|
| 29 |
+
|
| 30 |
+
# Copy the content into /home/user/app with the correct permissions
|
| 31 |
+
COPY --chown=user:user . $HOME/app
|
| 32 |
+
|
| 33 |
+
# Expose the port the app runs on
|
| 34 |
+
EXPOSE 7860
|
| 35 |
+
|
| 36 |
+
# Command to run the application
|
| 37 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/__init__.py
ADDED
|
File without changes
|
app/main.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form ,HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import HTMLResponse, FileResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
import pytesseract
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import PyPDF2
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Autoriser les requêtes CORS (pour l'interface web)
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"], # Autoriser toutes les origines
|
| 17 |
+
allow_methods=["*"], # Autoriser toutes les méthodes
|
| 18 |
+
allow_headers=["*"], # Autoriser tous les en-têtes
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Charger le modèle de question-réponse
|
| 22 |
+
qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
|
| 23 |
+
|
| 24 |
+
def extract_text_from_image(image):
|
| 25 |
+
return pytesseract.image_to_string(image)
|
| 26 |
+
|
| 27 |
+
def extract_text_from_pdf(file):
|
| 28 |
+
reader = PyPDF2.PdfReader(file)
|
| 29 |
+
text = ""
|
| 30 |
+
for page in reader.pages:
|
| 31 |
+
text += page.extract_text()
|
| 32 |
+
return text
|
| 33 |
+
|
| 34 |
+
# Endpoint pour répondre à des questions
|
| 35 |
+
@app.post("/ask")
|
| 36 |
+
async def ask_question(file: UploadFile = File(...), question: str = Form(...)):
|
| 37 |
+
# Extraire le texte en fonction du type de fichier
|
| 38 |
+
if file.filename.endswith(".pdf"):
|
| 39 |
+
text = extract_text_from_pdf(file.file)
|
| 40 |
+
elif file.filename.endswith((".png", ".jpg", ".jpeg")):
|
| 41 |
+
image = Image.open(file.file)
|
| 42 |
+
text = extract_text_from_image(image)
|
| 43 |
+
else:
|
| 44 |
+
return {"answer": "Format de fichier non supporté."}
|
| 45 |
+
|
| 46 |
+
# Répondre à la question
|
| 47 |
+
result = qa_pipeline(question=question, context=text)
|
| 48 |
+
return {"answer": result["answer"]}
|
| 49 |
+
|
| 50 |
+
# Démarrer l'application
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
import uvicorn
|
| 53 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
app/utils.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytesseract
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import PyPDF2
|
| 4 |
+
from docx import Document
|
| 5 |
+
import pandas as pd
|
requirements.txt
ADDED
|
Binary file (1.8 kB). View file
|
|
|
static/index.html
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="fr">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Service de Question-Réponse Intelligent</title>
|
| 7 |
+
<link rel="stylesheet" href="styles.css">
|
| 8 |
+
</head>
|
| 9 |
+
<body>
|
| 10 |
+
<div class="container">
|
| 11 |
+
<h1>Service de Question-Réponse Intelligent</h1>
|
| 12 |
+
|
| 13 |
+
<!-- Section pour téléverser un fichier -->
|
| 14 |
+
<div class="upload-section">
|
| 15 |
+
<label for="file-upload">Téléverser un document ou une image :</label>
|
| 16 |
+
<input type="file" id="file-upload" accept=".pdf,.png,.jpg,.jpeg">
|
| 17 |
+
</div>
|
| 18 |
+
|
| 19 |
+
<!-- Section pour poser une question -->
|
| 20 |
+
<div class="question-section">
|
| 21 |
+
<label for="question">Posez votre question :</label>
|
| 22 |
+
<textarea id="question" rows="3" placeholder="Exemple : Quel est le sujet de ce document ?"></textarea>
|
| 23 |
+
</div>
|
| 24 |
+
|
| 25 |
+
<!-- Bouton pour soumettre -->
|
| 26 |
+
<button id="submit-btn">Soumettre</button>
|
| 27 |
+
|
| 28 |
+
<!-- Section pour afficher les résultats -->
|
| 29 |
+
<div class="result-section">
|
| 30 |
+
<h2>Réponse :</h2>
|
| 31 |
+
<p id="answer"></p>
|
| 32 |
+
</div>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<script src="script.js"></script>
|
| 36 |
+
</body>
|
| 37 |
+
</html>
|
static/script.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.getElementById("submit-btn").addEventListener("click", async () => {
|
| 2 |
+
const fileInput = document.getElementById("file-upload");
|
| 3 |
+
const questionInput = document.getElementById("question");
|
| 4 |
+
const answerOutput = document.getElementById("answer");
|
| 5 |
+
|
| 6 |
+
const file = fileInput.files[0];
|
| 7 |
+
const question = questionInput.value;
|
| 8 |
+
|
| 9 |
+
if (!file || !question) {
|
| 10 |
+
alert("Veuillez téléverser un fichier et poser une question.");
|
| 11 |
+
return;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
// Créer un objet FormData pour envoyer le fichier et la question
|
| 15 |
+
const formData = new FormData();
|
| 16 |
+
formData.append("file", file);
|
| 17 |
+
formData.append("question", question);
|
| 18 |
+
|
| 19 |
+
try {
|
| 20 |
+
const response = await fetch("http://localhost:7860/ask", {
|
| 21 |
+
method: "POST",
|
| 22 |
+
body: formData,
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
if (!response.ok) {
|
| 26 |
+
throw new Error("Erreur lors de la requête.");
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
const data = await response.json();
|
| 30 |
+
answerOutput.textContent = data.answer || "Aucune réponse trouvée.";
|
| 31 |
+
} catch (error) {
|
| 32 |
+
console.error(error);
|
| 33 |
+
answerOutput.textContent = "Une erreur s'est produite. Veuillez réessayer.";
|
| 34 |
+
}
|
| 35 |
+
});
|
static/styles.css
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
font-family: Arial, sans-serif;
|
| 3 |
+
margin: 20px;
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
.container {
|
| 7 |
+
max-width: 600px;
|
| 8 |
+
margin: 0 auto;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
h1 {
|
| 12 |
+
text-align: center;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
.upload-section, .question-section, .result-section {
|
| 16 |
+
margin-bottom: 20px;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
textarea, input[type="file"], button {
|
| 20 |
+
width: 100%;
|
| 21 |
+
padding: 10px;
|
| 22 |
+
margin-top: 10px;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.result-section {
|
| 26 |
+
padding: 20px;
|
| 27 |
+
background-color: #f9f9f9;
|
| 28 |
+
border: 1px solid #ddd;
|
| 29 |
+
border-radius: 5px;
|
| 30 |
+
}
|