Spaces:
Sleeping
Sleeping
xavierbarbier
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,10 @@ import faiss
|
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
7 |
import numpy as np
|
8 |
from pypdf import PdfReader
|
9 |
-
from
|
|
|
|
|
|
|
10 |
|
11 |
title = "Mistral-7B-Instruct-GGUF Run On CPU-Basic Free Hardware"
|
12 |
|
@@ -29,116 +32,35 @@ hf_hub_download(repo_id="TheBloke/Mistral-7B-Instruct-v0.1-GGUF", filename=model
|
|
29 |
print("Start the model init process")
|
30 |
model = model = GPT4All(model_name, model_path, allow_download = False, device="cpu")
|
31 |
|
32 |
-
model_name = "HuggingFaceH4/zephyr-7b-beta"
|
33 |
-
#model_name = "gpt2"
|
34 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
35 |
|
|
|
|
|
|
|
36 |
|
|
|
37 |
|
38 |
# creating a pdf reader object
|
39 |
|
40 |
-
"""
|
41 |
-
reader = PdfReader("./resource/NGAP 01042024.pdf")
|
42 |
-
text = []
|
43 |
-
for p in np.arange(0, len(reader.pages), 1):
|
44 |
-
page = reader.pages[int(p)]
|
45 |
-
|
46 |
-
# extracting text from page
|
47 |
-
text.append(page.extract_text())
|
48 |
-
|
49 |
-
text = ' '.join(text)
|
50 |
-
|
51 |
-
chunk_size = 2048
|
52 |
-
chunks = [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
|
53 |
-
|
54 |
-
model_kwargs = {'device': 'cpu'}
|
55 |
-
encode_kwargs = {'normalize_embeddings': False}
|
56 |
-
embeddings = HuggingFaceEmbeddings(
|
57 |
-
|
58 |
-
model_kwargs=model_kwargs,
|
59 |
-
encode_kwargs=encode_kwargs
|
60 |
-
)
|
61 |
-
|
62 |
-
def get_text_embedding(text):
|
63 |
-
|
64 |
-
return embeddings.embed_query(text)
|
65 |
-
|
66 |
-
|
67 |
-
text_embeddings = np.array([get_text_embedding(chunk) for chunk in chunks])
|
68 |
-
|
69 |
-
d = text_embeddings.shape[1]
|
70 |
-
index = faiss.IndexFlatL2(d)
|
71 |
-
index.add(text_embeddings)
|
72 |
-
|
73 |
-
#index = faiss.read_index("./resourse/embeddings_ngap.faiss")
|
74 |
-
"""
|
75 |
print("Finish the model init process")
|
76 |
|
77 |
-
def format_chat_prompt(message, chat_history):
|
78 |
-
prompt = ""
|
79 |
-
for turn in chat_history:
|
80 |
-
user_message, bot_message = turn
|
81 |
-
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
82 |
-
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
83 |
-
return prompt
|
84 |
-
|
85 |
-
context = [
|
86 |
-
{
|
87 |
-
"role": "system",
|
88 |
-
"content": """Tu est un assitant virtuel et tu réponds en français.
|
89 |
-
""",
|
90 |
-
}
|
91 |
-
]
|
92 |
-
|
93 |
-
max_new_tokens = 2048
|
94 |
-
|
95 |
-
def respond(message, chat_history):
|
96 |
|
|
|
97 |
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
prompt = ""
|
104 |
-
for item in context:
|
105 |
-
for key, value in item.items():
|
106 |
-
prompt += f"{key}: {value}\n"
|
107 |
-
|
108 |
-
#tokenized_chat = tokenizer.apply_chat_template(context, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
109 |
-
|
110 |
-
bot_message = model.generate(prompt, temp=0.5, top_k = 40, top_p = 1, max_tokens = max_new_tokens)
|
111 |
-
|
112 |
-
#bot_message = tokenizer.decode(outputs[0]).split("<|assistant|>")[-1].replace("</s>","")
|
113 |
-
|
114 |
-
#bot_message = model.generate(prompt=prompt, temp=0.5, top_k = 40, top_p = 1, max_tokens = max_new_tokens, streaming=False)
|
115 |
-
|
116 |
-
context.append({'role':'assistant', 'content':f"{bot_message}"})
|
117 |
-
|
118 |
-
chat_history.append((message, bot_message))
|
119 |
-
return "", chat_history
|
120 |
-
|
121 |
-
with gr.Blocks() as demo:
|
122 |
-
gr.Markdown("# Assistant virtuel Ameli")
|
123 |
-
gr.Markdown("Mes réponses sont générées par IA. Elles peuvent être fausses ou imprécises.")
|
124 |
-
with gr.Row():
|
125 |
-
with gr.Column(scale=1):
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
text = gr.Textbox(lines =5)
|
130 |
-
|
131 |
-
#msg = gr.Textbox(label="Posez votre question")
|
132 |
-
btn = gr.Button("Soumettre la question")
|
133 |
-
|
134 |
-
|
135 |
-
with gr.Column(scale=2, min_width=50):
|
136 |
-
chatbot = gr.Chatbot(height=700) #just to fit the notebook
|
137 |
-
clear = gr.ClearButton(components=[text, chatbot], value="Clear console")
|
138 |
-
|
139 |
|
140 |
-
btn.click(respond, inputs=[text, chatbot], outputs=[text, chatbot])
|
141 |
-
text.submit(respond, inputs=[text, chatbot], outputs=[text, chatbot]) #Press enter to submit
|
142 |
|
|
|
|
|
|
|
|
|
|
|
143 |
if __name__ == "__main__":
|
144 |
demo.queue(max_size=3).launch()
|
|
|
6 |
from langchain_huggingface import HuggingFaceEmbeddings
|
7 |
import numpy as np
|
8 |
from pypdf import PdfReader
|
9 |
+
from gradio_pdf import PDF
|
10 |
+
from pdf2image import convert_from_path
|
11 |
+
from transformers import pipeline
|
12 |
+
from pathlib import Path
|
13 |
|
14 |
title = "Mistral-7B-Instruct-GGUF Run On CPU-Basic Free Hardware"
|
15 |
|
|
|
32 |
print("Start the model init process")
|
33 |
model = model = GPT4All(model_name, model_path, allow_download = False, device="cpu")
|
34 |
|
|
|
|
|
|
|
35 |
|
36 |
+
model.config["promptTemplate"] = "[INST] {0} [/INST]"
|
37 |
+
model.config["systemPrompt"] = "Tu es un assitant et tu dois répondre en français"
|
38 |
+
model._is_chat_session_activated = False
|
39 |
|
40 |
+
max_new_tokens = 2048
|
41 |
|
42 |
# creating a pdf reader object
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
print("Finish the model init process")
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
dir_ = Path(__file__).parent
|
48 |
|
49 |
+
p = pipeline(
|
50 |
+
"document-question-answering",
|
51 |
+
model="impira/layoutlm-document-qa",
|
52 |
+
)
|
53 |
|
54 |
+
def qa(question: str, doc: str) -> str:
|
55 |
+
img = convert_from_path(doc)[0]
|
56 |
+
output = p(img, question)
|
57 |
+
return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
|
|
|
|
59 |
|
60 |
+
demo = gr.Interface(
|
61 |
+
qa,
|
62 |
+
[gr.Textbox(label="Question"), PDF(label="Document")],
|
63 |
+
gr.Textbox()
|
64 |
+
)
|
65 |
if __name__ == "__main__":
|
66 |
demo.queue(max_size=3).launch()
|