NOBODY204 commited on
Commit
992eaef
·
verified ·
1 Parent(s): 3a2bab1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -1,23 +1,19 @@
1
  # -*- coding: utf-8 -*-
2
- """ArchivChat PRO v4 - Version Stable API (Flan-T5)"""
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 huggingface_hub import InferenceClient
10
  from typing import List, Tuple
11
 
12
  # ==============================
13
- # CONFIG & API CLIENT
14
  # ==============================
15
- MODEL_ID = "google/flan-t5-large" # modèle garanti compatible text-generation
16
- HF_TOKEN = os.environ.get("HF_TOKEN")
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 (API)
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 = client.text_generation(
88
- prompt,
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
- if "503" in str(e) or "overloading" in str(e):
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 (Stable API)")
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"])