Spaces:
Running
on
Zero
Running
on
Zero
import os | |
from threading import Thread | |
from typing import Iterator | |
import gradio as gr | |
import spaces | |
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer | |
from llama_index.core.prompts.prompts import SimpleInputPrompt | |
from llama_index.llms.huggingface import HuggingFaceLLM | |
from llama_index.legacy.embeddings.langchain import LangchainEmbedding | |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings | |
from llama_index.core import set_global_service_context, ServiceContext, VectorStoreIndex, Document | |
from pathlib import Path | |
import fitz # PyMuPDF | |
MAX_MAX_NEW_TOKENS = 2048 | |
DEFAULT_MAX_NEW_TOKENS = 1024 | |
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096")) | |
DESCRIPTION = """\ | |
# Llama-2 7B Chat with Document Context | |
This Space demonstrates model [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, a Llama 2 model with 7B parameters fine-tuned for chat instructions, now enhanced with document-based context. | |
Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints). | |
π For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2). | |
π¨ Looking for an even more powerful model? Check out the [13B version](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat) or the large [70B model demo](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI). | |
""" | |
LICENSE = """ | |
<p/> | |
--- | |
As a derivate work of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat) by Meta, | |
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). | |
""" | |
if not torch.cuda.is_available(): | |
DESCRIPTION += "\n<p>Running on CPU π₯Ά This demo does not work on CPU.</p>" | |
if torch.cuda.is_available(): | |
model_name = "meta-llama/Llama-2-7b-chat-hf" | |
token_file = open("HF_TOKEN.txt") | |
auth_token = token_file.readline().strip() | |
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", token=auth_token) | |
tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir='./model/', token=auth_token) | |
tokenizer.use_default_system_prompt = False | |
# Load documents and create the index | |
def read_pdf_to_documents(file_path): | |
doc = fitz.open(file_path) | |
documents = [] | |
for page_num in range(len(doc)): | |
page = doc.load_page(page_num) | |
text = page.get_text() | |
documents.append(Document(text=text)) | |
return documents | |
file_path = Path('/content/Full_Pamplet.pdf') # Update with your document path | |
documents = read_pdf_to_documents(file_path) | |
embeddings = LangchainEmbedding(HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")) | |
service_context = ServiceContext.from_defaults(chunk_size=1024, embed_model=embeddings) | |
set_global_service_context(service_context) | |
index = VectorStoreIndex.from_documents(documents) | |
query_engine = index.as_query_engine() | |
def generate( | |
message: str, | |
chat_history: list[tuple[str, str]], | |
system_prompt: str, | |
max_new_tokens: int = 1024, | |
temperature: float = 0.6, | |
top_p: float = 0.9, | |
top_k: int = 50, | |
repetition_penalty: float = 1.2, | |
) -> Iterator[str]: | |
conversation = [] | |
if system_prompt: | |
conversation.append({"role": "system", "content": system_prompt}) | |
for user, assistant in chat_history: | |
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}]) | |
conversation.append({"role": "user", "content": message}) | |
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt") | |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH: | |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:] | |
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.") | |
input_ids = input_ids.to(model.device) | |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True) | |
generate_kwargs = dict( | |
{"input_ids": input_ids}, | |
streamer=streamer, | |
max_new_tokens=max_new_tokens, | |
do_sample=True, | |
top_p=top_p, | |
top_k=top_k, | |
temperature=temperature, | |
num_beams=1, | |
repetition_penalty=repetition_penalty, | |
) | |
t = Thread(target=model.generate, kwargs=generate_kwargs) | |
t.start() | |
outputs = [] | |
for text in streamer: | |
outputs.append(text) | |
yield "".join(outputs) | |
def query_model(question): | |
response = query_engine.query(question) | |
return response.response | |
update_prompt_interface = gr.Interface( | |
fn=update_system_prompt, | |
inputs=gr.Textbox(lines=5, placeholder="Enter the system prompt here...", label="System Prompt", value=system_prompt), | |
outputs=gr.Textbox(label="Status"), | |
title="System Prompt Updater", | |
description="Update the system prompt used for context." | |
) | |
query_interface = gr.Interface( | |
fn=query_model, | |
inputs=gr.Textbox(lines=2, placeholder="Enter your question here...", label="User Question"), | |
outputs=gr.Textbox(label="Response"), | |
title="Document Query Assistant", | |
description="Ask questions based on the conte | |