Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,19 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
-
"""ArchivChat PRO v4 - Version Stable
|
| 3 |
import gradio as gr
|
| 4 |
import PyPDF2
|
| 5 |
from pdf2image import convert_from_path
|
| 6 |
import pytesseract
|
| 7 |
import os
|
| 8 |
import re
|
| 9 |
-
from
|
| 10 |
from typing import List, Tuple
|
| 11 |
|
| 12 |
# ==============================
|
| 13 |
-
# CONFIG
|
| 14 |
# ==============================
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if not HF_TOKEN:
|
| 19 |
-
print("⚠️ HF_TOKEN manquant ! Ajoutez votre token Hugging Face dans les variables d'environnement.")
|
| 20 |
-
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 21 |
|
| 22 |
IS_HF_SPACES = os.environ.get("SPACE_ID") is not None
|
| 23 |
if IS_HF_SPACES:
|
|
@@ -61,7 +57,7 @@ def extract_text_hybrid(pdf_file) -> Tuple[str, str]:
|
|
| 61 |
return re.sub(r'\s+', ' ', text).strip(), method
|
| 62 |
|
| 63 |
# ==============================
|
| 64 |
-
# LOGIQUE DE CHAT (
|
| 65 |
# ==============================
|
| 66 |
class DocumentChat:
|
| 67 |
def __init__(self):
|
|
@@ -82,19 +78,13 @@ class DocumentChat:
|
|
| 82 |
return "❌ Veuillez charger un PDF d'abord."
|
| 83 |
|
| 84 |
prompt = f"Répond uniquement sur le document fourni.\nContexte: {self.context[:3000]}\nQuestion: {question}"
|
| 85 |
-
|
| 86 |
try:
|
| 87 |
-
response =
|
| 88 |
-
|
| 89 |
-
max_new_tokens=250
|
| 90 |
-
)
|
| 91 |
-
answer = response.strip()
|
| 92 |
self.history.append({"q": question, "a": answer})
|
| 93 |
return answer
|
| 94 |
except Exception as e:
|
| 95 |
-
|
| 96 |
-
return "⚠️ Le modèle se réveille... réessayez dans 10 secondes."
|
| 97 |
-
return f"⚠️ Erreur API : {str(e)}"
|
| 98 |
|
| 99 |
chat_engine = DocumentChat()
|
| 100 |
|
|
@@ -102,8 +92,8 @@ chat_engine = DocumentChat()
|
|
| 102 |
# INTERFACE GRADIO
|
| 103 |
# ==============================
|
| 104 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 105 |
-
gr.Markdown("# 📂 ArchivChat PRO v4 (
|
| 106 |
-
|
| 107 |
with gr.Row():
|
| 108 |
with gr.Column(scale=1):
|
| 109 |
file_input = gr.File(label="Fichier PDF", file_types=[".pdf"])
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
+
"""ArchivChat PRO v4 - Version Locale Stable (sans token HF)"""
|
| 3 |
import gradio as gr
|
| 4 |
import PyPDF2
|
| 5 |
from pdf2image import convert_from_path
|
| 6 |
import pytesseract
|
| 7 |
import os
|
| 8 |
import re
|
| 9 |
+
from transformers import pipeline
|
| 10 |
from typing import List, Tuple
|
| 11 |
|
| 12 |
# ==============================
|
| 13 |
+
# CONFIG MODELE LOCAL
|
| 14 |
# ==============================
|
| 15 |
+
# Modèle léger Flan-T5 pour text2text-generation (fonctionne sans token)
|
| 16 |
+
generator = pipeline("text2text-generation", model="google/flan-t5-small")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
IS_HF_SPACES = os.environ.get("SPACE_ID") is not None
|
| 19 |
if IS_HF_SPACES:
|
|
|
|
| 57 |
return re.sub(r'\s+', ' ', text).strip(), method
|
| 58 |
|
| 59 |
# ==============================
|
| 60 |
+
# LOGIQUE DE CHAT (LOCAL)
|
| 61 |
# ==============================
|
| 62 |
class DocumentChat:
|
| 63 |
def __init__(self):
|
|
|
|
| 78 |
return "❌ Veuillez charger un PDF d'abord."
|
| 79 |
|
| 80 |
prompt = f"Répond uniquement sur le document fourni.\nContexte: {self.context[:3000]}\nQuestion: {question}"
|
|
|
|
| 81 |
try:
|
| 82 |
+
response = generator(prompt, max_new_tokens=200)
|
| 83 |
+
answer = response[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
| 84 |
self.history.append({"q": question, "a": answer})
|
| 85 |
return answer
|
| 86 |
except Exception as e:
|
| 87 |
+
return f"⚠️ Erreur locale : {str(e)}"
|
|
|
|
|
|
|
| 88 |
|
| 89 |
chat_engine = DocumentChat()
|
| 90 |
|
|
|
|
| 92 |
# INTERFACE GRADIO
|
| 93 |
# ==============================
|
| 94 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 95 |
+
gr.Markdown("# 📂 ArchivChat PRO v4 (Local Stable)")
|
| 96 |
+
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column(scale=1):
|
| 99 |
file_input = gr.File(label="Fichier PDF", file_types=[".pdf"])
|