Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,104 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
|
|
|
|
|
|
60 |
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from datasets import load_dataset
|
3 |
+
import os
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
5 |
+
import torch
|
6 |
+
from threading import Thread
|
7 |
+
from sentence_transformers import SentenceTransformer
|
8 |
+
from datasets import load_dataset
|
9 |
+
import time
|
10 |
+
|
11 |
+
ST = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
|
12 |
+
art_dataset= load_dataset("hichri-mo/arxiver-1000",revision="embedded")
|
13 |
+
|
14 |
+
data = art_dataset["train"]
|
15 |
+
data = data.add_faiss_index("embeddings")
|
16 |
+
|
17 |
+
|
18 |
+
model_id= "Qwen/Qwen2.5-3B-Instruct"
|
19 |
+
|
20 |
+
# use quantization to lower GPU usage
|
21 |
+
bnb_config = BitsAndBytesConfig(
|
22 |
+
load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16
|
23 |
+
)
|
24 |
+
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
26 |
+
model = AutoModelForCausalLM.from_pretrained(
|
27 |
+
model_id,
|
28 |
+
torch_dtype=torch.bfloat16,
|
29 |
+
device_map="auto",
|
30 |
+
quantization_config=bnb_config
|
31 |
+
)
|
32 |
+
terminators = [
|
33 |
+
tokenizer.eos_token_id,
|
34 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
35 |
+
]
|
36 |
+
|
37 |
+
SYS_PROMPT = """You are an assistant for answering questions.
|
38 |
+
You are given the extracted parts of a long document and a question. Provide a conversational answer.
|
39 |
+
If you don't know the answer, just say "I do not know." Don't make up an answer."""
|
40 |
+
|
41 |
+
|
42 |
+
def format_prompt(prompt,retrieved_documents,k):
|
43 |
+
"""using the retrieved documents we will prompt the model to generate our responses"""
|
44 |
+
PROMPT = f"Question: {prompt}\nContext: \n"
|
45 |
+
for idx in range(k) :
|
46 |
+
PROMPT+= f"{retrieved_documents['markdown'][idx]}\n"
|
47 |
+
return PROMPT
|
48 |
+
|
49 |
+
def generate(formatted_prompt):
|
50 |
+
formatted_prompt = formatted_prompt[:2000] # to avoid GPU OOM
|
51 |
+
messages = [{"role":"system","content":SYS_PROMPT},{"role":"user","content":formatted_prompt}]
|
52 |
+
# tell the model to generate
|
53 |
+
input_ids = tokenizer.apply_chat_template(
|
54 |
+
messages,
|
55 |
+
add_generation_prompt=True,
|
56 |
+
return_tensors="pt"
|
57 |
+
).to(model.device)
|
58 |
+
# Check if terminators contain None and replace with tokenizer.eos_token_id
|
59 |
+
eos_token_id = terminators[0] # Default to tokenizer.eos_token_id
|
60 |
+
if terminators[1] is not None:
|
61 |
+
eos_token_id = terminators[1] # Use "<|eot_id|>" if it exists
|
62 |
+
|
63 |
+
outputs = model.generate(
|
64 |
+
input_ids,
|
65 |
+
max_new_tokens=1024,
|
66 |
+
eos_token_id=eos_token_id, # Pass a single integer value
|
67 |
+
do_sample=True,
|
68 |
+
temperature=0.6,
|
69 |
+
top_p=0.9,
|
70 |
+
)
|
71 |
+
response = outputs[0][input_ids.shape[-1]:]
|
72 |
+
return tokenizer.decode(response, skip_special_tokens=True)
|
73 |
+
|
74 |
+
def rag_chatbot(prompt:str,k:int=2):
|
75 |
+
scores , retrieved_documents = search(prompt, k)
|
76 |
+
formatted_prompt = format_prompt(prompt,retrieved_documents,k)
|
77 |
+
return generate(formatted_prompt)
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
def rag_chatbot_interface(prompt, k):
|
83 |
+
return rag_chatbot(prompt, k)
|
84 |
+
|
85 |
+
iface = gr.Interface(
|
86 |
+
fn=rag_chatbot_interface,
|
87 |
+
inputs=[
|
88 |
+
gr.Textbox(label="Enter your question"),
|
89 |
+
gr.Slider(minimum=1, maximum=10, step=1, value=2, label="Number of documents to retrieve")
|
90 |
],
|
91 |
+
outputs=gr.Textbox(label="Response"),
|
92 |
+
title="Chatbot with RAG",
|
93 |
+
description="Ask questions and get answers based on retrieved documents."
|
94 |
)
|
95 |
|
96 |
+
iface.launch()
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
|
|
|
|