Spaces:
Runtime error
Runtime error
Zwea Htet commited on
Commit ·
0809507
1
Parent(s): 0e8a2bc
added codes
Browse filesFiles changed (5)
- .gitignoreAdded+5
- app.py+55-2
- models/bloom.pyAdded+63
- requirements.txtAdded+10
- utils/customLLM.pyAdded+36
12345venvdata/__pycache__models/__pycache__.env__pycache__1234import streamlit as stx = st.slider('Select a value')st.write(x, 'squared is', x * x)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import osimport requestsimport streamlit as stfrom models import bloomst.title("Welcome to RegBotBeta")st.header("Powered by `LlamaIndex🦙` and `OpenAI API`")def validate(token: str):api_endpoint = "https://api.openai.com/v1/chat/completions"api_key = tokenheaders = {"Content-Type" : "application/json","Authorization": f"Bearer {api_key}"}messages = [{"role": "user", "content": "Say this is a test!"}]data = {"model": "gpt-3.5-turbo","messages": messages}response = requests.post(api_endpoint, json=data, headers=headers)return responsedef create_index():index = bloom.initialize_index("")def get_response(vector_index, query_str):query_engine = vector_index.as_query_engine()response = query_engine.query(query_str)return responseapi_key = st.text_input("Enter your OpenAI API key here:", type="password")if api_key:st.write("---")input_text = st.text_area("Ask your question")if input_text is not None:if st.button("Ask"):st.info("Your query: \n" + input_text)with st.spinner("Processing your query..."):response = get_response(index, input_text)print(response)st.success(response)st.write("---")
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import osfrom json import dumps, loads
import numpy as npimport pandas as pdfrom dotenv import load_dotenvfrom llama_index import (Document, GPTVectorStoreIndex, LLMPredictor, PromptHelper, ServiceContext, StorageContext, load_index_from_storage)from transformers import AutoModelForCausalLM, AutoTokenizer
from utils.customLLM import CustomLLM
load_dotenv()
# get modelmodel_name = "bigscience/bloom-560m" tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, config='T5Config')
# define prompt helper# set maximum input sizemax_input_size = 2048# set number of output tokensnum_output = 525# set maximum chunk overlapmax_chunk_overlap = 20prompt_helper = PromptHelper(max_input_size, num_output, max_chunk_overlap)
# define llmllm_predictor = LLMPredictor(llm=CustomLLM(model, tokenizer))service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
def prepare_data(file_path:str): df = pd.read_json(file_path) df = df.replace(to_replace="", value=np.nan).dropna(axis=0) # remove null values parsed = loads(df.to_json(orient="records"))
documents = [] for item in parsed: document = Document(item['paragraphText'], item['_id']['$oid'], extra_info={"chapter": item['chapter'], "article": item['article'], "title": item['title']}) documents.append(document)
return documents
def initialize_index(index_name): file_path = f"./vectorStores/{index_name}" if os.path.exists(file_path): # rebuild storage context storage_context = StorageContext.from_defaults(persist_dir=file_path) # load index index = load_index_from_storage(storage_context) return GPTVectorStoreIndex.load_from_disk(file_path) else: documents = prepare_data(r"./assets/regItems.json") index = GPTVectorStoreIndex.from_documents(documents, service_context=service_context) index.storage_context.persist(file_path) return index12345678910llama_indextorchtransformerspandanumpylangchainopenaifaiss-cpupython-dotenvstreamlit123456789101112131415161718192021222324252627282930313233343536from typing import Any, List, Mapping, Optional
from langchain.llms.base import LLMfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
class CustomLLM(LLM): # Create the pipeline for question answering def __init__(self, model: AutoModelForCausalLM, tokenizer: AutoTokenizer): self.pipeline = pipeline( model=model, tokenizer=tokenizer, task="text-generation", # device=0, # GPU device number # max_length=512, do_sample=True, top_p=0.95, top_k=50, temperature=0.7 )
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str: prompt_length = len(prompt) response = self.pipeline(prompt, max_new_tokens=525)[0]["generated_text"]
# only return newly generated tokens return response[prompt_length:]
@property def _identifying_params(self) -> Mapping[str, Any]: return {"name_of_model": self.model_name}
@property def _llm_type(self) -> str: return "custom".gitignoreAdded+5
app.py+55-2
models/bloom.pyAdded+63
requirements.txtAdded+10
utils/customLLM.pyAdded+36