Spaces:
Runtime error
Runtime error
main__3 notebook should be fully transfered over
Browse files- app.py +100 -2
- requirements.txt +3 -1
app.py
CHANGED
@@ -9,7 +9,8 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStream
|
|
9 |
from llama_index.core.prompts.prompts import SimpleInputPrompt
|
10 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
11 |
from llama_index.legacy.embeddings.langchain import LangchainEmbedding
|
12 |
-
from langchain.embeddings.huggingface import HuggingFaceEmbeddings # This import should now work
|
|
|
13 |
from sentence_transformers import SentenceTransformer
|
14 |
|
15 |
from llama_index.core import set_global_service_context, ServiceContext
|
@@ -38,6 +39,59 @@ As a derivate work of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-
|
|
38 |
this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md).
|
39 |
"""
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
if not torch.cuda.is_available():
|
42 |
DESCRIPTION += "We won't be able to run this space! We need GPU processing"
|
43 |
|
@@ -48,7 +102,49 @@ if torch.cuda.is_available():
|
|
48 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
49 |
tokenizer.use_default_system_prompt = False
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
@spaces.GPU(duration=240)
|
53 |
def generate(
|
54 |
message: str,
|
@@ -143,10 +239,12 @@ chat_interface = gr.ChatInterface(
|
|
143 |
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
144 |
],
|
145 |
)
|
|
|
146 |
|
147 |
with gr.Blocks(css="style.css") as demo:
|
148 |
gr.Markdown(DESCRIPTION)
|
149 |
-
chat_interface.render()
|
|
|
150 |
gr.Markdown(LICENSE)
|
151 |
|
152 |
if __name__ == "__main__":
|
|
|
9 |
from llama_index.core.prompts.prompts import SimpleInputPrompt
|
10 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
11 |
from llama_index.legacy.embeddings.langchain import LangchainEmbedding
|
12 |
+
#from langchain.embeddings.huggingface import HuggingFaceEmbeddings # This import should now work
|
13 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
14 |
from sentence_transformers import SentenceTransformer
|
15 |
|
16 |
from llama_index.core import set_global_service_context, ServiceContext
|
|
|
39 |
this demo is governed by the original [license](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat/blob/main/USE_POLICY.md).
|
40 |
"""
|
41 |
|
42 |
+
def read_pdf_to_documents(file_path):
|
43 |
+
doc = fitz.open(file_path)
|
44 |
+
documents = []
|
45 |
+
for page_num in range(len(doc)):
|
46 |
+
page = doc.load_page(page_num)
|
47 |
+
text = page.get_text()
|
48 |
+
documents.append(Document(text=text)) # Now Document is defined
|
49 |
+
return documents
|
50 |
+
|
51 |
+
# Function to update the global system prompt
|
52 |
+
def update_system_prompt(new_prompt):
|
53 |
+
global system_prompt
|
54 |
+
system_prompt = new_prompt
|
55 |
+
query_wrapper_prompt = SimpleInputPrompt("{query_str} [/INST]")
|
56 |
+
return "System prompt updated."
|
57 |
+
|
58 |
+
def query_model(question):
|
59 |
+
llm = HuggingFaceLLM(
|
60 |
+
context_window=4096,
|
61 |
+
max_new_tokens=256,
|
62 |
+
system_prompt=system_prompt,
|
63 |
+
query_wrapper_prompt=query_wrapper_prompt,
|
64 |
+
model=model,
|
65 |
+
tokenizer=tokenizer
|
66 |
+
)
|
67 |
+
#embeddings = LangchainEmbedding(HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2"))
|
68 |
+
service_context = ServiceContext.from_defaults(chunk_size=1024, llm=llm, embed_model=embeddings)
|
69 |
+
set_global_service_context(service_context)
|
70 |
+
|
71 |
+
response = query_engine.query(question)
|
72 |
+
# formatted_response = format_paragraph(response.response)
|
73 |
+
return response.response
|
74 |
+
|
75 |
+
def format_paragraph(text, line_length=80):
|
76 |
+
words = text.split()
|
77 |
+
lines = []
|
78 |
+
current_line = []
|
79 |
+
current_length = 0
|
80 |
+
|
81 |
+
for word in words:
|
82 |
+
if current_length + len(word) + 1 > line_length:
|
83 |
+
lines.append(' '.join(current_line))
|
84 |
+
current_line = [word]
|
85 |
+
current_length = len(word) + 1
|
86 |
+
else:
|
87 |
+
current_line.append(word)
|
88 |
+
current_length += len(word) + 1
|
89 |
+
|
90 |
+
if current_line:
|
91 |
+
lines.append(' '.join(current_line))
|
92 |
+
|
93 |
+
return '\n'.join(lines)
|
94 |
+
|
95 |
if not torch.cuda.is_available():
|
96 |
DESCRIPTION += "We won't be able to run this space! We need GPU processing"
|
97 |
|
|
|
102 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
103 |
tokenizer.use_default_system_prompt = False
|
104 |
|
105 |
+
system_prompt = """<s>[INST] <<SYS>>
|
106 |
+
|
107 |
+
<</SYS>>"""
|
108 |
+
# Throw together the query wrapper
|
109 |
+
query_wrapper_prompt = SimpleInputPrompt("{query_str} [/INST]")
|
110 |
+
llm = HuggingFaceLLM(context_window=4096,
|
111 |
+
max_new_tokens=256,
|
112 |
+
system_prompt=system_prompt,
|
113 |
+
query_wrapper_prompt=query_wrapper_prompt,
|
114 |
+
model=model, tokenizer=tokenizer)
|
115 |
+
embeddings = LangchainEmbedding(HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2"))
|
116 |
+
service_context = ServiceContext.from_defaults(chunk_size=1024, llm=llm, embed_model=embeddings)
|
117 |
+
set_global_service_context(service_context)
|
118 |
+
file_path = Path('/content/Full Pamplet.pdf')#make sure to change this to the document path
|
119 |
+
documents = read_pdf_to_documents(file_path)
|
120 |
+
index = VectorStoreIndex.from_documents(documents)
|
121 |
+
query_engine = index.as_query_engine()
|
122 |
+
|
123 |
+
|
124 |
+
update_prompt_interface = gr.Interface(
|
125 |
+
fn=update_system_prompt,
|
126 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter the system prompt here...", label="System Prompt", value=system_prompt),
|
127 |
+
outputs=gr.Textbox(label="Status"),
|
128 |
+
title="System Prompt Updater",
|
129 |
+
description="Update the system prompt used for context."
|
130 |
+
)
|
131 |
+
|
132 |
+
# Create Gradio interface for querying the model
|
133 |
+
query_interface = gr.Interface(
|
134 |
+
fn=query_model,
|
135 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your question here...", label="User Question"),
|
136 |
+
outputs=gr.Textbox(label="Response"),
|
137 |
+
title="Document Query Assistant",
|
138 |
+
description="Ask questions based on the content of the loaded pamphlet."
|
139 |
+
)
|
140 |
|
141 |
+
# Combine the interfaces
|
142 |
+
combined_interface = gr.TabbedInterface([update_prompt_interface, query_interface], ["Update System Prompt", "Query Assistant"])
|
143 |
+
|
144 |
+
# Launch the combined interface
|
145 |
+
combined_interface.launch()
|
146 |
+
|
147 |
+
"""
|
148 |
@spaces.GPU(duration=240)
|
149 |
def generate(
|
150 |
message: str,
|
|
|
239 |
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
240 |
],
|
241 |
)
|
242 |
+
"""
|
243 |
|
244 |
with gr.Blocks(css="style.css") as demo:
|
245 |
gr.Markdown(DESCRIPTION)
|
246 |
+
#chat_interface.render()
|
247 |
+
combined_interface.render()
|
248 |
gr.Markdown(LICENSE)
|
249 |
|
250 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
@@ -11,4 +11,6 @@ sentence-transformers
|
|
11 |
PyPDF2
|
12 |
PyMuPDF
|
13 |
langchain
|
14 |
-
|
|
|
|
|
|
11 |
PyPDF2
|
12 |
PyMuPDF
|
13 |
langchain
|
14 |
+
langchain_huggingface
|
15 |
+
llama-index
|
16 |
+
llama-index-llms-huggingface
|