ftkd99 commited on
Commit
b763ff0
·
verified ·
1 Parent(s): d1d6611

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -1,25 +1,25 @@
1
- import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
- from utils import build_faiss_index, retrieve
4
-
5
- # Load documents
6
- with open("documents/1mg_rag.txt") as f:
7
- docs = [line.strip() for line in f if line.strip()]
8
-
9
- # Build FAISS index
10
- index, _ = build_faiss_index(docs)
11
-
12
- # Load quantized Mistral 7B
13
- model_id = "TheBloke/Mistral-7B-Instruct-v0.2-GPTQ"
14
- tokenizer = AutoTokenizer.from_pretrained(model_id)
15
- model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True)
16
-
17
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
18
-
19
- def answer_question(query):
20
- context = "\n".join(retrieve(query, index, docs))
21
- prompt = f"[INST] Use the following context to answer the question.\n\nContext:\n{context}\n\nQuestion: {query} [/INST]"
22
- result = generator(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
23
- return result[0]['generated_text']
24
-
25
- gr.Interface(fn=answer_question, inputs="text", outputs="text", title="Mistral RAG").launch()
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ from utils import build_faiss_index, retrieve
4
+
5
+ # Load documents
6
+ with open("documents/1mg_rag.txt") as f:
7
+ docs = [line.strip() for line in f if line.strip()]
8
+
9
+ # Build FAISS index
10
+ index, _ = build_faiss_index(docs)
11
+
12
+ # Load quantized Mistral 7B
13
+ model_id = "TheBloke/Mistral-7B-Instruct-v0.2-GPTQ"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
15
+ model = AutoGPTQForCausalLM.from_quantized(model_id, device_map="auto", trust_remote_code=True)
16
+
17
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
18
+
19
+ def answer_question(query):
20
+ context = "\n".join(retrieve(query, index, docs))
21
+ prompt = f"[INST] Use the following context to answer the question.\n\nContext:\n{context}\n\nQuestion: {query} [/INST]"
22
+ result = generator(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
23
+ return result[0]['generated_text']
24
+
25
+ gr.Interface(fn=answer_question, inputs="text", outputs="text", title="Mistral RAG").launch()