halimbahae commited on
Commit
21a022f
·
verified ·
1 Parent(s): 52e46ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import PyPDF2
 
4
 
5
  # Initialisation du modèle HF中国镜像站
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
@@ -11,17 +12,21 @@ SYSTEM_PROMPT = {
11
  "en": "You are an educational assistant helping teachers create courses and analyze PDF documents."
12
  }
13
 
14
- # 📄 Fonction pour lire et extraire le texte d'un PDF
15
- def extract_text_from_pdf(pdf_file):
16
  text = ""
17
- with pdf_file as f:
18
- reader = PyPDF2.PdfReader(f)
19
- for page in reader.pages:
20
- text += page.extract_text() + "\n"
21
- return text
22
-
23
- # 🧠 Fonction du chatbot avec gestion de l'historique + PDF RAG
24
- def generate_response(subject, history, lang, pdf_file, max_tokens, temperature, top_p):
 
 
 
 
25
  system_message = SYSTEM_PROMPT.get(lang, SYSTEM_PROMPT["en"]) # Sélection de la langue
26
 
27
  messages = [{"role": "system", "content": system_message}]
@@ -32,8 +37,8 @@ def generate_response(subject, history, lang, pdf_file, max_tokens, temperature,
32
  messages.append(message)
33
 
34
  # 📄 Ajouter le contenu du PDF s'il y en a un
35
- if pdf_file is not None:
36
- pdf_text = extract_text_from_pdf(pdf_file)
37
  messages.append({"role": "user", "content": f"Voici un document PDF pertinent : {pdf_text[:1000]}..."}) # On limite à 1000 caractères pour éviter la surcharge
38
 
39
  # Ajouter la demande de l'utilisateur
@@ -56,9 +61,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
56
  subject_input = gr.Textbox(label="📌 Sujet du cours", placeholder="Ex: Apprentissage automatique")
57
  lang_select = gr.Dropdown(choices=["fr", "en"], value="fr", label="🌍 Langue")
58
 
59
- pdf_upload = gr.File(label="📄 Télécharger un PDF (optionnel)", type="file")
60
 
61
- chat = gr.Chatbot(type="messages") # ✅ Correction : Format messages OK
62
 
63
  with gr.Row():
64
  max_tokens = gr.Slider(minimum=100, maximum=2048, value=512, step=1, label="📝 Max tokens")
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import PyPDF2
4
+ import os
5
 
6
  # Initialisation du modèle HF中国镜像站
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
12
  "en": "You are an educational assistant helping teachers create courses and analyze PDF documents."
13
  }
14
 
15
+ # 📄 Fonction pour extraire le texte d'un PDF
16
+ def extract_text_from_pdf(pdf_path):
17
  text = ""
18
+ try:
19
+ with open(pdf_path, "rb") as f:
20
+ reader = PyPDF2.PdfReader(f)
21
+ for page in reader.pages:
22
+ if page.extract_text():
23
+ text += page.extract_text() + "\n"
24
+ return text if text else "Impossible d'extraire du texte de ce PDF."
25
+ except Exception as e:
26
+ return f"Erreur lors de la lecture du PDF : {str(e)}"
27
+
28
+ # 🧠 Fonction du chatbot + PDF RAG
29
+ def generate_response(subject, history, lang, pdf_path, max_tokens, temperature, top_p):
30
  system_message = SYSTEM_PROMPT.get(lang, SYSTEM_PROMPT["en"]) # Sélection de la langue
31
 
32
  messages = [{"role": "system", "content": system_message}]
 
37
  messages.append(message)
38
 
39
  # 📄 Ajouter le contenu du PDF s'il y en a un
40
+ if pdf_path:
41
+ pdf_text = extract_text_from_pdf(pdf_path)
42
  messages.append({"role": "user", "content": f"Voici un document PDF pertinent : {pdf_text[:1000]}..."}) # On limite à 1000 caractères pour éviter la surcharge
43
 
44
  # Ajouter la demande de l'utilisateur
 
61
  subject_input = gr.Textbox(label="📌 Sujet du cours", placeholder="Ex: Apprentissage automatique")
62
  lang_select = gr.Dropdown(choices=["fr", "en"], value="fr", label="🌍 Langue")
63
 
64
+ pdf_upload = gr.File(label="📄 Télécharger un PDF (optionnel)", type="filepath") # ✅ Correction ici
65
 
66
+ chat = gr.Chatbot(type="messages") # ✅ Correction du format des messages
67
 
68
  with gr.Row():
69
  max_tokens = gr.Slider(minimum=100, maximum=2048, value=512, step=1, label="📝 Max tokens")