Spaces:
Sleeping
Sleeping
baptiste.bernard
commited on
Commit
·
370a2b2
1
Parent(s):
a602253
add pdf
Browse files- app.py +41 -22
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,23 +1,42 @@
|
|
1 |
import os
|
2 |
import sys
|
|
|
3 |
from dotenv import load_dotenv
|
4 |
import gradio as gr
|
5 |
from huggingface_hub import InferenceClient
|
6 |
-
import chardet
|
7 |
import re
|
8 |
|
9 |
load_dotenv()
|
10 |
-
|
11 |
hftoken = os.environ.get("HF_TOKEN")
|
12 |
|
|
|
13 |
from huggingface_hub import login
|
14 |
login(token=hftoken)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hftoken)
|
19 |
file_content = None
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def respond(message, history, system_message, max_tokens, temperature, top_p, file=None):
|
22 |
global file_content
|
23 |
|
@@ -27,27 +46,24 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, fi
|
|
27 |
messages.append({"role": "user", "content": val[0]})
|
28 |
if val[1]:
|
29 |
messages.append({"role": "assistant", "content": val[1]})
|
30 |
-
|
31 |
messages.append({"role": "user", "content": message})
|
32 |
-
|
33 |
response = ""
|
34 |
|
|
|
35 |
if file:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
except Exception as e:
|
44 |
-
file_content = f"Erreur de décodage du fichier : {e}"
|
45 |
-
|
46 |
-
if re.search(r"contenu du fichier|afficher le fichier|lire le fichier|voir le fichier| donnée du fichier", message.lower()) and file_content:
|
47 |
response += f"Contenu du fichier :\n{file_content}"
|
48 |
yield response
|
49 |
return
|
50 |
|
|
|
51 |
for message in client.chat_completion(
|
52 |
messages,
|
53 |
max_tokens=max_tokens,
|
@@ -59,6 +75,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, fi
|
|
59 |
response += token
|
60 |
yield response
|
61 |
|
|
|
62 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
63 |
gr.Markdown("# Chatbot Interface")
|
64 |
gr.Image(value="logo-gaia.png", label="Logo")
|
@@ -66,11 +83,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
66 |
with gr.Row():
|
67 |
with gr.Column():
|
68 |
gr.Markdown("## Paramètres")
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
74 |
|
75 |
with gr.Column():
|
76 |
gr.Markdown("## Chat")
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
+
import fitz # PyMuPDF
|
4 |
from dotenv import load_dotenv
|
5 |
import gradio as gr
|
6 |
from huggingface_hub import InferenceClient
|
|
|
7 |
import re
|
8 |
|
9 |
load_dotenv()
|
|
|
10 |
hftoken = os.environ.get("HF_TOKEN")
|
11 |
|
12 |
+
|
13 |
from huggingface_hub import login
|
14 |
login(token=hftoken)
|
15 |
|
|
|
|
|
16 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hftoken)
|
17 |
file_content = None
|
18 |
|
19 |
+
def extract_text_from_file(file_path):
|
20 |
+
"""Extrait le texte d'un fichier PDF ou TXT."""
|
21 |
+
try:
|
22 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
23 |
+
|
24 |
+
if file_extension == ".pdf":
|
25 |
+
# PDF
|
26 |
+
with fitz.open(file_path) as doc:
|
27 |
+
text = "\n".join([page.get_text("text") for page in doc])
|
28 |
+
elif file_extension == ".txt":
|
29 |
+
# TXT
|
30 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
31 |
+
text = file.read()
|
32 |
+
else:
|
33 |
+
return "Format de fichier non pris en charge. Veuillez télécharger un fichier PDF ou TXT."
|
34 |
+
|
35 |
+
return text.strip() if text.strip() else "Aucun texte extrait du fichier."
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return f"Erreur lors de la lecture du fichier : {e}"
|
39 |
+
|
40 |
def respond(message, history, system_message, max_tokens, temperature, top_p, file=None):
|
41 |
global file_content
|
42 |
|
|
|
46 |
messages.append({"role": "user", "content": val[0]})
|
47 |
if val[1]:
|
48 |
messages.append({"role": "assistant", "content": val[1]})
|
49 |
+
|
50 |
messages.append({"role": "user", "content": message})
|
|
|
51 |
response = ""
|
52 |
|
53 |
+
|
54 |
if file:
|
55 |
+
file_path = file.name if hasattr(file, 'name') else file
|
56 |
+
if os.path.exists(file_path):
|
57 |
+
file_content = extract_text_from_file(file_path)
|
58 |
+
else:
|
59 |
+
file_content = "Aucun fichier valide n'a été trouvé."
|
60 |
+
|
61 |
+
if re.search(r"contenu du fichier|afficher le fichier|lire le fichier|voir le fichier|donnée du fichier", message.lower()) and file_content:
|
|
|
|
|
|
|
|
|
62 |
response += f"Contenu du fichier :\n{file_content}"
|
63 |
yield response
|
64 |
return
|
65 |
|
66 |
+
|
67 |
for message in client.chat_completion(
|
68 |
messages,
|
69 |
max_tokens=max_tokens,
|
|
|
75 |
response += token
|
76 |
yield response
|
77 |
|
78 |
+
# interface
|
79 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
80 |
gr.Markdown("# Chatbot Interface")
|
81 |
gr.Image(value="logo-gaia.png", label="Logo")
|
|
|
83 |
with gr.Row():
|
84 |
with gr.Column():
|
85 |
gr.Markdown("## Paramètres")
|
86 |
+
with gr.Accordion("Réglages avancés", open=False):
|
87 |
+
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
|
88 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
89 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
90 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
91 |
+
|
92 |
+
file_upload = gr.File(label="Télécharger un fichier PDF ou TXT", file_types=[".pdf", ".txt"], type="filepath")
|
93 |
|
94 |
with gr.Column():
|
95 |
gr.Markdown("## Chat")
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1 |
chardet
|
2 |
huggingface_hub==0.25.2
|
3 |
python-dotenv
|
|
|
|
|
|
1 |
chardet
|
2 |
huggingface_hub==0.25.2
|
3 |
python-dotenv
|
4 |
+
fitz==1.0.3
|
5 |
+
gradio==3.0.0
|