xavierbarbier commited on
Commit
67a72fc
·
verified ·
1 Parent(s): 6ecad43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -81
app.py CHANGED
@@ -1,91 +1,26 @@
1
  import gradio as gr
2
- from gpt4all import GPT4All
3
- from huggingface_hub import hf_hub_download
4
- import faiss
5
- #from langchain_community.embeddings import HuggingFaceEmbeddings
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
- from langchain_chroma import Chroma
14
-
15
- title = "Mistral-7B-Instruct-GGUF Run On CPU-Basic Free Hardware"
16
-
17
- description = """
18
- 🔎 [Mistral AI's Mistral 7B Instruct v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1) [GGUF format model](https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF) , 4-bit quantization balanced quality gguf version, running on CPU. English Only (Also support other languages but the quality's not good). Using [GitHub - llama.cpp](https://github.com/ggerganov/llama.cpp) [GitHub - gpt4all](https://github.com/nomic-ai/gpt4all).
19
- 🔨 Running on CPU-Basic free hardware. Suggest duplicating this space to run without a queue.
20
- Mistral does not support system prompt symbol (such as ```<<SYS>>```) now, input your system prompt in the first message if you need. Learn more: [Guardrailing Mistral 7B](https://docs.mistral.ai/usage/guardrailing).
21
- """
22
-
23
- """
24
- [Model From TheBloke/Mistral-7B-Instruct-v0.1-GGUF](https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF)
25
- [Mistral-instruct-v0.1 System prompt](https://docs.mistral.ai/usage/guardrailing)
26
- """
27
-
28
- model_path = "models"
29
- model_name = "mistral-7b-instruct-v0.1.Q4_K_M.gguf"
30
-
31
- hf_hub_download(repo_id="TheBloke/Mistral-7B-Instruct-v0.1-GGUF", filename=model_name, local_dir=model_path, local_dir_use_symlinks=False)
32
-
33
- print("Start the model init process")
34
- model = model = GPT4All(model_name, model_path, allow_download = False, device="cpu")
35
-
36
-
37
- model.config["promptTemplate"] = "[INST] {0} [/INST]"
38
- model.config["systemPrompt"] = "Tu es un assitant et tu dois répondre en français"
39
- model._is_chat_session_activated = False
40
-
41
- max_new_tokens = 2048
42
-
43
- model_kwargs = {'device': 'cpu'}
44
- encode_kwargs = {'normalize_embeddings': False}
45
- embeddings = HuggingFaceEmbeddings(
46
-
47
- model_kwargs=model_kwargs,
48
- encode_kwargs=encode_kwargs
49
- )
50
-
51
- chunk_size = 2048
52
-
53
- # creating a pdf reader object
54
-
55
- vectordb = Chroma(
56
- persist_directory="./resource/chroma/",
57
- embedding_function=embeddings
58
- )
59
-
60
- print("Finish the model init process")
61
 
62
- def qa(question: str) -> str:
 
 
63
 
 
 
64
 
65
- docs = vectordb.max_marginal_relevance_search(question,k=3)
66
 
 
 
 
 
67
 
68
-
69
- # prompt = f"""<s>[INST]
70
- # Les informations contextuelles sont ci-dessous.
71
- # ---------------------
72
- # {docs[0].page_content}
73
- # ---------------------
74
- # [/INST]
75
- # Compte tenu des informations contextuelles et non des connaissances préalables, répondez à la requête. </s>
76
- # [INST] Requête: {question} [/INST]
77
- # Réponse:
78
- # """
79
-
80
- #outputs = model.generate(prompt=prompt, temp=0.5, top_k = 40, top_p = 1, max_tokens = max_new_tokens)
81
- return vectordb._collection.count() #"".join(outputs)
82
 
83
 
84
- demo = gr.Interface(
85
- qa,
86
- [gr.Textbox(label="Question")#, PDF(label="Document")
87
- ],
88
- gr.Textbox()
89
- )
90
  if __name__ == "__main__":
91
- demo.queue(max_size=3).launch()
 
1
  import gradio as gr
 
 
 
 
 
 
 
2
  from gradio_pdf import PDF
3
+ from pypdf import PdfReader
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ def upload_file(files):
6
+ file_paths = [file.name for file in files]
7
+ return file_paths
8
 
9
+ def summarise_file(file):
10
+ text = PdfReader(file)
11
 
12
+ return text
13
 
14
+ with gr.Blocks() as demo:
15
+ file_output = gr.File()
16
+ upload_button = gr.UploadButton("Click to Upload a File", file_types=["pdf", "doc"], file_count="multiple")
17
+ upload_button.upload(upload_file, upload_button, file_output)
18
 
19
+ file_summary = gr.Textbox()
20
+ sum_button = gr.Button("Click to summarise file")
21
+
22
+ sum_button.click(summarise_file, file_output, file_summary)
 
 
 
 
 
 
 
 
 
 
23
 
24
 
 
 
 
 
 
 
25
  if __name__ == "__main__":
26
+ demo.queue(max_size=3).launch()